aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-06-05 18:31:13 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-06-05 18:31:13 +0800
commit2ac15caf002394ee21b1bd40d32db66b8e9be8d9 (patch)
tree0aa88b895631b436e7a07940b211511ae79e63c7
parenteb5c9ae70886cb1a14ae154f363f052a76b24479 (diff)
downloaddexon-sol-tools-2ac15caf002394ee21b1bd40d32db66b8e9be8d9.tar
dexon-sol-tools-2ac15caf002394ee21b1bd40d32db66b8e9be8d9.tar.gz
dexon-sol-tools-2ac15caf002394ee21b1bd40d32db66b8e9be8d9.tar.bz2
dexon-sol-tools-2ac15caf002394ee21b1bd40d32db66b8e9be8d9.tar.lz
dexon-sol-tools-2ac15caf002394ee21b1bd40d32db66b8e9be8d9.tar.xz
dexon-sol-tools-2ac15caf002394ee21b1bd40d32db66b8e9be8d9.tar.zst
dexon-sol-tools-2ac15caf002394ee21b1bd40d32db66b8e9be8d9.zip
Add tests to number regex and BigNumber serialization & fix bugs
-rw-r--r--src/schemas/order_schemas.ts2
-rw-r--r--test/schema_test.ts41
2 files changed, 42 insertions, 1 deletions
diff --git a/src/schemas/order_schemas.ts b/src/schemas/order_schemas.ts
index 72012dc26..4999f3e9d 100644
--- a/src/schemas/order_schemas.ts
+++ b/src/schemas/order_schemas.ts
@@ -7,7 +7,7 @@ export const addressSchema = {
export const numberSchema = {
id: '/numberSchema',
type: 'string',
- format: '\d+(\.\d+)?',
+ pattern: '^\\d+(\\.\\d+)?$',
};
export const orderSchema = {
diff --git a/test/schema_test.ts b/test/schema_test.ts
new file mode 100644
index 000000000..1203a4cc1
--- /dev/null
+++ b/test/schema_test.ts
@@ -0,0 +1,41 @@
+import 'mocha';
+import * as chai from 'chai';
+import * as BigNumber from 'bignumber.js';
+import {numberSchema} from '../src/schemas/order_schemas';
+import {SchemaValidator} from '../src/utils/schema_validator';
+import promisify = require('es6-promisify');
+
+chai.config.includeStack = true;
+const expect = chai.expect;
+
+describe('Schema', () => {
+ const validator = new SchemaValidator();
+ describe('#numberSchema', () => {
+ describe('number regex', () => {
+ it('should validate valid numbers', () => {
+ expect(validator.validate('42' as any, numberSchema).errors).to.be.lengthOf(0);
+ expect(validator.validate('0' as any, numberSchema).errors).to.be.lengthOf(0);
+ expect(validator.validate('1.3' as any, numberSchema).errors).to.be.lengthOf(0);
+ expect(validator.validate('0.2' as any, numberSchema).errors).to.be.lengthOf(0);
+ expect(validator.validate('00.00' as any, numberSchema).errors).to.be.lengthOf(0);
+ });
+ it('should fail for invalid numbers', () => {
+ expect(validator.validate('.3' as any, numberSchema).errors).to.be.lengthOf(1);
+ expect(validator.validate('1.' as any, numberSchema).errors).to.be.lengthOf(1);
+ expect(validator.validate('abacaba' as any, numberSchema).errors).to.be.lengthOf(1);
+ expect(validator.validate('и' as any, numberSchema).errors).to.be.lengthOf(1);
+ expect(validator.validate('1..0' as any, numberSchema).errors).to.be.lengthOf(1);
+ });
+ });
+ });
+ describe('BigNumber serialization', () => {
+ it.only('should correctly serialize BigNumbers', async () => {
+ expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('42'))).to.be.equal('42');
+ expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('0'))).to.be.equal('0');
+ expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('1.3'))).to.be.equal('1.3');
+ expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('0.2'))).to.be.equal('0.2');
+ expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('00.00'))).to.be.equal('0');
+ expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('.3'))).to.be.equal('0.3');
+ });
+ });
+});