-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
100 lines (74 loc) · 2.2 KB
/
test.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict'
const join = require('psjoin')
const {Binary, MongoClient} = require('mongodb')
const test = require('tape')
const muuid = require('./')
const rx = /^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$/
const create = () => muuid.create(Binary)
const parse = (i) => muuid.parse(Binary, i)
const stringify = muuid.stringify
const isValid = muuid.isValid
test('Create', function (t) {
t.ok(create(), 'create()')
t.notEqual(create(), create(), 'unique')
t.equal(create().buffer.length, 16)
t.ok(muuid(Binary), 'muuid(Binary)')
t.notEqual(muuid(Binary), muuid(Binary), 'unique')
t.equal(muuid(Binary).buffer.length, 16)
const id = create()
t.deepEqual(parse(stringify(id)), id)
t.end()
})
test('Stringify', function (t) {
const uuid = create()
t.ok(rx.exec(stringify(uuid)), 'stringify')
t.end()
})
test('Parse', function (t) {
const i = 'dcc090ea-a65b-4ea4-9d91-22310bdad8af'
t.ok(parse(i), '.parse')
t.ok(muuid(Binary, i), '.parse')
t.equal(stringify(muuid(Binary, i)), i)
t.equal(stringify(parse(i)), i)
t.throws(() => parse('bad'), muuid.ParseError)
t.throws(
() => parse('notahexa-aaab-4ea4-9d91-22310bdad8af'),
muuid.ParseError
)
t.end()
})
test('Is valid', function (t) {
t.equal(typeof muuid.isValid, 'function')
t.equal(isValid(), false)
t.equal(isValid(2), false)
t.equal(isValid(false), false)
t.equal(isValid(''), false)
t.equal(isValid(''), false)
t.equal(isValid('xcc090ea-a65b-4ea4-9d91-22310bdad8af'), false)
t.equal(isValid('xdcc090eaa65b4ea49d9122310bdad8af'), false)
t.equal(isValid('dcc090ea-a65b-4ea4-9d91-22310bdad8af'), true)
t.equal(isValid('dcc0-90ea-a6-5b-4ea4-9d91-22310-bdad8af'), true)
t.equal(isValid('dcc090eaa65b4ea49d9122310bdad8af'), true)
t.end()
})
test('DB', function (t) {
t.plan(1)
const client = new MongoClient(
'mongodb://localhost:' + process.env.MONGODB_PORT + '/test'
)
const db = client.db()
const i = 'dcc090ea-a65b-4ea4-9d91-22310bdad8af'
const insert = db.collection('docs').insertOne({
_id: parse(i),
})
const found = join(insert, () =>
db
.collection('docs')
.find({
_id: parse(i),
})
.limit(1)
.next()
).then((doc) => t.equal(stringify(doc._id), i))
join(client, found, (c) => c.close())
})