I have a function that store data into database:
async function save(id, fields) {
...
const exist = await knex('table').select('..').where({ id });
if (exist) {
await knex('table').update([...fields...]).where({ id });
} else {
await knex('table').insert([...fields...]);
}
....
}
How I test this function? I searched in knex docs but got no answer there.
What are the best practices?
Should I connect to "test" database and execute the database? but I think in this case I'll need to update the schema and the data which is kind annoying.
Should I create transaction every time I change the database and undo after the test complete?
Maybe mock knex? but how I know if the type and the query is correct?
Something else?