Skip to content

Commit

Permalink
Merge pull request #17 from nilenso/feature/test_booking_flow
Browse files Browse the repository at this point in the history
Add tests for booking flow
  • Loading branch information
zzat authored Jan 15, 2024
2 parents fd6b334 + c00cf91 commit f57ab49
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 10 deletions.
18 changes: 11 additions & 7 deletions swift-ticketing/src/swift_ticketing/db/ticket.clj
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,17 @@
(select-reserved-tickets db-spec true booking-id))

(defn reserve-tickets [db-spec ticket-ids booking-id reservation-expiration-time]
(run-query!
db-spec
(sql/format {:update :ticket
:set {:booking_id [:cast booking-id :uuid]
:ticket_status [:cast reserved :ticket_status]
:reservation_expiration_time [:cast (.toString reservation-expiration-time) :timestamptz]}
:where [:in :ticket_id ticket-ids]})))
(let [expiration-time
(if (nil? reservation-expiration-time)
nil
[:cast (.toString reservation-expiration-time) :timestamptz])]
(run-query!
db-spec
(sql/format {:update :ticket
:set {:booking_id [:cast booking-id :uuid]
:ticket_status [:cast reserved :ticket_status]
:reservation_expiration_time expiration-time}
:where [:in :ticket_id ticket-ids]}))))

(defn reset-ticket-status
([db-spec ticket-ids status]
Expand Down
106 changes: 106 additions & 0 deletions swift-ticketing/test/swift_ticketing/booking_flow_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
(ns swift-ticketing.booking-flow-test
(:require [clojure.test :refer [deftest testing is use-fixtures]]
[swift-ticketing.fixtures :as fixtures]
[swift-ticketing.factory :as factory]
[swift-ticketing.client :as client]
[swift-ticketing.db.booking :as db-booking]
[camel-snake-kebab.extras :as cske]
[camel-snake-kebab.core :as csk]))

(use-fixtures :each fixtures/clear-tables)

(deftest general-ticket-booking-flow-test
(testing "Ticket booking (General)"
;; setup event and tickets
(let [{:keys [db-spec]} fixtures/test-env
event-id (->> (client/create-event)
:response
(cske/transform-keys csk/->kebab-case-keyword)
:event-id)
{:keys [ticket-type-id
tickets]} (->> (client/create-general-tickets event-id)
:response
(cske/transform-keys csk/->kebab-case-keyword))
total-tickets (count tickets)
reserve-tickets (fn [quantity]
(->> (factory/mk-reserve-general-ticket-request
quantity
ticket-type-id)
(client/reserve-ticket event-id)))
get-booking-id #(get % "booking_id")]

(testing "Reserving more tickets than available"
;; Reserving more tickets than available should return REJECTED
(let [{:keys [status response]} (reserve-tickets (inc total-tickets))
booking-id (get-booking-id response)]
(is (= 201 status))
(Thread/sleep 2000)
(is (= db-booking/rejected
(db-booking/get-booking-status db-spec booking-id)))))

(testing "Reserving available tickets"
;; Reserve available tickets -> PaymentPending
(let [{:keys [status response]} (reserve-tickets total-tickets)
booking-id (get-booking-id response)]
(is (= 201 status))
(Thread/sleep 2000)
(is (= db-booking/payment-pending
(db-booking/get-booking-status db-spec booking-id)))

(testing "Cancelling reserved ticket"
(client/cancel-booking booking-id)
(Thread/sleep 2000)
(is (= db-booking/canceled
(db-booking/get-booking-status db-spec booking-id))))))

(testing "Making Payment"
(let [booking-id (-> (reserve-tickets total-tickets)
:response
(get "booking_id"))]
(Thread/sleep 2000)
(client/make-payment booking-id)
(Thread/sleep 2000)
(is (= db-booking/confirmed
(db-booking/get-booking-status db-spec booking-id))))))))

(deftest seated-ticket-booking-flow-test
(testing "Ticket booking (Seated)"
(let [{:keys [db-spec]} fixtures/test-env
event-id (->> (client/create-event)
:response
(cske/transform-keys csk/->kebab-case-keyword)
:event-id)
tickets (->> (client/create-seated-tickets event-id)
:response
(cske/transform-keys csk/->kebab-case-keyword)
:tickets)
ticket-ids (map :ticket-id tickets)
reserve-tickets (fn [tids]
(->> (factory/mk-reserve-seated-ticket-request
tids)
(client/reserve-ticket event-id)))
get-booking-id #(get % "booking_id")]

(testing "Reserving available tickets"
(let [{:keys [status response]} (reserve-tickets ticket-ids)
booking-id (get-booking-id response)]
(is (= 201 status))
(Thread/sleep 2000)
(is (= db-booking/payment-pending
(db-booking/get-booking-status db-spec booking-id)))

(testing "Cancelling reserved ticket"
(client/cancel-booking booking-id)
(Thread/sleep 2000)
(is (= db-booking/canceled
(db-booking/get-booking-status db-spec booking-id))))))

(testing "Making Payment"
(let [booking-id (-> (reserve-tickets ticket-ids)
:response
(get "booking_id"))]
(Thread/sleep 2000)
(client/make-payment booking-id)
(Thread/sleep 2000)
(is (= db-booking/confirmed
(db-booking/get-booking-status db-spec booking-id))))))))
8 changes: 5 additions & 3 deletions swift-ticketing/test/swift_ticketing/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
:status (:status response)
:response (response-to-json response)})))

(defn reserve-general-ticket
[request]
(let [response (post-request "/event" request)]
(defn reserve-ticket
[event-id request]
(let [response (post-request
(str "/event/" event-id "/booking")
request)]
{:request request
:status (:status response)
:response (response-to-json response)}))
Expand Down

0 comments on commit f57ab49

Please sign in to comment.