Improved DB tests, now both local and remote DB can be tested easily

pull/809/head
Tzahi12345 3 years ago
parent f20a31ed0f
commit c724a8019a

@ -1,5 +1,8 @@
{ {
"mochaExplorer.files": "backend/test/**/*.js", "mochaExplorer.files": "backend/test/**/*.js",
"mochaExplorer.cwd": "backend", "mochaExplorer.cwd": "backend",
"mochaExplorer.globImplementation": "vscode" "mochaExplorer.globImplementation": "vscode",
"mochaExplorer.env": {
"YTDL_MODE": "debug"
}
} }

@ -2,6 +2,7 @@ var fs = require('fs-extra')
var path = require('path') var path = require('path')
const { MongoClient } = require("mongodb"); const { MongoClient } = require("mongodb");
const { uuid } = require('uuidv4'); const { uuid } = require('uuidv4');
const _ = require('lodash');
const config_api = require('./config'); const config_api = require('./config');
var utils = require('./utils') var utils = require('./utils')
@ -152,6 +153,7 @@ exports._connectToDB = async (custom_connection_string = null) => {
await database.collection(table).createIndex(text_search); await database.collection(table).createIndex(text_search);
} }
}); });
using_local_db = false; // needs to happen for tests (in normal operation using_local_db is guaranteed false)
return true; return true;
} catch(err) { } catch(err) {
logger.error(err); logger.error(err);
@ -578,7 +580,6 @@ exports.setVideoProperty = async (file_uid, assignment_obj) => {
exports.insertRecordIntoTable = async (table, doc, replaceFilter = null) => { exports.insertRecordIntoTable = async (table, doc, replaceFilter = null) => {
// local db override // local db override
if (using_local_db) { if (using_local_db) {
if (replaceFilter) local_db.get(table).remove(replaceFilter).write();
local_db.get(table).push(doc).write(); local_db.get(table).push(doc).write();
return true; return true;
} }
@ -1141,3 +1142,8 @@ exports.applyFilterLocalDB = (db_path, filter_obj, operation) => {
}); });
return return_val; return return_val;
} }
// should only be used for tests
exports.setLocalDBMode = (mode) => {
using_local_db = mode;
}

@ -32,6 +32,7 @@
"fs-extra": "^9.0.0", "fs-extra": "^9.0.0",
"gotify": "^1.1.0", "gotify": "^1.1.0",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"lowdb": "^1.0.0", "lowdb": "^1.0.0",
"md5": "^2.2.1", "md5": "^2.2.1",
"merge-files": "^0.1.2", "merge-files": "^0.1.2",

@ -104,8 +104,18 @@ describe('Database', async function() {
}); });
describe('Basic functions', async function() { describe('Basic functions', async function() {
// test both local_db and remote_db
const local_db_modes = [false, true];
for (const local_db_mode of local_db_modes) {
let use_local_db = local_db_mode;
describe(`Use local DB - ${use_local_db}`, async function() {
beforeEach(async function() { beforeEach(async function() {
await db_api.connectToDB(); if (!use_local_db) {
this.timeout(120000);
await db_api.connectToDB(0);
}
await db_api.removeAllRecords('test'); await db_api.removeAllRecords('test');
}); });
it('Add and read record', async function() { it('Add and read record', async function() {
@ -115,7 +125,23 @@ describe('Database', async function() {
assert(added_record['test_add'] === 'test'); assert(added_record['test_add'] === 'test');
await db_api.removeRecord('test', {test_add: 'test'}); await db_api.removeRecord('test', {test_add: 'test'});
}); });
it('Add and read record - Nested property', async function() {
this.timeout(120000);
await db_api.insertRecordIntoTable('test', {test_add: 'test', test_nested: {test_key1: 'test1', test_key2: 'test2'}});
const added_record = await db_api.getRecord('test', {test_add: 'test', 'test_nested.test_key1': 'test1', 'test_nested.test_key2': 'test2'});
const not_added_record = await db_api.getRecord('test', {test_add: 'test', 'test_nested.test_key1': 'test1', 'test_nested.test_key2': 'test3'});
assert(added_record['test_add'] === 'test');
assert(!not_added_record);
await db_api.removeRecord('test', {test_add: 'test'});
});
it('Replace filter', async function() {
this.timeout(120000);
await db_api.insertRecordIntoTable('test', {test_replace_filter: 'test', test_nested: {test_key1: 'test1', test_key2: 'test2'}}, {test_nested: {test_key1: 'test1', test_key2: 'test2'}});
await db_api.insertRecordIntoTable('test', {test_replace_filter: 'test', test_nested: {test_key1: 'test1', test_key2: 'test2'}}, {test_nested: {test_key1: 'test1', test_key2: 'test2'}});
const count = await db_api.getRecords('test', {test_replace_filter: 'test'}, true);
assert(count === 1);
await db_api.removeRecord('test', {test_replace_filter: 'test'});
});
it('Find duplicates by key', async function() { it('Find duplicates by key', async function() {
const test_duplicates = [ const test_duplicates = [
{ {
@ -250,6 +276,8 @@ describe('Database', async function() {
assert(success); assert(success);
}); });
}); });
}
});
describe('Local DB Filters', async function() { describe('Local DB Filters', async function() {
it('Basic', async function() { it('Basic', async function() {

Loading…
Cancel
Save