feat: preditable behavior of generated id and forceId

Signed-off-by: Yaapa Hage <hage.yaapa@in.ibm.com>
This commit is contained in:
Yaapa Hage 2020-11-05 12:41:42 +05:30
parent 06428247ad
commit 2f73a461c2
1 changed files with 151 additions and 0 deletions

151
test/id.test.js Normal file
View File

@ -0,0 +1,151 @@
// Copyright IBM Corp. 2013,2019. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
// This test written in mocha+should.js
'use strict';
/* global getSchema:false, connectorCapabilities:false */
const async = require('async');
const bdd = require('./helpers/bdd-if');
const should = require('./init.js');
const uid = require('./helpers/uid-generator');
let ds;
const ValidationError = require('..').ValidationError;
describe.only('id property', function() {
before(function(done) {
ds = getSchema();
done();
});
context('unspecified (defaults to generated:true)', function() {
context('with forceId:true (default)', function() {
let IdCheckA;
before(async () => {
IdCheckA = ds.define('IdCheckA', {
name: String,
});
await ds.automigrate(IdCheckA.modelName);
await IdCheckA.destroyAll();
});
it('should not require id value', async () => {
await IdCheckA.create({name: 'Pablo'}).should.not.be.rejected();
});
it('should not allow id value to be specified', async () => {
await IdCheckA.create({id: 10, name: 'Pablo'}).should.be.rejectedWith(/can\'t be set/);
});
});
context('with forceId:false', function() {
let IdCheckB;
before(async () => {
IdCheckB = ds.define('IdCheckB', {
name: String,
}, {forceId: false});
await ds.automigrate(IdCheckB.modelName);
await IdCheckB.destroyAll();
});
it('should not require id value', async () => {
await IdCheckB.create({name: 'Pablo'}).should.not.be.rejected();
});
it('should allow id value to be specified', async () => {
await IdCheckB.create({id: 10, name: 'Pablo'}).should.not.be.rejected();
});
});
});
context('generated:false (default)', function() {
context('with forceId:true (default)', function() {
let IdCheckC;
before(async () => {
IdCheckC = ds.define('IdCheckC', {
name: String,
});
await ds.automigrate(IdCheckC.modelName);
await IdCheckC.destroyAll();
});
// If id is not autogenerated and forceId is true, a value must be specified
it('should require id value', async () => {
await IdCheckC.create({name: 'Pablo'}).should.be.rejected();
});
// If id is not autogenerated and forceId is true, a value can be specified
it('should allow id value to be specified', async () => {
await IdCheckC.create({id: 10, name: 'Pablo'}).should.not.be.rejected();
});
});
context('with forceId:false', function() {
let IdCheckD;
before(async () => {
IdCheckD = ds.define('IdCheckD', {
name: String,
}, {forceId: false});
await ds.automigrate(IdCheckD.modelName);
await IdCheckD.destroyAll();
});
// If id is not autogenerated and forceId is false, a value must be specified
it('should require id value', async () => {
await IdCheckD.create({name: 'Pablo'}).should.be.rejected();
});
// If id is not autogenerated and forceId is false, a value can be specified
it('should allow id value to be specified', async () => {
await IdCheckD.create({id: 10, name: 'Pablo'}).should.not.be.rejected();
});
});
});
context('generated:true', function() {
context('with forceId:true (default)', function() {
let IdCheckE;
before(async () => {
IdCheckE = ds.define('IdCheckE', {
name: String,
});
await ds.automigrate(IdCheckE.modelName);
await IdCheckE.destroyAll();
});
// If id is autogenerated and forceId is true, a value need not be specified
it('should not require id value', async () => {
await IdCheckE.create({name: 'Pablo'}).should.not.be.rejected();
});
// If id is autogenerated and forceId is true, a value cannot be specified
it('should not allow id value to be specified', async () => {
await IdCheckE.create({id: 10, name: 'Pablo'}).should.not.be.rejected();
});
});
context('with forceId:false', function() {
let IdCheckF;
before(async () => {
IdCheckF = ds.define('IdCheckF', {
name: String,
}, {forceId: false});
await ds.automigrate(IdCheckF.modelName);
await IdCheckF.destroyAll();
});
// If id is autogenerated and forceId is false, a value need not be specified
it('should not require id value', async () => {
await IdCheckF.create({name: 'Pablo'}).should.not.be.rejected();
});
// If id is autogenerated and forceId is false, a value can be specified
it('should allow id value to be specified', async () => {
await IdCheckF.create({id: 10, name: 'Pablo'}).should.not.be.rejected();
});
});
});
});