<commit_before>'use strict';

var configParser = require('../lib/config-parser');
var ImplementationError = require('flora-errors').ImplementationError;

var _ = require('lodash');
var expect = require('chai').expect;

var mockDataSource = {
    prepare: function (rawRequest, attributes) {
        if (!rawRequest.expectedAttributes) {
            throw new Error('Mocked DataSource: Please set expectedAttributes for all DataSources in your test');
        }

        expect(attributes).to.eql(rawRequest.expectedAttributes);

        delete rawRequest.expectedAttributes;
    },
    process: function (/*request, callback*/) {}
};
var mockDataSources = {
    'testDataSource': mockDataSource,
    'mysql': mockDataSource,
    'solr': mockDataSource
};

var minimalResourceConfigs = {
    "test": {
        "config": {
            "primaryKey": "id",
            "dataSources": {
                "primary": {
                    "type": "testDataSource",
                    expectedAttributes: ['id']
                }
            },
            "attributes": {
                "id": {
                    "type": "int"
                }
            }
        }
    }
};
var minimalResourceConfigsParsed = {
    "test": {
        config: {
            "primaryKey": [["id"]],
            "resolvedPrimaryKey": {
                "primary": ["id"]
            },
            "dataSources": {
                "primary": {
                    "type": "testDataSource"
                }
            },
            "attributes": {
                "id": {
                    "type": "int",
                    "map": {
                        "default": {
                            "primary": "id"
                        }
                    },
                    "filter": ["equal"]
                }
            }
        }
    }
};

describe('config-parser', function () {
    describe('basic config parsing', function () {
        it('parses minimal resource', function () {
            var resourceConfigs = _.cloneDeep(minimalResourceConfigs);
            var resourceConfigsParsed = _.cloneDeep(minimalResourceConfigsParsed);

            configParser(resourceConfigs, mockDataSources);

            expect(resourceConfigs).to.eql(resourceConfigsParsed);
        });

        it('parses minimal "symlink"-resource', function () {
            var resourceConfigs = {
                "test": {
                    config: {"resource": "test2"}
                },
                "test2": _.cloneDeep(minimalResourceConfigs['test'])
            };
            var resourceConfigsParsed = {
                "test": {
                    config: {"resource": "test2"}
                },
                "test2": _.cloneDeep(minimalResourceConfigsParsed['test'])
            };

            configParser(resourceConfigs, mockDataSources);

            expect(resourceConfigs).to.eql(resourceConfigsParsed);
        });

        it('fails on unknown "symlink"-resource', function () {
            var resourceConfigs = {
                "test": {
                    config: {"resource": "test2"}
                }
            };

            expect(function () {
                configParser(resourceConfigs, mockDataSources);
            }).to.throw(ImplementationError, 'Unknown resource "test2" in resource "test:{root}"');
        });
    });

    describe.only('options in resource-context', function () {
        it('fails on invalid options in resource-context', function () {
            var resourceConfigs = _.cloneDeep(minimalResourceConfigs);

            resourceConfigs['test'].config.type = 'int';

            expect(function () {
                configParser(resourceConfigs, mockDataSources);
            }).to.throw(ImplementationError, 'Invalid option "type" in resource "test:{root}"');
        });

        it('fails on unknown sub-resource', function () {
            var resourceConfigs = _.cloneDeep(minimalResourceConfigs);

            resourceConfigs['test'].config.attributes['subResource'] = {resource: 'unknown'};

            expect(function () {
                configParser(resourceConfigs, mockDataSources);
            }).to.throw(ImplementationError, 'Unknown resource "unknown" in sub-resource "test:subResource"');
        });

        it('fails on DataSources without "type" option', function () {
            var resourceConfigs = _.cloneDeep(minimalResourceConfigs);

            resourceConfigs['test'].config.dataSources['articleBody'] = {};

            expect(function () {
                configParser(resourceConfigs, mockDataSources);
            }).to.throw(ImplementationError, 'DataSource "articleBody" misses "type" option in resource "test:{root}"');
        });

        it('fails on unknown DataSource types', function () {<commit_msg>tests: remove "describe.only"<commit_after>'use strict';

var configParser = require('../lib/config-parser');
var ImplementationError = require('flora-errors').ImplementationError;

var _ = require('lodash');
var expect = require('chai').expect;

var mockDataSource = {
    prepare: function (rawRequest, attributes) {
        if (!rawRequest.expectedAttributes) {
            throw new Error('Mocked DataSource: Please set expectedAttributes for all DataSources in your test');
        }

        expect(attributes).to.eql(rawRequest.expectedAttributes);

        delete rawRequest.expectedAttributes;
    },
    process: function (/*request, callback*/) {}
};
var mockDataSources = {
    'testDataSource': mockDataSource,
    'mysql': mockDataSource,
    'solr': mockDataSource
};

var minimalResourceConfigs = {
    "test": {
        "config": {
            "primaryKey": "id",
            "dataSources": {
                "primary": {
                    "type": "testDataSource",
                    expectedAttributes: ['id']
                }
            },
            "attributes": {
                "id": {
                    "type": "int"
                }
            }
        }
    }
};
var minimalResourceConfigsParsed = {
    "test": {
        config: {
            "primaryKey": [["id"]],
            "resolvedPrimaryKey": {
                "primary": ["id"]
            },
            "dataSources": {
                "primary": {
                    "type": "testDataSource"
                }
            },
            "attributes": {
                "id": {
                    "type": "int",
                    "map": {
                        "default": {
                            "primary": "id"
                        }
                    },
                    "filter": ["equal"]
                }
            }
        }
    }
};

describe('config-parser', function () {
    describe('basic config parsing', function () {
        it('parses minimal resource', function () {
            var resourceConfigs = _.cloneDeep(minimalResourceConfigs);
            var resourceConfigsParsed = _.cloneDeep(minimalResourceConfigsParsed);

            configParser(resourceConfigs, mockDataSources);

            expect(resourceConfigs).to.eql(resourceConfigsParsed);
        });

        it('parses minimal "symlink"-resource', function () {
            var resourceConfigs = {
                "test": {
                    config: {"resource": "test2"}
                },
                "test2": _.cloneDeep(minimalResourceConfigs['test'])
            };
            var resourceConfigsParsed = {
                "test": {
                    config: {"resource": "test2"}
                },
                "test2": _.cloneDeep(minimalResourceConfigsParsed['test'])
            };

            configParser(resourceConfigs, mockDataSources);

            expect(resourceConfigs).to.eql(resourceConfigsParsed);
        });

        it('fails on unknown "symlink"-resource', function () {
            var resourceConfigs = {
                "test": {
                    config: {"resource": "test2"}
                }
            };

            expect(function () {
                configParser(resourceConfigs, mockDataSources);
            }).to.throw(ImplementationError, 'Unknown resource "test2" in resource "test:{root}"');
        });
    });

    describe('options in resource-context', function () {
        it('fails on invalid options in resource-context', function () {
            var resourceConfigs = _.cloneDeep(minimalResourceConfigs);

            resourceConfigs['test'].config.type = 'int';

            expect(function () {
                configParser(resourceConfigs, mockDataSources);
            }).to.throw(ImplementationError, 'Invalid option "type" in resource "test:{root}"');
        });

        it('fails on unknown sub-resource', function () {
            var resourceConfigs = _.cloneDeep(minimalResourceConfigs);

            resourceConfigs['test'].config.attributes['subResource'] = {resource: 'unknown'};

            expect(function () {
                configParser(resourceConfigs, mockDataSources);
            }).to.throw(ImplementationError, 'Unknown resource "unknown" in sub-resource "test:subResource"');
        });

        it('fails on DataSources without "type" option', function () {
            var resourceConfigs = _.cloneDeep(minimalResourceConfigs);

            resourceConfigs['test'].config.dataSources['articleBody'] = {};

            expect(function () {
                configParser(resourceConfigs, mockDataSources);
            }).to.throw(ImplementationError, 'DataSource "articleBody" misses "type" option in resource "test:{root}"');
        });

        it('fails on unknown DataSource types', function () {
{'fraction_non_alphanumeric': 0.12720075007813314, 'fraction_numerical': 0.0012501302218981144, 'mean_word_length': 1.7737648078590003, 'pattern_counts': {'":': 62, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1432476', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2281, 'n_words': 456}