Skip to content

Commit

Permalink
Merge pull request #14 from nilenso/feature/db_queries_unit_tests
Browse files Browse the repository at this point in the history
Unit tests for db queries
  • Loading branch information
zzat authored Jan 15, 2024
2 parents 2246b5e + 11cccb3 commit fd6b334
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 18 deletions.
2 changes: 1 addition & 1 deletion swift-ticketing/src/swift_ticketing/db/booking.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
:where [[:= :booking_id [:cast booking-id :uuid]]]})))

(defn get-booking-status [db-spec booking-id]
(:booking_status (get-booking db-spec booking-id)))
(:booking-status (get-booking db-spec booking-id)))

(defn update-booking-status [db-spec booking-id booking-status]
(run-query!
Expand Down
2 changes: 1 addition & 1 deletion swift-ticketing/src/swift_ticketing/db/event.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
(let [{:keys [venue from to]} filters]
(run-query!
db-spec
(sql/format {:select [:event_id :event_name :event_description :event_date :venue] :from :event
(sql/format {:select [:*] :from :event
:where [:and
(if (nil? venue) [true] [:= :venue venue])
(if (nil? from) [true] [:>= :event_date [:cast from :date]])
Expand Down
29 changes: 17 additions & 12 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 Expand Up @@ -109,13 +109,18 @@
:reservation_expiration_time [:cast (.toString reservation-expiration-time) :timestamptz]}
:where [:in :ticket_id ticket-ids]})))

(defn reset-ticket-status [db-spec ticket-ids status]
(run-query!
db-spec
(sql/format {:update :ticket
:set {:ticket_status [:cast status :ticket_status]
:reservation_expiration_time nil}
:where [:in :ticket_id ticket-ids]})))
(defn reset-ticket-status
([db-spec ticket-ids status]
(reset-ticket-status db-spec ticket-ids status nil))
([db-spec ticket-ids status reservation-expiration-time]
(let [expiration-time (and reservation-expiration-time
[:cast (.toString reservation-expiration-time) :timestamptz])]
(run-query!
db-spec
(sql/format {:update :ticket
:set {:ticket_status [:cast status :ticket_status]
:reservation_expiration_time expiration-time}
:where [:in :ticket_id ticket-ids]})))))

(defn confirm-tickets [db-spec ticket-ids]
(reset-ticket-status db-spec ticket-ids booked))
Expand Down
55 changes: 55 additions & 0 deletions swift-ticketing/test/swift_ticketing/db/booking_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(ns swift-ticketing.db.booking-test
(:require [clojure.test :refer [deftest testing is use-fixtures]]
[swift-ticketing.fixtures :as fixtures]
[swift-ticketing.db.booking :as db-booking]
[swift-ticketing.factory :as factory]))

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

(deftest insert-booking-test
(let [{:keys [db-spec test-user-id]} fixtures/test-env
expected {:booking-id (random-uuid)
:user-id test-user-id
:booking-status (factory/random-booking-status)}]
(testing "Insert booking query"
(db-booking/insert-booking
db-spec
(:user-id expected)
(:booking-id expected)
(:booking-status expected))
(let [booking (db-booking/get-booking db-spec (:booking-id expected))]
(is (= expected
(dissoc booking :created-at :updated-at)))))))

(deftest get-booking-status-test
(testing "Get booking status query"
(let [{:keys [db-spec test-user-id]} fixtures/test-env
booking {:booking-id (random-uuid)
:user-id test-user-id
:booking-status (factory/random-booking-status)}]
(db-booking/insert-booking
db-spec
(:user-id booking)
(:booking-id booking)
(:booking-status booking))
(is (= (:booking-status booking)
(db-booking/get-booking-status db-spec (:booking-id booking)))))))

(deftest update-booking-status-test
(testing "Update booking status query"
(let [{:keys [db-spec test-user-id]} fixtures/test-env
booking {:booking-id (random-uuid)
:user-id test-user-id
:booking-status (factory/random-booking-status)}
new-booking-status (factory/random-booking-status)]
(db-booking/insert-booking
db-spec
(:user-id booking)
(:booking-id booking)
(:booking-status booking))
(db-booking/update-booking-status
db-spec
(:booking-id booking)
new-booking-status)
(is (= new-booking-status
(db-booking/get-booking-status db-spec (:booking-id booking)))))))
148 changes: 148 additions & 0 deletions swift-ticketing/test/swift_ticketing/db/event_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
(ns swift-ticketing.db.event-test
(:require [clojure.test :refer [deftest testing is use-fixtures]]
[camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :as cske]
[swift-ticketing.fixtures :as fixtures]
[swift-ticketing.factory :as factory]
[swift-ticketing.db.event :as db-event]
[swift-ticketing.db.ticket :as db-ticket])
(:import [java.time Instant Duration]))

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

(defn- format-date [date]
(.format
(java.text.SimpleDateFormat. "yyyy-MM-dd") date))

(defn- parse-date [date]
(.parse
(java.text.SimpleDateFormat. "yyyy-MM-dd") date))

(deftest insert-event-test
(let [{:keys [db-spec test-user-id]} fixtures/test-env
event-request (cske/transform-keys csk/->kebab-case-keyword
(factory/event-request))
event-id (random-uuid)
expected {:event-id event-id
:event-name (:name event-request)
:event-description (:description event-request)
:event-date (:date event-request)
:venue (:venue event-request)}]
(testing "Insert Event query"
(db-event/insert-event db-spec test-user-id event-id event-request)
(let [event (db-event/get-event db-spec event-id)
actual (-> event
(dissoc :organizer-id :created-at :updated-at)
(update :event-date format-date))]
(is (= expected actual))
(is (uuid? (:organizer-id event)))))))

(deftest get-events-test
(testing "Get events query"
(let [{:keys [db-spec test-user-id]} fixtures/test-env
event-request (cske/transform-keys csk/->kebab-case-keyword
(factory/event-request))
event-id (random-uuid)
expected {:event-id event-id
:event-name (:name event-request)
:event-description (:description event-request)
:event-date (:date event-request)
:venue (:venue event-request)}
prune-event
(fn [event]
(-> event
(dissoc :organizer-id :created-at :updated-at)
(update :event-date format-date)))]
(db-event/insert-event db-spec test-user-id event-id event-request)
(testing "without filters"
(let [events (->> (db-event/get-events db-spec {})
(map prune-event))]
(is (= [expected] events))))

(testing "with filters"
(let [invalid-venue {:venue "invalid"}
valid-venue {:venue (:venue expected)}
valid-from {:from (:event-date expected)}
valid-to {:to (:event-date expected)}
out-of-range-from {:from (-> (:event-date expected)
parse-date
(.toInstant)
(.plus (Duration/ofDays 1))
(java.util.Date/from)
format-date)}
out-of-range-to {:to (-> (:event-date expected)
parse-date
(.toInstant)
(.minus (Duration/ofDays 1))
(java.util.Date/from)
format-date)}]

(println (db-event/get-events db-spec valid-venue))
(println (map prune-event (db-event/get-events db-spec valid-venue)))
(is (every?
#(= [expected] %)
(->> [valid-venue
valid-from
valid-to]
(map #(map prune-event
(db-event/get-events db-spec %))))))
(is (every?
#(= [] %)
(map #(db-event/get-events db-spec %)
[invalid-venue
out-of-range-from
out-of-range-to]))))))))

(deftest get-event-with-tickets-test
(testing "Get Event with tickets info"
(let [{:keys [db-spec test-user-id]} fixtures/test-env
ticket-type-id (random-uuid)
event-request (cske/transform-keys csk/->kebab-case-keyword
(factory/event-request))
ticket-request (cske/transform-keys csk/->kebab-case-keyword
(factory/general-ticket-request))
event-id (random-uuid)
ticket-name (str (random-uuid))
ticket-id (random-uuid)
tickets [{:name ticket-name
:ticket-id ticket-id}]
ticket-price (bigdec (rand-int 1000))
event-with-tickets {:event-id event-id
:event-name (:name event-request)
:event-description (:description event-request)
:event-date (parse-date (:date event-request))
:venue (:venue event-request)
:ticket-type (:ticket-type ticket-request)
:ticket-type-id ticket-type-id
:ticket-description (:description ticket-request)
:ticket-name ticket-name
:ticket-price ticket-price
:ticket-count 1
:seat-type (:seat-type ticket-request)}]
(db-event/insert-event db-spec test-user-id event-id event-request)
(db-ticket/insert-ticket-type db-spec event-id ticket-type-id ticket-request)
(db-ticket/insert-tickets db-spec ticket-type-id tickets ticket-price)

(testing "when tickets are Available"
(let [result (db-event/get-event-with-tickets db-spec event-id)]
(is (= [event-with-tickets] result))))

(testing "when tickets are Booked"
(db-ticket/reset-ticket-status db-spec [ticket-id] db-ticket/booked)
(let [result (db-event/get-event-with-tickets db-spec event-id)]
(is (= [] result))))

(testing "when tickets are Reserved"
(let [current-time (Instant/now)
past-time (.minus current-time
(Duration/ofSeconds 1000))
future-time (.plus current-time
(Duration/ofSeconds 1000))]
(testing "but reservation time has expired"
(db-ticket/reset-ticket-status db-spec [ticket-id] db-ticket/reserved past-time)
(let [result (db-event/get-event-with-tickets db-spec event-id)]
(is (= [event-with-tickets] result))))
(testing "but reservation time has not expired"
(db-ticket/reset-ticket-status db-spec [ticket-id] db-ticket/reserved future-time)
(let [result (db-event/get-event-with-tickets db-spec event-id)]
(is (= [] result)))))))))
37 changes: 37 additions & 0 deletions swift-ticketing/test/swift_ticketing/db/ticket_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ns swift-ticketing.db.ticket-test
(:require [clojure.test :refer [deftest testing is use-fixtures]]
[swift-ticketing.fixtures :as fixtures]
[swift-ticketing.factory :as factory]
[swift-ticketing.db.ticket :as db-ticket]
[swift-ticketing.db.event :as db-event]
[camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :as cske]))

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

(defn- create-event-and-ticket-type [ticket-request]
(let [{:keys [db-spec test-user-id]} fixtures/test-env
ticket-type-id (random-uuid)
event-request (cske/transform-keys csk/->kebab-case-keyword
(factory/event-request))
event-id (random-uuid)]
(db-event/insert-event db-spec test-user-id event-id event-request)
(db-ticket/insert-ticket-type db-spec event-id ticket-type-id ticket-request)
{:event-id event-id
:ticket-type-id ticket-type-id}))

(deftest insert-ticket-type-test
(testing "Inserting Ticket Type"
(let [{:keys [db-spec]} fixtures/test-env
ticket-request (cske/transform-keys csk/->kebab-case-keyword
(factory/general-ticket-request))
{:keys [event-id ticket-type-id]} (create-event-and-ticket-type ticket-request)]
(let [expected {:ticket-type-id ticket-type-id
:ticket-type (:ticket_type ticket-request)
:ticket-type-description (:description ticket-request)
:event-id event-id
:reservation-timelimit-seconds nil
:seat-type (:seat_type ticket-request)}
ticket-type (db-ticket/get-ticket-type db-spec ticket-type-id)]
(is (= (dissoc ticket-type :created-at :updated-at) expected))))))

4 changes: 2 additions & 2 deletions swift-ticketing/test/swift_ticketing/model/booking_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
booking-id (random-uuid)
add-book-ticket-request-to-queue-args (atom {})]
(with-redefs
[worker/add-book-ticket-request-to-queue
[worker/request-ticket-booking
(fn [mq req]
(reset! add-book-ticket-request-to-queue-args
{:message-queue mq
Expand All @@ -48,7 +48,7 @@
booking-id (random-uuid)
add-cancel-ticket-request-to-queue-args (atom {})]
(with-redefs
[worker/add-cancel-ticket-request-to-queue
[worker/request-ticket-cancellation
(fn [mq req]
(reset! add-cancel-ticket-request-to-queue-args
{:message-queue mq
Expand Down
4 changes: 2 additions & 2 deletions swift-ticketing/test/swift_ticketing/model/ticket_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
(reset! insert-booking-args {:db-spec dbs
:user-id uid
:booking-status status}))
worker/add-reserve-ticket-request-to-queue
worker/request-ticket-reservation
(fn [mq req]
(reset! add-reserve-ticket-args
{:message-queue mq
Expand All @@ -67,7 +67,7 @@
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}
:booking-status db-booking/in-process}
@insert-booking-args))
(is (= {:message-queue message-queue
:ticket-type-id (:ticket-type-id ticket-req)
Expand Down

0 comments on commit fd6b334

Please sign in to comment.