Skip to content

Commit 703f575

Browse files
authored
feat: add creator and squad relation queries (#3764)
1 parent 00f8dbc commit 703f575

19 files changed

Lines changed: 1573 additions & 126 deletions

.infra/crons.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,8 @@ export const crons: Cron[] = [
6464
schedule: '15 4 * * *',
6565
},
6666
{
67-
name: 'update-source-tag-view',
68-
schedule: '20 3 * * 0',
69-
},
70-
{
71-
name: 'update-tag-recommendations',
72-
schedule: '5 3 * * 0',
67+
name: 'update-tag-materialized-views',
68+
schedule: '20 3 * * *',
7369
},
7470
{
7571
name: 'daily-digest',
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import cron from '../../src/cron/updateTagMaterializedViews';
2+
import { expectSuccessfulCron, saveFixtures } from '../helpers';
3+
import { DataSource } from 'typeorm';
4+
import createOrGetConnection from '../../src/db';
5+
import {
6+
ArticlePost,
7+
Keyword,
8+
PostKeyword,
9+
Source,
10+
User,
11+
} from '../../src/entity';
12+
import { postKeywordsFixture, postsFixture } from '../fixture/post';
13+
import { sourcesFixture } from '../fixture/source';
14+
import { postRecommendedKeywordsFixture } from '../fixture/keywords';
15+
import { usersFixture } from '../fixture/user';
16+
17+
let con: DataSource;
18+
19+
beforeAll(async () => {
20+
con = await createOrGetConnection();
21+
});
22+
23+
describe('updateTagMaterializedViews cron', () => {
24+
beforeEach(async () => {
25+
jest.resetAllMocks();
26+
27+
await saveFixtures(con, Source, sourcesFixture);
28+
await saveFixtures(con, User, usersFixture);
29+
await saveFixtures(
30+
con,
31+
ArticlePost,
32+
postsFixture.map((post) => {
33+
switch (post.id) {
34+
case 'p1':
35+
return { ...post, authorId: '1', upvotes: 2 };
36+
case 'p4':
37+
return { ...post, authorId: '1', upvotes: 3 };
38+
case 'p5':
39+
return { ...post, authorId: '2', upvotes: 10 };
40+
default:
41+
return post;
42+
}
43+
}),
44+
);
45+
await saveFixtures(
46+
con,
47+
Keyword,
48+
postRecommendedKeywordsFixture.map((item) => ({
49+
...item,
50+
status: 'allow',
51+
})),
52+
);
53+
await saveFixtures(
54+
con,
55+
PostKeyword,
56+
postKeywordsFixture.map((item) => ({
57+
...item,
58+
status: 'allow',
59+
})),
60+
);
61+
});
62+
63+
it('should refresh tag recommendations and the source/user tag similarity views', async () => {
64+
await expectSuccessfulCron(cron);
65+
66+
expect(
67+
await con.query(
68+
`SELECT count FROM source_tag_view WHERE "sourceId" = 'a' AND tag = 'javascript'`,
69+
),
70+
).toEqual([{ count: '5' }]);
71+
expect(
72+
await con.query(
73+
`SELECT count FROM user_tag_view WHERE "userId" = '1' AND tag = 'javascript'`,
74+
),
75+
).toEqual([{ count: '5' }]);
76+
expect(
77+
await con.query(
78+
`SELECT "similarSourceId", count FROM source_similarity_view WHERE "sourceId" = 'a'`,
79+
),
80+
).toEqual([{ similarSourceId: 'b', count: '1' }]);
81+
expect(
82+
await con.query(
83+
`SELECT "similarUserId", count FROM user_similarity_view WHERE "userId" = '1'`,
84+
),
85+
).toEqual([{ similarUserId: '2', count: '1' }]);
86+
expect(
87+
await con.query(`SELECT count(*)::int FROM tag_recommendation`),
88+
).toEqual([{ count: 10 }]);
89+
});
90+
});

__tests__/cron/updateTagRecommendations.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)