Skip to content

Commit

Permalink
Add test for ticket booking flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tarun-nil committed Nov 16, 2023
1 parent 3809bdd commit ae0d232
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 26 deletions.
2 changes: 1 addition & 1 deletion swift-ticketing/src/swift_ticketing/db/ddl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
[:ticket_description :text]
[:ticket_price :numeric [:not nil]]
[:event_id :uuid [:not nil]]
[:booking_id :uuid [:not nil]]
[:booking_id :uuid]
[:created_at :timestamptz [:default [:now]]]
[:updated_at :timestamptz [:default [:now]]]]
}))
Expand Down
6 changes: 3 additions & 3 deletions swift-ticketing/test/swift_ticketing/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
(:require [clojure.test :refer :all]
[swift-ticketing.core :refer :all]))

(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))
; (deftest a-test
; (testing "FIXME, I fail."
; (is (= 0 1))))
101 changes: 79 additions & 22 deletions swift-ticketing/test/swift_ticketing/swift_ticketing_app_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [clojure.test :refer :all]
[ring.mock.request :as mock]
[next.jdbc :as jdbc]
[honey.sql :as sql]
[next.jdbc.connection :as connection]
[clojure.data.json :as json]
[swift-ticketing.app :refer [swift-ticketing-app]]
Expand All @@ -21,32 +22,88 @@
(defn teardown-db [schema db-spec]
(jdbc/execute! db-spec [(ddl/delete-schema schema)]))

(defn add-user-table-entry [db-spec]
(let [user-id (java.util.UUID/randomUUID)
insert-user (sql/format {:insert-into :user_account
:columns [:user_id :name]
:values [[user-id
"Test User"]]})]
(jdbc/execute! db-spec insert-user)
user-id))

(defn setup-db [schema db-spec]
(init-db schema db-spec)
(add-user-table-entry db-spec))

(deftest test-app
(testing "Swift ticketing app"
(let [db-config (:database (read-config "config.test.edn"))]
(let [datasource (jdbc/get-datasource (assoc db-config :user (:username db-config)))
ds (jdbc/get-connection datasource)]
(jdbc/execute! ds [(ddl/create-schema (:schema db-config))]))
(with-open [^HikariDataSource db-spec (create-connection-pool db-config)]
(init-db (:schema db-config) db-spec)
(testing "GET event route"
(let [response ((swift-ticketing-app db-spec) (mock/request :get "/event"))]
(is (= (:status response) 200))
(is (vector? (json/read-str (:body response))))))

(testing "POST event route"
(let [response ((swift-ticketing-app db-spec)
(-> (mock/request :post "/event")
(mock/json-body
{"name" "Mock Event"
"description" "Mock Event description"
"date" "2024-10-01"
"venue" "Bangalore"})
(mock/cookie "uid" "e0119c9d-67b0-4eef-a5f1-a9463f070c57")))]
(is (= (:status response) 201))
(is (contains? (json/read-str (:body response)) "event_id"))))

(testing "not-found route"
(let [response ((swift-ticketing-app db-spec) (mock/request :get "/invalid"))]
(is (= (:status response) 404))))
(teardown-db (:schema db-config) db-spec)))))
(let [test-user-id (setup-db (:schema db-config) db-spec)]

(testing "GET event"
(let [response ((swift-ticketing-app db-spec) (mock/request :get "/event"))]
(is (= (:status response) 200))
(is (vector? (json/read-str (:body response))))))

(testing "POST event"
(let [response ((swift-ticketing-app db-spec)
(-> (mock/request :post "/event")
(mock/json-body
{"name" "Mock Event"
"description" "Mock Event description"
"date" "2024-10-01"
"venue" "Bangalore"})
(mock/cookie "uid" test-user-id)))]
(is (= (:status response) 201))
(is (contains? (json/read-str (:body response)) "event_id"))))

(testing "Create event and its tickets"
(let [event-response ((swift-ticketing-app db-spec)
(-> (mock/request :post "/event")
(mock/json-body
{"name" "Mock Event"
"description" "Mock Event description"
"date" "2024-10-01"
"venue" "Bangalore"})
(mock/cookie "uid" test-user-id)))]
(testing "POST event"
(is (= (:status event-response) 201))
(is (contains? (json/read-str (:body event-response)) "event_id")))

(testing "POST ticket"
(let [created-event-id (get (json/read-str (:body event-response)) "event_id")
ticket-response (-> ((swift-ticketing-app db-spec)
(-> (mock/request :post (str "/event/" created-event-id "/ticket"))
(mock/json-body
{"name" "Ticket 1"
"description" "Ticket 1"
"quantity" 2
"price" 250})
(mock/cookie "uid" test-user-id)))
:body
json/read-str)]
; (is (= (:status ticket-response) 201))
(is (contains? ticket-response "tickets"))

(testing "Ticket Booking"
(let [booking-response (-> ((swift-ticketing-app db-spec)
(-> (mock/request :post (str "/event/" created-event-id "/booking"))
(mock/json-body
{"ticket_name" "Ticket 1"
"quantity" 1})
(mock/cookie "uid" test-user-id)))
:body
json/read-str)]
; (is (= (:status booking-response) 201))
(is (contains? booking-response "booking_id"))))))))

(testing "not-found route"
(let [response ((swift-ticketing-app db-spec) (mock/request :get "/invalid"))]
(is (= (:status response) 404))))

(teardown-db (:schema db-config) db-spec)
)))))

0 comments on commit ae0d232

Please sign in to comment.