<commit_before>import { BaseApp, JovoError } from "jovo-core";
import { CosmosDb } from "../src/CosmosDb";
import {MongoClient} from  "mongodb";
import MongoMemoryServer from 'mongodb-memory-server';
import _set = require('lodash.set');

jest.setTimeout(600000);

describe('test install()', () => {
    describe('test install() setting app.$db', () => {
        test('test should set app.$db to be CosmosDb if no default db was set in config', () => {
            const cosmosdb = new CosmosDb();
            const app = new BaseApp();

            cosmosdb.install(app);

            expect(app.$db).toBeInstanceOf(CosmosDb);
        });

        test('test app.$db should not be an instance of CosmosDb if default db set in config is not CosmosDb', () => {
            const cosmosdb = new CosmosDb();
            const app = new BaseApp();
            _set(app.config, 'db.default', 'test');

            cosmosdb.install(app);

            expect(app.$db).not.toBeInstanceOf(CosmosDb);
        });

        test('test app.$db should be an instance CosmosDb if default db is set to CosmosDb', () => {
            const cosmosdb = new CosmosDb();
            const app = new BaseApp();
            _set(app.config, 'db.default', 'CosmosDb');

            cosmosdb.install(app);

            expect(app.$db).toBeInstanceOf(CosmosDb);
        });
    });
});

describe('test errorHandling() which checks config parameters', () => {
    test('test should throw an error if uri isn\'t set', () => {
        const config = {
            databaseName: 'test',
            collectionName: 'UserData',
            primaryKeyColumn: 'userId'
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).toThrow(Error);
    });

    test('test should throw an error if databaseName isn\'t set', () => {
        const config = {
            uri: 'test',
            collectionName: 'UserData',
            primaryKeyColumn: 'userId'
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).toThrow(Error);
    });

    test('test should throw an error if primaryKeyColumn isn\'t set', () => {
        const config = {
            uri: 'test',
            databaseName: 'test',
            primaryKeyColumn: '',
            collectionName: 'UserData'
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).toThrow(Error);
    });

    test('test should throw an error if collectionName isn\'t set', () => {
        const config = {
            uri: 'test',
            databaseName: 'test',
            primaryKeyColumn: 'userId',
            collectionName: ''
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).toThrow(Error);
    });

    test('test shouldn\'t throw an error if config is valid', () => {
        const config = {
            uri: 'test',
            databaseName: 'test',
            primaryKeyColumn: 'userId',
            collectionName: 'UserData'
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).not.toThrow(Error);
    });<commit_msg>:white_check_mark: set properties as empty strings instead of leaving them out. Switch to JovoError.<commit_after>import { BaseApp, JovoError } from "jovo-core";
import { CosmosDb } from "../src/CosmosDb";
import {MongoClient} from  "mongodb";
import MongoMemoryServer from 'mongodb-memory-server';
import _set = require('lodash.set');

jest.setTimeout(600000);

describe('test install()', () => {
    describe('test install() setting app.$db', () => {
        test('test should set app.$db to be CosmosDb if no default db was set in config', () => {
            const cosmosdb = new CosmosDb();
            const app = new BaseApp();

            cosmosdb.install(app);

            expect(app.$db).toBeInstanceOf(CosmosDb);
        });

        test('test app.$db should not be an instance of CosmosDb if default db set in config is not CosmosDb', () => {
            const cosmosdb = new CosmosDb();
            const app = new BaseApp();
            _set(app.config, 'db.default', 'test');

            cosmosdb.install(app);

            expect(app.$db).not.toBeInstanceOf(CosmosDb);
        });

        test('test app.$db should be an instance CosmosDb if default db is set to CosmosDb', () => {
            const cosmosdb = new CosmosDb();
            const app = new BaseApp();
            _set(app.config, 'db.default', 'CosmosDb');

            cosmosdb.install(app);

            expect(app.$db).toBeInstanceOf(CosmosDb);
        });
    });
});

describe('test errorHandling() which checks config parameters', () => {
    test('test should throw an error if uri isn\'t set', () => {
        const config = {
            uri: '',
            databaseName: 'test',
            collectionName: 'UserData',
            primaryKeyColumn: 'userId'
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).toThrow(JovoError);
    });

    test('test should throw an error if databaseName isn\'t set', () => {
        const config = {
            uri: 'test',
            database: '',
            collectionName: 'UserData',
            primaryKeyColumn: 'userId'
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).toThrow(JovoError);
    });

    test('test should throw an error if primaryKeyColumn isn\'t set', () => {
        const config = {
            uri: 'test',
            databaseName: 'test',
            primaryKeyColumn: '',
            collectionName: 'UserData'
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).toThrow(JovoError);
    });

    test('test should throw an error if collectionName isn\'t set', () => {
        const config = {
            uri: 'test',
            databaseName: 'test',
            primaryKeyColumn: 'userId',
            collectionName: ''
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).toThrow(JovoError);
    });

    test('test shouldn\'t throw an error if config is valid', () => {
        const config = {
            uri: 'test',
            databaseName: 'test',
            primaryKeyColumn: 'userId',
            collectionName: 'UserData'
        };
        const cosmosdb = new CosmosDb(config);

        expect(() => {
            cosmosdb.errorHandling();
        }).not.toThrow(Error);
    });
{'fraction_non_alphanumeric': 0.13799177921315325, 'fraction_numerical': 0.0017615971814445098, 'mean_word_length': 1.9596003475238923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '4820332', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1861, 'n_words': 495}