@@ -8,6 +8,7 @@ process.env.NODE_ENV = 'test';
8
8
require ( 'should' ) ;
9
9
10
10
const assert = require ( 'assert' ) ;
11
+ const should = require ( 'should' ) ;
11
12
const _ = require ( 'lodash' ) ;
12
13
13
14
const DataSource = require ( 'loopback-datasource-juggler' ) . DataSource ;
@@ -239,6 +240,36 @@ describe('Discover model properties', function() {
239
240
} ) ;
240
241
241
242
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
+
242
273
it ( 'should return an array of primary keys for product' , function ( done ) {
243
274
db . discoverPrimaryKeys ( 'product' , function ( err , models ) {
244
275
if ( err ) {
@@ -257,6 +288,32 @@ describe('Discover model primary keys', function() {
257
288
} ) ;
258
289
} ) ;
259
290
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
+
260
317
it ( 'should return an array of primary keys for strongloop.product' , function ( done ) {
261
318
db . discoverPrimaryKeys ( 'product' , { owner : 'strongloop' } , function ( err , models ) {
262
319
if ( err ) {
0 commit comments