/* global describe, it */
'use strict';

var assert = require('chai').assert;
var config = require('../server/config');
var helper = require('../server/helper');
var items = require('../server/data/items');
var leagues = require('../server/data/leagues');
var competitions = require('../server/data/competitions');

describe('Data intergrity', () => {
    items.forEach(item => {
        describe(item.name, () => {
            config.years.availables.forEach(year => {
                describe(year, () => {
                    if (item.code !== competitions.championsLeague.code && item.code !== competitions.europaLeague.code) {
                        testTableData(item.code, year);
                    }

                    testScorersData(item.code, year);
                    testAssistsData(item.code, year);
                });
            });
        });
    });
});

function testTableData(code, year) {
    var data = helper.readJsonFile(helper.stringFormat(config.paths.tableData, code, year));

    it('Table should have the right number of teams', () => {
        var expectedNumber = code === leagues.bundesliga.code ? 18 : 20;
        assert.lengthOf(data, expectedNumber);
    });

    testDataIsNotEmpty('Table', data);
    testDataIsNumber('Table', data);
}

function testScorersData(code, year) {
    var data = helper.readJsonFile(helper.stringFormat(config.paths.scorersData, code, year));

    it('Scorers should have the right number of players', () => {
        assert.lengthOf(data, 20);
    });

    testDataIsNotEmpty('Scorers', data);
    testDataIsNumber('Scorers', data);
}

function testAssistsData(code, year) {
    var data = helper.readJsonFile(helper.stringFormat(config.paths.assistsData, code, year));

    it('Assists should have the right number of players', () => {
        assert.lengthOf(data, 20);
    });

    testDataIsNotEmpty('Assists', data);
    testDataIsNumber('Assists', data);
}

function testDataIsNotEmpty(dataName, data) {
    it(dataName + ' should not have empty data', () => {
        data.forEach((item, index) => {
            for (var key in item) {
                if (item.hasOwnProperty(key)) {
                    assert.notEqual('', item[key], 'Key "' + key + '" is empty for item ' + index);
                }
            }
        });
    });
}

function testDataIsNumber(dataName, data) {
    it(dataName + ' should have number data', () => {
        data.forEach((item, index) => {
            for (var key in item) {
                if (item.hasOwnProperty(key) && key !== 'rank' && key !== 'country' && key !== 'team' && key !== 'name' && key !== 'logo' && key !== 'flag' && key !== 'goalDifference') {
                    assert.isFalse(isNaN(item[key]), 'Key "' + key + '" is not a number for item ' + index + ', with the value "' + item[key] + '"');
                }
            }
        });
    });
}
@@ -690,32 +690,90 @@
 m.code, year);%0D%0A
+                        testResultData(item.code, year);%0D%0A
                 
@@ -2041,32 +2041,216 @@
 ', data);%0D%0A%7D%0D%0A%0D%0A
+function testResultData(code, year) %7B%0D%0A    var data = helper.readJsonFile(helper.stringFormat(config.paths.resultsData, code, year));%0D%0A%0D%0A    testDataIsNotEmpty('Results', data);%0D%0A%7D%0D%0A%0D%0A
 function testDat
@@ -2460,16 +2460,34 @@
 rty(key)
+ && key !== 'date'
 ) %7B%0D%0A   

Add test for result data