|
| 1 | +// Integration test for SearchFeatureRepository.deleteSearchRecordsBySubmissionId — |
| 2 | +// verifies the subquery-based delete correctly removes records from all 4 search tables |
| 3 | +// for a given submission, without affecting records belonging to other submissions. |
| 4 | +// |
| 5 | +// Uses a transaction that is ROLLED BACK after each test, so no data is persisted. |
| 6 | +// |
| 7 | +// Run: make test-db |
| 8 | +// Requires: make web (database must be running with seed data) |
| 9 | + |
| 10 | +import { expect } from 'chai'; |
| 11 | +import SQL from 'sql-template-strings'; |
| 12 | +import { defaultPoolConfig, getAPIUserDBConnection, IDBConnection, initDBPool } from '../../database/db'; |
| 13 | +import { SearchFeatureRepository } from '../../repositories/search-feature-repository'; |
| 14 | + |
| 15 | +describe('SearchFeatureRepository (integration)', function () { |
| 16 | + this.timeout(15000); |
| 17 | + |
| 18 | + let connection: IDBConnection; |
| 19 | + let repo: SearchFeatureRepository; |
| 20 | + |
| 21 | + before(() => { |
| 22 | + initDBPool(defaultPoolConfig); |
| 23 | + }); |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + connection = getAPIUserDBConnection(); |
| 27 | + await connection.open(); |
| 28 | + repo = new SearchFeatureRepository(connection); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(async () => { |
| 32 | + await connection.rollback(); |
| 33 | + connection.release(); |
| 34 | + }); |
| 35 | + |
| 36 | + /** |
| 37 | + * Helper: insert a minimal submission and return its ID. |
| 38 | + */ |
| 39 | + async function createTestSubmission(): Promise<number> { |
| 40 | + const systemUserId = connection.systemUserId(); |
| 41 | + |
| 42 | + const result = await connection.sql(SQL` |
| 43 | + INSERT INTO submission (uuid, system_user_id, source_system, name, description, comment, create_user) |
| 44 | + VALUES (gen_random_uuid(), ${systemUserId}, 'SIMS', 'Search Index Test', 'Test', 'Test', ${systemUserId}) |
| 45 | + RETURNING submission_id; |
| 46 | + `); |
| 47 | + |
| 48 | + return result.rows[0].submission_id; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Helper: insert a submission_feature and return its ID. |
| 53 | + */ |
| 54 | + async function createTestFeature(submissionId: number): Promise<number> { |
| 55 | + const systemUserId = connection.systemUserId(); |
| 56 | + |
| 57 | + const result = await connection.sql(SQL` |
| 58 | + INSERT INTO submission_feature (submission_id, feature_type_id, data, data_byte_size, create_user) |
| 59 | + VALUES ( |
| 60 | + ${submissionId}, |
| 61 | + (SELECT feature_type_id FROM feature_type WHERE name = 'dataset' LIMIT 1), |
| 62 | + '{"name": "test"}'::jsonb, |
| 63 | + 100, |
| 64 | + ${systemUserId} |
| 65 | + ) |
| 66 | + RETURNING submission_feature_id; |
| 67 | + `); |
| 68 | + |
| 69 | + return result.rows[0].submission_feature_id; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Helper: insert a search_string record. |
| 74 | + */ |
| 75 | + async function insertSearchString(submissionFeatureId: number, featurePropertyId: number, value: string) { |
| 76 | + const systemUserId = connection.systemUserId(); |
| 77 | + |
| 78 | + await connection.sql(SQL` |
| 79 | + INSERT INTO search_string (submission_feature_id, feature_property_id, value, create_user) |
| 80 | + VALUES (${submissionFeatureId}, ${featurePropertyId}, ${value}, ${systemUserId}); |
| 81 | + `); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Helper: insert a search_number record. |
| 86 | + */ |
| 87 | + async function insertSearchNumber(submissionFeatureId: number, featurePropertyId: number, value: number) { |
| 88 | + const systemUserId = connection.systemUserId(); |
| 89 | + |
| 90 | + await connection.sql(SQL` |
| 91 | + INSERT INTO search_number (submission_feature_id, feature_property_id, value, create_user) |
| 92 | + VALUES (${submissionFeatureId}, ${featurePropertyId}, ${value}, ${systemUserId}); |
| 93 | + `); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Helper: count records in a search table for a given submission_feature_id. |
| 98 | + */ |
| 99 | + async function countSearchRecords(table: string, submissionFeatureId: number): Promise<number> { |
| 100 | + const result = await connection.sql( |
| 101 | + SQL` |
| 102 | + SELECT count(*)::integer as count |
| 103 | + FROM `.append(table).append(SQL` |
| 104 | + WHERE submission_feature_id = ${submissionFeatureId}; |
| 105 | + `) |
| 106 | + ); |
| 107 | + |
| 108 | + return result.rows[0].count; |
| 109 | + } |
| 110 | + |
| 111 | + describe('deleteSearchRecordsBySubmissionId', () => { |
| 112 | + it('deletes search_string and search_number records for the given submission', async () => { |
| 113 | + // Arrange: create submission with a feature and search records |
| 114 | + const submissionId = await createTestSubmission(); |
| 115 | + const featureId = await createTestFeature(submissionId); |
| 116 | + |
| 117 | + // feature_property_id 1 = site_identifier (string type), 6 = measurement_value (number type) |
| 118 | + await insertSearchString(featureId, 1, 'test-site'); |
| 119 | + await insertSearchNumber(featureId, 6, 42); |
| 120 | + |
| 121 | + // Verify records exist |
| 122 | + expect(await countSearchRecords('search_string', featureId)).to.equal(1); |
| 123 | + expect(await countSearchRecords('search_number', featureId)).to.equal(1); |
| 124 | + |
| 125 | + // Act |
| 126 | + await repo.deleteSearchRecordsBySubmissionId(submissionId); |
| 127 | + |
| 128 | + // Assert: all records deleted |
| 129 | + expect(await countSearchRecords('search_string', featureId)).to.equal(0); |
| 130 | + expect(await countSearchRecords('search_number', featureId)).to.equal(0); |
| 131 | + }); |
| 132 | + |
| 133 | + it('does not delete records belonging to a different submission', async () => { |
| 134 | + // Arrange: create two submissions with search records |
| 135 | + const submissionId1 = await createTestSubmission(); |
| 136 | + const featureId1 = await createTestFeature(submissionId1); |
| 137 | + await insertSearchString(featureId1, 1, 'submission-1-value'); |
| 138 | + |
| 139 | + const submissionId2 = await createTestSubmission(); |
| 140 | + const featureId2 = await createTestFeature(submissionId2); |
| 141 | + await insertSearchString(featureId2, 1, 'submission-2-value'); |
| 142 | + |
| 143 | + // Act: delete only submission 1's records |
| 144 | + await repo.deleteSearchRecordsBySubmissionId(submissionId1); |
| 145 | + |
| 146 | + // Assert: submission 1 records gone, submission 2 records intact |
| 147 | + expect(await countSearchRecords('search_string', featureId1)).to.equal(0); |
| 148 | + expect(await countSearchRecords('search_string', featureId2)).to.equal(1); |
| 149 | + }); |
| 150 | + |
| 151 | + it('succeeds with no error when submission has no search records', async () => { |
| 152 | + const submissionId = await createTestSubmission(); |
| 153 | + await createTestFeature(submissionId); |
| 154 | + |
| 155 | + // Act: should not throw |
| 156 | + await repo.deleteSearchRecordsBySubmissionId(submissionId); |
| 157 | + }); |
| 158 | + |
| 159 | + it('deletes records across multiple features in the same submission', async () => { |
| 160 | + // Arrange: one submission, two features, each with search records |
| 161 | + const submissionId = await createTestSubmission(); |
| 162 | + const featureId1 = await createTestFeature(submissionId); |
| 163 | + const featureId2 = await createTestFeature(submissionId); |
| 164 | + |
| 165 | + await insertSearchString(featureId1, 1, 'feature-1-value'); |
| 166 | + await insertSearchString(featureId2, 1, 'feature-2-value'); |
| 167 | + await insertSearchNumber(featureId1, 6, 10); |
| 168 | + await insertSearchNumber(featureId2, 6, 20); |
| 169 | + |
| 170 | + // Act |
| 171 | + await repo.deleteSearchRecordsBySubmissionId(submissionId); |
| 172 | + |
| 173 | + // Assert: all records across both features deleted |
| 174 | + expect(await countSearchRecords('search_string', featureId1)).to.equal(0); |
| 175 | + expect(await countSearchRecords('search_string', featureId2)).to.equal(0); |
| 176 | + expect(await countSearchRecords('search_number', featureId1)).to.equal(0); |
| 177 | + expect(await countSearchRecords('search_number', featureId2)).to.equal(0); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments