UNPKG

4.42 kBJavaScriptView Raw
1'use strict';
2
3const Config = require('getconfig');
4
5let lab = exports.lab = require('lab').script();
6let expect = require('code').expect;
7let describe = lab.describe;
8let it = lab.test;
9let before = lab.before;
10let after = lab.after;
11
12let Gatepost = require('../index');
13let pg = require('pg');
14let SQL = require('sql-template-strings');
15let Joi = require('joi');
16let knex = require('knex')({ client: 'pg', connection: Config.db.uri });
17
18Gatepost.setConnection(Config.db.uri);
19
20let dbDone, db;
21
22let Author = new Gatepost.Model({
23 name: {
24 validate: Joi.string()
25 },
26 id: {}
27}, {
28 cache: true,
29 name: 'Author'
30});
31
32let Book = new Gatepost.Model({
33 title: {
34 validate: Joi.string()
35 },
36 id: {}
37}, {
38 cache: true,
39 name: 'Book'
40});
41
42
43Author.fromSQL({
44 name: 'createTable',
45 sql: () => `CREATE TEMP TABLE authors_tmp
46 (id SERIAL PRIMARY KEY, name TEXT)`
47});
48
49Author.fromSQL({
50 name: 'createTable2',
51 sql: () => `CREATE TEMP TABLE books_tmp
52 (id SERIAL PRIMARY KEY, author_id INTEGER REFERENCES authors_tmp(id), title TEXT)`
53});
54
55Author.fromSQL({
56 name: 'dropAuthor',
57 sql: () => `DROP TABLE authors_tmp`
58});
59
60Book.fromSQL({
61 name: 'dropBook',
62 sql: () => `DROP TABLE books_tmp`
63});
64
65Author.fromSQL({
66 name: 'addAuthor',
67 sql: (args, model) => SQL`INSERT INTO authors_tmp (name) VALUES (${model.name}) RETURNING id, name`,
68 instance: true,
69 oneResult: true
70});
71
72Author.fromSQL({
73 name: 'addAuthorKnex',
74 sql: (args, model) => knex('authors_tmp').insert({ name: model.name }).returning(['id', 'name']),
75 instance: true,
76 oneResult: true
77});
78
79Author.fromSQL({
80 name: 'queryWithValidate',
81 sql: (args, model) => SQL`SELECT * FROM authors_tmp WHERE name=${args.name}`,
82 validate: {
83 name: Joi.string()
84 }
85});
86
87Author.fromSQL({
88 name: 'multiQuery',
89 sql: (args, model) => {
90 return [
91 SQL`SELECT * FROM authors_tmp WHERE id=1`,
92 SQL`SELECT * FROM authors_tmp WHERE id=2`,
93 SQL`SELECT * FROM authors_tmp WHERE id=1`,
94 ];
95 }
96})
97
98
99describe('Add and remove', () => {
100
101 before((done) => {
102
103 Author.createTable((err) => {
104
105 if (err) {
106 throw err;
107 }
108 Author.createTable2((err) => {
109
110 if (err) {
111 throw err;
112 }
113 done();
114 });
115 });
116 });
117
118 after((done) => {
119
120 Book.dropBook()
121 .then(Author.dropAuthor)
122 .then(() => {
123
124 pg.end();
125 done();
126 })
127 .catch((err) => console.log(err));
128 });
129
130 it('instance: create row', (done) => {
131
132 let author = Author.create({name: 'Nathan Fritz'});
133 author.addAuthor((err, a2) => {
134
135 expect(err).to.be.null();
136 expect(a2.id).to.equal(1);
137 expect(a2.name).to.equal('Nathan Fritz');
138 done();
139 });
140 });
141
142 it('instance: create row knex', (done) => {
143
144 let author = Author.create({name: "Nathan 'z"});
145 author.addAuthorKnex((err, a2) => {
146
147 expect(err).to.be.null();
148 expect(a2.id).to.equal(2);
149 expect(a2.name).to.equal("Nathan 'z");
150 done();
151 });
152 });
153
154 it('instance: create row fail', (done) => {
155
156 let author = Author.create({name: 34});
157 author.addAuthor((err, a2) => {
158
159 expect(err).to.exist();
160 done();
161 });
162 });
163
164 it('model: getDB', (done) => {
165 Author.getDB((err, db, close) => {
166 expect(err).to.be.null();
167 expect(db.connectionParameters.database).to.equal('gatepost_test');
168 close();
169 done();
170 });
171 });
172
173 it('instance: getDB', (done) => {
174 let author = Author.create({ name: 'Nathan Fritz' });
175 author.getDB((err, db, close) => {
176 expect(err).to.be.null();
177 expect(db.connectionParameters.database).to.equal('gatepost_test');
178 close();
179 done();
180 });
181 })
182
183 it('throws error on invalid query arguments', (done) => {
184 Author.queryWithValidate({ name: 123 }, (err) => {
185 expect(err).to.not.be.null();
186 expect(err.name).to.equal('ValidationError');
187 done();
188 });
189 });
190
191 it('does not error on valid query arguments', (done) => {
192 Author.queryWithValidate({ name: 'Nathan' }, (err) => {
193 expect(err).to.be.null();
194 done();
195 });
196 });
197
198 it('multi queries are concatenated', (done) => {
199 Author.multiQuery((err, results) => {
200 expect(err).to.be.null();
201 expect(results.length).to.equal(3);
202 expect(results[0].id).to.equal(1);
203 expect(results[1].id).to.equal(2);
204 expect(results[2].id).to.equal(1);
205 done();
206 });
207 });
208});