From 2a15926318c777f421d87d05ce9707df3e3f2bfa Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 28 May 2013 15:26:12 -0700 Subject: [PATCH] Add test cases for loading json doc --- test/adl.test.js | 45 ++++++++++++++++++++++ test/test1-schemas.json | 15 ++++++++ test/test2-schemas.json | 83 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 test/test1-schemas.json create mode 100644 test/test2-schemas.json diff --git a/test/adl.test.js b/test/adl.test.js index abf31bf4..546bbf1a 100644 --- a/test/adl.test.js +++ b/test/adl.test.js @@ -146,3 +146,48 @@ describe('DataSource define model', function () { }); }); + + +describe('Load models from json', function () { + it('should be able to define models from json', function () { + var path = require('path'), + fs = require('fs'); + + + /** + * Load ADL schemas from a json doc + * @param schemaFile The schema json file + * @returns A map of schemas keyed by name + */ + function loadSchemasSync(schemaFile, dataSource) { + // Set up the data source + if (!dataSource) { + dataSource = new DataSource('memory'); + } + + // Read the schema JSON file + var schemas = JSON.parse(fs.readFileSync(schemaFile)); + + return DataSource.buildModels(dataSource, schemas); + + } + + var models = loadSchemasSync(path.join(__dirname, 'test1-schemas.json')); + + models.should.have.property('anonymous'); + models.anonymous.should.have.property('modelName', 'Anonymous'); + + var m1 = new models.anonymous({title: 'Test'}); + m1.should.have.property('title', 'Test'); + + models = loadSchemasSync(path.join(__dirname, 'test2-schemas.json')); + models.should.have.property('address'); + models.should.have.property('account'); + models.should.have.property('customer'); + for (var s in models) { + var m = models[s]; + console.log(m.modelName, new m()); + } + }); +}); + diff --git a/test/test1-schemas.json b/test/test1-schemas.json new file mode 100644 index 00000000..05dd5079 --- /dev/null +++ b/test/test1-schemas.json @@ -0,0 +1,15 @@ +{ + "title": { + "type": "String" + }, + "author": { + "type": "String", + "default": "Raymond" + }, + "body": "String", + "date": { + "type": "Date" + }, + "hidden": "Boolean", + "comments": ["String"] +} \ No newline at end of file diff --git a/test/test2-schemas.json b/test/test2-schemas.json new file mode 100644 index 00000000..447726b4 --- /dev/null +++ b/test/test2-schemas.json @@ -0,0 +1,83 @@ +[ + { + "name": "Address", + "properties": { + "label": "string", + "street": "string", + "city": "string", + "zipCode": "string" + } + }, + + { + "name": "Account", + "properties": { + "id": "string", + "customer": { + "type": "Customer", + "association": { + "type": "belongsTo", + "inverse": "account" + } + }, + "balance": "number" + } + }, + + { + "name": "Customer", + "options": { + "oracle": { + "owner": "STRONGLOOP", + "table": "CUSTOMER" + } + }, + "properties": { + "id": { + "type": "number", + "id": true, + "doc": "Customer ID" + }, + "firstName": { + "type": "string", + "trim": true, + "required": true, + "oracle": { + "column": "FNAME", + "type": "VARCHAR", + "length": 32 + } + }, + "lastName": { + "type": "string", + "trim": true, + "required": true, + "oracle": { + "column": "LNAME", + "type": "VARCHAR", + "length": 32 + } + }, + "vip": { + "type": "boolean", + "doc": "indicate if the customer is a VIP", + "oracle": { + "column": "VIP", + "type": "CHAR", + "length": 1 + } + }, + "emails": [ + { + "type": "string", + "trim": true + } + ], + "address": { + "type": "Address" + }, + "account": "Account" + } + } + +] \ No newline at end of file