From 74df2db4717fd51cca536b13bf7a04df75151363 Mon Sep 17 00:00:00 2001 From: Jan Hapke Date: Tue, 8 Mar 2016 18:54:02 +0100 Subject: [PATCH] #39: added failing tests for when primaryKey set to false --- test/foreignKeys.js | 33 ++++++++++++++++++++++++ test/joinTables.js | 62 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/test/foreignKeys.js b/test/foreignKeys.js index 20c0f93..8ef6c5b 100644 --- a/test/foreignKeys.js +++ b/test/foreignKeys.js @@ -3,6 +3,39 @@ var ForeignKeys = require('../lib/waterline-schema/foreignKeys'), describe('ForeignKeys', function() { + describe('ignore attributes with primaryKey set to false', function() { + var collections = {}; + + before(function() { + collections.foo = { + attributes: { + id: { + type: 'integer', + autoIncrement: true, + primaryKey: true, + unique: true + }, + count: { + type: 'integer', + primaryKey: false + } + } + }; + + collections.bar = { + attributes: { + foo: { model: 'foo' } + } + }; + }); + + it('should only use the column with primaryKey set to true as foreignKey', function() { + var obj = new ForeignKeys(collections); + assert(obj.bar.attributes.foo); + assert(obj.bar.attributes.foo.on === 'id'); + }); + }); + describe('with automatic column names', function() { var collections = {}; diff --git a/test/joinTables.js b/test/joinTables.js index e4feafd..0a5e0cb 100644 --- a/test/joinTables.js +++ b/test/joinTables.js @@ -3,6 +3,68 @@ var JoinTables = require('../lib/waterline-schema/joinTables'), describe('JoinTables', function() { + describe('ignore attributes with primaryKey set to false', function() { + var collections = {}; + + before(function() { + + collections.foo = { + tableName: 'foo', + connection: 'bar', + attributes: { + id: { + type: 'integer', + autoIncrement: true, + primaryKey: true, + unique: true + }, + count: { + type: 'integer', + primaryKey: false + }, + bars: { + collection: 'bar', + via: 'foos', + dominant: true + } + } + }; + + collections.bar = { + tableName: 'bar', + connection: 'bar', + attributes: { + id: { + type: 'integer', + autoIncrement: true, + primaryKey: true, + unique: true + }, + size: { + type: 'integer', + primaryKey: false + }, + foos: { + collection: 'foo', + via: 'bars' + } + } + }; + }); + + it('should only use columns with primaryKey set to true for the junction table', function() { + var obj = new JoinTables(collections); + assert(obj.bar_foos__foo_bars); + assert(obj.bar_foos__foo_bars.attributes); + + assert(obj.bar_foos__foo_bars.attributes.foo_bars); + assert(obj.bar_foos__foo_bars.attributes.foo_bars.on === 'id'); + + assert(obj.bar_foos__foo_bars.attributes.bar_foos); + assert(obj.bar_foos__foo_bars.attributes.bar_foos.on === 'id'); + }); + }); + describe('auto mapping of foreign keys', function() { var collections = {};