UNPKG

5.13 kBJavaScriptView Raw
1/* eslint-env mocha */
2
3'use strict'
4
5const expect = require('chai').expect
6const tymly = require('@wmfs/tymly')
7const path = require('path')
8const process = require('process')
9
10const sqlScriptRunner = require('./fixtures/sql-script-runner.js')
11const studentsModels = require('./fixtures/school-blueprint/models/students.json')
12const studentsSearchDocs = require('./fixtures/school-blueprint/search-docs/students.json')
13
14// note that state-machines usually inherit their namespace from the blueprint.json file.
15// the studentsAndStaffModels json files have explicitly defined the namespaces as
16// they are used by tests as-is, and so tymly will automatically "inherit" it
17const studentsAndStaffModels = require('./fixtures/test-resources/students-and-staff-models.json')
18const studentsAndStaffSearchDocs = require('./fixtures/test-resources/students-and-staff-search-docs.json')
19
20describe('tymly-solr-plugin tests', function () {
21 this.timeout(process.env.TIMEOUT || 5000)
22
23 let tymlyService
24 let solrService
25 let client
26
27 before(function () {
28 if (process.env.PG_CONNECTION_STRING && !/^postgres:\/\/[^:]+:[^@]+@(?:localhost|127\.0\.0\.1).*$/.test(process.env.PG_CONNECTION_STRING)) {
29 console.log(`Skipping tests due to unsafe PG_CONNECTION_STRING value (${process.env.PG_CONNECTION_STRING})`)
30 this.skip()
31 }
32 })
33
34 it('should create some basic tymly services', function (done) {
35 tymly.boot(
36 {
37 pluginPaths: [
38 path.resolve(__dirname, './../lib'),
39 require.resolve('@wmfs/tymly-pg-plugin'),
40 require.resolve('@wmfs/tymly-test-helpers/plugins/allow-everything-rbac-plugin')
41 ],
42 blueprintPaths: [
43 path.resolve(__dirname, './fixtures/school-blueprint')
44 ],
45 config: {
46 solrSchemaFields: [
47 'id',
48 'actorName',
49 'characterName'
50 ]
51 }
52 },
53 function (err, tymlyServices) {
54 expect(err).to.eql(null)
55 tymlyService = tymlyServices.tymly
56 client = tymlyServices.storage.client
57 solrService = tymlyServices.solr
58 done()
59 }
60 )
61 })
62
63 it('should generate a SQL SELECT statement', function () {
64 const select = solrService.buildSelectStatement(studentsModels, studentsSearchDocs)
65
66 expect(select).to.be.a('string')
67 expect(select).to.eql('SELECT \'student#\' || student_no AS id, upper(first_name || \' \' || last_name) AS actor_name, ' +
68 'upper(character_name) AS character_name FROM tymly_test.students')
69 })
70
71 it('should generate a SQL CREATE VIEW statement', function () {
72 const sqlString = solrService.buildCreateViewStatement(
73 studentsAndStaffModels, studentsAndStaffSearchDocs)
74
75 expect(sqlString).to.be.a('string')
76 expect(sqlString).to.eql('CREATE OR REPLACE VIEW tymly.solr_data AS \n' +
77 'SELECT \'student#\' || student_no AS id, first_name || \' \' || last_name AS actor_name, ' +
78 'character_name AS character_name FROM tymly_test.students\n' +
79 'UNION ALL\n' +
80 'SELECT \'staff#\' || staff_no AS id, first_name || \' \' || last_name AS actor_name, ' +
81 'character_first_name || \' \' || character_last_name AS character_name FROM tymly_test.staff;')
82 })
83
84 it('should have generated a SQL CREATE VIEW statement on tymly boot', function () {
85 expect(solrService.createViewSQL).to.be.a('string')
86 })
87
88 it('should create test resources', function (done) {
89 sqlScriptRunner(
90 './db-scripts/setup.sql',
91 client,
92 err => done(err)
93 )
94 })
95
96 it('should create a database view using the test resources', function (done) {
97 const createViewStatement = solrService.buildCreateViewStatement(
98 studentsAndStaffModels, studentsAndStaffSearchDocs)
99
100 client.query(createViewStatement, [], err => done(err))
101 })
102
103 it('should return 19 rows when selecting from the view', function (done) {
104 client.query(`SELECT * FROM tymly.solr_data ORDER BY character_name ASC;`, [],
105 function (err, result) {
106 if (err) {
107 return done(err)
108 }
109 try {
110 expect(result.rowCount).to.eql(19)
111 expect(result.rows[0].id).to.eql('staff#1')
112 expect(result.rows[18].id).to.eql('staff#3')
113 done()
114 } catch (e) {
115 done(e)
116 }
117 }
118 )
119 })
120
121 // it('should instruct apache solr to index data from the view', function (done) {
122 // solrService.executeSolrFullReindex('addressbase', function (err, solrResponse) {
123 // expect(err).to.eql(null)
124 // if (err) {
125 // debug(err)
126 // } else {
127 // debug('total number of database rows indexed: ', solrResponse.statusMessages['Total Rows Fetched'])
128 // }
129 // done()
130 // })
131 // })
132
133 it('should cleanup test resources', function (done) {
134 sqlScriptRunner(
135 './db-scripts/cleanup.sql',
136 client,
137 err => done(err)
138 )
139 })
140
141 it('should shutdown Tymly', async () => {
142 await tymlyService.shutdown()
143 })
144})