-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcombineJsAndCljsApi.spec.js
64 lines (57 loc) · 2.13 KB
/
combineJsAndCljsApi.spec.js
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
54
55
56
57
58
59
60
61
62
63
64
import {datascript, mori, helpers} from '../datascript-mori'
import {assert} from 'chai'
var djs = datascript.js // use datascript_mori.datascript.js API
var dcljs = datascript.core // cljs API for quering
var {hashMap, vector, parse, toJs, equals, isMap, hasKey, isSet, set, getIn, get} = mori
var {DB_VALUE_TYPE, DB_TYPE_REF, DB_ADD, DB_ID, TEMPIDS} = helpers
describe('Use regular JS API for create connection and add data to DB', () => {
// schema is JS Object
var schema = {"aka": {":db/cardinality": ":db.cardinality/many"}, "friend": {":db/valueType": ":db.type/ref"}};
var conn = djs.create_conn(schema);
var reports = []
djs.listen(conn, "main", report => {
reports.push(report)
})
// Tx is Js Array of Object or Array
djs.transact(conn, [
{
":db/id": -1,
"name": "Ivan",
"age": 18,
"aka": ["X", "Y"]
},
{
":db/id": -2,
"name": "Igor",
"aka": ["Grigory", "Egor"]
},
[":db/add", -1, "friend", -2]
], "initial info about Igor and Ivan")
it('report is regular JS object', () => {
assert(typeof reports[0] === 'object', 'is Object')
assert(
reports[0].hasOwnProperty('tx_data')
&& reports[0].hasOwnProperty('tx_meta')
&& reports[0].hasOwnProperty('db_after')
&& reports[0].hasOwnProperty('db_before')
&& reports[0].hasOwnProperty('tempids'), 'reports has all keys')
assert(reports[0].tempids["-1"] === 1, 'Ivan ID equal 1')
assert(reports[0].tempids["-2"] === 2, 'Igor ID equal 2')
})
it('query mori values from conn with CLJS API', () => {
var result = dcljs.q(parse('[:find ?n :in $ ?a :where [?e "friend" ?f] [?e "age" ?a] [?f "name" ?n]]'), djs.db(conn), 18);
assert(isSet(result), 'result is mori set')
assert(equals(result, set([vector("Igor")])), 'result equals #{["Igor"]}')
})
it('cas is just work', () => {
var conn = djs.create_conn();
djs.transact(conn, [
[':db/add', 1, 'weight', 200]
]);
djs.transact(conn, [
[':db.fn/cas', 1, 'weight', 200, 300]
]);
var e = djs.entity(djs.db(conn), 1);
assert(e.get('weight') === 300, 'CAS changed weight correctly');
});
})