Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
521 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
'use strict' | ||
|
||
const sqlite = require('sqlite-async') | ||
|
||
module.exports = class ToDo { | ||
|
||
constructor(dbName = ':memory:') { | ||
return (async() => { | ||
this.db = await sqlite.open(dbName) | ||
const sql = 'CREATE TABLE IF NOT EXISTS items(id INTEGER PRIMARY KEY AUTOINCREMENT, item TEXT, qty NUMERIC)' | ||
await this.db.run(sql) | ||
return this | ||
})() | ||
} | ||
|
||
async add(item, qty) { | ||
if (item === '') throw new Error('item cannot be empty') | ||
if (qty === '') throw new Error('qty cannot be empty') | ||
qty = Number(qty) | ||
if(isNaN(qty)) throw new Error('the quantity must be a number') | ||
let sql = `SELECT * FROM items WHERE ITEM = "${item}"` | ||
const data = await this.db.all(sql) | ||
if(data.length === 0) { | ||
sql = `INSERT INTO items(item, qty) VALUES("${item}", ${qty})` | ||
await this.db.run(sql) | ||
} else { | ||
const newQty = data[0].qty + qty | ||
sql = `UPDATE items SET qty=${newQty} WHERE ITEM = "${item}"` | ||
await this.db.run(sql) | ||
} | ||
} | ||
|
||
async getAll() { | ||
const sql = 'SELECT * FROM items' | ||
const data = await this.db.all(sql) | ||
console.log(data) | ||
return data | ||
} | ||
|
||
async delete(id) { | ||
if (id === undefined) throw new Error('id cannot be undefined') | ||
id = Number(id) | ||
if (isNaN(id)) throw new Error('id must be a number') | ||
const sql = `DELETE FROM items WHERE id=${id}` | ||
if ((await this.db.run(sql)).changes === 0) throw new Error('id has to exist') | ||
} | ||
|
||
async countItems() { | ||
const sql = 'SELECT COUNT(*) as items FROM items' | ||
const data = await this.db.get(sql) | ||
return data.items | ||
} | ||
|
||
async clear() { | ||
await this.db.run('DELETE FROM items') | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
|
||
'use strict' | ||
|
||
const ToDo = require('../modules/todo.js') | ||
|
||
beforeAll( async() => { | ||
// stuff to do before any of the tests run | ||
}) | ||
|
||
afterAll( async() => { | ||
// runs after all the tests have completed | ||
}) | ||
|
||
describe('add()', () => { | ||
// block of tests | ||
// beforeEach( async() => { | ||
// todo.clear() | ||
// }) | ||
afterEach( async() => { | ||
// runs after each test completes | ||
}) | ||
test('add a single item', async done => { | ||
expect.assertions(1) | ||
// ARRANGE | ||
const todo = await new ToDo() // DB runs in-memory if no name supplied | ||
// ACT | ||
await todo.add('bread', 3) | ||
const count = await todo.countItems() | ||
// ASSERT | ||
expect(count).toBe(1) | ||
done() | ||
}) | ||
|
||
test('qty must be a number', async done => { | ||
expect.assertions(1) | ||
const todo = await new ToDo() | ||
await expect( todo.add('bread', 'three') ).rejects.toEqual( Error('the quantity must be a number') ) | ||
done() | ||
}) | ||
|
||
test('duplicates should increase qty', async done => { | ||
expect.assertions(2) | ||
// ARRANGE | ||
const todo = await new ToDo() | ||
// ACT | ||
await todo.add('bread', 4) | ||
await todo.add('bread', 2) | ||
const data = await todo.getAll() | ||
const qty = data[0].qty | ||
// ASSERT (note there are two assertions as stated on line 42) | ||
expect(data).toHaveLength(1) | ||
expect(qty).toEqual(6) | ||
done() | ||
}) | ||
|
||
test('item cannot be empty', async done => { | ||
expect.assertions(1) | ||
const todo = await new ToDo() | ||
await expect( todo.add('', 1) ).rejects.toEqual( Error('item cannot be empty') ) | ||
done() | ||
}) | ||
|
||
test('qty cannot be empty', async done => { | ||
expect.assertions(1) | ||
const todo = await new ToDo() | ||
await expect( todo.add('bread', '') ).rejects.toEqual( Error('qty cannot be empty') ) | ||
done() | ||
}) | ||
|
||
}) | ||
|
||
describe('delete()', () => { | ||
beforeEach( async() => { | ||
this.todo = await new ToDo() | ||
await this.todo.add('bread', 5) | ||
}) | ||
|
||
test('id cannot be undefined', async done => { | ||
expect.assertions(1) | ||
await expect( this.todo.delete() ).rejects.toEqual( Error('id cannot be undefined') ) | ||
done() | ||
}) | ||
|
||
test('id must be a number', async done => { | ||
expect.assertions(1) | ||
await expect( this.todo.delete('a') ).rejects.toEqual( Error('id must be a number') ) | ||
done() | ||
}) | ||
|
||
test('id has to exist', async done => { | ||
expect.assertions(1) | ||
await expect( this.todo.delete(2) ).rejects.toEqual( Error('id has to exist') ) | ||
done() | ||
}) | ||
|
||
test('delete an item', async done => { | ||
expect.assertions(2) | ||
await this.todo.delete(1) | ||
expect(await this.todo.countItems()).toEqual(0) | ||
expect(await this.todo.getAll()).toEqual([]) | ||
done() | ||
}) | ||
}) | ||
|
||
describe('getAll()', () => { | ||
beforeEach( async() => { | ||
this.todo = await new ToDo() | ||
}) | ||
|
||
test('getAll() with no items', async done => { | ||
expect.assertions(1) | ||
expect(await this.todo.getAll()).toEqual([]) | ||
done() | ||
}) | ||
|
||
test('getAll() with a single item', async done => { | ||
expect.assertions(1) | ||
await this.todo.add('bread', 2) | ||
expect(await this.todo.getAll()).toEqual([{"item":"bread", "qty":2, "id":1}]) | ||
done() | ||
}) | ||
|
||
test('getAll() with two items', async done => { | ||
expect.assertions(1) | ||
await this.todo.add('bread', 2) | ||
await this.todo.add('ham', 3) | ||
expect(await this.todo.getAll()).toEqual([{"item":"bread", "qty":2, "id":1}, {"item":"ham", "qty":3, "id":2}]) | ||
done() | ||
}) | ||
}) | ||
|
||
describe('clear()', () => { | ||
beforeEach( async() => { | ||
this.todo = await new ToDo() | ||
}) | ||
|
||
test('clear() with no items', async done => { | ||
expect.assertions(2) | ||
await this.todo.clear() | ||
expect(await this.todo.countItems()).toEqual(0) | ||
expect(await this.todo.getAll()).toEqual([]) | ||
done() | ||
}) | ||
|
||
test('clear() with a single item', async done => { | ||
expect.assertions(2) | ||
await this.todo.add('bread', 2) | ||
await this.todo.clear() | ||
expect(await this.todo.countItems()).toEqual(0) | ||
expect(await this.todo.getAll()).toEqual([]) | ||
done() | ||
}) | ||
|
||
test('clear() with two items', async done => { | ||
expect.assertions(2) | ||
await this.todo.add('bread', 2) | ||
await this.todo.add('ham', 3) | ||
await this.todo.clear() | ||
expect(await this.todo.countItems()).toEqual(0) | ||
expect(await this.todo.getAll()).toEqual([]) | ||
done() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
'use strict' | ||
|
||
let data = [] | ||
|
||
module.exports.clear = () => { | ||
data = [] | ||
} | ||
|
||
module.exports.add = (item, qty) => { | ||
if (item === '') throw new Error('item cannot be empty') | ||
if (qty === '') throw new Error('qty cannot be empty') | ||
qty = Number(qty) | ||
if(isNaN(qty)) throw new Error('qty must be a number') | ||
let flag = false | ||
for(const index in data) { | ||
if (data[index].item === item) { | ||
data[index].qty += qty | ||
flag = true | ||
} | ||
} | ||
if(flag === false) data.push({item: item, qty: qty}) | ||
} | ||
|
||
module.exports.getAll = () => { | ||
for(const key in data) data[key].key = Number(key) | ||
return data | ||
} | ||
|
||
module.exports.delete = key => { | ||
console.log(`delete key ${key}`) | ||
if (key === undefined) throw new Error('key cannot be undefined') | ||
key = Number(key) | ||
if (isNaN(key)) throw new Error('key must be a number') | ||
try { | ||
this.getAll()[key].key // throws error unless the item with the target key exists | ||
data.splice(key, 1) | ||
} catch { | ||
throw new Error('key has to exist') | ||
} | ||
} | ||
|
||
module.exports.countItems = () => data.length |
Oops, something went wrong.