<commit_before>"use strict";

let path = require("path");
let assert = require("assert");
let qtopology = require("qtopology");
let dbu = require("../built/db_updater");

//////////////////////////////////////////////////////////////////////////////////////

const scripts_dir = path.normalize("some/scripts/dir");
const settings_table = "SomeWeirdNameForSettingsTable";
const version_record_key = "SomeWeirdNameForASetting";

qtopology.logger().setLevel("none");

describe('DB updater', function () {
    describe('run', function () {
        it('should handle empty query result', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        case path.join(scripts_dir, "init.sql"): return "init.sql";
                        case "v1.sql": return "v1.sql";
                        case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, []);
                        case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                            return cb(null, []);
                        case `init.sql`:
                            return cb(null, []);
                        case `v1.sql`:
                            return cb(null, []);
                        case `v2.sql`:
                            return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
        it('should handle DB lower', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        case path.join(scripts_dir, "init.sql"): return "init.sql";
                        //case "v1.sql": return "v1.sql";
                        case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, [{ name: "", value: "1"}]);
                        //case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                            return cb(null, []);
                        case `init.sql`:
                            return cb(null, []);
                        // case `v1.sql`:
                        //     return cb(null, []);
                        case `v2.sql`:
                            return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
        it('should handle DB at latest', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        case path.join(scripts_dir, "init.sql"): return "init.sql";
                        //case "v1.sql": return "v1.sql";
                        //case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, [{ name: "", value: "2"}]);
                        //case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        // case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                        //     return cb(null, []);
                        case `init.sql`:
                            return cb(null, []);
                        // case `v1.sql`:
                        //     return cb(null, []);
                        // case `v2.sql`:
                        //     return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
    });
    describe('check', function () {
        it('should handle empty query result', function (done) {
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    assert.equal(sql, `select value from ${settings_table} where name = '${version_record_key}';`);
                    cb(null, []);<commit_msg>Added unit tests<commit_after>"use strict";

let path = require("path");
let assert = require("assert");
let qtopology = require("qtopology");
let dbu = require("../built/db_updater");

//////////////////////////////////////////////////////////////////////////////////////

const scripts_dir = path.normalize("some/scripts/dir");
const settings_table = "SomeWeirdNameForSettingsTable";
const version_record_key = "SomeWeirdNameForASetting";

qtopology.logger().setLevel("none");

describe('DB updater', function () {
    describe('run', function () {
        it('should handle empty query result', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        case path.join(scripts_dir, "init.sql"): return "init.sql";
                        case "v1.sql": return "v1.sql";
                        case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, []);
                        case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                            return cb(null, []);
                        case `init.sql`:
                            return cb(null, []);
                        case `v1.sql`:
                            return cb(null, []);
                        case `v2.sql`:
                            return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
        it('should handle DB lower', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        case path.join(scripts_dir, "init.sql"): return "init.sql";
                        //case "v1.sql": return "v1.sql";
                        case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, [{ name: "", value: "1"}]);
                        //case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                            return cb(null, []);
                        case `init.sql`:
                            return cb(null, []);
                        // case `v1.sql`:
                        //     return cb(null, []);
                        case `v2.sql`:
                            return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
        it('should handle DB at latest', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        case path.join(scripts_dir, "init.sql"): return "init.sql";
                        //case "v1.sql": return "v1.sql";
                        //case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, [{ name: "", value: "2"}]);
                        //case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        // case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                        //     return cb(null, []);
                        case `init.sql`:
                            return cb(null, []);
                        // case `v1.sql`:
                        //     return cb(null, []);
                        // case `v2.sql`:
                        //     return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
    });
    describe('run with injected init script name', function () {
        it('should handle empty query result', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        case path.join(scripts_dir, "init2.sql"): return "init2.sql";
                        case "v1.sql": return "v1.sql";
                        case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, []);
                        case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                            return cb(null, []);
                        case `init2.sql`:
                            return cb(null, []);
                        case `v1.sql`:
                            return cb(null, []);
                        case `v2.sql`:
                            return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs,
                init_script_name: "init2.sql"
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
        it('should handle DB lower', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        case path.join(scripts_dir, "init2.sql"): return "init2.sql";
                        //case "v1.sql": return "v1.sql";
                        case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, [{ name: "", value: "1"}]);
                        //case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                            return cb(null, []);
                        case `init2.sql`:
                            return cb(null, []);
                        // case `v1.sql`:
                        //     return cb(null, []);
                        case `v2.sql`:
                            return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs,
                init_script_name: "init2.sql"
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
        it('should handle DB at latest', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        case path.join(scripts_dir, "init2.sql"): return "init2.sql";
                        //case "v1.sql": return "v1.sql";
                        //case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, [{ name: "", value: "2"}]);
                        //case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        // case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                        //     return cb(null, []);
                        case `init2.sql`:
                            return cb(null, []);
                        // case `v1.sql`:
                        //     return cb(null, []);
                        // case `v2.sql`:
                        //     return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs,
                init_script_name: "init2.sql"
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
    });
    describe('run with no init script', function () {
        it('should handle empty query result', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        //case path.join(scripts_dir, "init2.sql"): return "init2.sql";
                        case "v1.sql": return "v1.sql";
                        case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, []);
                        case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                            return cb(null, []);
                        // case `init2.sql`:
                        //     return cb(null, []);
                        case `v1.sql`:
                            return cb(null, []);
                        case `v2.sql`:
                            return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs,
                use_init_script: false
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
        it('should handle DB lower', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        //case path.join(scripts_dir, "init2.sql"): return "init2.sql";
                        //case "v1.sql": return "v1.sql";
                        case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, [{ name: "", value: "1"}]);
                        //case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                            return cb(null, []);
                        // case `init2.sql`:
                        //     return cb(null, []);
                        // case `v1.sql`:
                        //     return cb(null, []);
                        case `v2.sql`:
                            return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs,
                use_init_script: false
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
        it('should handle DB at latest', function (done) {
            let mock_fs = {
                readFileSync: (fname) => {
                    switch (fname) {
                        //case path.join(scripts_dir, "init2.sql"): return "init2.sql";
                        //case "v1.sql": return "v1.sql";
                        //case "v2.sql": return "v2.sql";
                    }
                    assert.fail("Unexpected file to read: " + fname);
                }
            };
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    switch (sql) {
                        case `select value from ${settings_table} where name = '${version_record_key}';`:
                            return cb(null, [{ name: "", value: "2"}]);
                        //case `update ${settings_table} set value = '1' where name = '${version_record_key}'`:
                        // case `update ${settings_table} set value = '2' where name = '${version_record_key}'`:
                        //     return cb(null, []);
                        // case `init2.sql`:
                        //     return cb(null, []);
                        // case `v1.sql`:
                        //     return cb(null, []);
                        // case `v2.sql`:
                        //     return cb(null, []);
                    }
                    assert.fail("Unexpected sql: " + sql);
                }
            }
            let target = new dbu.DbUpgrader({
                conn: mock_conn,
                glob: mock_glob,
                scripts_dir: scripts_dir,
                settings_table: settings_table,
                version_record_key: version_record_key,
                fs: mock_fs,
                use_init_script: false
            });
            target.run((err) => {
                assert.ok(err == null);
                done();
            });
        });
    });
    describe('check', function () {
        it('should handle empty query result', function (done) {
            let mock_glob = {
                sync: (p) => {
                    assert.equal(p, path.normalize(scripts_dir + "/v*.sql"));
                    return ["v1.sql", "v2.sql"];
                }
            };
            let mock_conn = {
                query: (sql, cb) => {
                    assert.equal(sql, `select value from ${settings_table} where name = '${version_record_key}';`);
                    cb(null, []);
{'fraction_non_alphanumeric': 0.13157415832575067, 'fraction_numerical': 0.00556869881710646, 'mean_word_length': 0.9969474525764954, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 55, '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': '3069776', 'n_tokens_mistral': 7198, 'n_tokens_neox': 6727, 'n_words': 1527}