|
| 1 | +import testingDB from 'api/utils/testing_db'; |
| 2 | +import { Db } from 'mongodb'; |
| 3 | +import migration from '../index'; |
| 4 | +import { fixtures } from './fixtures'; |
| 5 | + |
| 6 | +let db: Db | null; |
| 7 | + |
| 8 | +beforeAll(async () => { |
| 9 | + jest.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 10 | +}); |
| 11 | + |
| 12 | +afterAll(async () => { |
| 13 | + await testingDB.tearDown(); |
| 14 | +}); |
| 15 | + |
| 16 | +describe('migration fix_property_name_mismatches', () => { |
| 17 | + beforeEach(async () => { |
| 18 | + await testingDB.setupFixturesAndContext(fixtures); |
| 19 | + db = testingDB.mongodb; |
| 20 | + }); |
| 21 | + |
| 22 | + it('should have a delta number', () => { |
| 23 | + expect(migration.delta).toBe(183); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should skip migration when newNameGeneration setting is false', async () => { |
| 27 | + await db?.collection('settings').updateOne({}, { $set: { newNameGeneration: false } }); |
| 28 | + |
| 29 | + await migration.up(db!); |
| 30 | + |
| 31 | + const templates = await db?.collection('templates').find({}).toArray(); |
| 32 | + const templateWithMismatch = templates?.find(t => t.name === 'Template with Mismatches'); |
| 33 | + |
| 34 | + expect(templateWithMismatch?.properties?.[0].name).toBe('text'); |
| 35 | + expect(migration.reindex).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should not modify templates that already have correct property names', async () => { |
| 39 | + await migration.up(db!); |
| 40 | + |
| 41 | + const templates = await db?.collection('templates').find({}).toArray(); |
| 42 | + const correctTemplate = templates?.find(t => t.name === 'Template Already Correct'); |
| 43 | + |
| 44 | + expect(correctTemplate?.properties?.[0].name).toBe('text_field'); |
| 45 | + expect(correctTemplate?.properties?.[1].name).toBe('simple_name'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should fix template property names that do not match their labels', async () => { |
| 49 | + await migration.up(db!); |
| 50 | + |
| 51 | + const templates = await db?.collection('templates').find({}).toArray(); |
| 52 | + const fixedTemplate = templates?.find(t => t.name === 'Template with Mismatches'); |
| 53 | + |
| 54 | + expect(fixedTemplate?.properties?.[0].name).toBe('text_field_'); |
| 55 | + expect(fixedTemplate?.properties?.[1].name).toBe('email_address_'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should update entity metadata keys to match new template property names', async () => { |
| 59 | + await migration.up(db!); |
| 60 | + |
| 61 | + const entities = await db?.collection('entities').find({}).toArray(); |
| 62 | + const entityEN = entities?.find(e => e.title === 'Entity 1 EN'); |
| 63 | + const entityES = entities?.find(e => e.title === 'Entity 1 ES'); |
| 64 | + const entityPT = entities?.find(e => e.title === 'Entity 1 PT'); |
| 65 | + |
| 66 | + // Check EN entity |
| 67 | + expect(entityEN?.metadata?.text).toBeUndefined(); |
| 68 | + expect(entityEN?.metadata?.text_field_).toBeDefined(); |
| 69 | + expect(entityEN?.metadata?.text_field_?.[0].value).toBe('some text'); |
| 70 | + |
| 71 | + expect(entityEN?.metadata?.emailaddress).toBeUndefined(); |
| 72 | + expect(entityEN?.metadata?.email_address_).toBeDefined(); |
| 73 | + expect(entityEN?.metadata?.email_address_?.[0].value).toBe('test@example.com'); |
| 74 | + |
| 75 | + // Check ES entity |
| 76 | + expect(entityES?.metadata?.text).toBeUndefined(); |
| 77 | + expect(entityES?.metadata?.text_field_).toBeDefined(); |
| 78 | + expect(entityES?.metadata?.text_field_?.[0].value).toBe('algún texto'); |
| 79 | + |
| 80 | + // Check PT entity |
| 81 | + expect(entityPT?.metadata?.text).toBeUndefined(); |
| 82 | + expect(entityPT?.metadata?.text_field_).toBeDefined(); |
| 83 | + expect(entityPT?.metadata?.text_field_?.[0].value).toBe('algum texto'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle geolocation properties with _geolocation suffix', async () => { |
| 87 | + await migration.up(db!); |
| 88 | + |
| 89 | + const templates = await db?.collection('templates').find({}).toArray(); |
| 90 | + const geoTemplate = templates?.find(t => t.name === 'Template with Geolocation'); |
| 91 | + |
| 92 | + expect(geoTemplate?.properties?.[0].name).toBe('location_geolocation'); |
| 93 | + |
| 94 | + const entities = await db?.collection('entities').find({}).toArray(); |
| 95 | + const geoEntity = entities?.find(e => e.title === 'Entity 3'); |
| 96 | + |
| 97 | + expect(geoEntity?.metadata?.location).toBeUndefined(); |
| 98 | + expect(geoEntity?.metadata?.location_geolocation).toBeDefined(); |
| 99 | + expect(geoEntity?.metadata?.location_geolocation?.[0].value).toEqual({ |
| 100 | + lat: 40.7128, |
| 101 | + lon: -74.006, |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not modify commonProperties array', async () => { |
| 106 | + await migration.up(db!); |
| 107 | + |
| 108 | + const templates = await db?.collection('templates').find({}).toArray(); |
| 109 | + |
| 110 | + templates?.forEach(template => { |
| 111 | + if (template.commonProperties) { |
| 112 | + template.commonProperties.forEach((prop: any) => { |
| 113 | + // Common properties should remain unchanged |
| 114 | + if (prop.label === 'Date added') { |
| 115 | + expect(prop.name).toBe('creationDate'); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should handle templates with multiple properties needing fixes', async () => { |
| 123 | + await migration.up(db!); |
| 124 | + |
| 125 | + const templates = await db?.collection('templates').find({}).toArray(); |
| 126 | + const multiTemplate = templates?.find(t => t.name === 'Template Multiple Mismatches'); |
| 127 | + |
| 128 | + expect(multiTemplate?.properties?.[0].name).toBe('property_one'); |
| 129 | + expect(multiTemplate?.properties?.[1].name).toBe('property_two_'); |
| 130 | + expect(multiTemplate?.properties?.[2].name).toBe('property_three'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should update all entities for a template in a single operation', async () => { |
| 134 | + await migration.up(db!); |
| 135 | + |
| 136 | + const entities = await db?.collection('entities').find({}).toArray(); |
| 137 | + const entity4EN = entities?.find(e => e.title === 'Entity 4 EN'); |
| 138 | + const entity4ES = entities?.find(e => e.title === 'Entity 4 ES'); |
| 139 | + |
| 140 | + // Both entities should have updated property names |
| 141 | + expect(entity4EN?.metadata?.prop1).toBeUndefined(); |
| 142 | + expect(entity4EN?.metadata?.property_one).toBeDefined(); |
| 143 | + expect(entity4EN?.metadata?.property_one?.[0].value).toBe('value one'); |
| 144 | + |
| 145 | + expect(entity4ES?.metadata?.prop1).toBeUndefined(); |
| 146 | + expect(entity4ES?.metadata?.property_one).toBeDefined(); |
| 147 | + expect(entity4ES?.metadata?.property_one?.[0].value).toBe('valor uno'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should handle entities with empty metadata gracefully', async () => { |
| 151 | + await migration.up(db!); |
| 152 | + |
| 153 | + const entities = await db?.collection('entities').find({}).toArray(); |
| 154 | + const entityEmpty = entities?.find(e => e.title === 'Entity 5 Empty'); |
| 155 | + |
| 156 | + expect(entityEmpty?.metadata).toEqual({}); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should handle entities with partial metadata (not all template properties present)', async () => { |
| 160 | + await migration.up(db!); |
| 161 | + |
| 162 | + const entities = await db?.collection('entities').find({}).toArray(); |
| 163 | + const partialEntity = entities?.find(e => e.title === 'Entity 6 Partial'); |
| 164 | + |
| 165 | + // Only prop1 was present, so only it should be renamed |
| 166 | + expect(partialEntity?.metadata?.prop1).toBeUndefined(); |
| 167 | + expect(partialEntity?.metadata?.property_one).toBeDefined(); |
| 168 | + expect(partialEntity?.metadata?.property_one?.[0].value).toBe('only first property'); |
| 169 | + |
| 170 | + // Other properties should not exist |
| 171 | + expect(partialEntity?.metadata?.prop2).toBeUndefined(); |
| 172 | + expect(partialEntity?.metadata?.property_two_).toBeUndefined(); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should set reindex flag to true when changes are made', async () => { |
| 176 | + const reindexAfterMigration = await migration.up(db!); |
| 177 | + expect(reindexAfterMigration).toBe(true); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should set reindex flag to false when no changes are needed', async () => { |
| 181 | + // First, run the migration to fix everything |
| 182 | + await migration.up(db!); |
| 183 | + expect(migration.reindex).toBe(true); |
| 184 | + |
| 185 | + // Then run it again - should return false since nothing needs changing |
| 186 | + await migration.up(db!); |
| 187 | + expect(migration.reindex).toBe(false); |
| 188 | + }); |
| 189 | +}); |
0 commit comments