/
count.ts
69 lines (53 loc) · 1.56 KB
/
count.ts
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
65
66
67
68
69
import test from 'ava'
import { connect } from '../src'
const db = connect(':memory:')
const persons = [
{ name: 'Dale', age: 30 },
{ name: 'Lelu', age: 6 },
{ name: 'Gurlak', age: 302 }
]
test.before(async () => {
await db.model('people', {
name: { type: String, primary: true },
age: Number
})
await Promise.all(persons.map(person => db.create('people', person)))
})
test.after.always(() => db.close())
test('returns the number of models when parameter count === 0', async t => {
t.is(await db.count(), 1)
await db.model('count_crayons', { color: String })
t.is(await db.count(), 2)
})
test('returns the total number of rows', async t => {
const res = await db.count('people')
t.is(res, 3)
})
test('returns the number of matching rows', async t => {
const res = await db.count('people', ['age', '<', 200])
t.is(res, 2)
})
test('allows for multiple where clauses', async t => {
const res = await db.count('people', [
['age', '<', 200],
['age', '>', 20]
])
t.is(res, 1)
})
test('countIn() variant counts on the given column', async t => {
const db = connect(':memory:')
type Person = {
name: string,
// allow null in order to test that null values
// in the target column aren't counted
age: number | null
}
const people = await db.model<Person>('people', {
name: { type: String, primary: true },
age: Number
})
await Promise.all(persons.map(async person => people.create(person)))
await people.create({ name: '', age: null })
const res = await people.countIn('age')
t.is(res, persons.length)
})