Skip to content

Commit

Permalink
Merge pull request #12 from pboyd04/FullTestCoverage
Browse files Browse the repository at this point in the history
Complete Test coverage
  • Loading branch information
pboyd04 committed Feb 28, 2019
2 parents bd26b66 + 2748511 commit aa3aa20
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/CSDLSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function findByType(metadata, typeName) {
var justType = typeName.substring(index+1);
var schema = metadata[namespace];
if(schema === undefined) {
let schemas = metadata._options.cache.getSchemas(namespace);
let schemas = metadata._options.cache.getSchemas(namespace);
if(schemas.length === 0) {
if(metadata.References === undefined) {
return null;
Expand Down
22 changes: 13 additions & 9 deletions lib/Metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class Metadata {
constructor(options) {
this._options = {useLocal: null, useNetwork: true};
this.References = [];
if(options) {
this.setOptions(options);
}
this.setOptions(options);
}

setOptions(options) {
this._options = Object.assign(this._options, options);
if(options) {
this._options = Object.assign(this._options, options);
}
if(this._options.cache === undefined) {
this._options.cache = new CSDLCache(this._options.useLocal, this._options.useNetwork);
}
Expand All @@ -29,7 +29,14 @@ class Metadata {

parse(string, callback, context) {
let arr = [];
let doc = new xmldoc.XmlDocument(string);
let doc;
try {
doc = new xmldoc.XmlDocument(string);
}
catch(e) {
callback(e, null);
return;
}
let me = this;
doc.eachChild(function(child, index, array) {
let elemName = child.name;
Expand All @@ -51,9 +58,7 @@ class Metadata {
});
Promise.all(arr).then((values) => {
callback(null, me);
if(me._options.cache) {
me._options.cache.addMetadata(me);
}
me._options.cache.addMetadata(me);
}).catch((e) => {
callback(e, null);
});
Expand All @@ -70,7 +75,6 @@ class Metadata {
break;
default:
arr.push(Promise.reject(new Error('Unknown element name '+elemName)));
break;
}
});
//Just resolve this with null as we have no new references to add
Expand Down
12 changes: 12 additions & 0 deletions test/corner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ describe('Corner Cases', function() {
});
});
});
describe('EnumType', function() {
it('Member Child Element other than Annotation', function() {
csdl.parseMetadata('<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx"><edmx:DataServices><Schema Namespace="Test1" xmlns="http://docs.oasis-open.org/odata/ns/edm"><EnumType Name="Test2"><Member Name="Test3"><Child Name="Test4"></Child></Member></EnumType></Schema></edmx:DataServices></edmx:Edmx>', {}, (error, meta) => {
assert.equal(error, null);
assert.notEqual(meta, null);
assert.notEqual(meta.Test1, undefined);
let schema = meta.Test1;
assert.notEqual(schema.Test2, undefined);
done();
});
});
});
describe('ParserCommon', function() {
it('No attribute or element case', function() {
let myobj = new ParserCommon();
Expand Down
29 changes: 28 additions & 1 deletion test/csdlcache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const CSDLCache = require('../lib/cache/csdlCache');
var assert = require('assert');
var assert = require('chai').assert;

describe('CSDLCache', function() {
describe('Construct', function() {
Expand Down Expand Up @@ -75,6 +75,11 @@ describe('CSDLCache', function() {
it('Not Existant Schema', function() {
assert.equal(fc.getSchema('NonExistant'), undefined);
});
it('Not Existant Schemas', function() {
let res = fc.getSchemasThatStartWith('NonExistant');
assert.isArray(res);
assert.lengthOf(res, 0);
});
it('Non existant file', function(done) {
let myFC = new CSDLCache([__dirname + '/fixtures/'], true);
let promise = myFC.getFile('http://example.com/NonExistant.xml');
Expand All @@ -86,6 +91,17 @@ describe('CSDLCache', function() {
done();
});
});
it('Non existant metadata', function(done) {
let myFC = new CSDLCache([__dirname + '/fixtures/'], true);
let promise = myFC.getMetadata('http://example.com/NonExistant.xml');
promise.then(() => {
assert.fail('Should not have file!');
done();
}).catch(e => {
assert.notEqual(e, null);
done();
});
});
it('Bad URI', function(done) {
let myFC = new CSDLCache([__dirname + '/fixtures/'], true);
let promise = myFC.getFile('fake://_?*example.com/NonExistant.xml');
Expand All @@ -97,6 +113,17 @@ describe('CSDLCache', function() {
done();
});
});
it('Bad metadata', function(done) {
let myFC = new CSDLCache([__dirname + '/fixtures/'], true);
let promise = myFC.getMetadata('http://example.com/Bad.xml');
promise.then(() => {
assert.fail('Should not have file!');
done();
}).catch(e => {
assert.notEqual(e, null);
done();
});
});
});
});
/* vim: set tabstop=2 shiftwidth=2 expandtab: */
11 changes: 11 additions & 0 deletions test/filecache.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ describe('FileCache', function() {
done();
});
});
it('Non existant file, no network', function(done) {
let myFC = new FileCache([__dirname + '/fixtures/'], false);
let promise = myFC.getFile('http://example.com/NonExistant.xml');
promise.then(() => {
assert.fail('Should not have file!');
done();
}).catch(e => {
assert.notEqual(e, null);
done();
});
});
it('Bad URI', function(done) {
let myFC = new FileCache([__dirname + '/fixtures/'], true);
let promise = myFC.getFile('fake://_?*example.com/NonExistant.xml');
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/Bad.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root>Text</bob>
18 changes: 16 additions & 2 deletions test/redfish.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var csdl = require('../index');
var assert = require('assert');
var assert = require('chai').assert;

/*Redfish uses CSDL very differently, let's make sure we can also parse Redfish style CSDL*/
describe('Redfish', function() {
Expand Down Expand Up @@ -66,12 +66,26 @@ describe('Redfish', function() {
it('Has References', function() {
assert.notEqual(metadata.References, undefined);
});
it('Schema Cache', function() {
let cache = metadata._options.cache;
assert.notEqual(cache, undefined);
let res = cache.getSchemasThatStartWith('Resource.');
assert.isArray(res);
assert.lengthOf(res, 10);
res = cache.getSchema('Resource.v1_0_0');
assert.notEqual(res, undefined);
assert.isNotArray(res);
});
it('Search', function() {
let resource = csdl.findByType(metadata, 'Resource.Resource');
assert.notEqual(resource, null);
});
});
describe('Remote File: Resource', function() {
let metadata;
before(function(done) {
this.timeout(10000);
csdl.parseMetadataUri('https://redfish.dmtf.org/schemas/Resource_v1.xml', {}, function(error, meta) {
csdl.parseMetadataUri('https://redfish.dmtf.org/schemas/Resource_v1.xml', null, function(error, meta) {
assert.equal(error, null);
metadata = meta;
done();
Expand Down
64 changes: 63 additions & 1 deletion test/search.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,73 @@
var csdl = require('../index');
var assert = require('assert');
var assert = require('chai').assert;

let fakeMeta1 = {'Test1': {'Type1': 'a'}};
let fakeMeta2 = {'Test1': {'Type2': 'b', 'Annotations': {'_Name': {'Name': '_Name'}}}};

describe('Search', function(){
let metadata;
let fakeCache;
let fakeCache2;
before(function(done) {
csdl.parseMetadataFile(__dirname + '/fixtures/SimpleMetadata.xml', {}, function(error, meta) {
assert.equal(error, null);
metadata = meta;
fakeCache = {_options: meta._options};
//Copy the cache
fakeCache._options.cache.metadataCache = fakeCache._options.cache.metadataCache.slice();
fakeCache._options.cache.metadataCache.push(fakeMeta1);
fakeCache._options.cache.metadataCache.push(fakeMeta2);
fakeCache2 = Object.assign({References: [{Includes: {'Test3': 'Test1'}}]}, fakeCache);
done();
});
});
it('Simple Type: Edm.String', function() {
var type = csdl.findByType(metadata, 'Edm.String');
assert.equal(type, 'Edm.String');
});
it('EntityType: Person', function() {
var person = csdl.findByType(metadata, 'Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person');
assert.equal(person.Name, 'Person');
assert.deepEqual(person.Annotations, {});
assert.notDeepEqual(person.Properties, {});
});
it('Cache Search', function() {
var person = csdl.findByType({_options: metadata._options}, 'Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person');
assert.equal(person.Name, 'Person');
assert.deepEqual(person.Annotations, {});
assert.notDeepEqual(person.Properties, {});
});
it('Invalid Type', function() {
var type = csdl.findByType(metadata, 'Bob');
assert.equal(type, null);
});
it('Non-Existant Schema', function() {
var type = csdl.findByType(metadata, 'Bob.Type');
assert.equal(type, null);
});
it('Have Schema, but not type', function() {
var person = csdl.findByType(metadata, 'Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person1');
assert.equal(person, undefined);
});
it('Multiple Schemas with Same Name', function() {
var type = csdl.findByType(fakeCache, 'Test1.Type1');
assert.notEqual(type, undefined);
assert.notEqual(type, null);
type = csdl.findByType(fakeCache, 'Test1.Type2');
assert.notEqual(type, undefined);
assert.notEqual(type, null);
type = csdl.findByType(fakeCache, 'Test1.Type3');
assert.equal(type, undefined);
type = csdl.findByType(fakeCache, 'Test2.Type2');
assert.equal(type, null);
});
it('Reference Search', function() {
var type = csdl.findByType(fakeCache2, 'Test3.Type1');
assert.notEqual(type, undefined);
assert.notEqual(type, null);
type = csdl.findByType(fakeCache2, 'Test4.Type1');
assert.equal(type, undefined);
});
it('Function: GetFavoriteAirline', function() {
var favoriteAirline = csdl.search(metadata, 'Function', 'GetFavoriteAirline');
assert.notEqual(favoriteAirline[0], undefined);
Expand All @@ -29,8 +81,18 @@ describe('Search', function(){
});
it('All Annotations', function(){
var annotations = csdl.search(metadata, 'Annotation');
assert.isArray(annotations);
assert.notEqual(annotations[0], undefined);
assert.notEqual(annotations[0].Collection, undefined);
annotations = csdl.search(fakeMeta2, 'Annotation');
assert.isArray(annotations);
for(let i = 0; i < annotations.length; i++) {
assert.notEqual(annotations[i].Name, '_Name', 'Found Annotation with Bad Name!');
}
});
it('Get first', function(){
let type = csdl.search(metadata, undefined, undefined);
assert.notEqual(type, undefined);
});
});
/* vim: set tabstop=2 shiftwidth=2 expandtab: */

0 comments on commit aa3aa20

Please sign in to comment.