Skip to content

Commit e1c81dd

Browse files
authored
Merge pull request #10 from pboyd04/MochaTests
Switch to Mocha for testing and get rid of some double callback cases
2 parents 0700291 + 6022885 commit e1c81dd

14 files changed

+662
-310
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/node_modules
22
/lib-cov
33
.idea
4+
.nyc_output
5+
#While NPM recommends commiting this file, it causes nothing but grief moving between windows and linux builds. So don't commit it to this project
6+
package-lock.json

.travis.yml

+5-9
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@ addons:
1111
sudo: false
1212
node_js:
1313
- "stable"
14-
- "7.1.0"
15-
- "7.0.0"
16-
- "6.9.1"
17-
- "6.5"
18-
- "6.4"
14+
- "10.15"
15+
- "9.11"
16+
- "8.15"
17+
- "7.1"
18+
- "6.9"
1919
- "6.3"
20-
- "5.12"
2120
cache:
2221
bundler: true
2322
directories:
2423
- node_modules # NPM packages
25-
install:
26-
- npm install
27-
- npm install jscoverage coveralls
2824
after_script:
2925
- npm run coveralls

index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var Metadata = process.env.CSDL_COV ? require('./lib-cov/Metadata') : require('./lib/Metadata');
2-
var CSDLCache = process.env.CSDL_COV ? require('./lib-cov/cache/csdlCache') : require('./lib/cache/csdlCache');
3-
var CSDLSearch = process.env.CSDL_COV ? require('./lib-cov/CSDLSearch') : require('./lib/CSDLSearch');
1+
var Metadata = require('./lib/Metadata');
2+
var CSDLCache = require('./lib/cache/csdlCache');
3+
var CSDLSearch = require('./lib/CSDLSearch');
44

55
//constants
66
module.exports.version = require('./package.json').version;

lib/Metadata.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Metadata.prototype.parse = function(string, callback, context) {
5050
var doc = xmljs.parseXml(string);
5151
} catch(e) {
5252
this.done(e);
53+
return;
5354
}
5455
var root = doc.root();
5556
var parseElement = this.parseElement.bind(this);
@@ -106,7 +107,6 @@ Metadata.prototype.parseElement = function(element) {
106107
break;
107108
default:
108109
throw new Error('Unknown element name '+element.name());
109-
break;
110110
}
111111
}
112112

@@ -130,7 +130,6 @@ Metadata.prototype.parseDataServiceElement = function(element) {
130130
break;
131131
default:
132132
throw new Error('Unknown element name '+element.name());
133-
break;
134133
}
135134
}
136135

@@ -157,6 +156,7 @@ module.exports.parseMetadataFile = function(filename, options, callback) {
157156
fs.readFile(filename, 'utf8', function(err, data) {
158157
if(err) {
159158
callback(err, null);
159+
return;
160160
}
161161
module.exports.parseMetadata(data, options, callback);
162162
});
@@ -166,6 +166,7 @@ module.exports.parseMetadataUri = function(uri, options, callback) {
166166
request.get(uri, function(error, request, body) {
167167
if(error) {
168168
callback(error, null);
169+
return;
169170
}
170171
module.exports.parseMetadata(body, options, callback);
171172
});

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
"description": "CSDL Metadata Parser",
55
"main": "./index.js",
66
"scripts": {
7-
"test": "nodeunit test",
8-
"coverage": "jscoverage lib && set CSDL_COV=1 && nodeunit --reporter=lcov test",
9-
"coveralls": "jscoverage lib && CSDL_COV=1 nodeunit --reporter=lcov test | coveralls"
7+
"test": "nyc mocha",
8+
"coveralls": "nyc report --reporter=text-lcov | coveralls"
109
},
1110
"engines": {
1211
"node": ">=4.3.2"
@@ -22,6 +21,9 @@
2221
"request": "^2.81.0"
2322
},
2423
"devDependencies": {
25-
"nodeunit": "^0.11.0"
24+
"chai": "^4.2.0",
25+
"coveralls": "^3.0.2",
26+
"mocha": "^6.0.1",
27+
"nyc": "^13.3.0"
2628
}
2729
}

test/corner.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var csdl = require('../index');
2+
var assert = require('assert');
3+
var ParserCommon = require('../lib/ParserCommon');
4+
var XML = require('libxmljs');
5+
6+
describe('Corner Cases', function() {
7+
describe('Key Attribute', function() {
8+
it('Simple Case', function(done) {
9+
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"><EntityType Name="Category"><Key><PropertyRef Name="ID" /></Key><Property Name="ID" Type="Edm.Int32" Nullable="false" /><Property Name="Name" Type="Edm.String" /></EntityType></Schema></edmx:DataServices></edmx:Edmx>', {}, (error, meta) => {
10+
assert.equal(error, null);
11+
assert.notEqual(meta, null);
12+
assert.notEqual(meta.Test1, undefined);
13+
let schema = meta.Test1;
14+
assert.notEqual(schema.Category, undefined);
15+
let entity = schema.Category;
16+
assert.notEqual(entity.Properties['ID'], undefined);
17+
let prop = entity.Properties['ID'];
18+
assert.equal(prop.IsKey, true);
19+
done();
20+
});
21+
});
22+
it('Key is inherited prop', function(done) {
23+
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"><EntityType Name="Parent"><Property Name="ID" Type="Edm.Int32" Nullable="false" /></EntityType><EntityType Name="Category"><Key><PropertyRef Name="ID" /></Key><Property Name="Name" Type="Edm.String" /></EntityType></Schema></edmx:DataServices></edmx:Edmx>', {}, (error, meta) => {
24+
assert.equal(error, null);
25+
assert.notEqual(meta, null);
26+
assert.notEqual(meta.Test1, undefined);
27+
let schema = meta.Test1;
28+
assert.notEqual(schema.Category, undefined);
29+
done();
30+
});
31+
});
32+
});
33+
describe('ParserCommon', function() {
34+
it('No attribute or element case', function() {
35+
let myobj = {};
36+
let init = ParserCommon.initEntity.bind(myobj);
37+
init(null, 'Test');
38+
assert.ok('Passed');
39+
});
40+
it('No element callback case', function() {
41+
ParserCommon.parseEntity(null, 'Test', undefined, undefined);
42+
assert.ok('Passed');
43+
});
44+
it('Attribute parent case', function() {
45+
let test = {};
46+
let myobj = {validAttributes: {'x': {'parent': test}}};
47+
let doc = new XML.Document();
48+
let node = doc.node('Test');
49+
let attr = node.attr('x', 'value');
50+
let init = ParserCommon.initEntity.bind(myobj);
51+
init(node, 'Test');
52+
assert.equal(test.x, 'value');
53+
assert.equal(myobj.x, undefined);
54+
});
55+
});
56+
});

test/csdlcache.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const CSDLCache = require('../lib/cache/csdlCache');
2+
var assert = require('assert');
3+
4+
describe('CSDLCache', function() {
5+
describe('Construct', function() {
6+
it('Default', function() {
7+
let cc = new CSDLCache();
8+
assert.equal(cc.fileCache.localDirs, null);
9+
assert.equal(cc.fileCache.useNetwork, true);
10+
});
11+
it('Local Dir as string', function() {
12+
let fc = new CSDLCache('test');
13+
assert.equal(fc.fileCache.localDirs, 'test');
14+
assert.equal(fc.fileCache.useNetwork, true);
15+
});
16+
it('Local Dir as array', function() {
17+
let fc = new CSDLCache(['test', 'test2']);
18+
assert.deepEqual(fc.fileCache.localDirs, ['test', 'test2']);
19+
assert.equal(fc.fileCache.useNetwork, true);
20+
});
21+
it('Full params with useNetwork true', function() {
22+
let fc = new CSDLCache(['test', 'test2'], true);
23+
assert.deepEqual(fc.fileCache.localDirs, ['test', 'test2']);
24+
assert.equal(fc.fileCache.useNetwork, true);
25+
});
26+
it('Full params with useNetwork false', function() {
27+
let fc = new CSDLCache(['test', 'test2'], false);
28+
assert.deepEqual(fc.fileCache.localDirs, ['test', 'test2']);
29+
assert.equal(fc.fileCache.useNetwork, false);
30+
});
31+
it('No local cache', function() {
32+
let fc = new CSDLCache(null, false);
33+
assert.deepEqual(fc.fileCache.localDirs, null);
34+
assert.equal(fc.fileCache.useNetwork, false);
35+
});
36+
});
37+
describe('Has File', function() {
38+
let fc = new CSDLCache([__dirname + '/fixtures/'], false);
39+
it('Starts empty', function() {
40+
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), false);
41+
});
42+
it('Gets file', function() {
43+
let promise = fc.getFile('http://example.com/SimpleMetadata.xml');
44+
return promise.then(data => {
45+
assert.notEqual(data, undefined);
46+
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), true);
47+
});
48+
});
49+
it('Gets file Same File', function() {
50+
let promise = fc.getFile('http://example.com/SimpleMetadata.xml');
51+
return promise.then(data => {
52+
assert.notEqual(data, undefined);
53+
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), true);
54+
});
55+
});
56+
it('Gets metadata', function() {
57+
let promise = fc.getMetadata('http://example.com/SimpleMetadata.xml');
58+
return promise.then(data => {
59+
assert.notEqual(data, undefined);
60+
assert.equal(fc.hasMetadata('http://example.com/SimpleMetadata.xml'), true);
61+
});
62+
});
63+
it('Gets same metadata', function() {
64+
let promise = fc.getMetadata('http://example.com/SimpleMetadata.xml');
65+
return promise.then(data => {
66+
assert.notEqual(data, undefined);
67+
assert.equal(fc.hasMetadata('http://example.com/SimpleMetadata.xml'), true);
68+
});
69+
});
70+
it('Clear', function() {
71+
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), true);
72+
fc.clear();
73+
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), false);
74+
});
75+
it('Not Existant Schema', function() {
76+
assert.equal(fc.getSchema('NonExistant'), undefined);
77+
});
78+
it('Non existant file', function(done) {
79+
let myFC = new CSDLCache([__dirname + '/fixtures/'], true);
80+
let promise = myFC.getFile('http://example.com/NonExistant.xml');
81+
promise.then(() => {
82+
assert.fail('Should not have file!');
83+
done();
84+
}).catch(e => {
85+
assert.notEqual(e, null);
86+
done();
87+
});
88+
});
89+
it('Bad URI', function(done) {
90+
let myFC = new CSDLCache([__dirname + '/fixtures/'], true);
91+
let promise = myFC.getFile('fake://_?*example.com/NonExistant.xml');
92+
promise.then(() => {
93+
assert.fail('Should not have file!');
94+
done();
95+
}).catch(e => {
96+
assert.notEqual(e, null);
97+
done();
98+
});
99+
});
100+
});
101+
});
102+
/* vim: set tabstop=2 shiftwidth=2 expandtab: */

test/filecache.js

+77-41
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,80 @@
11
const FileCache = require('../lib/cache/fileCache');
2+
var assert = require('assert');
23

3-
module.exports.construct = function(assert) {
4-
var fc = new FileCache();
5-
assert.equal(fc.localDirs, null);
6-
assert.equal(fc.useNetwork, true);
7-
8-
fc = new FileCache('test');
9-
assert.equal(fc.localDirs, 'test');
10-
assert.equal(fc.useNetwork, true);
11-
12-
fc = new FileCache(['test', 'test2']);
13-
assert.deepEqual(fc.localDirs, ['test', 'test2']);
14-
assert.equal(fc.useNetwork, true);
15-
16-
fc = new FileCache(['test', 'test2'], true);
17-
assert.deepEqual(fc.localDirs, ['test', 'test2']);
18-
assert.equal(fc.useNetwork, true);
19-
20-
fc = new FileCache(['test', 'test2'], false);
21-
assert.deepEqual(fc.localDirs, ['test', 'test2']);
22-
assert.equal(fc.useNetwork, false);
23-
24-
fc = new FileCache(null, true);
25-
assert.equal(fc.localDirs, null);
26-
assert.equal(fc.useNetwork, true);
27-
28-
assert.done();
29-
}
30-
31-
module.exports.hasFile = function(assert) {
32-
var fc = new FileCache([__dirname + '/fixtures/'], false);
33-
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), false);
34-
var promise = fc.getFile('http://example.com/SimpleMetadata.xml');
35-
promise.then(function(data) {
36-
assert.notEqual(data, undefined);
37-
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), true);
38-
}). catch(function(err) {
39-
throw err;
40-
});
41-
42-
assert.done();
43-
}
4+
describe('FileCache', function() {
5+
describe('Construct', function() {
6+
it('Default', function() {
7+
let fc = new FileCache();
8+
assert.equal(fc.localDirs, null);
9+
assert.equal(fc.useNetwork, true);
10+
});
11+
it('Local Dir as string', function() {
12+
let fc = new FileCache('test');
13+
assert.equal(fc.localDirs, 'test');
14+
assert.equal(fc.useNetwork, true);
15+
});
16+
it('Local Dir as array', function() {
17+
let fc = new FileCache(['test', 'test2']);
18+
assert.deepEqual(fc.localDirs, ['test', 'test2']);
19+
assert.equal(fc.useNetwork, true);
20+
});
21+
it('Full params with useNetwork true', function() {
22+
let fc = new FileCache(['test', 'test2'], true);
23+
assert.deepEqual(fc.localDirs, ['test', 'test2']);
24+
assert.equal(fc.useNetwork, true);
25+
});
26+
it('Full params with useNetwork false', function() {
27+
let fc = new FileCache(['test', 'test2'], false);
28+
assert.deepEqual(fc.localDirs, ['test', 'test2']);
29+
assert.equal(fc.useNetwork, false);
30+
});
31+
it('No local cache', function() {
32+
let fc = new FileCache(null, false);
33+
assert.deepEqual(fc.localDirs, null);
34+
assert.equal(fc.useNetwork, false);
35+
});
36+
});
37+
describe('Has File', function() {
38+
let fc = new FileCache([__dirname + '/fixtures/'], false);
39+
it('Starts empty', function() {
40+
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), false);
41+
});
42+
it('Gets file', function() {
43+
let promise = fc.getFile('http://example.com/SimpleMetadata.xml');
44+
return promise.then(data => {
45+
assert.notEqual(data, undefined);
46+
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), true);
47+
});
48+
});
49+
it('Gets file Same File', function() {
50+
let promise = fc.getFile('http://example.com/SimpleMetadata.xml');
51+
return promise.then(data => {
52+
assert.notEqual(data, undefined);
53+
assert.equal(fc.hasFile('http://example.com/SimpleMetadata.xml'), true);
54+
});
55+
});
56+
it('Non existant file', function(done) {
57+
let myFC = new FileCache([__dirname + '/fixtures/'], true);
58+
let promise = myFC.getFile('http://example.com/NonExistant.xml');
59+
promise.then(() => {
60+
assert.fail('Should not have file!');
61+
done();
62+
}).catch(e => {
63+
assert.notEqual(e, null);
64+
done();
65+
});
66+
});
67+
it('Bad URI', function(done) {
68+
let myFC = new FileCache([__dirname + '/fixtures/'], true);
69+
let promise = myFC.getFile('fake://_?*example.com/NonExistant.xml');
70+
promise.then(() => {
71+
assert.fail('Should not have file!');
72+
done();
73+
}).catch(e => {
74+
assert.notEqual(e, null);
75+
done();
76+
});
77+
});
78+
});
79+
});
4480
/* vim: set tabstop=2 shiftwidth=2 expandtab: */

0 commit comments

Comments
 (0)