Skip to content

Commit 700d1bc

Browse files
authored
Merge pull request loopbackio#422 from strongloop/disc-migr
tests: checks if column is discovered and mapped correctly
2 parents 9422df0 + 53a3953 commit 700d1bc

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-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: 57 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,32 @@ describe('Discover model primary keys', function() {
257288
});
258289
});
259290

291+
it('should discover primary key 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.length.should.be.equal(1);
298+
assert.deepEqual(models[0], {owner: 'public',
299+
tableName: 'demo',
300+
columnName: 'demoId',
301+
keySeq: 1,
302+
pkName: 'demo_pkey'});
303+
}
304+
});
305+
Demo.create({id: 1, name: 'checkCase'}, function(err) {
306+
should.not.exist(err);
307+
Demo.findOne({}, function(err, d) {
308+
should.not.exist(err);
309+
should.exist(d);
310+
d.should.have.property('id', 1);
311+
d.should.have.property('name', 'checkCase');
312+
done();
313+
});
314+
});
315+
});
316+
260317
it('should return an array of primary keys for strongloop.product', function(done) {
261318
db.discoverPrimaryKeys('product', {owner: 'strongloop'}, function(err, models) {
262319
if (err) {

test/schema.sql

Lines changed: 3 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
--
@@ -1243,7 +1243,7 @@ ALTER TABLE ONLY account
12431243
--
12441244

12451245
ALTER TABLE ONLY demo
1246-
ADD CONSTRAINT demo_pkey PRIMARY KEY (id);
1246+
ADD CONSTRAINT demo_pkey PRIMARY KEY ("demoId");
12471247

12481248

12491249
--

0 commit comments

Comments
 (0)