-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathjson.test.js
More file actions
executable file
·89 lines (79 loc) · 3.08 KB
/
Copy pathjson.test.js
File metadata and controls
executable file
·89 lines (79 loc) · 3.08 KB
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
'use strict'
const { TestEnv } = require('./env/test-env')
const env = new TestEnv()
const chai = require('chai')
const expect = chai.expect
const sql = require('../lib/sql')
/* globals describe it beforeEach afterEach */
const { configureTestLogging } = require('./common/logging-helper')
// Configure logging based on environment variables
// By default, tests run silently. To enable logging:
// - MSNODESQLV8_TEST_VERBOSE=true npm test (for full trace logging)
// - MSNODESQLV8_TEST_LOG_LEVEL=DEBUG MSNODESQLV8_TEST_LOG_CONSOLE=true npm test
// - MSNODESQLV8_TEST_LOG_LEVEL=INFO MSNODESQLV8_TEST_LOG_FILE=/tmp/test.log npm test
configureTestLogging(sql)
describe('json', function () {
this.timeout(30000)
this.beforeEach(done => {
sql.logger.info('Starting test setup', 'json.test.beforeEach')
env.open().then(() => {
sql.logger.info('Test environment opened successfully', 'json.test.beforeEach')
done()
}).catch(e => {
sql.logger.error(`Failed to open test environment: ${e}`, 'json.test.beforeEach')
sql.logger.error(e)
})
})
this.afterEach(done => {
sql.logger.info('Starting test cleanup', 'json.test.afterEach')
env.close().then(() => {
sql.logger.info('Test environment closed successfully', 'json.test.afterEach')
done()
}).catch(e => {
sql.logger.error(`Failed to close test environment: ${e}`, 'json.test.afterEach')
sql.logger.error(e)
})
})
function insertRec (p, id, element) {
const txt = JSON.stringify(element, null, 4)
return p.promises.call({
ID: id++,
json: txt
}).then(() => {
return txt
})
}
it('use proc to insert a JSON array and bulk parse on server', async function handler () {
const tableName = 'employeeJson'
const procName = 'AddUpdateEmployeeJsonRecord'
const procNameJson = 'ParseJsonArray'
const h = env.jsonHelper(tableName, procName, procNameJson)
await h.create()
const parsedJSON = env.helper.getJSON()
const promises = env.theConnection.promises
const json = JSON.stringify(parsedJSON, null, 4)
const res = await promises.callProc(procNameJson, { json })
expect(Array.isArray(res.first))
const expected = parsedJSON.map(r => {
const { OrganizationNode, ...rest } = r
return rest
})
expect(res.first).to.deep.equals(expected)
})
it('use proc to insert a JSON based complex object', async function handler () {
const tableName = 'employeeJson'
const procName = 'AddUpdateEmployeeJsonRecord'
const procNameJson = 'ParseJsonArray'
const h = env.jsonHelper(tableName, procName, procNameJson)
await h.create()
const parsedJSON = env.helper.getJSON()
const promises = env.theConnection.promises
const p = await promises.getProc(procName)
let id = 0
const inserts = parsedJSON.map(r => insertRec(p, id++, r))
const expected = await Promise.all(inserts)
const selectedRecords = await promises.query(`select * from ${tableName} order by id asc`)
const selected = selectedRecords.first.map(rec => rec.json)
expect(selected).to.deep.equals(expected)
})
})