Created
March 12, 2015 16:57
-
-
Save deanlandolt/849eb63d28f7e7bb8c97 to your computer and use it in GitHub Desktop.
tuples/test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var test = require('tape'); | |
var bytewise = require('bytewise'); | |
var encode = bytewise.encode; | |
var MIN = bytewise.MIN; | |
var MAX = bytewise.MAX; | |
test('tuple queries', function (t) { | |
var yearly = tuples('reports', 'yearly'); | |
// component keys of the tuple space query are available by index | |
t.equal(yearly[0], 'reports'); | |
t.equal(yearly[1], 'yearly'); | |
// the underlying array can be retrieved with the `valueOf` method | |
t.deepEqual(yearly.valueOf(), [ 'reports', 'yearly' ]); | |
// the tuple space query can be used as an explicit key value | |
// the `key` accessor is available to compute and memoize bytewise-encoded key | |
t.equal(yearly.key, encode([ 'reports', 'yearly' ])); | |
// the `range` accessor can be used to create a query stream config object | |
t.deepEqual(yearly.range, { | |
gt: encode([ 'reports', 'yearly', MIN ]), | |
lt: encode([ 'reports', 'yearly', MAX ]) | |
}); | |
// a tuple space query ranges over all child tuples spaces by default | |
// this can be constrained by clamping a query's range | |
var since2010 = yearly.clamp({ gte: 2010 }); | |
t.deepEqual(since2010.range, { | |
gte: encode([ 'reports', 'yearly', 2010 ]), | |
lt: encode([ 'reports', 'yearly', MAX ]) | |
}); | |
// a clamped query can be narrowed further | |
var from2010to2020 = since2010.clamp({ lte: 2020 }); | |
t.deepEqual(from2010to2020.range, { | |
gte: encode([ 'reports', 'yearly', 2010 ]), | |
lte: encode([ 'reports', 'yearly', 2020 ]) | |
}); | |
// and further | |
var from2010to2020exclusive = from2010to2020.clamp({ lt: 2020 }); | |
t.deepEqual(from2010to2020exclusive.range, { | |
gte: encode([ 'reports', 'yearly', 2010 ]), | |
lt: encode([ 'reports', 'yearly', 2020 ]) | |
}); | |
// but a query's range is not automatically widened | |
var from2010to2020_ = from2010to2020.clamp({ gte: 2000 }); | |
t.deepEqual(from2010to2020_.range, from2010to2020.range); | |
// a query may be constrained by restricting to a specific subspace | |
var in2015 = yearly.subspace(2015); | |
t.equal(in2015.key, encode([ 'reports', 'yearly', 2015 ])); | |
t.deepEqual(in2015.range, { | |
gt: encode([ 'reports', 'yearly', 2015, MIN ]), | |
lt: encode([ 'reports', 'yearly', 2015, MAX ]) | |
}); | |
// any query range can be restricted to a subspace in this way | |
var in2015_ = from2010to2020.subspace(2015); | |
t.deepEqual(in2015, in2015_); | |
// however the subspace key provided must exist within the range | |
t.equal(from2010to2020.subspace(2000), tuples.EMPTY); | |
// multiple subspace keys can be provided at once | |
var tpsReports2015 = yearly.subspace(2015, 'tps'); | |
t.deepEqual(tpsReports2015.range, { | |
gt: encode([ 'reports', 'yearly', 2015, 'tps', MIN ]), | |
lt: encode([ 'reports', 'yearly', 2015, 'tps', MAX ]) | |
}); | |
t.deepEqual(tpsReports2015, in2015.subspace('tps')); | |
t.end(); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment