Add test cases for loading json doc

This commit is contained in:
Raymond Feng 2013-05-28 15:26:12 -07:00
parent 465a963d2d
commit 2a15926318
3 changed files with 143 additions and 0 deletions

View File

@ -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());
}
});
});

15
test/test1-schemas.json Normal file
View File

@ -0,0 +1,15 @@
{
"title": {
"type": "String"
},
"author": {
"type": "String",
"default": "Raymond"
},
"body": "String",
"date": {
"type": "Date"
},
"hidden": "Boolean",
"comments": ["String"]
}

83
test/test2-schemas.json Normal file
View File

@ -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"
}
}
]