Skip to content

Commit

Permalink
[TM-1452] Sync database by running one isolated test first.
Browse files Browse the repository at this point in the history
  • Loading branch information
roguenet committed Nov 21, 2024
1 parent 40ede90 commit 4045b2c
Show file tree
Hide file tree
Showing 29 changed files with 134 additions and 133 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ jobs:

- run: docker-compose up -d

# First run just the small database test to get the test database synced to the current schema
# in a clean way. For some reason, the `run-many` is necessary here. If this line simply uses
# nx test database, the connection to the DB gets cut off before the sync is complete.
- run: NX_CLOUD_DISTRIBUTED_EXECUTION=false npx nx run-many -t test --projects database

- run: NX_CLOUD_DISTRIBUTED_EXECUTION=false npx nx run-many -t test --coverage --passWithNoTests
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getJestProjectsAsync } from '@nx/jest';
import { getJestProjectsAsync } from "@nx/jest";

export default async () => ({
projects: await getJestProjectsAsync(),
projects: await getJestProjectsAsync()
});
8 changes: 4 additions & 4 deletions jest.preset.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const nxPreset = require('@nx/jest/preset').default;
const nxPreset = require("@nx/jest/preset").default;

module.exports = {
...nxPreset,
Expand All @@ -8,9 +8,9 @@ module.exports = {
branches: 85,
functions: 95,
lines: 95,
statements: 95,
statements: 95
}
},

setupFilesAfterEnv: ['./setup-jest.ts'],
}
setupFilesAfterEnv: ["./jest/setup-jest.ts"]
};
26 changes: 26 additions & 0 deletions jest/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Sequelize } from "sequelize-typescript";
import { FactoryGirl, SequelizeAdapter } from "factory-girl-ts";
import * as Entities from "@terramatch-microservices/database/entities";

let sequelize: Sequelize;

beforeAll(async () => {
// To create this database, run the ./setup-test-database.sh script.
sequelize = new Sequelize({
dialect: "mariadb",
host: "localhost",
port: 3360,
username: "wri",
password: "wri",
database: "terramatch_microservices_test",
models: Object.values(Entities),
logging: false
});

await sequelize.sync();
FactoryGirl.setAdapter(new SequelizeAdapter());
});

afterAll(async () => {
await sequelize.close();
});
35 changes: 18 additions & 17 deletions libs/common/src/lib/policies/user.policy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PolicyService } from './policy.service';
import { mockPermissions, mockUserId } from './policy.service.spec';
import { User } from '@terramatch-microservices/database/entities';
import { UnauthorizedException } from '@nestjs/common';
import { Test, TestingModule } from "@nestjs/testing";
import { PolicyService } from "./policy.service";
import { mockPermissions, mockUserId } from "./policy.service.spec";
import { User } from "@terramatch-microservices/database/entities";
import { UnauthorizedException } from "@nestjs/common";
import { UserFactory } from "@terramatch-microservices/database/factories";

describe('UserPolicy', () => {
describe("UserPolicy", () => {
let service: PolicyService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PolicyService],
providers: [PolicyService]
}).compile();

service = module.get<PolicyService>(PolicyService);
Expand All @@ -19,21 +20,21 @@ describe('UserPolicy', () => {
jest.restoreAllMocks();
});

it('allows reading any user as admin', async () => {
it("allows reading any user as admin", async () => {
mockUserId(123);
mockPermissions('users-manage');
await expect(service.authorize('read', new User())).resolves.toBeUndefined();
})
mockPermissions("users-manage");
await expect(service.authorize("read", new User())).resolves.toBeUndefined();
});

it('disallows reading other users as non-admin', async () => {
it("disallows reading other users as non-admin", async () => {
mockUserId(123);
mockPermissions();
await expect(service.authorize('read', new User())).rejects.toThrow(UnauthorizedException);
})
await expect(service.authorize("read", new User())).rejects.toThrow(UnauthorizedException);
});

it('allows reading own user as non-admin', async () => {
it("allows reading own user as non-admin", async () => {
mockUserId(123);
mockPermissions();
await expect(service.authorize('read', new User({ id: 123 }))).resolves.toBeUndefined();
})
await expect(service.authorize("read", await UserFactory.build({ id: 123 }))).resolves.toBeUndefined();
});
});
20 changes: 14 additions & 6 deletions libs/database/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/* eslint-disable */
export default {
displayName: 'database',
preset: '../../jest.preset.js',
testEnvironment: 'node',
displayName: "database",
preset: "../../jest.preset.js",
testEnvironment: "node",
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
"^.+\\.[tj]s$": ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }]
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/database',
moduleFileExtensions: ["ts", "js", "html"],
coverageDirectory: "../../coverage/libs/database",
coverageThreshold: {
global: {
branches: 0,
functions: 0,
lines: 0,
statements: 0
}
}
};
9 changes: 9 additions & 0 deletions libs/database/src/lib/database.module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Entities from "./entities";

describe("DatabaseModule", () => {
it("Successfully syncs the database schema", async () => {
for (const Entity of Object.values(Entities)) {
await expect(Entity.sequelize?.getQueryInterface().tableExists(Entity.tableName)).resolves.toBe(true);
}
});
});
19 changes: 5 additions & 14 deletions libs/database/src/lib/entities/delayed-job.entity.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import {
AllowNull,
AutoIncrement,
Column,
Default,
Index,
Model,
PrimaryKey,
Table,
} from 'sequelize-typescript';
import { BIGINT, INTEGER, JSON, STRING, UUID } from 'sequelize';
import { AllowNull, AutoIncrement, Column, Default, Index, Model, PrimaryKey, Table } from "sequelize-typescript";
import { BIGINT, INTEGER, JSON, STRING, UUID } from "sequelize";

@Table({ tableName: 'delayed_jobs', underscored: true })
export class DelayedJob extends Model {
@Table({ tableName: "delayed_jobs", underscored: true })
export class DelayedJob extends Model<DelayedJob> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand All @@ -21,7 +12,7 @@ export class DelayedJob extends Model {
@Column(UUID)
uuid: string;

@Default('pending')
@Default("pending")
@Column(STRING)
status: string;

Expand Down
12 changes: 6 additions & 6 deletions libs/database/src/lib/entities/framework-user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AutoIncrement, Column, ForeignKey, Model, PrimaryKey, Table } from 'sequelize-typescript';
import { BIGINT } from 'sequelize';
import { Framework } from './framework.entity';
import { User } from './user.entity';
import { AutoIncrement, Column, ForeignKey, Model, PrimaryKey, Table } from "sequelize-typescript";
import { BIGINT } from "sequelize";
import { Framework } from "./framework.entity";
import { User } from "./user.entity";

@Table({ tableName: 'framework_user', underscored: true })
export class FrameworkUser extends Model {
@Table({ tableName: "framework_user", underscored: true })
export class FrameworkUser extends Model<FrameworkUser> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
8 changes: 4 additions & 4 deletions libs/database/src/lib/entities/framework.entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AutoIncrement, Column, Model, PrimaryKey, Table } from 'sequelize-typescript';
import { BIGINT, STRING } from 'sequelize';
import { AutoIncrement, Column, Model, PrimaryKey, Table } from "sequelize-typescript";
import { BIGINT, STRING } from "sequelize";

// A quick stub to get the information needed for users/me
@Table({ tableName: 'frameworks', underscored: true })
export class Framework extends Model {
@Table({ tableName: "frameworks", underscored: true })
export class Framework extends Model<Framework> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SitePolygon } from "./site-polygon.entity";
import { INDICATOR_SLUGS, IndicatorSlug } from "../constants";

@Table({ tableName: "indicator_output_field_monitoring", underscored: true, paranoid: true })
export class IndicatorOutputFieldMonitoring extends Model {
export class IndicatorOutputFieldMonitoring extends Model<IndicatorOutputFieldMonitoring> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SitePolygon } from "./site-polygon.entity";
import { INDICATOR_SLUGS, IndicatorSlug } from "../constants";

@Table({ tableName: "indicator_output_hectares", underscored: true, paranoid: true })
export class IndicatorOutputHectares extends Model {
export class IndicatorOutputHectares extends Model<IndicatorOutputHectares> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SitePolygon } from "./site-polygon.entity";
import { INDICATOR_SLUGS, IndicatorSlug } from "../constants";

@Table({ tableName: "indicator_output_msu_carbon", underscored: true, paranoid: true })
export class IndicatorOutputMsuCarbon extends Model {
export class IndicatorOutputMsuCarbon extends Model<IndicatorOutputMsuCarbon> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SitePolygon } from "./site-polygon.entity";
import { INDICATOR_SLUGS, IndicatorSlug } from "../constants";

@Table({ tableName: "indicator_output_tree_count", underscored: true, paranoid: true })
export class IndicatorOutputTreeCount extends Model {
export class IndicatorOutputTreeCount extends Model<IndicatorOutputTreeCount> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SitePolygon } from "./site-polygon.entity";
import { INDICATOR_SLUGS, IndicatorSlug } from "../constants";

@Table({ tableName: "indicator_output_tree_cover_loss", underscored: true, paranoid: true })
export class IndicatorOutputTreeCoverLoss extends Model {
export class IndicatorOutputTreeCoverLoss extends Model<IndicatorOutputTreeCoverLoss> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SitePolygon } from "./site-polygon.entity";
import { INDICATOR_SLUGS, IndicatorSlug } from "../constants";

@Table({ tableName: "indicator_output_tree_cover", underscored: true, paranoid: true })
export class IndicatorOutputTreeCover extends Model {
export class IndicatorOutputTreeCover extends Model<IndicatorOutputTreeCover> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
10 changes: 5 additions & 5 deletions libs/database/src/lib/entities/model-has-role.entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Column, ForeignKey, Model, Table } from 'sequelize-typescript';
import { Role } from './role.entity';
import { BIGINT, STRING } from 'sequelize';
import { Column, ForeignKey, Model, Table } from "sequelize-typescript";
import { Role } from "./role.entity";
import { BIGINT, STRING } from "sequelize";

@Table({ tableName: 'model_has_roles', underscored: true, timestamps: false })
export class ModelHasRole extends Model {
@Table({ tableName: "model_has_roles", underscored: true, timestamps: false })
export class ModelHasRole extends Model<ModelHasRole> {
@ForeignKey(() => Role)
@Column({ type: BIGINT.UNSIGNED, primaryKey: true })
roleId: number;
Expand Down
12 changes: 6 additions & 6 deletions libs/database/src/lib/entities/organisation-user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AutoIncrement, Column, ForeignKey, Model, PrimaryKey, Table } from 'sequelize-typescript';
import { BIGINT, STRING } from 'sequelize';
import { Organisation } from './organisation.entity';
import { User } from './user.entity';
import { AutoIncrement, Column, ForeignKey, Model, PrimaryKey, Table } from "sequelize-typescript";
import { BIGINT, STRING } from "sequelize";
import { Organisation } from "./organisation.entity";
import { User } from "./user.entity";

@Table({ tableName: 'organisation_user', underscored: true, timestamps: false })
export class OrganisationUser extends Model {
@Table({ tableName: "organisation_user", underscored: true, timestamps: false })
export class OrganisationUser extends Model<OrganisationUser> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
2 changes: 1 addition & 1 deletion libs/database/src/lib/entities/organisation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AllowNull, AutoIncrement, Column, Default, Index, Model, PrimaryKey, Ta
import { BIGINT, BOOLEAN, DATE, DECIMAL, ENUM, INTEGER, STRING, TEXT, TINYINT, UUID } from "sequelize";

@Table({ tableName: "organisations", underscored: true })
export class Organisation extends Model {
export class Organisation extends Model<Organisation> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
12 changes: 6 additions & 6 deletions libs/database/src/lib/entities/permission.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AutoIncrement, Column, Model, PrimaryKey, Table } from 'sequelize-typescript';
import { BIGINT, QueryTypes, STRING } from 'sequelize';
import { AutoIncrement, Column, Model, PrimaryKey, Table } from "sequelize-typescript";
import { BIGINT, QueryTypes, STRING } from "sequelize";

@Table({ tableName: 'permissions', underscored: true })
export class Permission extends Model {
@Table({ tableName: "permissions", underscored: true })
export class Permission extends Model<Permission> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down Expand Up @@ -35,8 +35,8 @@ export class Permission extends Model {
model_has_roles.model_id = :modelId
`,
{
replacements: { modelType: 'App\\Models\\V2\\User', modelId: userId },
type: QueryTypes.SELECT,
replacements: { modelType: "App\\Models\\V2\\User", modelId: userId },
type: QueryTypes.SELECT
}
)) as { name: string }[];

Expand Down
2 changes: 1 addition & 1 deletion libs/database/src/lib/entities/point-geometry.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Point } from "geojson";
import { User } from "./user.entity";

@Table({ tableName: "point_geometry", underscored: true, paranoid: true })
export class PointGeometry extends Model {
export class PointGeometry extends Model<PointGeometry> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
2 changes: 1 addition & 1 deletion libs/database/src/lib/entities/polygon-geometry.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Polygon } from "geojson";
import { User } from "./user.entity";

@Table({ tableName: "polygon_geometry", underscored: true, paranoid: true })
export class PolygonGeometry extends Model {
export class PolygonGeometry extends Model<PolygonGeometry> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
20 changes: 6 additions & 14 deletions libs/database/src/lib/entities/project-user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import {
AllowNull,
AutoIncrement,
Column, Default,
ForeignKey,
Model,
PrimaryKey,
Table
} from 'sequelize-typescript';
import { Project } from './project.entity';
import { User } from './user.entity';
import { BIGINT, BOOLEAN, STRING } from 'sequelize';
import { AllowNull, AutoIncrement, Column, Default, ForeignKey, Model, PrimaryKey, Table } from "sequelize-typescript";
import { Project } from "./project.entity";
import { User } from "./user.entity";
import { BIGINT, BOOLEAN, STRING } from "sequelize";

@Table({ tableName: 'v2_project_users', underscored: true })
export class ProjectUser extends Model {
@Table({ tableName: "v2_project_users", underscored: true })
export class ProjectUser extends Model<ProjectUser> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
2 changes: 1 addition & 1 deletion libs/database/src/lib/entities/project.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BIGINT, BOOLEAN, STRING, UUID } from "sequelize";

// A quick stub to get the information needed for users/me
@Table({ tableName: "v2_projects", underscored: true, paranoid: true })
export class Project extends Model {
export class Project extends Model<Project> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
Expand Down
Loading

0 comments on commit 4045b2c

Please sign in to comment.