test-utils.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { dataSource } from '@app/infra';
  2. import { IJobRepository, JobItem, JobItemHandler, QueueName } from '@app/domain';
  3. import { AppModule } from '@app/immich';
  4. import { INestApplication, Logger } from '@nestjs/common';
  5. import { Test, TestingModule } from '@nestjs/testing';
  6. import * as fs from 'fs';
  7. import path from 'path';
  8. import { AppService } from '../src/microservices/app.service';
  9. export const IMMICH_TEST_ASSET_PATH = process.env.IMMICH_TEST_ASSET_PATH;
  10. export const IMMICH_TEST_ASSET_TEMP_PATH = path.normalize(`${IMMICH_TEST_ASSET_PATH}/temp/`);
  11. export const db = {
  12. reset: async () => {
  13. if (!dataSource.isInitialized) {
  14. await dataSource.initialize();
  15. }
  16. await dataSource.transaction(async (em) => {
  17. for (const entity of dataSource.entityMetadatas) {
  18. if (entity.tableName === 'users') {
  19. continue;
  20. }
  21. await em.query(`DELETE FROM ${entity.tableName} CASCADE;`);
  22. }
  23. await em.query(`DELETE FROM "users" CASCADE;`);
  24. });
  25. },
  26. disconnect: async () => {
  27. if (dataSource.isInitialized) {
  28. await dataSource.destroy();
  29. }
  30. },
  31. };
  32. let _handler: JobItemHandler = () => Promise.resolve();
  33. export async function createTestApp(runJobs = false, log = false): Promise<INestApplication> {
  34. const moduleBuilder = Test.createTestingModule({
  35. imports: [AppModule],
  36. providers: [AppService],
  37. })
  38. .overrideProvider(IJobRepository)
  39. .useValue({
  40. addHandler: (_queueName: QueueName, _concurrency: number, handler: JobItemHandler) => (_handler = handler),
  41. queue: (item: JobItem) => runJobs && _handler(item),
  42. resume: jest.fn(),
  43. empty: jest.fn(),
  44. setConcurrency: jest.fn(),
  45. getQueueStatus: jest.fn(),
  46. getJobCounts: jest.fn(),
  47. pause: jest.fn(),
  48. } as IJobRepository);
  49. const moduleFixture: TestingModule = await moduleBuilder.compile();
  50. const app = moduleFixture.createNestApplication();
  51. if (log) {
  52. app.useLogger(new Logger());
  53. } else {
  54. app.useLogger(false);
  55. }
  56. await app.init();
  57. const appService = app.get(AppService);
  58. await appService.init();
  59. return app;
  60. }
  61. export const runAllTests: boolean = process.env.IMMICH_RUN_ALL_TESTS === 'true';
  62. export const itif = (condition: boolean) => (condition ? it : it.skip);
  63. const directoryExists = async (dirPath: string) =>
  64. await fs.promises
  65. .access(dirPath)
  66. .then(() => true)
  67. .catch(() => false);
  68. export async function restoreTempFolder(): Promise<void> {
  69. if (await directoryExists(`${IMMICH_TEST_ASSET_TEMP_PATH}`)) {
  70. // Temp directory exists, delete all files inside it
  71. await fs.promises.rm(IMMICH_TEST_ASSET_TEMP_PATH, { recursive: true });
  72. }
  73. // Create temp folder
  74. await fs.promises.mkdir(IMMICH_TEST_ASSET_TEMP_PATH);
  75. }