Adding unit tests for data validation

This commit is contained in:
Colin McLeod
2015-06-10 00:05:32 -07:00
parent a253c488a5
commit ba9051783a
5 changed files with 80 additions and 9 deletions

1
.gitignore vendored
View File

@@ -5,5 +5,6 @@ build
.DS_Store
*.log
app/js/db.js
app/db.json
nginx.pid
template_cache.js

View File

@@ -18,6 +18,7 @@ var gulp = require('gulp'),
svgmin = require( 'gulp-svgmin' ),
jsonlint = require("gulp-jsonlint"),
appCache = require("gulp-manifest"),
jasmine = require('gulp-jasmine'),
pkg = require('./package.json');
var cdnHostStr = '';
@@ -111,7 +112,7 @@ gulp.task('js', function() {
});
gulp.task('copy', function() {
return gulp.src(['app/images/**','app/fonts/**','app/.htaccess'], {base: 'app/'})
return gulp.src(['app/images/**','app/fonts/**','app/db.json'], {base: 'app/'})
.pipe(gulp.dest('build'));
});
@@ -177,7 +178,7 @@ gulp.task('serve-stop', function(cb) {
gulp.task('watch', function() {
gulp.watch(['app/index.html','app/icons/*.svg'], ['generateIndexHTML']);
gulp.watch(['app/images/**','app/fonts/**'], ['copy']);
gulp.watch(['app/images/**','app/fonts/**', 'app/db.json'], ['copy']);
gulp.watch('app/less/*.less', ['less']);
gulp.watch('app/views/**/*', ['html2js']);
gulp.watch('app/js/**/*.js', ['js']);
@@ -237,6 +238,11 @@ gulp.task('upload', function(done) {
);
});
gulp.task('test', function () {
return gulp.src('tests/test-*.js')
.pipe(jasmine());
});
gulp.task('lint', ['js-lint', 'json-lint']);
gulp.task('clean', function (done) { del(['build'], done); });
gulp.task('build', function (done) { runSequence('clean', ['html2js','jsonToDB'], ['generateIndexHTML','bower','js','less','copy'], done); });

View File

@@ -8,13 +8,13 @@
"private": true,
"dependencies": {},
"devDependencies": {
"gulp-manifest": "^0.0.6",
"async": "^0.9.0",
"del": "^1.1.1",
"gulp": "^3.8.11",
"gulp-angular-templatecache": "^1.6.0",
"gulp-concat": "^2.5.2",
"gulp-htmlmin": "^1.1.1",
"gulp-jasmine": "^2.0.1",
"gulp-jshint": "^1.10.0",
"gulp-jsonlint": "^1.0.2",
"gulp-less": "^3.0.2",

View File

@@ -84,11 +84,7 @@ function writeDB(err, arr) {
fs.writeFile(db_filename, code, function(err) {});
});
if (!fs.existsSync('./build')){
fs.mkdirSync('./build');
}
fs.open('./build/db.json', 'w', function() {
fs.writeFile('./build/db.json', JSON.stringify(db), function(err) {});
fs.open('./app/db.json', 'w', function() {
fs.writeFile('./app/db.json', JSON.stringify(db), function(err) {});
});
}

68
tests/test-data.js Normal file
View File

@@ -0,0 +1,68 @@
var data = require('../app/db.json');
var shipProperties = ["grp", "name", "manufacturer", "class", "cost", "speed", "boost", "agility", "shields", "armour", "fuelcost", "mass"];
describe("Database", function() {
it("has ships and components", function() {
expect(data.ships).toBeDefined()
expect(data.components.common).toBeDefined();
expect(data.components.hardpoints).toBeDefined();
expect(data.components.internal).toBeDefined();
expect(data.components.bulkheads).toBeDefined();
});
it("has unique IDs for every hardpoint", function() {
var ids = {};
var groups = data.components.hardpoints;
for (var g in groups) {
var group = groups[g];
for (var i = 0; i < group.length; i++) {
var id = group[i].id;
expect(ids[id]).toBeFalsy('ID already exists: ' + id);
expect(group[i].grp).toBeDefined('Hardpoint has no group defined, ID:' + id);
ids[id] = true;
}
}
});
it("has valid internal components", function() {
var ids = {};
var groups = data.components.internal;
for (var g in groups) {
var group = groups[g];
for (var i = 0; i < group.length; i++) {
var id = group[i].id;
expect(ids[id]).toBeFalsy('ID already exists: ' + id);
expect(group[i].grp).toBeDefined('Internal component has no group defined, ID:' + id);
ids[id] = true;
}
}
});
it("has data for every ship", function() {
for (var s in data.ships) {
for (var p = 0; p < shipProperties.length; p++) {
expect(data.ships[s].properties[shipProperties[p]]).toBeDefined(shipProperties[p] + ' is missing for ' + s);
}
expect(data.ships[s].slots.common.length).toEqual(7, s + ' is missing common slots');
expect(data.ships[s].defaults.common.length).toEqual(7, s + ' is missing common defaults');
expect(data.ships[s].slots.hardpoints.length).toEqual(data.ships[s].defaults.hardpoints.length, s + ' hardpoint slots and defaults dont match');
expect(data.ships[s].slots.internal.length).toEqual(data.ships[s].defaults.internal.length, s + ' hardpoint slots and defaults dont match');
expect(data.ships[s].retailCost).toBeGreaterThan(data.ships[s].properties.cost, s + ' has invalid retail cost');
expect(data.components.bulkheads[s]).toBeDefined(s + ' is missing bulkheads');
}
});
it("has components with a group defined", function() {
for (var i = 0; i < data.components.common.length; i++) {
var group = data.components.common[i];
for (var c in group) {
expect(group[c].grp).toBeDefined('Common component has no group defined, Type: ' + i + ', ID: ' + c);
}
}
});
});