File size: 6,038 Bytes
25059bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f0f638
25059bd
 
9c74555
25059bd
 
9c74555
 
25059bd
 
 
 
 
 
 
9c74555
25059bd
 
9c74555
 
25059bd
 
 
 
 
 
9c74555
25059bd
 
1f0f638
9c74555
25059bd
 
 
 
9c74555
25059bd
 
 
1f0f638
9c74555
25059bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c74555
25059bd
 
 
1f0f638
9c74555
25059bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c74555
25059bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { describe, it, expect, vi, afterAll } from 'vitest'
import fs from 'fs'
import path from 'path'
import { mkdtempSync } from 'fs'
import { tmpdir } from 'os'
import { createRequire } from 'module'
import { save, restore, setEnv, configMockFactory } from '../helpers/coverage'

const tmpDir = mkdtempSync(path.join(tmpdir(), 'cov-db-'))
const _require = createRequire(import.meta.url)
function clearCjs(...keys) {
  for (const key of keys) {
    const resolved = _require.resolve(key)
    if (_require.cache[resolved]) delete _require.cache[resolved]
  }
}

afterAll(() => {
  try { fs.rmSync(tmpDir, { recursive: true, force: true }) } catch {}
})

vi.mock('../../src/config/index.js', () => configMockFactory())

describe('database.js', () => {
  it('covers getWrappedDb with DEBUG_SQL enabled', () => {
    const s = save('DEBUG_SQL', 'DB_PATH')
    setEnv({ DEBUG_SQL: 'true', DB_PATH: path.join(tmpDir, 'db1.db') })
    clearCjs('../../src/db/index.js', '../../src/config/index.js')
    const mod = _require('../../src/db/index.js')
    const w = mod.getWrappedDb()
    expect(w).toBeDefined()
    const w2 = mod.getWrappedDb()
    expect(w2).toBe(w)
    restore(s)
  })

  it('covers getDb when db is already initialized', () => {
    const s = save('DB_PATH')
    process.env.DB_PATH = path.join(tmpDir, 'db2.db')
    clearCjs('../../src/db/index.js', '../../src/config/index.js')
    const mod = _require('../../src/db/index.js')
    const first = mod.getDb()
    const second = mod.getDb()
    expect(first).toBe(second)
    restore(s)
  })

  it('covers getWrappedDb without DEBUG_SQL', () => {
    const s = save('DEBUG_SQL', 'DB_PATH')
    setEnv({ DEBUG_SQL: 'false', DB_PATH: path.join(tmpDir, 'db3.db') })
    clearCjs('../../src/db/index.js', '../../src/config/index.js')
    const mod = _require('../../src/db/index.js')
    expect(mod.getWrappedDb()).toBe(mod.getDb())
    restore(s)
  })

  it('covers parseRow, buildWhere, listAll, and getOne', () => {
    const dbPath = path.join(tmpDir, 'db-crud.db')
    const s = save('DB_PATH', 'DEBUG_SQL')
    setEnv({ DB_PATH: dbPath, DEBUG_SQL: 'true' })
    clearCjs('../../src/db/index.js', '../../src/config/index.js')
    const mod = _require('../../src/db/index.js')
    const db = mod.getDb()
    db.exec(`
      CREATE TABLE users (
        id INTEGER PRIMARY KEY,
        name TEXT,
        username TEXT,
        email TEXT,
        phone TEXT,
        website TEXT,
        address TEXT,
        company TEXT
      );
      CREATE TABLE todos (
        id INTEGER PRIMARY KEY,
        userId INTEGER,
        title TEXT,
        completed INTEGER
      );
    `)
    db.prepare(`
      INSERT INTO users (id, name, username, email, phone, website, address, company)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    `).run(1, 'Alice', 'alice', 'alice@example.com', '123', 'example.com', '{"street":"1"}', '{"name":"ACME"}')
    db.prepare(`
      INSERT INTO users (id, name, username, email, phone, website, address, company)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    `).run(2, 'Bob', 'bob', 'bob@example.com', '456', 'bob.example.com', 'not-json', '{bad')
    db.prepare('INSERT INTO todos (id, userId, title, completed) VALUES (?, ?, ?, ?)').run(1, 1, 'Done', 1)
    db.prepare('INSERT INTO todos (id, userId, title, completed) VALUES (?, ?, ?, ?)').run(2, 1, 'Todo', 0)

    const users = mod.listAll('users', { name: 'Alice', ignored: 'value' })
    expect(users).toHaveLength(1)
    expect(users[0].address).toEqual({ street: '1' })
    expect(users[0].company).toEqual({ name: 'ACME' })
    expect(users[0].completed).toBeUndefined()

    const done = mod.listAll('todos', { completed: 'true', ignored: 'value' })
    expect(done).toHaveLength(1)
    expect(done[0].completed).toBe(true)
    const pending = mod.listAll('todos', { completed: 'false' })
    expect(pending).toHaveLength(1)
    expect(pending[0].completed).toBe(false)
    expect(mod.getOne('users', 999)).toBeNull()
    restore(s)
  })

  it('covers insertOne, updateOne, deleteOne, and nextId', () => {
    const dbPath = path.join(tmpDir, 'db-write.db')
    const s = save('DB_PATH', 'DEBUG_SQL')
    setEnv({ DB_PATH: dbPath, DEBUG_SQL: 'false' })
    clearCjs('../../src/db/index.js', '../../src/config/index.js')
    const mod = _require('../../src/db/index.js')
    const db = mod.getDb()
    db.exec(`
      CREATE TABLE users (
        id INTEGER PRIMARY KEY,
        name TEXT,
        username TEXT,
        email TEXT,
        phone TEXT,
        website TEXT,
        address TEXT,
        company TEXT
      );
      CREATE TABLE todos (
        id INTEGER PRIMARY KEY,
        userId INTEGER,
        title TEXT,
        completed INTEGER
      );
    `)

    expect(mod.nextId('users')).toBe(1)
    const inserted = mod.insertOne('users', {
      id: 5,
      name: 'Carol',
      username: 'carol',
      email: 'carol@example.com',
      phone: '789',
      website: 'carol.example.com',
      address: { city: 'Hanoi' },
      company: { name: 'XYZ' },
    })
    expect(inserted.address).toEqual({ city: 'Hanoi' })
    expect(inserted.company).toEqual({ name: 'XYZ' })
    expect(mod.nextId('users')).toBe(6)

    mod.insertOne('todos', { id: 1, userId: 1, title: 'Initial', completed: true })
    mod.insertOne('todos', { id: 2, userId: 1, title: 'Another', completed: false })
    expect(mod.updateOne('users', 999, { name: 'Missing' }, false)).toBeNull()
    const replaced = mod.updateOne('users', 5, {
      name: 'Carol Updated',
      address: { city: 'Danang' },
      company: null,
    }, true)
    expect(replaced.id).toBe(5)
    expect(replaced.name).toBe('Carol Updated')
    expect(replaced.company).toBeNull()
    expect(replaced.username).toBe('carol')

    const patchedTodo = mod.updateOne('todos', 1, { completed: false, title: 'Patched' }, false)
    expect(patchedTodo.completed).toBe(false)
    expect(patchedTodo.userId).toBe(1)
    expect(mod.deleteOne('todos', 999)).toBeNull()
    expect(mod.deleteOne('todos', 1).id).toBe(1)
    restore(s)
  })
})