-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve testing framework, add tests for eudr admin-regions
- Loading branch information
Showing
7 changed files
with
196 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 26 additions & 58 deletions
84
api/test/e2e/admin-regions/admin-regions-eudr-smart-filters.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const randomName = (length = 10): string => { | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | ||
let result = ''; | ||
for (let i = 0; i < length; i++) { | ||
result += characters.charAt(Math.floor(Math.random() * characters.length)); | ||
} | ||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { DataSource } from 'typeorm'; | ||
import ApplicationManager, { TestApplication } from './application-manager'; | ||
import { clearTestDataFromDatabase } from './database-test-helper'; | ||
import { setupTestUser } from './userAuth'; | ||
import * as request from 'supertest'; | ||
import { | ||
createGeoRegion, | ||
createMaterial, | ||
createSupplier, | ||
} from '../entity-mocks'; | ||
import { Material } from 'modules/materials/material.entity'; | ||
import { Supplier } from 'modules/suppliers/supplier.entity'; | ||
import { GeoRegion } from 'modules/geo-regions/geo-region.entity'; | ||
import { randomName } from './random-name'; | ||
|
||
export class TestManager { | ||
testApp: TestApplication; | ||
jwtToken: string; | ||
dataSource: DataSource; | ||
response?: request.Response; | ||
materials?: Material[]; | ||
suppliers?: Supplier[]; | ||
geoRegions?: GeoRegion[]; | ||
|
||
constructor(app: TestApplication, jwtToken: string, dataSource: DataSource) { | ||
this.testApp = app; | ||
this.jwtToken = jwtToken; | ||
this.dataSource = dataSource; | ||
} | ||
|
||
static async createManager() { | ||
const testApplication = await ApplicationManager.init(); | ||
const dataSource = testApplication.get<DataSource>(DataSource); | ||
const { jwtToken } = await setupTestUser(testApplication); | ||
return new TestManager(testApplication, jwtToken, dataSource); | ||
} | ||
|
||
async refreshState() { | ||
const { jwtToken } = await setupTestUser(this.testApp); | ||
this.jwtToken = jwtToken; | ||
this.materials = undefined; | ||
this.suppliers = undefined; | ||
this.geoRegions = undefined; | ||
this.response = undefined; | ||
} | ||
|
||
async clearDatabase() { | ||
await clearTestDataFromDatabase(this.dataSource); | ||
} | ||
|
||
async GET(options: { url: string; query?: object | string }) { | ||
this.response = await request(this.testApp.getHttpServer()) | ||
.get(options.url) | ||
.query(options?.query || '') | ||
.set('Authorization', `Bearer ${this.token}`); | ||
return this.response; | ||
} | ||
|
||
get token() { | ||
if (!this.jwtToken) { | ||
throw new Error('TestManager has no token available!'); | ||
} | ||
return this.jwtToken; | ||
} | ||
|
||
async close() { | ||
await this.testApp.close(); | ||
} | ||
|
||
async createMaterials(names?: string[]) { | ||
const namesToCreate = names || [randomName()]; | ||
const createdMaterials: Material[] = []; | ||
for (let i = 0; i < namesToCreate.length; i++) { | ||
createdMaterials.push(await createMaterial({ name: namesToCreate[i] })); | ||
} | ||
this.materials?.push(...createdMaterials); | ||
return createdMaterials; | ||
} | ||
|
||
async createSuppliers(names?: string[]) { | ||
const namesToCreate = names || [randomName()]; | ||
const createdSuppliers: Supplier[] = []; | ||
for (let i = 0; i < namesToCreate.length; i++) { | ||
createdSuppliers.push(await createSupplier({ name: namesToCreate[i] })); | ||
} | ||
this.suppliers?.push(...createdSuppliers); | ||
return createdSuppliers; | ||
} | ||
|
||
async createGeoRegion(names?: string[]) { | ||
const namesToCreate = names || [randomName()]; | ||
const createdGeoRegions: GeoRegion[] = []; | ||
for (let i = 0; i < namesToCreate.length; i++) { | ||
createdGeoRegions.push(await createGeoRegion({ name: namesToCreate[i] })); | ||
} | ||
this.geoRegions?.push(...createdGeoRegions); | ||
return createdGeoRegions; | ||
} | ||
} |
Oops, something went wrong.