Skip to content

Commit

Permalink
Merge pull request #11 from nilenso/feature/model_unit_tests
Browse files Browse the repository at this point in the history
Unit tests for domain model
  • Loading branch information
zzat authored Jan 11, 2024
2 parents 6dc3ab0 + 759626c commit f6d6bdb
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 0 deletions.
10 changes: 10 additions & 0 deletions swift-ticketing/test/swift_ticketing/factory.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
"from" (random-date)
"to" (random-date)})

(defn events-result []
(let [mk-event
(fn [event-id]
{:event-id event-id
:event-name (mk-event-name event-id)
:event-description (mk-event-description event-id)
:event-date (random-date)
:venue (mk-event-venue event-id)})]
(map mk-event (repeatedly (inc (rand-int 10)) random-uuid))))

(defn event-request []
(let [random-id (rand-int 10000)]
{"name" (mk-event-name random-id)
Expand Down
59 changes: 59 additions & 0 deletions swift-ticketing/test/swift_ticketing/model/booking_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(ns swift-ticketing.model.booking-test
(:require [clojure.test :refer [deftest testing is]]
[clojure.core.async :as async]
[swift-ticketing.fixtures :as fixtures]
[swift-ticketing.factory :as factory]
[swift-ticketing.worker :as worker]
[swift-ticketing.model.booking :as booking]
[swift-ticketing.db.booking :as db-booking]))

(deftest get-booking-status-test
(testing "Fetching Booking Status"
(let [{:keys [db-spec]} fixtures/test-env
booking-id (random-uuid)
booking-status (factory/random-booking-status)
get-booking-status-args (atom {})]
(with-redefs
[db-booking/get-booking-status
(fn [dbs bid]
(reset! get-booking-status-args
{:db-spec dbs
:booking-id bid})
booking-status)]
(is (= booking-status
(booking/get-booking-status db-spec booking-id)))
(is (= {:db-spec db-spec
:booking-id booking-id}
@get-booking-status-args))))))

(deftest make-payment-test
(testing "Payment"
(let [message-queue (async/chan)
booking-id (random-uuid)
add-book-ticket-request-to-queue-args (atom {})]
(with-redefs
[worker/add-book-ticket-request-to-queue
(fn [mq req]
(reset! add-book-ticket-request-to-queue-args
{:message-queue mq
:booking-request req}))]
(booking/make-payment message-queue booking-id)
(is (= {:message-queue message-queue
:booking-request {:booking-id booking-id}}
@add-book-ticket-request-to-queue-args))))))

(deftest cancel-booking-test
(testing "Ticket Cancellation"
(let [message-queue (async/chan)
booking-id (random-uuid)
add-cancel-ticket-request-to-queue-args (atom {})]
(with-redefs
[worker/add-cancel-ticket-request-to-queue
(fn [mq req]
(reset! add-cancel-ticket-request-to-queue-args
{:message-queue mq
:cancellation-request req}))]
(booking/cancel-booking message-queue booking-id)
(is (= {:message-queue message-queue
:cancellation-request {:booking-id booking-id}}
@add-cancel-ticket-request-to-queue-args))))))
60 changes: 60 additions & 0 deletions swift-ticketing/test/swift_ticketing/model/event_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
(ns swift-ticketing.model.event-test
(:require [clojure.test :refer [deftest testing is]]
[swift-ticketing.fixtures :as fixtures]
[swift-ticketing.factory :as factory]
[swift-ticketing.model.event :as event]
[swift-ticketing.db.event :as db-event]))

(deftest get-events-test
(testing "Listing Events"
(let [{:keys [db-spec]} fixtures/test-env
filters (factory/get-events-params)
get-events-args (atom {})
results [(factory/events-result)]]
(with-redefs
[db-event/get-events
(fn [dbs flts]
(reset! get-events-args {:db-spec dbs
:filters flts})
results)]
(is (= results (event/get-events db-spec filters)))
(is (= {:db-spec db-spec
:filters filters}
@get-events-args))))))

(deftest get-event-test
(testing "Fetching Event Details"
(let [{:keys [db-spec]} fixtures/test-env
event-id (random-uuid)
get-event-with-tickets-args (atom {})
result (factory/event-with-tickets event-id)]
(with-redefs
[db-event/get-event-with-tickets
(fn [dbs eid]
(reset! get-event-with-tickets-args {:db-spec dbs
:event-id eid})
result)]
(is (= result (event/get-event db-spec event-id)))
(is (= {:db-spec db-spec
:event-id event-id}
@get-event-with-tickets-args))))))

(deftest create-event-test
(testing "Creating Event"
(let [{:keys [db-spec test-user-id]} fixtures/test-env
create-req (factory/event-request)
insert-event-args (atom {})]
(with-redefs
[db-event/insert-event
(fn [dbs uid eid req]
(reset! insert-event-args
{:db-spec dbs
:user-id uid
:create-request req})
eid)]
(is (uuid?
(event/create-event db-spec test-user-id create-req)))
(is (= {:db-spec db-spec
:user-id test-user-id
:create-request create-req}
@insert-event-args))))))
109 changes: 109 additions & 0 deletions swift-ticketing/test/swift_ticketing/model/ticket_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
(ns swift-ticketing.model.ticket-test
(:require [clojure.test :refer [deftest testing is]]
[swift-ticketing.fixtures :as fixtures]
[swift-ticketing.factory :as factory]
[swift-ticketing.model.ticket :as ticket]
[swift-ticketing.db.ticket :as db-ticket]
[swift-ticketing.db.booking :as db-booking]
[swift-ticketing.worker :as worker]
[camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :as cske]))

(deftest create-tickets-test
(testing "Creating Tickets"
(let [{:keys [db-spec test-user-id]} fixtures/test-env
event-id (str (random-uuid))
ticket-req (cske/transform-keys csk/->kebab-case-keyword
(factory/seated-ticket-request))
insert-ticket-type-args (atom {})
insert-tickets-args (atom {})]
(with-redefs
[db-ticket/insert-ticket-type
(fn [dbs eid _ req]
(reset! insert-ticket-type-args {:db-spec dbs
:event-id eid
:ticket-request req}))
db-ticket/insert-tickets
(fn [dbs _ _ price]
(reset! insert-tickets-args {:db-spec dbs
:price price}))]
(let [{:keys [ticket-type-id tickets]}
(ticket/create-tickets db-spec test-user-id event-id ticket-req)]
(is (uuid? ticket-type-id))
(is (not (nil? tickets)))
(is (= {:db-spec db-spec
:event-id event-id
:ticket-request ticket-req}
@insert-ticket-type-args))
(is (= {:db-spec db-spec
:price (:price ticket-req)}
@insert-tickets-args)))))))

(deftest reserve-ticket-test
(testing "Reserving tickets"
(let [{:keys [db-spec test-user-id]} fixtures/test-env
message-queue :message-queue
event-id (str (random-uuid))
ticket-req (cske/transform-keys
csk/->kebab-case-keyword
(factory/mk-reserve-seated-ticket-request
[(random-uuid)]))
insert-booking-args (atom {})
add-reserve-ticket-args (atom {})]
(with-redefs
[db-booking/insert-booking
(fn [dbs uid bid status]
(reset! insert-booking-args {:db-spec dbs
:user-id uid
:booking-status status}))
worker/add-reserve-ticket-request-to-queue
(fn [mq req]
(reset! add-reserve-ticket-args
{:message-queue mq
:ticket-type-id (:ticket-type-id req)
:quantity (:quantity req)}))]
(is (uuid?
(ticket/reserve-ticket
db-spec message-queue test-user-id event-id ticket-req)))
(is (= {:db-spec db-spec
:user-id test-user-id
:booking-status db-booking/INPROCESS}
@insert-booking-args))
(is (= {:message-queue message-queue
:ticket-type-id (:ticket-type-id ticket-req)
:quantity (:quantity ticket-req)}
@add-reserve-ticket-args))))))

(deftest get-tickets-test
(testing "Fetching unbooked tickets"
(let [{:keys [db-spec]} fixtures/test-env
ticket-type-id (str (random-uuid))
get-unbooked-tickets-args (atom {})
results (repeatedly (inc (rand-int 10)) factory/mk-ticket)]
(with-redefs
[db-ticket/get-unbooked-tickets
(fn [dbs tid]
(reset! get-unbooked-tickets-args {:db-spec dbs
:ticket-type-id tid})
results)]
(is (= results (ticket/get-tickets db-spec ticket-type-id)))
(is (= {:db-spec db-spec
:ticket-type-id ticket-type-id}
@get-unbooked-tickets-args))))))

(deftest get-tickets-by-booking-id-test
(testing "Fetching tickets by booking id"
(let [{:keys [db-spec]} fixtures/test-env
booking-id (str (random-uuid))
get-tickets-by-booking-id-args (atom {})
results (repeatedly (inc (rand-int 10)) factory/mk-ticket)]
(with-redefs
[db-ticket/get-tickets-by-booking-id
(fn [dbs bid]
(reset! get-tickets-by-booking-id-args {:db-spec dbs
:booking-id bid})
results)]
(is (= results (ticket/get-tickets-by-booking-id db-spec booking-id)))
(is (= {:db-spec db-spec
:booking-id booking-id}
@get-tickets-by-booking-id-args))))))

0 comments on commit f6d6bdb

Please sign in to comment.