Skip to content

Commit c2fd35b

Browse files
author
Agnes Lin
committed
tests: column should be discovered and mapped
1 parent 9422df0 commit c2fd35b

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"bluebird": "^3.4.6",
2525
"chalk": "^3.0.0",
2626
"debug": "^4.1.1",
27-
"loopback-connector": "^4.2.2",
27+
"loopback-connector": "^4.10.2",
2828
"pg": "^7.0.0",
2929
"strong-globalize": "^4.0.0",
3030
"uuid": "^3.0.1"

test/postgresql.discover.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ process.env.NODE_ENV = 'test';
88
require('should');
99

1010
const assert = require('assert');
11+
const should = require('should');
1112
const _ = require('lodash');
1213

1314
const DataSource = require('loopback-datasource-juggler').DataSource;
@@ -239,6 +240,36 @@ describe('Discover model properties', function() {
239240
});
240241

241242
describe('Discover model primary keys', function() {
243+
let Demo;
244+
before(function(done) {
245+
const demo_schema = {
246+
'name': 'Demo',
247+
'options': {
248+
'idInjection': false,
249+
'postgresql': {
250+
'schema': 'public',
251+
'table': 'demo',
252+
},
253+
},
254+
'properties': {
255+
'id': {
256+
'type': 'Number',
257+
'required': true,
258+
'id': 1,
259+
'postgresql': {
260+
'columnName': 'demoId',
261+
},
262+
},
263+
'name': {
264+
'type': 'String',
265+
'required': true,
266+
},
267+
},
268+
};
269+
Demo = db.createModel(demo_schema.name, demo_schema.properties, demo_schema.options);
270+
Demo.destroyAll(done);
271+
});
272+
242273
it('should return an array of primary keys for product', function(done) {
243274
db.discoverPrimaryKeys('product', function(err, models) {
244275
if (err) {
@@ -257,6 +288,33 @@ describe('Discover model primary keys', function() {
257288
});
258289
});
259290

291+
it('primary key should be discovered, and db generates instances properly', function(done) {
292+
db.discoverPrimaryKeys('demo', function(err, models) {
293+
if (err) {
294+
console.error(err);
295+
done(err);
296+
} else {
297+
models.forEach(function(m) {
298+
assert.deepEqual(m, {owner: 'public',
299+
tableName: 'demo',
300+
columnName: 'demoId',
301+
keySeq: 1,
302+
pkName: 'demo_pkey'});
303+
});
304+
}
305+
});
306+
Demo.create({id: 1, name: 'checkCase'}, function(err) {
307+
should.not.exists(err);
308+
Demo.findOne({}, function(err, d) {
309+
should.not.exists(err);
310+
should.exists(d);
311+
d.should.have.property('id', 1);
312+
d.should.have.property('name', 'checkCase');
313+
done();
314+
});
315+
});
316+
});
317+
260318
it('should return an array of primary keys for strongloop.product', function(done) {
261319
db.discoverPrimaryKeys('product', {owner: 'strongloop'}, function(err, models) {
262320
if (err) {

test/schema.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ ALTER SEQUENCE account_id_seq OWNED BY account.id;
113113

114114
CREATE TABLE demo (
115115
name text NOT NULL,
116-
id integer NOT NULL
116+
"demoId" integer NOT NULL
117117
);
118118

119119

@@ -133,7 +133,7 @@ CREATE SEQUENCE demo_id_seq
133133
-- Name: demo_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: strongloop
134134
--
135135

136-
ALTER SEQUENCE demo_id_seq OWNED BY demo.id;
136+
ALTER SEQUENCE demo_id_seq OWNED BY demo."demoId";
137137

138138

139139
--
@@ -352,6 +352,7 @@ CREATE TABLE product (
352352
);
353353

354354

355+
355356
--
356357
-- Name: inventory_view; Type: VIEW; Schema: strongloop; Owner: strongloop
357358
--
@@ -1243,7 +1244,7 @@ ALTER TABLE ONLY account
12431244
--
12441245

12451246
ALTER TABLE ONLY demo
1246-
ADD CONSTRAINT demo_pkey PRIMARY KEY (id);
1247+
ADD CONSTRAINT demo_pkey PRIMARY KEY ("demoId");
12471248

12481249

12491250
--
@@ -1312,6 +1313,7 @@ ALTER TABLE ONLY location
13121313
ADD CONSTRAINT location_pkey PRIMARY KEY (id);
13131314

13141315

1316+
13151317
--
13161318
-- Name: product_pkey; Type: CONSTRAINT; Schema: strongloop; Owner: strongloop
13171319
--

0 commit comments

Comments
 (0)