sqlite.spec.js
 1  const db = require('../../src/persistence/sqlite');
 2  const fs = require('fs');
 3  const location = process.env.SQLITE_DB_LOCATION || '/etc/todos/todo.db';
 4  
 5  const ITEM = {
 6      id: '7aef3d7c-d301-4846-8358-2a91ec9d6be3',
 7      name: 'Test',
 8      completed: false,
 9  };
10  
11  beforeEach(() => {
12      if (fs.existsSync(location)) {
13          fs.unlinkSync(location);
14      }
15  });
16  
17  test('it initializes correctly', async () => {
18      await db.init();
19  });
20  
21  test('it can store and retrieve items', async () => {
22      await db.init();
23  
24      await db.storeItem(ITEM);
25  
26      const items = await db.getItems();
27      expect(items.length).toBe(1);
28      expect(items[0]).toEqual(ITEM);
29  });
30  
31  test('it can update an existing item', async () => {
32      await db.init();
33  
34      const initialItems = await db.getItems();
35      expect(initialItems.length).toBe(0);
36  
37      await db.storeItem(ITEM);
38  
39      await db.updateItem(
40          ITEM.id,
41          Object.assign({}, ITEM, { completed: !ITEM.completed }),
42      );
43  
44      const items = await db.getItems();
45      expect(items.length).toBe(1);
46      expect(items[0].completed).toBe(!ITEM.completed);
47  });
48  
49  test('it can remove an existing item', async () => {
50      await db.init();
51      await db.storeItem(ITEM);
52  
53      await db.removeItem(ITEM.id);
54  
55      const items = await db.getItems();
56      expect(items.length).toBe(0);
57  });
58  
59  test('it can get a single item', async () => {
60      await db.init();
61      await db.storeItem(ITEM);
62  
63      const item = await db.getItem(ITEM.id);
64      expect(item).toEqual(ITEM);
65  });