parse-rest-dataset-import.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import assert from 'node:assert/strict';
  2. import test from 'node:test';
  3. import type { ParseRestClient } from '../src/db/parse-rest.client.js';
  4. import { ParseRestDatasetImportService } from '../src/modules/domestic-voc/services/parse-rest-dataset-import.service.js';
  5. test('Parse REST dataset batches update existing natural keys and create only missing rows', async () => {
  6. const batches: Array<Array<{ method: string; path: string; body?: unknown }>> = [];
  7. const client = {
  8. findAll: async () => [{
  9. objectId: 'existing-object',
  10. naturalKey: 'existing-key',
  11. createdAt: '2026-01-01T00:00:00.000Z',
  12. updatedAt: '2026-01-01T00:00:00.000Z',
  13. }],
  14. batch: async (requests: Array<{ method: string; path: string; body?: unknown }>) => {
  15. batches.push(requests);
  16. },
  17. } as unknown as ParseRestClient;
  18. const service = new ParseRestDatasetImportService(client);
  19. const upsertMany = (service as unknown as {
  20. upsertMany(className: string, objects: Array<Record<string, unknown>>): Promise<void>;
  21. }).upsertMany.bind(service);
  22. await upsertMany('VocProduct', [
  23. { naturalKey: 'existing-key', title: 'updated' },
  24. { naturalKey: 'new-key', title: 'created' },
  25. ]);
  26. assert.equal(batches.length, 1);
  27. assert.deepEqual(batches[0]!.map((request) => ({ method: request.method, path: request.path })), [
  28. { method: 'PUT', path: '/classes/VocProduct/existing-object' },
  29. { method: 'POST', path: '/classes/VocProduct' },
  30. ]);
  31. });
  32. test('Parse REST dataset batches reject rows without a natural key', async () => {
  33. const client = {
  34. findAll: async () => [],
  35. batch: async () => undefined,
  36. } as unknown as ParseRestClient;
  37. const service = new ParseRestDatasetImportService(client);
  38. const upsertMany = (service as unknown as {
  39. upsertMany(className: string, objects: Array<Record<string, unknown>>): Promise<void>;
  40. }).upsertMany.bind(service);
  41. await assert.rejects(
  42. () => upsertMany('VocProduct', [{ title: 'missing identity' }]),
  43. /missing naturalKey/,
  44. );
  45. });