Skip to content

Commit

Permalink
test(release-js): add test and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alef committed Mar 8, 2016
1 parent 2b751f2 commit b3edf16
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target/
release-js/node_modules
.lein-git-deps
release-js/datascript-mori.bare.js
release-js/datascript-mori.js
4 changes: 4 additions & 0 deletions release-js/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015", "stage-0", "stage-1", "stage-2"],
"only": "test/*.spec.js"
}
14 changes: 14 additions & 0 deletions release-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
"node": ">=0.8.22"
},
"dependencies": {},
"devDependencies": {
"babel-core": "^6.5.0",
"babel-cli": "^6.5.0",
"babel-register": "^6.3.13",
"babel-preset-es2015": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.5.0",
"chai": "~3.5.0",
"mocha": "^2.3.4"
},
"scripts": {
"test": "mocha --harmony --compilers js:babel-core/register test/*.spec.js"
},
"files": [
"datascript-mori.js"
]
Expand Down
52 changes: 52 additions & 0 deletions release-js/test/combineJsAndCljsApi.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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"]}')
})
})
114 changes: 114 additions & 0 deletions release-js/test/onlyCljsApiUsage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {datascript, mori, helpers} from '../datascript-mori'
import {assert} from 'chai'
var d = datascript.core // use datascript_mori.datascript.core API
var djs = datascript.js
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('add data to DB and query them', () => {
// scheme must be a mori structure or use helpers.schema_to_clj({friend: {":db/valueType": ":db.type/ref"}})
var scheme = hashMap(
"friend", hashMap(
DB_VALUE_TYPE, DB_TYPE_REF // or just keyword("db/valueType"), keyword("db.type/ref")
)
)
var db = d.empty_db(scheme)
// TX must be a Mori struct - vector of vector or hashMap

var dbWithData = d.db_with(db, vector(
vector(DB_ADD, -1, "name", "Ivan"),
vector(DB_ADD, -1, "age", 17),
hashMap(
DB_ID, -2,
"name", "Igor",
"age", 35
),
vector(DB_ADD, -1, "friend", -2)
)) // add data to db.
/* or use helpers.entities_to_clj( ....])
var dbWithData = d.db_with(db,
helpers.entities_to_clj([
[":db/add", 1, "name", "Ivan"],
[":db/add", 1, "age", 17],
{
":db/id": 2,
"name": "Igor",
"age": 35
},
[":db/add", 1, "friend", 2]
])
);
*/

it('query all user name for users with age equals 17', () => {
// `parse` convert EDN string to mori structures. Also see babel-plugin-datascript for compile-time converting
var queryResponse = d.q(parse('[:find ?n :in $ ?a :where [?e "name" ?n] [?e "age" ?a]]'), dbWithData, 17)

assert(isSet(queryResponse), 'query response is Mori Set');
assert(
equals(queryResponse, set([vector("Ivan")])) // #{["Ivan"]}
, 'query response struct is true');

});

it('query all user name and age', () => {
var queryResponse = d.q(parse('[:find ?n ?a :where [?e "name" ?n] [?e "age" ?a]]'), dbWithData)

assert(isSet(queryResponse), 'query response is Mori Set');
assert(
equals(queryResponse, set([vector("Igor", 35), vector("Ivan", 17)])) // #{["Igor", 35] ["Ivan", 17]}
, 'query response struct is true');
});
});

describe('create conn and use transact API', () => {
var scheme = hashMap(
"friend", hashMap(
DB_VALUE_TYPE, DB_TYPE_REF // or just keyword("db/valueType"), keyword("db.type/ref")
)
)
var conn = d.create_conn(scheme)
var reportsFromListen = []
d.listen_BANG_(conn, 'main', report => { // or djs.listen(conn, "main", callback) is fully equal definition
reportsFromListen.push(report);
})

var firstReport = d.transact_BANG_(conn, vector(
vector(DB_ADD, -1, "name", "Ivan"),
vector(DB_ADD, -1, "age", 17)
))

var secondReport = d.transact_BANG_(conn, vector(
hashMap(
DB_ID, -1,
"name", "Igor",
"age", 35
)
))

it('reports equals callback argument and is mori datastucts', () => {
assert(equals(firstReport, reportsFromListen[0]), 'firstReport equal first callback arg of d.listen');
assert(equals(secondReport, reportsFromListen[1]), 'secondReport equal first callback arg of d.listen');
assert(isMap(firstReport) && isMap(secondReport), 'reports is mori struct');
})

var ivanId = getIn(firstReport, [TEMPIDS, -1])
var igorId = getIn(secondReport, [TEMPIDS, -1])
it('resolve tempid from reports', () => {
assert(ivanId === 1, 'First Id === 1')
assert(igorId === 2, 'Second Id === 2')
})

d.transact_BANG_(conn, vector(
vector(DB_ADD, ivanId, "friend", igorId)
))

it('query name friends of Igor from conn', () => {
var result = d.q(parse('[:find ?n :in $ ?person :where [?e "name" ?n] [?e "friend" ?person]]'),
djs.db(conn), igorId
)
assert(isSet(result), 'result of query is Mori Set')
assert(equals(result, set([vector("Ivan")])), 'result is #{["Ivan"]}')
})
d.unlisten_BANG_(conn, "main") //or djs.unlisten(conn, "main", callback) is fully equal definition
});

0 comments on commit b3edf16

Please sign in to comment.