5
5
Demographic ,
6
6
DemographicEntry ,
7
7
Framework ,
8
+ FundingProgramme ,
8
9
Nursery ,
9
10
NurseryReport ,
10
11
Organisation ,
@@ -39,10 +40,12 @@ import {
39
40
ApplicationEntity ,
40
41
DemographicEntity ,
41
42
DemographicEntryEntity ,
43
+ FundingProgrammeEntity ,
42
44
NurseryEntity ,
43
45
NurseryReportEntity ,
44
46
OrganisationEntity ,
45
47
ProjectEntity ,
48
+ ProjectPitchEntity ,
46
49
ProjectReportEntity ,
47
50
SiteEntity ,
48
51
SiteReportEntity ,
@@ -53,6 +56,14 @@ import { Model } from "sequelize-typescript";
53
56
import { FrameworkKey } from "@terramatch-microservices/database/constants/framework" ;
54
57
import { FindOptions , Op } from "sequelize" ;
55
58
import { DateTime } from "luxon" ;
59
+ import { DataApiService } from "@terramatch-microservices/data-api" ;
60
+ import { createMock } from "@golevelup/ts-jest" ;
61
+ import {
62
+ COUNTRIES ,
63
+ gadmLevel0Mock ,
64
+ gadmLevel1Mock ,
65
+ STATES
66
+ } from "@terramatch-microservices/database/util/gadm-mock-data" ;
56
67
57
68
const airtableUpdate = jest . fn < Promise < unknown > , [ { fields : object } [ ] , object ] > ( ( ) => Promise . resolve ( ) ) ;
58
69
const airtableSelectFirstPage = jest . fn < Promise < unknown > , never > ( ( ) => Promise . resolve ( [ ] ) ) ;
@@ -64,6 +75,8 @@ const Base = jest.fn(() => ({
64
75
destroy : airtableDestroy
65
76
} ) ) as unknown as Airtable . Base ;
66
77
78
+ const dataApi = createMock < DataApiService > ( { gadmLevel0 : gadmLevel0Mock , gadmLevel1 : gadmLevel1Mock } ) ;
79
+
67
80
const mapEntityColumns = jest . fn ( ( ) => Promise . resolve ( { } ) ) ;
68
81
export class StubEntity extends AirtableEntity < Site > {
69
82
readonly TABLE_NAME = "stubs" ;
@@ -121,18 +134,18 @@ describe("AirtableEntity", () => {
121
134
122
135
it ( "re-raises mapping errors" , async ( ) => {
123
136
mapEntityColumns . mockRejectedValue ( new Error ( "mapping error" ) ) ;
124
- await expect ( new StubEntity ( ) . updateBase ( null , { startPage : 0 } ) ) . rejects . toThrow ( "mapping error" ) ;
137
+ await expect ( new StubEntity ( dataApi ) . updateBase ( null , { startPage : 0 } ) ) . rejects . toThrow ( "mapping error" ) ;
125
138
mapEntityColumns . mockReset ( ) ;
126
139
} ) ;
127
140
128
141
it ( "re-raises airtable errors" , async ( ) => {
129
142
airtableUpdate . mockRejectedValue ( new Error ( "airtable error" ) ) ;
130
- await expect ( new StubEntity ( ) . updateBase ( Base ) ) . rejects . toThrow ( "airtable error" ) ;
143
+ await expect ( new StubEntity ( dataApi ) . updateBase ( Base ) ) . rejects . toThrow ( "airtable error" ) ;
131
144
airtableUpdate . mockReset ( ) ;
132
145
} ) ;
133
146
134
147
it ( "includes the updatedSince timestamp in the query" , async ( ) => {
135
- const entity = new StubEntity ( ) ;
148
+ const entity = new StubEntity ( dataApi ) ;
136
149
const spy = jest . spyOn ( entity as never , "getUpdatePageFindOptions" ) as jest . SpyInstance < FindOptions < Site > > ;
137
150
const updatedSince = new Date ( ) ;
138
151
await entity . updateBase ( Base , { updatedSince } ) ;
@@ -143,7 +156,7 @@ describe("AirtableEntity", () => {
143
156
} ) ;
144
157
145
158
it ( "skips the updatedSince timestamp if the model doesn't support it" , async ( ) => {
146
- const entity = new StubEntity ( ) ;
159
+ const entity = new StubEntity ( dataApi ) ;
147
160
// @ts -expect-error overriding readonly property for test.
148
161
( entity as never ) . SUPPORTS_UPDATED_SINCE = false ;
149
162
const spy = jest . spyOn ( entity as never , "getUpdatePageFindOptions" ) as jest . SpyInstance < FindOptions < Site > > ;
@@ -180,13 +193,13 @@ describe("AirtableEntity", () => {
180
193
181
194
it ( "re-raises search errors" , async ( ) => {
182
195
airtableSelectFirstPage . mockRejectedValue ( new Error ( "select error" ) ) ;
183
- await expect ( new SiteEntity ( ) . deleteStaleRecords ( Base , deletedSince ) ) . rejects . toThrow ( "select error" ) ;
196
+ await expect ( new SiteEntity ( dataApi ) . deleteStaleRecords ( Base , deletedSince ) ) . rejects . toThrow ( "select error" ) ;
184
197
} ) ;
185
198
186
199
it ( "re-raises delete errors" , async ( ) => {
187
200
airtableSelectFirstPage . mockResolvedValue ( [ { id : "fakeid" , fields : { uuid : "fakeuuid" } } ] ) ;
188
201
airtableDestroy . mockRejectedValue ( new Error ( "delete error" ) ) ;
189
- await expect ( new SiteEntity ( ) . deleteStaleRecords ( Base , deletedSince ) ) . rejects . toThrow ( "delete error" ) ;
202
+ await expect ( new SiteEntity ( dataApi ) . deleteStaleRecords ( Base , deletedSince ) ) . rejects . toThrow ( "delete error" ) ;
190
203
airtableDestroy . mockReset ( ) ;
191
204
} ) ;
192
205
@@ -196,7 +209,7 @@ describe("AirtableEntity", () => {
196
209
fields : { uuid }
197
210
} ) ) ;
198
211
airtableSelectFirstPage . mockResolvedValue ( searchResult ) ;
199
- await new SiteEntity ( ) . deleteStaleRecords ( Base , deletedSince ) ;
212
+ await new SiteEntity ( dataApi ) . deleteStaleRecords ( Base , deletedSince ) ;
200
213
expect ( airtableSelect ) . toHaveBeenCalledTimes ( 1 ) ;
201
214
expect ( airtableSelect ) . toHaveBeenCalledWith (
202
215
expect . objectContaining ( {
@@ -254,7 +267,7 @@ describe("AirtableEntity", () => {
254
267
255
268
it ( "sends all records to airtable" , async ( ) => {
256
269
await testAirtableUpdates (
257
- new ApplicationEntity ( ) ,
270
+ new ApplicationEntity ( dataApi ) ,
258
271
applications ,
259
272
( { uuid, organisationUuid, fundingProgrammeUuid } ) => ( {
260
273
fields : {
@@ -327,7 +340,7 @@ describe("AirtableEntity", () => {
327
340
328
341
it ( "sends all records to airtable" , async ( ) => {
329
342
await testAirtableUpdates (
330
- new DemographicEntity ( ) ,
343
+ new DemographicEntity ( dataApi ) ,
331
344
demographics ,
332
345
( { uuid, collection, demographicalType, demographicalId } ) => ( {
333
346
fields : {
@@ -385,7 +398,7 @@ describe("AirtableEntity", () => {
385
398
386
399
it ( "sends all records to airtable" , async ( ) => {
387
400
await testAirtableUpdates (
388
- new DemographicEntryEntity ( ) ,
401
+ new DemographicEntryEntity ( dataApi ) ,
389
402
entries ,
390
403
( { id, type, subtype, name, amount, demographicId } ) => ( {
391
404
fields : {
@@ -401,6 +414,21 @@ describe("AirtableEntity", () => {
401
414
} ) ;
402
415
} ) ;
403
416
417
+ describe ( "FundingProgrammeEntity" , ( ) => {
418
+ let fundingProgrammes : FundingProgramme [ ] ;
419
+
420
+ beforeAll ( async ( ) => {
421
+ await FundingProgramme . truncate ( ) ;
422
+ fundingProgrammes = await FundingProgrammeFactory . createMany ( 2 ) ;
423
+ } ) ;
424
+
425
+ it ( "sends all records to airtable" , async ( ) => {
426
+ await testAirtableUpdates ( new FundingProgrammeEntity ( dataApi ) , fundingProgrammes , ( { uuid, name } ) => ( {
427
+ fields : { uuid, name }
428
+ } ) ) ;
429
+ } ) ;
430
+ } ) ;
431
+
404
432
describe ( "NurseryEntity" , ( ) => {
405
433
let projectUuids : Record < number , string > ;
406
434
let nurseries : Nursery [ ] ;
@@ -422,7 +450,7 @@ describe("AirtableEntity", () => {
422
450
} ) ;
423
451
424
452
it ( "sends all records to airtable" , async ( ) => {
425
- await testAirtableUpdates ( new NurseryEntity ( ) , nurseries , ( { uuid, name, projectId, status } ) => ( {
453
+ await testAirtableUpdates ( new NurseryEntity ( dataApi ) , nurseries , ( { uuid, name, projectId, status } ) => ( {
426
454
fields : {
427
455
uuid,
428
456
name,
@@ -454,7 +482,7 @@ describe("AirtableEntity", () => {
454
482
} ) ;
455
483
456
484
it ( "sends all records to airtable" , async ( ) => {
457
- await testAirtableUpdates ( new NurseryReportEntity ( ) , reports , ( { uuid, nurseryId, status, dueAt } ) => ( {
485
+ await testAirtableUpdates ( new NurseryReportEntity ( dataApi ) , reports , ( { uuid, nurseryId, status, dueAt } ) => ( {
458
486
fields : {
459
487
uuid,
460
488
nurseryUuid : nurseryUuids [ nurseryId ] ,
@@ -479,7 +507,7 @@ describe("AirtableEntity", () => {
479
507
} ) ;
480
508
481
509
it ( "sends all records to airtable" , async ( ) => {
482
- await testAirtableUpdates ( new OrganisationEntity ( ) , organisations , ( { uuid, name, status } ) => ( {
510
+ await testAirtableUpdates ( new OrganisationEntity ( dataApi ) , organisations , ( { uuid, name, status } ) => ( {
483
511
fields : {
484
512
uuid,
485
513
name,
@@ -574,7 +602,7 @@ describe("AirtableEntity", () => {
574
602
575
603
it ( "sends all records to airtable" , async ( ) => {
576
604
await testAirtableUpdates (
577
- new ProjectEntity ( ) ,
605
+ new ProjectEntity ( dataApi ) ,
578
606
projects ,
579
607
( { uuid, name, frameworkKey, organisationId, applicationId } ) => ( {
580
608
fields : {
@@ -590,6 +618,27 @@ describe("AirtableEntity", () => {
590
618
} ) ;
591
619
} ) ;
592
620
621
+ describe ( "ProjectPitchEntity" , ( ) => {
622
+ let pitches : ProjectPitch [ ] ;
623
+
624
+ beforeAll ( async ( ) => {
625
+ await ProjectPitch . truncate ( ) ;
626
+ pitches = await ProjectPitchFactory . createMany ( 3 ) ;
627
+ } ) ;
628
+
629
+ it ( "sends all records to airtable" , async ( ) => {
630
+ await testAirtableUpdates ( new ProjectPitchEntity ( dataApi ) , pitches , ( { uuid, projectCountry, states } ) => ( {
631
+ fields : {
632
+ uuid,
633
+ projectCountry,
634
+ projectCountryName : COUNTRIES [ projectCountry ] ,
635
+ states,
636
+ stateNames : states . map ( state => STATES [ state . split ( "." ) [ 0 ] ] [ state ] )
637
+ }
638
+ } ) ) ;
639
+ } ) ;
640
+ } ) ;
641
+
593
642
describe ( "ProjectReportEntity" , ( ) => {
594
643
let projectUuids : Record < number , string > ;
595
644
let reports : ProjectReport [ ] ;
@@ -654,7 +703,7 @@ describe("AirtableEntity", () => {
654
703
} ) ;
655
704
656
705
it ( "sends all records to airtable" , async ( ) => {
657
- await testAirtableUpdates ( new ProjectReportEntity ( ) , reports , ( { uuid, projectId, status, dueAt } ) => ( {
706
+ await testAirtableUpdates ( new ProjectReportEntity ( dataApi ) , reports , ( { uuid, projectId, status, dueAt } ) => ( {
658
707
fields : {
659
708
uuid,
660
709
projectUuid : projectUuids [ projectId ] ,
@@ -684,7 +733,7 @@ describe("AirtableEntity", () => {
684
733
} ) ;
685
734
686
735
it ( "sends all records to airtable" , async ( ) => {
687
- await testAirtableUpdates ( new SiteEntity ( ) , sites , ( { uuid, name, projectId, status } ) => ( {
736
+ await testAirtableUpdates ( new SiteEntity ( dataApi ) , sites , ( { uuid, name, projectId, status } ) => ( {
688
737
fields : {
689
738
uuid,
690
739
name,
@@ -720,7 +769,7 @@ describe("AirtableEntity", () => {
720
769
} ) ;
721
770
722
771
it ( "sends all records to airtable" , async ( ) => {
723
- await testAirtableUpdates ( new SiteReportEntity ( ) , reports , ( { id, uuid, siteId, status, dueAt } ) => ( {
772
+ await testAirtableUpdates ( new SiteReportEntity ( dataApi ) , reports , ( { id, uuid, siteId, status, dueAt } ) => ( {
724
773
fields : {
725
774
uuid,
726
775
siteUuid : siteUuids [ siteId ] ,
@@ -796,7 +845,7 @@ describe("AirtableEntity", () => {
796
845
797
846
it ( "sends all records to airtable" , async ( ) => {
798
847
await testAirtableUpdates (
799
- new TreeSpeciesEntity ( ) ,
848
+ new TreeSpeciesEntity ( dataApi ) ,
800
849
trees ,
801
850
( { uuid, name, amount, collection, speciesableType, speciesableId } ) => ( {
802
851
fields : {
@@ -826,7 +875,7 @@ describe("AirtableEntity", () => {
826
875
super . getDeletePageFindOptions ( deletedSince , page ) ;
827
876
}
828
877
const deletedSince = new Date ( ) ;
829
- const result = new Test ( ) . getDeletePageFindOptions ( deletedSince , 0 ) ;
878
+ const result = new Test ( dataApi ) . getDeletePageFindOptions ( deletedSince , 0 ) ;
830
879
expect ( result . where [ Op . or ] ) . not . toBeNull ( ) ;
831
880
expect ( result . where [ Op . or ] ?. [ Op . and ] ?. updatedAt ?. [ Op . gte ] ) . toBe ( deletedSince ) ;
832
881
} ) ;
0 commit comments