// Database client tests
import { DatabaseClient } from '../src/database.js';
describe('DatabaseClient', () => {
let dbClient;
beforeAll(() => {
dbClient = new DatabaseClient();
});
describe('getProviders', () => {
it('should return list of providers', async () => {
const providers = await dbClient.getProviders();
expect(Array.isArray(providers)).toBe(true);
expect(providers.length).toBeGreaterThan(0);
// Check structure
providers.forEach(provider => {
expect(provider).toHaveProperty('provider_id');
expect(provider).toHaveProperty('provider_name');
expect(provider).toHaveProperty('transaction_count');
expect(provider).toHaveProperty('account_count');
expect(typeof provider.provider_id).toBe('string');
expect(typeof provider.provider_name).toBe('string');
expect(typeof Number(provider.transaction_count)).toBe('number');
expect(typeof Number(provider.account_count)).toBe('number');
});
});
});
describe('getTransactions', () => {
it('should return transactions with default parameters', async () => {
const transactions = await dbClient.getTransactions();
expect(Array.isArray(transactions)).toBe(true);
expect(transactions.length).toBeGreaterThan(0);
expect(transactions.length).toBeLessThanOrEqual(1000); // Default limit
// Check structure
if (transactions.length > 0) {
const transaction = transactions[0];
expect(transaction).toHaveProperty('id');
expect(transaction).toHaveProperty('date');
expect(transaction).toHaveProperty('amount');
expect(transaction).toHaveProperty('description');
expect(transaction).toHaveProperty('category');
expect(transaction).toHaveProperty('account_id');
expect(transaction).toHaveProperty('provider_id');
expect(transaction).toHaveProperty('status');
expect(transaction).toHaveProperty('created_at');
expect(transaction).toHaveProperty('updated_at');
}
});
it('should respect limit parameter', async () => {
const limit = 5;
const transactions = await dbClient.getTransactions({ limit });
expect(transactions.length).toBeLessThanOrEqual(limit);
});
it('should filter by date range', async () => {
const dateFrom = '2025-01-01';
const dateTo = '2025-12-31';
const transactions = await dbClient.getTransactions({ dateFrom, dateTo });
transactions.forEach(transaction => {
const transactionDate = new Date(transaction.date);
expect(transactionDate.getTime()).toBeGreaterThanOrEqual(new Date(dateFrom).getTime());
expect(transactionDate.getTime()).toBeLessThanOrEqual(new Date(dateTo).getTime());
});
});
it('should filter by category', async () => {
const category = 'Food';
const transactions = await dbClient.getTransactions({ category });
transactions.forEach(transaction => {
expect(transaction.category).toBe(category);
});
});
});
describe('getMonthlyData', () => {
it('should return monthly data', async () => {
const monthlyData = await dbClient.getMonthlyData();
expect(Array.isArray(monthlyData)).toBe(true);
expect(monthlyData.length).toBeGreaterThan(0);
// Check structure
if (monthlyData.length > 0) {
const month = monthlyData[0];
expect(month).toHaveProperty('year_month');
expect(month).toHaveProperty('total_income');
expect(month).toHaveProperty('total_expenses');
expect(month).toHaveProperty('net_flow');
expect(month).toHaveProperty('transaction_count');
expect(month).toHaveProperty('provider_id');
}
});
it('should respect months parameter', async () => {
const months = 3;
const monthlyData = await dbClient.getMonthlyData({ months });
expect(monthlyData.length).toBeLessThanOrEqual(months);
});
});
describe('getAccountBalances', () => {
it('should return account balances', async () => {
const balances = await dbClient.getAccountBalances();
expect(Array.isArray(balances)).toBe(true);
// Check structure
balances.forEach(balance => {
expect(balance).toHaveProperty('account_id');
expect(balance).toHaveProperty('account_name');
expect(balance).toHaveProperty('provider_id');
expect(balance).toHaveProperty('provider_name');
expect(balance).toHaveProperty('current_balance');
expect(balance).toHaveProperty('available_balance');
expect(balance).toHaveProperty('currency');
expect(balance).toHaveProperty('last_updated');
});
});
});
describe('getSpendingAnalysis', () => {
it('should return spending analysis', async () => {
const spending = await dbClient.getSpendingAnalysis();
expect(Array.isArray(spending)).toBe(true);
// Check structure
spending.forEach(item => {
expect(item).toHaveProperty('category');
expect(item).toHaveProperty('total_amount');
expect(item).toHaveProperty('transaction_count');
expect(item).toHaveProperty('average_amount');
expect(item).toHaveProperty('provider_id');
expect(item).toHaveProperty('provider_name');
});
});
it('should respect months parameter', async () => {
const months = 6;
const spending = await dbClient.getSpendingAnalysis({ months });
// Should return data (exact validation depends on data availability)
expect(Array.isArray(spending)).toBe(true);
});
});
describe('getFinancialOverview', () => {
it('should return financial overview', async () => {
const overview = await dbClient.getFinancialOverview();
expect(overview).toHaveProperty('total_income');
expect(overview).toHaveProperty('total_expenses');
expect(overview).toHaveProperty('net_cash_flow');
expect(overview).toHaveProperty('transaction_count');
expect(overview).toHaveProperty('providers');
expect(typeof Number(overview.total_income)).toBe('number');
expect(typeof Number(overview.total_expenses)).toBe('number');
expect(typeof Number(overview.net_cash_flow)).toBe('number');
expect(typeof Number(overview.transaction_count)).toBe('number');
expect(Array.isArray(overview.providers)).toBe(true);
});
});
});
//# sourceMappingURL=database.test.js.map