Skip to content

Commit

Permalink
Add tests for booking flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tarun-nil committed Jan 13, 2024
1 parent 103194e commit c00cf91
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
10 changes: 5 additions & 5 deletions swift-ticketing/src/swift_ticketing/db/ticket.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
[:cast seat-type :seat_type]]]}))))

(defn get-ticket-type [db-spec ticket-type-id]
(run-query-one!
db-spec
(sql/format {:select [:*]
:from :ticket_type
:where [:= :ticket_type_id [:cast ticket-type-id :uuid]]})))
(run-query-one!
db-spec
(sql/format {:select [:*]
:from :ticket_type
:where [:= :ticket_type_id [:cast ticket-type-id :uuid]]})))

(defn insert-tickets [db-spec ticket-type-id tickets price]
(run-query!
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 c00cf91

Please sign in to comment.