Skip to content

Commit 44177d6

Browse files
Fix DB tests
1 parent f5c347b commit 44177d6

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

src/flamebin/db.clj

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,26 @@
1212

1313
;;;; Preparation
1414

15-
(def ^:private db-lock (ReentrantLock.))
16-
17-
(defn- db-options []
18-
{:dbtype "sqlite"
19-
:dbname (@config :db :path)})
20-
21-
(defn- migratus-config []
15+
(defn- migratus-config [connectable]
2216
{:store :database
2317
:migration-dir "migrations/"
2418
:init-script "init.sql"
2519
:init-in-transaction? false
26-
:db (db-options)})
20+
:db connectable})
2721

28-
(defn migrate []
29-
(with-locking db-lock
30-
(migratus/init (migratus-config))
31-
(migratus/migrate (migratus-config))))
32-
33-
#_(migrate)
22+
(defn- migrate [db]
23+
(with-locking (:lock db)
24+
(let [config (migratus-config db)]
25+
(migratus/init config)
26+
(migratus/migrate config))))
3427

3528
(mount/defstate db
36-
:start (migrate))
29+
:start (doto {:dbtype "sqlite"
30+
:dbname (@config :db :path)
31+
:lock (ReentrantLock.)}
32+
migrate))
33+
34+
#_(mount/start #'db)
3735

3836
;;;; DB interaction
3937

@@ -42,8 +40,8 @@
4240
(if (<= tries 0)
4341
(raise 500 "Can't create a proper unused ID.")
4442
(let [id (new-id)
45-
res (with-locking db-lock
46-
(jdbc/execute-one! (db-options) ["SELECT count(id) AS cnt FROM profile WHERE id = ?" id]))]
43+
res (with-locking (:lock @db)
44+
(jdbc/execute-one! @db ["SELECT count(id) AS cnt FROM profile WHERE id = ?" id]))]
4745
(if (zero? (:cnt res))
4846
id
4947
(recur (dec tries)))))))
@@ -55,40 +53,42 @@
5553
(let [{:keys [id file_path profile_type sample_count owner upload_ts
5654
edit_token is_public]} profile]
5755
(log/infof "Inserting profile %s from %s" id owner)
58-
(with-locking db-lock
59-
(sql-helpers/insert! (db-options) :profile
60-
{:id id
61-
:file_path file_path
62-
:profile_type (name profile_type)
63-
:upload_ts (str upload_ts)
64-
:sample_count sample_count
65-
:is_public is_public
66-
:edit_token edit_token
67-
:owner owner}))
56+
(with-locking (:lock @db)
57+
(jdbc/with-transaction [tx @db]
58+
(sql-helpers/insert! tx :profile
59+
{:id id
60+
:file_path file_path
61+
:profile_type (name profile_type)
62+
:upload_ts (str upload_ts)
63+
:sample_count sample_count
64+
:is_public is_public
65+
:edit_token edit_token
66+
:owner owner})
67+
(jdbc/execute-one! tx ["UPDATE stats SET val = val + 1 WHERE stat = 'total_uploaded'"])))
6868
profile))
6969

70-
#_(jdbc/execute! (db-options) ["SELECT * FROM profile"])
70+
#_(jdbc/execute! @db ["SELECT * FROM profile"])
7171

7272
(defn- unqualify-keys [m] (update-keys m (comp keyword name)))
7373

7474
(defn list-profiles []
75-
(with-locking db-lock
76-
(->> (jdbc/execute! (db-options) ["SELECT id, file_path, profile_type, sample_count, owner, upload_ts, is_public FROM profile"])
75+
(with-locking (:lock @db)
76+
(->> (jdbc/execute! @db ["SELECT id, file_path, profile_type, sample_count, owner, upload_ts, is_public FROM profile"])
7777
(mapv #(-> (unqualify-keys %)
7878
(assoc :edit_token nil)
7979
(coerce Profile))))))
8080

8181
(defn list-public-profiles [n]
82-
(with-locking db-lock
83-
(->> (jdbc/execute! (db-options) ["SELECT id, file_path, profile_type, sample_count, owner, upload_ts, is_public, edit_token FROM profile
82+
(with-locking (:lock @db)
83+
(->> (jdbc/execute! @db ["SELECT id, file_path, profile_type, sample_count, owner, upload_ts, is_public, edit_token FROM profile
8484
WHERE is_public = 1 ORDER BY upload_ts DESC LIMIT ?" n])
8585
(mapv #(-> (unqualify-keys %)
8686
(coerce Profile))))))
8787

8888
(defn get-profile [profile-id]
89-
(with-locking db-lock
89+
(with-locking (:lock @db)
9090
(let [q ["SELECT id, file_path, profile_type, sample_count, owner, upload_ts, edit_token, is_public FROM profile WHERE id = ?" profile-id]
91-
row (some-> (jdbc/execute-one! (db-options) q)
91+
row (some-> (jdbc/execute-one! @db q)
9292
unqualify-keys
9393
(coerce Profile))]
9494
(or row
@@ -97,16 +97,16 @@ WHERE is_public = 1 ORDER BY upload_ts DESC LIMIT ?" n])
9797
(raise 404 msg))))))
9898

9999
(defn delete-profile [profile-id]
100-
(with-locking db-lock
100+
(with-locking (:lock @db)
101101
(let [q ["DELETE FROM profile WHERE id = ?" profile-id]
102-
{::jdbc/keys [update-count]} (jdbc/execute-one! (db-options) q)]
102+
{::jdbc/keys [update-count]} (jdbc/execute-one! @db q)]
103103
(when (zero? update-count)
104104
(raise 404 (format "Profile with ID '%s' not found." profile-id))))))
105105

106106
(defn clear-db []
107-
(with-locking db-lock
107+
(with-locking (:lock @db)
108108
(.delete (clojure.java.io/file (@config :db :path)))
109-
(migrate)))
109+
(migrate @db)))
110110

111111
(comment
112112
(clear-db)

test/flamebin/db_test.clj

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77
[flamebin.dto :as dto]
88
[flamebin.test-utils :refer :all]
99
[flamebin.util :refer :all]
10+
[mount.lite :as mount]
11+
[flamebin.test-utils :refer :all]
1012
malli.generator
1113
[taoensso.timbre :as timbre]))
1214

13-
(defmacro with-temp-db [& body]
14-
`(let [f# (java.io.File/createTempFile "test-db" ".db")]
15-
(try (with-config-redefs [[:db :path] f#]
16-
(db/migrate)
17-
~@body)
18-
(finally (.delete f#)))))
15+
(defmacro with-temp-db-and-state [& body]
16+
`(try (with-temp-db
17+
(doto #'db/db mount/stop mount/start)
18+
~@body)
19+
(finally (doto #'db/db mount/stop mount/start))))
1920

2021
(deftest init-test
21-
(with-temp-db
22+
(with-temp-db-and-state
2223
(is (= [] (db/list-profiles)))))
2324

2425
(def ^:private inst1 (java.time.Instant/ofEpochSecond 1234567890))
2526

2627
(deftest manual-test
27-
(with-temp-db
28+
(with-temp-db-and-state
2829
(db/insert-profile (dto/->Profile "QcXAqv" "some-path.dpf" "cpu" 12345
2930
nil "alhdslfglksjdfhg" true inst1))
3031
(is (= {:id "QcXAqv", :file_path "some-path.dpf", :profile_type :cpu,
@@ -47,7 +48,7 @@
4748
;; Disable shrinking because it does little but slows down testing.
4849
[inserts (gen/vector (malli.generator/generator dto/Profile) 10 200)]
4950
(timbre/with-min-level :warn
50-
(with-temp-db
51+
(with-temp-db-and-state
5152
(run! db/insert-profile inserts)
5253
(let [fetched (db/list-profiles)
5354
no-pwd (fn [l] (mapv #(dissoc % :edit_token) l))]

0 commit comments

Comments
 (0)