|
| 1 | +(ns rethinkdb.async-test |
| 2 | + (:require [clojure.test :refer :all] |
| 3 | + [rethinkdb.query :as r] |
| 4 | + [clojure.core.async :as async]) |
| 5 | + (:import (clojure.core.async.impl.protocols ReadPort))) |
| 6 | + |
| 7 | +(def test-db "cljrethinkdb_test") |
| 8 | +(def test-table :pokedex) |
| 9 | + |
| 10 | +(defn ensure-table |
| 11 | + "Ensures that an empty table \"table-name\" exists" |
| 12 | + [table-name optargs conn] |
| 13 | + (if (some #{table-name} (r/run (r/table-list) conn)) |
| 14 | + (r/run (r/table-drop table-name) conn)) |
| 15 | + (r/run (r/table-create (r/db test-db) table-name optargs) conn)) |
| 16 | + |
| 17 | +(defn ensure-db |
| 18 | + "Ensures that an empty database \"db-name\" exists" |
| 19 | + [db-name conn] |
| 20 | + (if (some #{db-name} (r/run (r/db-list) conn)) |
| 21 | + (r/run (r/db-drop db-name) conn)) |
| 22 | + (r/run (r/db-create db-name) conn)) |
| 23 | + |
| 24 | +(defn setup-each [test-fn] |
| 25 | + (with-open [conn (r/connect :db test-db)] |
| 26 | + (-> (r/table test-table) |
| 27 | + (r/delete {:durability :soft :return-changes false}) |
| 28 | + (r/run conn)) |
| 29 | + (test-fn))) |
| 30 | + |
| 31 | +(defn setup-once [test-fn] |
| 32 | + (with-open [conn (r/connect)] |
| 33 | + (ensure-db test-db conn) |
| 34 | + (ensure-table test-table {:primary-key :national_no} conn) |
| 35 | + (test-fn) |
| 36 | + (r/run (r/db-drop test-db) conn))) |
| 37 | + |
| 38 | +(use-fixtures :each setup-each) |
| 39 | +(use-fixtures :once setup-once) |
| 40 | + |
| 41 | +(deftest always-return-async |
| 42 | + (with-open [conn (r/connect :async? true)] |
| 43 | + (are [query] (instance? ReadPort (r/run query conn)) |
| 44 | + (r/db-list) |
| 45 | + (-> (r/db :non-existent) (r/table :nope)) |
| 46 | + (-> (r/db test-db) (r/table test-table) (r/insert {:a 1}))))) |
| 47 | + |
| 48 | +(deftest async-results |
| 49 | + (let [conn (r/connect :async? true :db test-db) |
| 50 | + pokemon [{:national_no 25 :name "Pikachu"} |
| 51 | + {:national_no 26 :name "Raichu"}]] |
| 52 | + (are [expected query] (= (->> (r/run query conn) |
| 53 | + (async/into []) |
| 54 | + (async/<!!)) |
| 55 | + expected) |
| 56 | + [{:deleted 0 |
| 57 | + :errors 0 |
| 58 | + :inserted 2 |
| 59 | + :replaced 0 |
| 60 | + :skipped 0 |
| 61 | + :unchanged 0}] |
| 62 | + (-> (r/table test-table) |
| 63 | + (r/insert pokemon)) |
| 64 | + pokemon (-> (r/table test-table)) |
| 65 | + [pokemon] (-> (r/table test-table) (r/order-by :name)) |
| 66 | + |
| 67 | + ))) |
0 commit comments