git ssb

2+

Dominic / flumedb



Tree: 8f18c58c8aa15bbde2a5464401866c1dc61bf372

Files: 8f18c58c8aa15bbde2a5464401866c1dc61bf372 / test / memlog.js

2067 bytesRaw
1var statistics = require('statistics')
2var pull = require('pull-stream')
3
4var tape = require('tape')
5
6var Flume = require('../')
7
8var MemLog = require('flumelog-memory')
9var Reduce = require('flumeview-reduce')
10
11module.exports = function (db) {
12 db.use('stats', Reduce(1, function (acc, data) {
13 return statistics(acc, data.foo)
14 }))
15
16 tape('empty db', function (t) {
17 db.stats.get(function (err, value) {
18 if(err) throw err
19 t.equal(value, undefined)
20 t.end()
21 })
22 })
23
24 tape('simple', function (t) {
25
26 db.since(function (v) {
27 console.log("SINCE", v)
28 }, false)
29
30 db.append({foo: 1}, function (err, seq) {
31 if(err) throw err
32 console.log("GET", err, seq, db.stats)
33 db.stats.get([], function (err, value) {
34 if(err) throw err
35 console.log( "GET", value)
36 t.deepEqual(value.mean, 1)
37 t.deepEqual(value.stdev, 0)
38 t.end()
39 })
40 })
41 })
42
43 tape('append', function (t) {
44 db.append({foo: 3}, function (err, seq) {
45 if(err) throw err
46 console.log("GET", err, seq)
47 db.stats.get([], function (err, value) {
48 if(err) throw err
49 console.log( "GET", value)
50 t.deepEqual(value.mean, 2)
51 t.deepEqual(value.stdev, 1)
52 t.end()
53 })
54 })
55 })
56
57 tape('get items in stream', function (t) {
58 pull(
59 db.stream({seqs: true, values: false}),
60 pull.asyncMap(function (seq, cb) {
61 db.get(seq, cb)
62 }),
63 pull.collect(function (err, ary) {
64 if(err) throw err
65 t.deepEqual(ary, [{foo: 1}, {foo: 3}])
66 t.end()
67 })
68 )
69
70 })
71
72 tape('disable ready to stall all reads', function (t) {
73 var called = false
74 db.ready.set(false)
75 db.stats.get([], function (err, value) {
76 called = true
77 })
78 setTimeout(function () {
79 t.equal(db.ready.value, false)
80 t.equal(called, false)
81 db.ready.set(true)
82 t.equal(called, true) //this should fire immediately
83 t.end()
84 }, 100)
85
86 })
87
88}
89
90if(!module.parent)
91 module.exports(Flume(MemLog()))
92
93

Built with git-ssb-web