Skip to content

Commit

Permalink
/libs/model test type fixes (#7544)
Browse files Browse the repository at this point in the history
* update tsconfig configuration in `/model`

* update tsconfig configuration in `/core`

* update tsconfig configuration in `/chains`

* update tsconfig configuration in `/adapters`

* update tsconfig configuration in `shared`

* update tsconfig configuration in `logging`

* `packages/commonwealth` tsconfig update + copy `references` to build tsconfig (microsoft/TypeScript#27098)

* specify tsconfig.build.json in references

* discobot and snapshot tsconfig.build.json updates

* remove test from includes (follow-up PR)

* merge resolution

* merge resolution

* remove "test" from include in snapshot

* model test type fixes

* leaner tsconfig

---------

Co-authored-by: rotorsoft <rotorsoft@outlook.com>
  • Loading branch information
timolegros and rotorsoft authored Apr 25, 2024
1 parent 6b4d376 commit 4478748
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 83 deletions.
12 changes: 6 additions & 6 deletions libs/core/src/schemas/entities.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,19 +560,19 @@ export const SubscriptionPreference = z.object({
});

export const ThreadSubscription = z.object({
id: PG_INT,
id: PG_INT.optional(),
user_id: PG_INT,
thread_id: PG_INT,
created_at: z.date(),
updated_at: z.date(),
created_at: z.date().optional(),
updated_at: z.date().optional(),
});

export const CommentSubscription = z.object({
id: PG_INT,
id: PG_INT.optional(),
user_id: PG_INT,
comment_id: PG_INT,
created_at: z.date(),
updated_at: z.date(),
created_at: z.date().optional(),
updated_at: z.date().optional(),
});

export const CommunityAlert = z.object({
Expand Down
2 changes: 1 addition & 1 deletion libs/model/test/community/group-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ chai.use(chaiAsPromised);
const chance = Chance();

describe('Group lifecycle', () => {
let id;
let id: string;
let actor: Actor;

const payload = {
Expand Down
29 changes: 21 additions & 8 deletions libs/model/test/community/stake-historical-price.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dispose, query } from '@hicommonwealth/core';
import { Actor, dispose, query } from '@hicommonwealth/core';
import { BalanceType } from '@hicommonwealth/shared';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
Expand All @@ -8,7 +8,8 @@ import { seed } from '../../src/tester/index';
chai.use(chaiAsPromised);

describe('Stake Historical Price', () => {
let community_id;
let community_id: string;
let actor: Actor;

before(async () => {
const [node] = await seed('ChainNode', {
Expand Down Expand Up @@ -36,7 +37,6 @@ describe('Stake Historical Price', () => {
{
stake_id: 2,
stake_token: '',
stake_weight: 1,
vote_weight: 1,
stake_enabled: true,
},
Expand All @@ -47,14 +47,19 @@ describe('Stake Historical Price', () => {
discord_config_id: null,
});

actor = {
user: { id: user!.id!, email: user!.email! },
address_id: community!.Addresses![0].address,
};

community_id = community!.id!;

await seed('StakeTransaction', {
transaction_hash: '1',
stake_id: 2,
community_id,
timestamp: Math.floor(Date.now() / 1000),
stake_price: 88,
stake_price: '88',
stake_direction: 'buy',
stake_amount: 1,
});
Expand All @@ -64,7 +69,7 @@ describe('Stake Historical Price', () => {
stake_id: 2,
community_id,
timestamp: 1000,
stake_price: 10,
stake_price: '10',
stake_direction: 'buy',
stake_amount: 1,
});
Expand All @@ -73,7 +78,7 @@ describe('Stake Historical Price', () => {
stake_id: 2,
community_id,
timestamp: 1,
stake_price: 99,
stake_price: '99',
stake_direction: 'buy',
stake_amount: 1,
});
Expand All @@ -85,18 +90,26 @@ describe('Stake Historical Price', () => {

it('should return undefined if no historical price', async () => {
const results = await query(GetStakeHistoricalPrice(), {
payload: { past_date_epoch: 1, community_id: 'non-existing' },
actor,
payload: {
past_date_epoch: 1,
community_id: 'non-existing',
stake_id: 2,
},
});
expect(results).to.deep.equal([]);
});

it('should return the historical price', async () => {
const results = await query(GetStakeHistoricalPrice(), {
actor,
payload: {
past_date_epoch: Math.floor(Date.now() / 1000) - 24 * 60 * 60, // date 24 horus ago
community_id,
stake_id: 2,
},
});
expect(results[0].old_price).to.equal('88');
expect(results).to.exist;
expect(results![0]!.old_price).to.equal('88');
});
});
21 changes: 8 additions & 13 deletions libs/model/test/community/stake-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { seed } from '../../src/tester';
chai.use(chaiAsPromised);

describe('Stake lifecycle', () => {
let id_with_stake;
let id_without_stake_to_set;
let id_without_stake;
let id_with_stake: string;
let id_without_stake_to_set: string;
let id_without_stake: string;
let actor: Actor;

const payload = {
Expand All @@ -30,16 +30,11 @@ describe('Stake lifecycle', () => {
};

before(async () => {
const [node] = await seed(
'ChainNode',
{ contracts: [] },
// { mock: true, log: true },
);
const [user] = await seed(
'User',
{ isAdmin: true, selected_community_id: null },
// { mock: true, log: true },
);
const [node] = await seed('ChainNode', { contracts: [] });
const [user] = await seed('User', {
isAdmin: true,
selected_community_id: null,
});
const [community_with_stake] = await seed(
'Community',
{
Expand Down
4 changes: 2 additions & 2 deletions libs/model/test/community/stake-transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ chai.use(chaiAsPromised);
describe('Stake transactions', () => {
const actor: Actor = { user: { email: '' } };
let payload;
let community_id;
let community_id: string;

before(async () => {
const [node] = await seed('ChainNode', {
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('Stake transactions', () => {
payload,
});
} catch (e) {
expect(e.message).to.equal(
expect((e as Error).message).to.equal(
'Transaction is not associated with provided community',
);
return;
Expand Down
2 changes: 1 addition & 1 deletion libs/model/test/seed/model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const generateSchemas = async () => {
};

describe('Model schema', () => {
let schemas;
let schemas: { [x: string]: { model: any; migration: any } };

before(async () => {
schemas = await generateSchemas();
Expand Down
5 changes: 3 additions & 2 deletions libs/model/test/seed/seed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ async function testSeed<T extends schemas.Aggregates>(
// perform schema validation on created entity (throws)
const schema = schemas.entities[name];
const model = models[name];
const data = await schema.parse(record);
const data: ReturnType<typeof schema.parse> = schema.parse(record);

// attempt to find entity that was created
const existingEntity = await (model as ModelStatic<Model>).findOne({
where: {
[model.primaryKeyAttribute]: data[model.primaryKeyAttribute],
[model.primaryKeyAttribute]:
data[model.primaryKeyAttribute as keyof typeof data],
},
});
expect(existingEntity, 'failed to find created entity after creation').not.to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Actor, command, dispose, query } from '@hicommonwealth/core';
import { Actor, command, dispose, query, schemas } from '@hicommonwealth/core';
import { BalanceType } from '@hicommonwealth/shared';
import { expect } from 'chai';
import z from 'zod';
import { models } from '../../src/database';
import {
CreateCommentSubscription,
Expand All @@ -11,7 +12,8 @@ import { seed } from '../../src/tester';

describe('Comment subscription lifecycle', () => {
let actor: Actor;
let commentOne, commentTwo;
let commentOne: z.infer<typeof schemas.entities.Comment> | undefined;
let commentTwo: z.infer<typeof schemas.entities.Comment> | undefined;
before(async () => {
const [user] = await seed('User', {
isAdmin: false,
Expand Down Expand Up @@ -71,23 +73,23 @@ describe('Comment subscription lifecycle', () => {

it('should create a new comment subscription', async () => {
const payload = {
comment_id: commentOne.id,
comment_id: commentOne!.id!,
};
const res = await command(CreateCommentSubscription(), {
payload,
actor,
});
expect(res).to.deep.contains({
user_id: actor.user.id,
comment_id: commentOne.id,
comment_id: commentOne!.id,
});
});

it('should get comment subscriptions', async () => {
const [commentSubOne, commentSubTwo] =
await models.CommentSubscription.bulkCreate([
{ user_id: actor.user.id, comment_id: commentOne.id },
{ user_id: actor.user.id, comment_id: commentTwo.id },
{ user_id: actor.user.id!, comment_id: commentOne!.id! },
{ user_id: actor.user.id!, comment_id: commentTwo!.id! },
]);

const res = await query(GetCommentSubscriptions(), {
Expand All @@ -110,12 +112,12 @@ describe('Comment subscription lifecycle', () => {

it('should delete a comment subscriptions', async () => {
await models.CommentSubscription.bulkCreate([
{ user_id: actor.user.id, comment_id: commentOne.id },
{ user_id: actor.user.id, comment_id: commentTwo.id },
{ user_id: actor.user.id!, comment_id: commentOne!.id! },
{ user_id: actor.user.id!, comment_id: commentTwo!.id! },
]);

const payload = {
comment_ids: [commentOne.id, commentTwo.id],
comment_ids: [commentOne!.id!, commentTwo!.id!],
};

const res = await command(DeleteCommentSubscription(), {
Expand Down
40 changes: 21 additions & 19 deletions libs/model/test/subscription/community-alerts-lifecycle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Actor, command, dispose, query } from '@hicommonwealth/core';
import { Actor, command, dispose, query, schemas } from '@hicommonwealth/core';
import { BalanceType } from '@hicommonwealth/shared';
import { expect } from 'chai';
import z from 'zod';
import { models } from '../../src/database';
import {
CreateCommunityAlert,
Expand All @@ -11,7 +12,8 @@ import { seed } from '../../src/tester';

describe('Community alerts lifecycle', () => {
let actor: Actor;
let community, communityTwo;
let community: z.infer<typeof schemas.entities.Community> | undefined;
let communityTwo: z.infer<typeof schemas.entities.Community> | undefined;
before(async () => {
const [user] = await seed('User', {
isAdmin: false,
Expand Down Expand Up @@ -45,7 +47,7 @@ describe('Community alerts lifecycle', () => {
});
actor = {
user: { id: user!.id!, email: user!.email! },
address_id: null,
address_id: '0x',
};
});

Expand All @@ -59,7 +61,7 @@ describe('Community alerts lifecycle', () => {

it('should create a new community alert', async () => {
const payload = {
community_id: community.id,
community_id: community!.id!,
};

const res = await command(CreateCommunityAlert(), {
Expand All @@ -69,18 +71,18 @@ describe('Community alerts lifecycle', () => {

expect(res).to.deep.contains({
user_id: actor.user.id,
community_id: community.id,
community_id: community!.id!,
});
});

it('should delete a single community alert via id', async () => {
const [alert] = await seed('CommunityAlert', {
user_id: actor.user.id,
community_id: community.id,
community_id: community!.id!,
});
const payload = {
community_ids: [community.id],
ids: [alert.id],
community_ids: [community!.id!],
ids: [alert!.id!],
};
const res = await command(DeleteCommunityAlerts(), {
payload,
Expand All @@ -92,15 +94,15 @@ describe('Community alerts lifecycle', () => {
it('should delete multiple community alerts via ids', async () => {
const [alertOne] = await seed('CommunityAlert', {
user_id: actor.user.id,
community_id: community.id,
community_id: community!.id!,
});
const [alertTwo] = await seed('CommunityAlert', {
user_id: actor.user.id,
community_id: communityTwo.id,
community_id: communityTwo!.id!,
});
const payload = {
community_ids: [community.id, communityTwo.id],
ids: [alertOne.id, alertTwo.id],
community_ids: [community!.id!, communityTwo!.id!],
ids: [alertOne!.id!, alertTwo!.id!],
};
const res = await command(DeleteCommunityAlerts(), {
payload,
Expand All @@ -112,10 +114,10 @@ describe('Community alerts lifecycle', () => {
it('should delete a single community alert via community id', async () => {
await seed('CommunityAlert', {
user_id: actor.user.id,
community_id: community.id,
community_id: community!.id!,
});
const payload = {
community_ids: [community.id],
community_ids: [community!.id!],
};
const res = await command(DeleteCommunityAlerts(), {
payload,
Expand All @@ -127,14 +129,14 @@ describe('Community alerts lifecycle', () => {
it('should delete multiple community alerts via community ids', async () => {
await seed('CommunityAlert', {
user_id: actor.user.id,
community_id: community.id,
community_id: community!.id!,
});
await seed('CommunityAlert', {
user_id: actor.user.id,
community_id: communityTwo.id,
community_id: communityTwo!.id!,
});
const payload = {
community_ids: [community.id, communityTwo.id],
community_ids: [community!.id!, communityTwo!.id!],
};
const res = await command(DeleteCommunityAlerts(), {
payload,
Expand All @@ -146,11 +148,11 @@ describe('Community alerts lifecycle', () => {
it('should get community alerts', async () => {
const [alertOne] = await seed('CommunityAlert', {
user_id: actor.user.id,
community_id: community.id,
community_id: community!.id!,
});
const [alertTwo] = await seed('CommunityAlert', {
user_id: actor.user.id,
community_id: communityTwo.id,
community_id: communityTwo!.id!,
});

const res = await query(GetCommunityAlerts(), {
Expand Down
Loading

0 comments on commit 4478748

Please sign in to comment.