Initial commit: Inventory Barcode System

This commit is contained in:
2025-07-22 20:24:51 -04:00
commit 511b01748d
63 changed files with 26932 additions and 0 deletions

View 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.');
});
});