From e3b1083878732e951ac7cc4125dc2cb02d33b4d7 Mon Sep 17 00:00:00 2001 From: Jerome Simeon Date: Thu, 12 Dec 2019 12:56:19 -0500 Subject: [PATCH] test(validation) More atomic value validation tests Signed-off-by: Jerome Simeon --- .../test/serializer/jsonpopulator.js | 106 +++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/packages/concerto-core/test/serializer/jsonpopulator.js b/packages/concerto-core/test/serializer/jsonpopulator.js index 4b75af9b63..d0203bd4ab 100644 --- a/packages/concerto-core/test/serializer/jsonpopulator.js +++ b/packages/concerto-core/test/serializer/jsonpopulator.js @@ -123,6 +123,30 @@ describe('JSONPopulator', () => { }).should.throw(ValidationException, /Expected value "foo" to be of type DateTime/); }); + it('should not convert to dates from null', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('DateTime'); + (() => { + jsonPopulator.convertToObject(field, null); + }).should.throw(ValidationException, /Expected value null to be of type DateTime/); + }); + + it('should not convert to dates from undefined', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('DateTime'); + (() => { + jsonPopulator.convertToObject(field, undefined); + }).should.throw(ValidationException, /Expected value undefined to be of type DateTime/); + }); + + it('should not convert to dates when not in ISO 8601 format', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('DateTime'); + (() => { + jsonPopulator.convertToObject(field, 'December 17, 1995 03:24:00'); + }).should.throw(ValidationException, /Expected value "December 17, 1995 03:24:00" to be of type DateTime/); + }); + it('should not convert to integers from strings', () => { let field = sinon.createStubInstance(Field); field.getType.returns('Integer'); @@ -131,6 +155,22 @@ describe('JSONPopulator', () => { }).should.throw(ValidationException, /Expected value "32768" to be of type Integer/); }); + it('should not convert to integer from null', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('Integer'); + (() => { + jsonPopulator.convertToObject(field, null); + }).should.throw(ValidationException, /Expected value null to be of type Integer/); + }); + + it('should not convert to integer from undefined', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('Integer'); + (() => { + jsonPopulator.convertToObject(field, undefined); + }).should.throw(ValidationException, /Expected value undefined to be of type Integer/); + }); + it('should convert to integers from numbers', () => { let field = sinon.createStubInstance(Field); field.getType.returns('Integer'); @@ -148,6 +188,22 @@ describe('JSONPopulator', () => { }).should.throw(ValidationException, /Expected value "32768" to be of type Long/); }); + it('should not convert to long from null', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('Long'); + (() => { + jsonPopulator.convertToObject(field, null); + }).should.throw(ValidationException, /Expected value null to be of type Long/); + }); + + it('should not convert to long from undefined', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('Long'); + (() => { + jsonPopulator.convertToObject(field, undefined); + }).should.throw(ValidationException, /Expected value undefined to be of type Long/); + }); + it('should convert to longs from numbers', () => { let field = sinon.createStubInstance(Field); field.getType.returns('Long'); @@ -157,7 +213,7 @@ describe('JSONPopulator', () => { value.should.equal(32768); }); - it('should convert to longs from numbers that are not integers', () => { + it('should not convert to longs from numbers that are not integers', () => { let field = sinon.createStubInstance(Field); field.getType.returns('Long'); (() => { @@ -173,6 +229,22 @@ describe('JSONPopulator', () => { }).should.throw(ValidationException, /Expected value "32.768" to be of type Double/); }); + it('should not convert to double from null', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('Double'); + (() => { + jsonPopulator.convertToObject(field, null); + }).should.throw(ValidationException, /Expected value null to be of type Double/); + }); + + it('should not convert to double from undefined', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('Double'); + (() => { + jsonPopulator.convertToObject(field, undefined); + }).should.throw(ValidationException, /Expected value undefined to be of type Double/); + }); + it('should convert to doubles from numbers', () => { let field = sinon.createStubInstance(Field); field.getType.returns('Double'); @@ -203,6 +275,22 @@ describe('JSONPopulator', () => { }).should.throw(ValidationException, /Expected value 32.768 to be of type Boolean/); }); + it('should not convert to boolean from null', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('Boolean'); + (() => { + jsonPopulator.convertToObject(field, null); + }).should.throw(ValidationException, /Expected value null to be of type Boolean/); + }); + + it('should not convert to boolean from undefined', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('Boolean'); + (() => { + jsonPopulator.convertToObject(field, undefined); + }).should.throw(ValidationException, /Expected value undefined to be of type Boolean/); + }); + it('should convert to strings from strings', () => { let field = sinon.createStubInstance(Field); field.getType.returns('String'); @@ -218,6 +306,22 @@ describe('JSONPopulator', () => { }).should.throw(ValidationException, /Expected value 32.768 to be of type String/); }); + it('should not convert to string from null', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('String'); + (() => { + jsonPopulator.convertToObject(field, null); + }).should.throw(ValidationException, /Expected value null to be of type String/); + }); + + it('should not convert to string from undefined', () => { + let field = sinon.createStubInstance(Field); + field.getType.returns('String'); + (() => { + jsonPopulator.convertToObject(field, undefined); + }).should.throw(ValidationException, /Expected value undefined to be of type String/); + }); + }); describe('#convertItem', () => {