UNPKG

3.26 kBJavaScriptView Raw
1var test = require('tape')
2var levelup = require('levelup')
3var through = require('through2')
4var inherits = require('util').inherits
5
6var model = require('./index')
7
8var db = levelup('test', {
9 db: require('memdown'),
10 valueEncoding: 'json'
11})
12
13function Example (db, opts) {
14 model.call(this, db, opts)
15}
16
17inherits(Example, model)
18
19var example = new Example(db, {
20 modelName: 'example',
21 properties: {
22 key: { type: 'string' },
23 test: { type: 'string' },
24 ok: { type: 'integer' }
25 },
26 indexKeys: ['test', 'ok'],
27 required: ['test']
28})
29
30test('model exists', function (t) {
31 t.ok(example, 'example model exists')
32 t.ok(example.schema, 'schema of example model exists')
33 t.end()
34})
35
36test('create a model', function (t) {
37 var data = {
38 test: 'first instance!',
39 ok: 1
40 }
41
42 example.create(data, function (err, instance) {
43 t.ifError(err, 'no error')
44 t.ok(instance, 'instance of model exists')
45 t.ok(instance.key, 'has a key')
46 t.end()
47 })
48})
49
50test('get a model', function (t) {
51 var data = {
52 test: 'second instance!',
53 ok: 2
54 }
55
56 example.create(data, function (err, instance) {
57 example.get(instance.key, function (err, retrieved) {
58 t.ifError(err, 'no error')
59 t.ok(retrieved, 'instance of model exists')
60 t.ok(retrieved.key, 'has a key')
61 t.end()
62 })
63 })
64})
65
66test('update a model', function (t) {
67 var data = {
68 test: 'third instance!',
69 ok: 3
70 }
71
72 example.create(data, function (err, instance) {
73 instance.ok = 2
74
75 example.update(instance, function (err, updated) {
76 t.ifError(err, 'no error')
77 t.equals(updated.ok, 2)
78 t.end()
79 })
80 })
81})
82
83test('reject model creation if it doesnt fit the schema' , function (t) {
84 var data = {
85 test: 'huh',
86 ok: 'oops' // should be an integer
87 }
88
89 example.create(data, function (err, instance) {
90 t.ok(err, 'error saying the object does not match the schema')
91 t.notOk(instance, 'instance of model not created')
92 t.end()
93 })
94})
95
96test('list models', function (t) {
97 var count = 0
98
99 function iterator (item, enc, next) {
100 count++
101 next()
102 }
103
104 function end () {
105 t.equals(count, 3)
106 t.end()
107 }
108
109 example.createReadStream().pipe(through.obj(iterator, end))
110})
111
112test('find models by indexed key', function (t) {
113 var count = 0
114
115 function iterator (item, enc, next) {
116 count++
117 next()
118 }
119
120 function end (a, b, c) {
121 t.end()
122 }
123
124 example.find('ok', 2).pipe(through.obj(iterator, end))
125})
126
127test('create, update, delete events', function (t) {
128 var data = {
129 test: 'fourth instance!',
130 ok: 4
131 }
132
133 example.on('create', function (model) {
134 t.ok(model)
135 })
136
137 example.on('update', function (model) {
138 t.ok('model')
139 })
140
141 example.on('delete', function () {
142 t.ok(true)
143 })
144
145 example.create(data, function (err, instance) {
146 instance.ok = 2
147
148 example.update(instance, function (err, updated) {
149 t.ifError(err, 'no error')
150 t.equals(updated.ok, 2)
151 t.end()
152 })
153 })
154})
155
156test('delete models', function (t) {
157 example.createReadStream()
158 .on('data', function (data) {
159 example.delete(data.key, function (err) {
160 t.ifError(err, 'no error')
161 })
162 })
163 .on('end', function () {
164 t.end()
165 })
166})