summaryrefslogtreecommitdiff
path: root/app/spec/persistence
diff options
context:
space:
mode:
Diffstat (limited to 'app/spec/persistence')
-rw-r--r--app/spec/persistence/sqlite.spec.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/app/spec/persistence/sqlite.spec.js b/app/spec/persistence/sqlite.spec.js
new file mode 100644
index 0000000..996d451
--- /dev/null
+++ b/app/spec/persistence/sqlite.spec.js
@@ -0,0 +1,65 @@
+const db = require('../../src/persistence/sqlite');
+const fs = require('fs');
+const location = process.env.SQLITE_DB_LOCATION || '/etc/todos/todo.db';
+
+const ITEM = {
+ id: '7aef3d7c-d301-4846-8358-2a91ec9d6be3',
+ name: 'Test',
+ completed: false,
+};
+
+beforeEach(() => {
+ if (fs.existsSync(location)) {
+ fs.unlinkSync(location);
+ }
+});
+
+test('it initializes correctly', async () => {
+ await db.init();
+});
+
+test('it can store and retrieve items', async () => {
+ await db.init();
+
+ await db.storeItem(ITEM);
+
+ const items = await db.getItems();
+ expect(items.length).toBe(1);
+ expect(items[0]).toEqual(ITEM);
+});
+
+test('it can update an existing item', async () => {
+ await db.init();
+
+ const initialItems = await db.getItems();
+ expect(initialItems.length).toBe(0);
+
+ await db.storeItem(ITEM);
+
+ await db.updateItem(
+ ITEM.id,
+ Object.assign({}, ITEM, { completed: !ITEM.completed }),
+ );
+
+ const items = await db.getItems();
+ expect(items.length).toBe(1);
+ expect(items[0].completed).toBe(!ITEM.completed);
+});
+
+test('it can remove an existing item', async () => {
+ await db.init();
+ await db.storeItem(ITEM);
+
+ await db.removeItem(ITEM.id);
+
+ const items = await db.getItems();
+ expect(items.length).toBe(0);
+});
+
+test('it can get a single item', async () => {
+ await db.init();
+ await db.storeItem(ITEM);
+
+ const item = await db.getItem(ITEM.id);
+ expect(item).toEqual(ITEM);
+});