-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
160 lines (129 loc) · 4.52 KB
/
test.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { MongoClient, Collection, assertEquals } from './deps.ts'
import { Backend } from './mod.ts'
import { getOneLevel, setOneLevel } from './lib/props.ts'
// Global variable
const DB_HOST = '127.0.0.1'
const DB_PORT = 27017
const DB_NAME = 'test-i18n'
const DB_COL = 'i18n-data'
const LANG_FIELD_NAME = 'lng'
const NS_FIELD_NAME = 'ns'
const DATA_FIELD_NAME = 'data'
const BACKEND_BASE_OPTS = {
langFieldName: LANG_FIELD_NAME,
nsFieldName: NS_FIELD_NAME,
dataFieldName: DATA_FIELD_NAME,
}
const BACKEND_STANDARD_OPTS = {
hosts: [`${DB_HOST}:${DB_PORT}`],
dbName: DB_NAME,
colName: DB_COL,
...BACKEND_BASE_OPTS
}
const getBackendCollectionOpts = (collection: Collection) => ({
...BACKEND_BASE_OPTS,
collection
})
const TEST_DATA = [
{
[LANG_FIELD_NAME]: 'id',
[NS_FIELD_NAME]: 'translation',
[DATA_FIELD_NAME]: {
title: 'Halo Indonesia'
}
},
{
[LANG_FIELD_NAME]: 'en',
[NS_FIELD_NAME]: 'translation',
[DATA_FIELD_NAME]: {
title: 'Halo Indonesia'
}
},
]
// Helper function
async function prepareDatabase() {
const client = new MongoClient()
client.connectWithOptions({
hosts: [`${DB_HOST}:${DB_PORT}`]
})
const collection = client.database(DB_NAME).collection(DB_COL)
await collection.insertMany(TEST_DATA)
return collection
}
async function cleanDatabase(collection: Collection) {
// `mongo@v0.7.0` doesn't support `db.dropDatabase` functions. temporarily replaced by erasing all data in collection.
await collection.deleteMany({})
}
async function read(backend: Backend) {
for (let i = 0; i < TEST_DATA.length; i += 1) {
const doc = TEST_DATA[i]
const expectedData = doc[DATA_FIELD_NAME]
const targetData = await new Promise((resolve, reject) => backend.read(doc[LANG_FIELD_NAME], doc[NS_FIELD_NAME], (err, data) => err ? reject(err) : resolve(data)))
assertEquals(expectedData, targetData, 'Does not return correct translation data.')
}
}
async function readMultiTest(backend: Backend) {
const expected = {}
TEST_DATA.forEach((data) => {
const lang = data[LANG_FIELD_NAME];
const ns = data[NS_FIELD_NAME];
const val = data[DATA_FIELD_NAME]
if (getOneLevel(expected, lang))
setOneLevel(getOneLevel(expected, lang), ns, val)
else
setOneLevel(expected, lang, {
[ns]: val,
})
})
const target = await new Promise((resolve, reject) => backend.readMulti(TEST_DATA.map(doc => doc[LANG_FIELD_NAME]), TEST_DATA.map(doc => doc[NS_FIELD_NAME]), (err, data) => err ? reject(err) : resolve(data)))
assertEquals(expected, target, 'Does not return correct translation data.')
}
async function createTest(backend: Backend, collection: Collection) {
const expected = {
[LANG_FIELD_NAME]: 'de',
[NS_FIELD_NAME]: 'translation',
[DATA_FIELD_NAME]: {
title: 'Hallo Indonesien'
}
}
await new Promise((resolve, reject) => backend.create(expected[LANG_FIELD_NAME], expected[NS_FIELD_NAME], 'title', expected[DATA_FIELD_NAME].title, (err, data) => err ? reject(err) : resolve(data)))
const findOutput = await collection.findOne({
[LANG_FIELD_NAME]: expected[LANG_FIELD_NAME],
[NS_FIELD_NAME]: expected[NS_FIELD_NAME]
})
const target = {
[LANG_FIELD_NAME]: findOutput[LANG_FIELD_NAME],
[NS_FIELD_NAME]: findOutput[NS_FIELD_NAME],
[DATA_FIELD_NAME]: findOutput[DATA_FIELD_NAME]
}
assertEquals(expected, target, 'Does not write correct translation data.')
}
function wrapTest(testFunction: (collection: Collection) => Promise<void>) {
return async function () {
const collection = await prepareDatabase()
try {
await testFunction(collection)
} catch (error) {
await cleanDatabase(collection)
}
}
}
// Test
Deno.test('Read', wrapTest(async function () {
await read(new Backend(null, BACKEND_STANDARD_OPTS))
}))
Deno.test('Read Multi', wrapTest(async function () {
await readMultiTest(new Backend(null, BACKEND_STANDARD_OPTS))
}))
Deno.test('Create', wrapTest(async function (collection) {
await createTest(new Backend(null, BACKEND_STANDARD_OPTS), collection)
}))
Deno.test('Read with custom collection', wrapTest(async function (collection) {
await read(new Backend(null, getBackendCollectionOpts(collection)))
}))
Deno.test('Read Multi with custom collection', wrapTest(async function (collection) {
await readMultiTest(new Backend(null, getBackendCollectionOpts(collection)))
}))
Deno.test('Create with custom collection', wrapTest(async function (collection) {
await createTest(new Backend(null, getBackendCollectionOpts(collection)), collection)
}))