Browse Source

add tests for different params for where clause

Kieran Gibb 2 years ago
parent
commit
1132af99a4
No known key found for this signature in database
2 changed files with 131 additions and 4 deletions
  1. 1
    1
      index.js
  2. 130
    3
      test/index.test.js

+ 1
- 1
index.js View File

@@ -61,7 +61,7 @@ class GroupStore {
61 61
       return response
62 62
     } catch (err) {
63 63
       debug(err)
64
-      return false
64
+      return []
65 65
     }
66 66
   }
67 67
 

+ 130
- 3
test/index.test.js View File

@@ -41,7 +41,135 @@ describe('group store: basic', (context) => {
41 41
     cleanup(storage, next)
42 42
   })
43 43
 
44
-  context('where()', async function (assert, next) {
44
+  context('where({ name, address })', async function (assert, next) {
45
+    let storage = tmp(),
46
+      config = Config(storage),
47
+      params = Object.assign(crypto.keySet(), { name: 'magma' })
48
+
49
+    config.groups.set(params.address, params)
50
+
51
+    const store = Store(Decrypted, storage, { config })
52
+
53
+    await store.ready()
54
+
55
+    var response = await store.where({
56
+      name: 'magma',
57
+      address: params.address
58
+    })
59
+
60
+    assert.ok(Array.isArray(response), 'returns an array')
61
+    var group = response[0]
62
+    assert.ok(group, 'finds a group')
63
+    assert.same(group && group.address, params.address, 'finds the correct group')
64
+    cleanup(storage, next)
65
+  })
66
+
67
+  context('where({ name, address: null })', async function (assert, next) {
68
+    let storage = tmp(),
69
+      config = Config(storage),
70
+      params = Object.assign(crypto.keySet(), { name: 'magma' })
71
+
72
+    config.groups.set(params.address, params)
73
+
74
+    const store = Store(Decrypted, storage, { config })
75
+
76
+    await store.ready()
77
+
78
+    var response = await store.where({
79
+      name: 'magma',
80
+      address: null
81
+    })
82
+
83
+    assert.ok(Array.isArray(response), 'returns an array')
84
+    var group = response[0]
85
+    assert.notOk(group, 'fails to find a group')
86
+    cleanup(storage, next)
87
+  })
88
+
89
+  context('where({ name: null, address })', async function (assert, next) {
90
+    let storage = tmp(),
91
+      config = Config(storage),
92
+      params = Object.assign(crypto.keySet(), { name: 'magma' })
93
+
94
+    config.groups.set(params.address, params)
95
+
96
+    const store = Store(Decrypted, storage, { config })
97
+
98
+    await store.ready()
99
+
100
+    var response = await store.where({
101
+      name: null,
102
+      address: params.address
103
+    })
104
+
105
+    assert.ok(Array.isArray(response), 'returns an array')
106
+    var group = response[0]
107
+    assert.notOk(group, 'fails to find a group')
108
+    cleanup(storage, next)
109
+  })
110
+
111
+  context('where({ name, address: "wrong address" })', async function (assert, next) {
112
+    let storage = tmp(),
113
+      config = Config(storage),
114
+      params = Object.assign(crypto.keySet(), { name: 'magma' })
115
+
116
+    config.groups.set(params.address, params)
117
+
118
+    const store = Store(Decrypted, storage, { config })
119
+
120
+    await store.ready()
121
+
122
+    var response = await store.where({
123
+      name: 'magma',
124
+      address: crypto.randomBytes(32).toString('hex')
125
+    })
126
+
127
+    var group = response[0]
128
+    assert.notOk(group, 'fails to find a group')
129
+    cleanup(storage, next)
130
+  })
131
+
132
+  context('where({ name: "wrong name", address })', async function (assert, next) {
133
+    let storage = tmp(),
134
+      config = Config(storage),
135
+      params = Object.assign(crypto.keySet(), { name: 'magma' })
136
+
137
+    config.groups.set(params.address, params)
138
+
139
+    const store = Store(Decrypted, storage, { config })
140
+
141
+    await store.ready()
142
+
143
+    var response = await store.where({
144
+      name: 'cobox',
145
+      address: params.address
146
+    })
147
+
148
+    var group = response[0]
149
+    assert.notOk(group, 'fails to find a group')
150
+    cleanup(storage, next)
151
+  })
152
+
153
+  context('where({ name })', async function (assert, next) {
154
+    let storage = tmp(),
155
+      config = Config(storage),
156
+      params = Object.assign(crypto.keySet(), { name: 'magma' })
157
+
158
+    config.groups.set(params.address, params)
159
+
160
+    const store = Store(Decrypted, storage, { config })
161
+
162
+    await store.ready()
163
+
164
+    var response = await store.where({ name: 'magma' })
165
+    assert.ok(Array.isArray(response), 'returns an array')
166
+    var group = response[0]
167
+    assert.ok(group, 'finds a group')
168
+    assert.same(group && group.address, params.address, 'finds the correct group')
169
+    cleanup(storage, next)
170
+  })
171
+
172
+  context('where({ address })', async function (assert, next) {
45 173
     let storage = tmp(),
46 174
       config = Config(storage),
47 175
       params = crypto.keySet()
@@ -52,8 +180,7 @@ describe('group store: basic', (context) => {
52 180
 
53 181
     await store.ready()
54 182
 
55
-    delete params.encryptionKey
56
-    var response = await store.where(params)
183
+    var response = await store.where({ address: params.address })
57 184
     assert.ok(Array.isArray(response), 'returns an array')
58 185
     var group = response[0]
59 186
     assert.ok(group, 'finds a group')