-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
64 lines (56 loc) · 1.54 KB
/
tests.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
var _ = require("lodash");
var test = require("tape");
var levelup = require("levelup");
var memdown = require("memdown");
var safejson = require("./");
var assertRecode = function(db, t, input, has_in_err, output, has_out_err){
var key = _.uniqueId();
db.put(key, input, function(err){
if(has_in_err){
t.ok(err, "expected write error");
return;
}else{
if(err) return t.fail(err + "");
}
db.get(key, function(err, data){
if(has_out_err){
t.ok(err, "expected write error");
return;
}else{
if(err) return t.fail(err + "");
}
t.equals(data, output);
});
});
};
test("the problem exists in level-codec json", function(t){
var db = levelup("/blah0", {
db: memdown,
keyEncoding: "utf-8",
valueEncoding: "json"
});
var a = _.partial(assertRecode, db, t);
t.plan(4);
a("a", false, "a", false);
a(null, false, null, false);
a(NaN, false, null, false);
a(void 0, false, void 0, true);//expected bad behavior
});
test("the problem is fixed with this codec", function(t){
var db = levelup("/blah1", {
db: memdown,
keyEncoding: "utf-8",
valueEncoding: safejson
});
var a = _.partial(assertRecode, db, t);
t.plan(4);
a("a", false, "a", false);
a(null, false, null, false);
a(NaN, false, null, false);
a(void 0, false, null, false);//notice it coreced to null
});
test("decode an undefined encoded by the default json codec", function(t){
//default json codec encodes the key to ""
t.equals(safejson.decode(""), null);
t.end();
});