Initial commit: Inventory Barcode System
This commit is contained in:
80
__tests__/database.test.js
Normal file
80
__tests__/database.test.js
Normal file
@ -0,0 +1,80 @@
|
||||
const DatabaseManager = require('../models/database');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
describe('Database Manager', () => {
|
||||
const testDbPath = path.join(__dirname, '..', 'test_inventory.db');
|
||||
|
||||
beforeEach(() => {
|
||||
// Clean up any existing test database
|
||||
if (fs.existsSync(testDbPath)) {
|
||||
fs.unlinkSync(testDbPath);
|
||||
}
|
||||
|
||||
// Override the database path for testing
|
||||
DatabaseManager.dbPath = testDbPath;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
DatabaseManager.close();
|
||||
|
||||
// Clean up test database
|
||||
if (fs.existsSync(testDbPath)) {
|
||||
fs.unlinkSync(testDbPath);
|
||||
}
|
||||
});
|
||||
|
||||
test('should initialize database successfully', () => {
|
||||
expect(() => {
|
||||
DatabaseManager.initialize();
|
||||
}).not.toThrow();
|
||||
|
||||
expect(DatabaseManager.getDatabase()).toBeDefined();
|
||||
});
|
||||
|
||||
test('should create tables with correct schema', () => {
|
||||
DatabaseManager.initialize();
|
||||
const db = DatabaseManager.getDatabase();
|
||||
|
||||
// Check if items table exists
|
||||
const itemsTable = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='items'").get();
|
||||
expect(itemsTable).toBeDefined();
|
||||
|
||||
// Check if transactions table exists
|
||||
const transactionsTable = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='transactions'").get();
|
||||
expect(transactionsTable).toBeDefined();
|
||||
});
|
||||
|
||||
test('should create indexes correctly', () => {
|
||||
DatabaseManager.initialize();
|
||||
const db = DatabaseManager.getDatabase();
|
||||
|
||||
// Check if indexes exist
|
||||
const indexes = db.prepare("SELECT name FROM sqlite_master WHERE type='index'").all();
|
||||
const indexNames = indexes.map(idx => idx.name);
|
||||
|
||||
expect(indexNames).toContain('idx_items_barcode');
|
||||
expect(indexNames).toContain('idx_items_name');
|
||||
expect(indexNames).toContain('idx_transactions_item_id');
|
||||
expect(indexNames).toContain('idx_transactions_created_at');
|
||||
});
|
||||
|
||||
test('should handle transaction execution', () => {
|
||||
DatabaseManager.initialize();
|
||||
const db = DatabaseManager.getDatabase();
|
||||
|
||||
const result = DatabaseManager.executeTransaction(() => {
|
||||
const insert = db.prepare('INSERT INTO items (name, quantity) VALUES (?, ?)');
|
||||
return insert.run('Test Item', 10);
|
||||
});
|
||||
|
||||
expect(result.changes).toBe(1);
|
||||
expect(result.lastInsertRowid).toBeDefined();
|
||||
});
|
||||
|
||||
test('should throw error when accessing uninitialized database', () => {
|
||||
expect(() => {
|
||||
DatabaseManager.getDatabase();
|
||||
}).toThrow('Database not initialized. Call initialize() first.');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user