File: defer_test.clj

package info (click to toggle)
next-jdbc-clojure 1.3.955-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 876 kB
  • sloc: xml: 62; sh: 39; makefile: 13
file content (53 lines) | stat: -rw-r--r-- 2,432 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
;; copyright (c) 2024 Sean Corfield, all rights reserved

(ns next.jdbc.defer-test
  "The idea behind the next.jdbc.defer namespace is to provide a
   way to defer the execution of a series of SQL statements until
   a later time, but still provide a way for inserted keys to be
   used in later SQL statements.

   The principle is to provide a core subset of the next.jdbc
   and next.jdbc.sql API that produces a data structure that
   describes a series of SQL operations to be performed, that
   are held in a dynamic var, and that can be executed at a
   later time, in a transaction."
  (:require [clojure.test :refer [deftest is testing use-fixtures]]
            [next.jdbc :as jdbc]
            [next.jdbc.defer :as sut]
            [next.jdbc.test-fixtures
             :refer [ds with-test-db]]))

(set! *warn-on-reflection* true)

(use-fixtures :once with-test-db)

(deftest basic-test
  (testing "data structures"
    (is (= [{:sql-p ["INSERT INTO foo (name) VALUES (?)" "Sean"]
             :key-fn :GENERATED_KEY
             :key    :id
             :opts   {:key-fn :GENERATED_KEY :key :id}}]
           @(sut/defer-ops
             #(sut/insert! :foo {:name "Sean"} {:key-fn :GENERATED_KEY :key :id})))))
  (testing "execution"
    (let [effects (sut/with-deferred (ds)
                    (sut/insert! :fruit {:name "Mango"} {:key :test}))]
      (is (= {:test 1} @effects))
      (is (= 1 (count (jdbc/execute! (ds)
                                     ["select * from fruit where name = ?"
                                      "Mango"])))))
    (let [effects (sut/with-deferred (ds)
                    (sut/insert! :fruit {:name "Dragonfruit"} {:key :test})
                    (sut/update! :fruit {:cost 123} {:name "Dragonfruit"})
                    (sut/delete! :fruit {:name "Dragonfruit"}))]
      (is (= {:test 1} @effects))
      (is (= 0 (count (jdbc/execute! (ds)
                                     ["select * from fruit where name = ?"
                                      "Dragonfruit"])))))
    (let [effects (sut/with-deferred (ds)
                    (sut/insert! :fruit {:name "Grapefruit" :bad_column 0} {:key :test}))]
      (is (= :failed (try @effects
                          (catch Exception _ :failed))))
      (is (= 0 (count (jdbc/execute! (ds)
                                     ["select * from fruit where name = ?"
                                      "Grapefruit"])))))))