Skip to content

Commit

Permalink
Tests: avoid deep imports in favor of the standard module entrypoint
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
Page- committed Jan 3, 2025
1 parent cf6351d commit b3135fc
Show file tree
Hide file tree
Showing 52 changed files with 100 additions and 101 deletions.
38 changes: 20 additions & 18 deletions test/03-async-migrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import supertest from 'supertest';
import type { ChildProcess } from 'child_process';
import { assert, expect } from 'chai';
import { setTimeout } from 'timers';
import { dbModule } from '../out/server-glue/module';
import type { Migrator } from '..';
import { dbModule } from '..';
import { testInit, testDeInit, testLocalServer } from './lib/test-init';
import type { MigrationStatus } from '../out/migrator/utils';

const fixturesBasePath = __dirname + '/fixtures/03-async-migrator/';

Expand Down Expand Up @@ -88,7 +88,9 @@ const waitForAllDataMigrated = async function (
return [result, allResults];
};

const getMigrationStatus = async function (): Promise<MigrationStatus[]> {
const getMigrationStatus = async function (): Promise<
Migrator.MigrationStatus[]
> {
const res = await supertest(testLocalServer).get(
'/migrations/migration_status',
);
Expand Down Expand Up @@ -142,7 +144,7 @@ describe('03 Async Migrations', function () {
.that.has.ownProperty('d')
.to.be.an('array');

res.body.d.map(async (migration: MigrationStatus) => {
res.body.d.map(async (migration: Migrator.MigrationStatus) => {
await supertest(testLocalServer)
.patch(`/migrations/migration_status('${migration.migration_key}')`)
.send({
Expand All @@ -153,7 +155,7 @@ describe('03 Async Migrations', function () {
});

it('should run one async migrator', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
while (result.length === 0 || result[0]?.migrated_row_count < 1) {
result = await getMigrationStatus();
Expand All @@ -165,7 +167,7 @@ describe('03 Async Migrations', function () {

it('should complete / catch up data in one async migrator', async function () {
// active wait to check if migrations have catched up
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
await waitForAllDataMigrated('device');
// active wait as updating migration status after migrations have done takes some time.
// dont want to wait for an artificial magic time delay.
Expand All @@ -177,7 +179,7 @@ describe('03 Async Migrations', function () {
});

it('should migrate future data change after first catch up', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];

const startTime = Date.now().valueOf();
// first catch up is precondition from above test case.
Expand Down Expand Up @@ -218,7 +220,7 @@ describe('03 Async Migrations', function () {
pineFirstInstace = await testInit({ configPath, deleteDb: false });

await delay(200); // wait for one to have happened
let result: MigrationStatus[] = await getMigrationStatus();
let result: Migrator.MigrationStatus[] = await getMigrationStatus();
for (const row of result) {
expect(row.run_count).to.be.equal(0);
}
Expand All @@ -239,7 +241,7 @@ describe('03 Async Migrations', function () {
pineFirstInstace = await testInit({ configPath, deleteDb: false });

await delay(200); // wait for one to have happened
const result: MigrationStatus[] = await getMigrationStatus();
const result: Migrator.MigrationStatus[] = await getMigrationStatus();
for (const row of result) {
expect(row.run_count).to.be.greaterThan(0);
}
Expand Down Expand Up @@ -307,7 +309,7 @@ describe('03 Async Migrations', function () {
});

it('should start 2 migrations competitive', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
while (
result.length < 2 ||
Expand All @@ -325,7 +327,7 @@ describe('03 Async Migrations', function () {
});

it('should complete / catch up all data in 2 migrations competitive', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
// active wait to check if migrations have catched up
await waitForAllDataMigrated('device');
await waitForAllDataMigrated('deviceb');
Expand All @@ -347,7 +349,7 @@ describe('03 Async Migrations', function () {
});

it('should migrate future data change after first catch up in 2 migrators', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
while (
result.length < 2 ||
result[0]?.is_backing_off === false ||
Expand Down Expand Up @@ -401,7 +403,7 @@ describe('03 Async Migrations', function () {
// It's meant to be a wait until we can 'surely' assume that the async migrations
// would have run at least one iteration. Still a magic number is undesired as it's error prone
await delay(2000); // wait for some migrations to have happened
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];

result = await getMigrationStatus();
expect(result).to.be.empty;
Expand Down Expand Up @@ -436,7 +438,7 @@ describe('03 Async Migrations', function () {
});

it('should report error in error count', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
const errorMigrationKeys = ['0002', '0003'];
while (rows.length < 2 || !rows[0]?.error_count || !rows[1]?.run_count) {
Expand All @@ -455,7 +457,7 @@ describe('03 Async Migrations', function () {
});

it('should switch to backoff when exceeding error threshold and give error message', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
const errorMigrationKeys = ['0002', '0003'];
while (
Expand All @@ -479,7 +481,7 @@ describe('03 Async Migrations', function () {
});

it('should remain in backoff when exceeding error threshold and give error message', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
const errorMigrationKeys = ['0002', '0003'];
while (
Expand All @@ -504,7 +506,7 @@ describe('03 Async Migrations', function () {
});

it('should recover from error backoff when no migration error occurs and rows get migrated', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
const errorMigrationKeys = ['0002'];
while (rows.length === 0 || rows[0]?.error_count <= 5) {
Expand Down Expand Up @@ -580,7 +582,7 @@ describe('03 Async Migrations', function () {
});

it('should complete / catch up massive data in one async migrator', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];

const res = await supertest(testLocalServer)
.get('/example/device/$count')
Expand Down
2 changes: 1 addition & 1 deletion test/07-permissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import supertest from 'supertest';
import { expect } from 'chai';
const configPath = __dirname + '/fixtures/07-permissions/config.js';
import { testInit, testDeInit, testLocalServer } from './lib/test-init';
import { sbvrUtils, permissions } from '../out/server-glue/module';
import { sbvrUtils, permissions } from '..';
import type UserModel from '../out/sbvr-api/user';

describe('07 permissions tests', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/00-basic/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/01-constrain/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'university';
const modelName = 'university';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-sync-migrator/00-execute-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-sync-migrator/01-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Migrator } from '../../../../out/server-glue/module';
import type { Migrator } from '../../../..';

const migration: Migrator.MigrationFn = async (tx) => {
const staticSql = `\
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-sync-migrator/02-migrations-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Migrator } from '../../../../out/server-glue/module';
import type { Migrator } from '../../../..';

const migration: Migrator.MigrationFn = async (tx) => {
const staticSql = `\
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-sync-migrator/03-exclusive-category.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-sync-migrator/04-new-model-with-init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/00-execute-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/01-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "device"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx) => {
const staticSql = `\
SELECT 1;`;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/02-parallel-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "device"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "deviceb"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/03-finalize-async.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "device"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncSql: `\
UPDATE "deviceb"
SET "note" = "deviceb"."name"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/04-migration-errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "device"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "device-not-exists"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx) => {
const staticSql = `\
UPDATE "device"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/05-massive-data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path';
import type { ConfigLoader } from '../../../../out/server-glue/module';
import type { ConfigLoader } from '../../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../out/migrator/utils';
import type { Migrator } from '../../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "device"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/06-setup-errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../out/migrator/utils';
import type { Migrator } from '../../../..';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx) => {
return (await tx.executeSql(`SELECT 1`)).rowsAffected;
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/08-01-async-lock-taker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/04-translations/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AbstractSqlQuery } from '@balena/abstract-sql-compiler';
import { getAbstractSqlModelFromFile } from '../../../out/bin/utils';
import type { ConfigLoader } from '../../../out/server-glue/module';
import type { ConfigLoader } from '../../..';

const apiRoot = 'university';
const modelName = 'university';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/04-translations/translations/v1/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sbvrUtils } from '../../../../../out/server-glue/module';
import { sbvrUtils } from '../../../../..';

const addHook = (
methods: Array<Parameters<typeof sbvrUtils.addPureHook>[0]>,
Expand Down
Loading

0 comments on commit b3135fc

Please sign in to comment.