I am working on the following database connector for SQLite:
states.js
const sqlite3 = require('sqlite3').verbose();
const util = require('util');
async function getDB() {
return new Promise(function(resolve, reject) {
let db = new sqlite3.Database('./project.db', (err) => {
if (err) {
console.error(err.message);
reject(err)
} else {
console.log('Connected to the project database.');
resolve(db)
}
});
return db
});
}
exports.getDB = getDB
try {
// run these statements once to set up the db
// let db = getDB();
// db.run(`CREATE TABLE services(id INTEGER PRIMARY KEY, service text, date text)`);
// db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
} catch(err) {
console.log(err)
}
exports.get = async function(service) {
function getResults(service) {
return new Promise(async function (resolve, reject) {
const db = await getDB();
let sql = `SELECT Id id,
Service service,
Date date
FROM services
WHERE service = ?`;
db.get(sql, [service], (err, row) => {
if (err) {
console.error(err.message);
reject(err)
} else {
if (row) {
let this_row = {'id': row.id, 'service': row.service, 'date': row.date};
this_row ? console.log(row.id, row.service, row.date) : console.log(`No service found with the name ${service}`);
resolve(this_row)
} else {
resolve(null)
}
}
})
});
}
let row = await getResults(service)
return row
}
exports.set = async function(service, state) {
function setResults(service) {
return new Promise(async function (resolve, reject) {
const db = await getDB();
let sql = `UPDATE services
set id = ?,
service = ?,
date = ?
WHERE service = ?`;
try {
db.run(sql, [state.id, service, state.date, service]);
resolve();
} catch(err) {
reject(err);
}
});
}
await setResults(service)
}
exports.clear = async function(service) {
function deleteResults(service) {
return new Promise(async function (resolve, reject) {
const db = await getDB();
let sql = `DELETE from services
WHERE service = ?`;
db.run(sql, [service]);
});
}
await deleteResults(service)
}
My test file is as follows
const mocha = require('mocha');
const assert = require('assert');
const expect = require('chai').expect;
const should = require('chai').should();
const state = require('../state');
let deletion_sql = `DELETE from services WHERE service = ?`;
it("get() should return the expected row", async function() {
let db = await state.getDB()
await db.run(deletion_sql, 'blah')
await db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
let result = await state.get('blah')
console.log("get test result is")
console.log(result)
assert.deepEqual(result, { 'id': 1, 'service': 'blah', 'date': '01-23-1987' })
});
it("set() should set to new values as expected", async function() {
let db = await state.getDB()
await db.run(deletion_sql, 'blah')
await db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
await state.set('blah', {'id': 5, 'date': '05-22-2222'})
let result = await state.get('blah')
console.log("set test result is")
console.log(result)
assert.deepEqual(result, { 'id': 5, 'service': 'blah', 'date': '05-22-2222' })
});
it("clear() should delete row from db", async function() {
state.clear('blah').then(async function() {
let result = await state.get('blah')
assert.equal(result, null)
})
})
The first and third test pass, but the second test fails about half the time, and works half the time, when it fails it shows the object as being the old one, { 'id': 1, 'service': 'blah', 'date': '01-23-1987' }
. This tells me that the update (set
) has not finished by the time the test runs .assert.deepEqual
.
I am having problem understand why there is a race condition when I call await
during the test for each database operation. I thought each would wait for the other to finish and there would be no issue by the time I went to read from the database.