@craftapit/tester
Version:
A focused, LLM-powered testing framework for natural language test scenarios
130 lines (129 loc) • 4.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatabaseAdapter = void 0;
const BaseAdapter_1 = require("./BaseAdapter");
class DatabaseAdapter extends BaseAdapter_1.BaseAdapter {
constructor(config) {
super(config);
this.connection = null;
this.lastResult = null;
this.mockResults = {};
this.dbType = config.type;
this.mockMode = config.mockMode || false;
this.mockResults = config.mockResults || {};
}
async initialize() {
console.log(`Initializing ${this.dbType} database adapter`);
if (this.mockMode) {
console.log('Running in mock mode - no actual database connection will be made');
return;
}
// In a real implementation, this would establish the database connection
// based on the database type and connection details
try {
// This is just a placeholder - actual implementation would depend on the database driver
this.connection = { connected: true };
console.log('Database connection established');
}
catch (error) {
console.error('Failed to connect to database:', error);
throw error;
}
}
async cleanup() {
console.log('Cleaning up database adapter');
if (this.mockMode || !this.connection) {
return;
}
// In a real implementation, this would close the database connection
try {
// This is just a placeholder
this.connection = null;
console.log('Database connection closed');
}
catch (error) {
console.error('Error closing database connection:', error);
}
}
async executeQuery(query, params = []) {
console.log(`Executing query: ${query}`);
console.log('With parameters:', params);
if (this.mockMode) {
// In mock mode, return predefined results based on the query
const mockResult = this.mockResults[query] || { rows: [] };
this.lastResult = mockResult;
return mockResult;
}
if (!this.connection) {
throw new Error('Database connection not established');
}
try {
// This is a placeholder - actual implementation would depend on the database driver
const result = {
rows: [{ id: 1, name: 'Test' }],
rowCount: 1
};
this.lastResult = result;
return result;
}
catch (error) {
console.error('Query execution failed:', error);
throw error;
}
}
async verifyQueryResult(expectations) {
if (!this.lastResult) {
return {
success: false,
reason: 'No previous query result to verify'
};
}
// Verify row count
if (expectations.rowCount !== undefined &&
this.lastResult.rowCount !== expectations.rowCount) {
return {
success: false,
reason: `Expected ${expectations.rowCount} rows but got ${this.lastResult.rowCount}`
};
}
// Verify has rows
if (expectations.hasRows !== undefined) {
const hasRows = this.lastResult.rows.length > 0;
if (expectations.hasRows !== hasRows) {
return {
success: false,
reason: expectations.hasRows
? 'Expected rows but got none'
: 'Expected no rows but got some'
};
}
}
// Verify rows contain expected data
if (expectations.rowsContain) {
for (const expectedRow of expectations.rowsContain) {
const matchingRow = this.lastResult.rows.find(row => {
return Object.entries(expectedRow).every(([key, value]) => row[key] === value);
});
if (!matchingRow) {
return {
success: false,
reason: `Could not find row matching ${JSON.stringify(expectedRow)}`
};
}
}
}
// Custom validation
if (expectations.customValidation &&
!expectations.customValidation(this.lastResult)) {
return {
success: false,
reason: 'Custom validation failed'
};
}
return { success: true };
}
getLastResult() {
return this.lastResult;
}
}
exports.DatabaseAdapter = DatabaseAdapter;