mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
feat(db): provide better-sqlite3 connection via DI token and complete DatabaseService surface
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Locks in the delegation direction: DatabaseService's trip-access helpers
|
||||
* must call the db/database exports (which e2e suites stub in their vi.mock
|
||||
* factories), never reimplement the SQL against the injected connection.
|
||||
*/
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
|
||||
const { canAccessTrip, isOwner, getPlaceWithTags } = vi.hoisted(() => ({
|
||||
canAccessTrip: vi.fn(() => ({ id: -1, user_id: -2, currency: 'XXX' })),
|
||||
isOwner: vi.fn(() => true),
|
||||
getPlaceWithTags: vi.fn(() => null),
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/db/database', () => ({
|
||||
db: {},
|
||||
closeDb: () => {},
|
||||
reinitialize: () => {},
|
||||
canAccessTrip,
|
||||
isOwner,
|
||||
getPlaceWithTags,
|
||||
}));
|
||||
|
||||
import { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
|
||||
describe('DatabaseService (helper delegation)', () => {
|
||||
it('routes trip-access helpers through the db/database exports', async () => {
|
||||
const { db } = await import('../../../src/db/database');
|
||||
const svc = new DatabaseService(db);
|
||||
|
||||
expect(svc.canAccessTrip(7, 8)).toEqual({ id: -1, user_id: -2, currency: 'XXX' });
|
||||
expect(canAccessTrip).toHaveBeenCalledWith(7, 8);
|
||||
|
||||
expect(svc.isOwner(7, 8)).toBe(true);
|
||||
expect(isOwner).toHaveBeenCalledWith(7, 8);
|
||||
|
||||
expect(svc.getPlaceWithTags(9)).toBeNull();
|
||||
expect(getPlaceWithTags).toHaveBeenCalledWith(9);
|
||||
});
|
||||
});
|
||||
@@ -3,10 +3,11 @@
|
||||
* helper against the real connection so the typed query surface is covered.
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { db } from '../../../src/db/database';
|
||||
import { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
|
||||
describe('DatabaseService (typed query helpers)', () => {
|
||||
const svc = new DatabaseService();
|
||||
const svc = new DatabaseService(db);
|
||||
|
||||
it('exposes the shared connection', () => {
|
||||
expect(typeof svc.connection.prepare).toBe('function');
|
||||
@@ -34,3 +35,40 @@ describe('DatabaseService (typed query helpers)', () => {
|
||||
svc.run('DROP TABLE _dbsvc_test');
|
||||
});
|
||||
});
|
||||
|
||||
describe('DatabaseService (trip-access helpers)', () => {
|
||||
const svc = new DatabaseService(db);
|
||||
|
||||
it('canAccessTrip / isOwner / getPlaceWithTags delegate to the shared helpers', () => {
|
||||
svc.run(
|
||||
"INSERT INTO users (id, username, email, password_hash) VALUES (900, 'dbsvc-owner', 'dbsvc-owner@x.test', 'x')"
|
||||
);
|
||||
svc.run(
|
||||
"INSERT INTO users (id, username, email, password_hash) VALUES (901, 'dbsvc-member', 'dbsvc-member@x.test', 'x')"
|
||||
);
|
||||
svc.run("INSERT INTO trips (id, user_id, title) VALUES (900, 900, 'DbSvc Trip')");
|
||||
svc.run("INSERT INTO trip_members (trip_id, user_id) VALUES (900, 901)");
|
||||
|
||||
expect(svc.canAccessTrip(900, 900)).toMatchObject({ id: 900, user_id: 900 });
|
||||
expect(svc.canAccessTrip(900, 901)).toMatchObject({ id: 900 });
|
||||
expect(svc.canAccessTrip(900, 999)).toBeUndefined();
|
||||
|
||||
expect(svc.isOwner(900, 900)).toBe(true);
|
||||
expect(svc.isOwner(900, 901)).toBe(false);
|
||||
|
||||
expect(svc.getPlaceWithTags(999999)).toBeNull();
|
||||
svc.run("INSERT INTO places (id, trip_id, name) VALUES (900, 900, 'DbSvc Place')");
|
||||
expect(svc.getPlaceWithTags(900)).toMatchObject({
|
||||
id: 900,
|
||||
name: 'DbSvc Place',
|
||||
category: null,
|
||||
tags: [],
|
||||
ratings: [],
|
||||
rating_avg: null,
|
||||
rating_count: 0,
|
||||
});
|
||||
|
||||
svc.run('DELETE FROM trips WHERE id = 900');
|
||||
svc.run('DELETE FROM users WHERE id IN (900, 901)');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { HttpException } from '@nestjs/common';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { db } from '../../../src/db/database';
|
||||
import { AppModule } from '../../../src/nest/app.module';
|
||||
import { HealthController } from '../../../src/nest/health/health.controller';
|
||||
import { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
@@ -33,7 +34,7 @@ describe('AdminGuard', () => {
|
||||
|
||||
describe('DatabaseService (shared connection)', () => {
|
||||
it('runs real queries against the existing SQLite connection', () => {
|
||||
const svc = new DatabaseService();
|
||||
const svc = new DatabaseService(db);
|
||||
expect(svc.get('SELECT 1 AS one')).toEqual({ one: 1 });
|
||||
expect(svc.all('SELECT 1 AS one')).toEqual([{ one: 1 }]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user