@@ -146,6 +146,22 @@ describe("Project data export and import", () => {
146146 }
147147 }
148148
149+ const createProjectDataArchive = async ( {
150+ options = { } ,
151+ } : { options ?: ImportProjectRequest } = { } ) => {
152+ const source = await createDataProject ( )
153+ const archive = await config . api . project . export ( source . project . _id , {
154+ includeRows : true ,
155+ ...options ,
156+ } )
157+ return { source, archive }
158+ }
159+
160+ const getRowUsage = async ( workspaceId : string ) =>
161+ await config . doInContext ( workspaceId , ( ) =>
162+ quotas . getCurrentUsageValues ( QuotaUsageType . STATIC , StaticQuotaName . ROWS )
163+ )
164+
149165 const readImportedData = async ( imported : ImportProjectResponse ) => {
150166 const tables = ( await config . api . table . fetch ( ) ) . filter ( table =>
151167 imported . resources . table ?. includes ( table . _id ! )
@@ -201,11 +217,7 @@ describe("Project data export and import", () => {
201217 "imports related rows and attachment content from an $format package" ,
202218 async ( { options } ) => {
203219 await withProjectsEnabled ( async ( ) => {
204- const source = await createDataProject ( )
205- const archive = await config . api . project . export ( source . project . _id , {
206- includeRows : true ,
207- ...options ,
208- } )
220+ const { source, archive } = await createProjectDataArchive ( { options } )
209221 const destination = await config . api . workspace . create ( {
210222 name : "Destination" ,
211223 } )
@@ -259,11 +271,7 @@ describe("Project data export and import", () => {
259271 "re-imports an $format package independently, preserving existing data and counting new rows without triggering automations" ,
260272 async ( { options } ) => {
261273 await withProjectsEnabled ( async ( ) => {
262- const source = await createDataProject ( )
263- const archive = await config . api . project . export ( source . project . _id , {
264- includeRows : true ,
265- ...options ,
266- } )
274+ const { source, archive } = await createProjectDataArchive ( { options } )
267275 const destination = await config . api . workspace . create ( {
268276 name : "Destination" ,
269277 } )
@@ -276,14 +284,7 @@ describe("Project data export and import", () => {
276284 const existingRow = await config . api . row . save ( existing . _id ! , {
277285 name : "Keep me" ,
278286 } )
279- const usageBefore = await config . doInContext (
280- destination . appId ,
281- ( ) =>
282- quotas . getCurrentUsageValues (
283- QuotaUsageType . STATIC ,
284- StaticQuotaName . ROWS
285- )
286- )
287+ const usageBefore = await getRowUsage ( destination . appId )
287288 const emitRow = jest . spyOn ( BudibaseEmitter . prototype , "emitRow" )
288289 try {
289290 const firstImport = await importProjectData ( { archive, options } )
@@ -298,14 +299,7 @@ describe("Project data export and import", () => {
298299 firstImport . rows [ 0 ] . attachment
299300 const secondAttachment : RowAttachment =
300301 secondImport . rows [ 0 ] . attachment
301- const usageAfter = await config . doInContext (
302- destination . appId ,
303- ( ) =>
304- quotas . getCurrentUsageValues (
305- QuotaUsageType . STATIC ,
306- StaticQuotaName . ROWS
307- )
308- )
302+ const usageAfter = await getRowUsage ( destination . appId )
309303
310304 expect (
311305 new Set ( [
@@ -376,10 +370,7 @@ describe("Project data export and import", () => {
376370
377371 it ( "allocates the next Auto ID above imported values" , async ( ) => {
378372 await withProjectsEnabled ( async ( ) => {
379- const source = await createDataProject ( )
380- const archive = await config . api . project . export ( source . project . _id , {
381- includeRows : true ,
382- } )
373+ const { archive } = await createProjectDataArchive ( )
383374 const imported = await config . api . project . import ( archive )
384375 const data = await readImportedData ( imported )
385376 const next = await config . api . row . save ( data . tasks . _id ! , {
@@ -464,10 +455,7 @@ describe("Project data export and import", () => {
464455
465456 it ( "rolls back partial row writes and uploaded attachments without changing existing data" , async ( ) => {
466457 await withProjectsEnabled ( async ( ) => {
467- const source = await createDataProject ( )
468- const archive = await config . api . project . export ( source . project . _id , {
469- includeRows : true ,
470- } )
458+ const { archive } = await createProjectDataArchive ( )
471459 const destination = await createDestination ( )
472460 const before = await snapshotWorkspace ( destination . appId )
473461 const bulkDocs = failRowImportAfterFirstWrite ( {
@@ -498,10 +486,7 @@ describe("Project data export and import", () => {
498486
499487 it ( "rejects imports exceeding the total row quota without changing the destination" , async ( ) => {
500488 await withProjectsEnabled ( async ( ) => {
501- const source = await createDataProject ( )
502- const archive = await config . api . project . export ( source . project . _id , {
503- includeRows : true ,
504- } )
489+ const { archive } = await createProjectDataArchive ( )
505490 const destination = await createDestination ( )
506491 const before = await snapshotWorkspace ( destination . appId )
507492 const licence = cloneDeep ( mocks . licenses . useUnlimited ( ) )
0 commit comments