From 462638810375ea800ee43a79dd9cd28e146243d5 Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Sat, 19 Oct 2024 14:58:06 +0300 Subject: [PATCH] fix: posts tests --- .../src/database/DatabaseQetaStore.test.ts | 27 ++++++++++++------ .../src/database/DatabaseQetaStore.ts | 14 +++++++++- .../qeta-backend/src/database/QetaStore.ts | 2 ++ .../qeta-backend/src/service/router.test.ts | 28 +++++++++---------- .../src/service/routes/questions.ts | 13 +++++++-- 5 files changed, 58 insertions(+), 26 deletions(-) diff --git a/plugins/qeta-backend/src/database/DatabaseQetaStore.test.ts b/plugins/qeta-backend/src/database/DatabaseQetaStore.test.ts index 12846b42..09f5f204 100644 --- a/plugins/qeta-backend/src/database/DatabaseQetaStore.test.ts +++ b/plugins/qeta-backend/src/database/DatabaseQetaStore.test.ts @@ -146,14 +146,14 @@ describe.each(databases.eachSupportedId())( expect(ans2?.comments?.length).toEqual(1); }); - it('should fetch list of posts', async () => { + it('should fetch list of questions', async () => { await insertPost(post); await insertPost({ ...post, title: 'title2' }); - const ret = await storage.getPosts('user1', {}); + const ret = await storage.getPosts('user1', { type: 'question' }); expect(ret?.posts.length).toEqual(2); }); - it('should fetch posts within fromDate and toDate DateRange', async () => { + it('should fetch questions within fromDate and toDate DateRange', async () => { await insertPost({ ...post, title: 'title2', @@ -168,19 +168,23 @@ describe.each(databases.eachSupportedId())( const ret = await storage.getPosts('user', { fromDate: '2024-04-02', toDate: '2024-04-02', + type: 'question', }); expect(ret?.posts.length).toEqual(1); }); - it('should fetch list of random posts', async () => { + it('should fetch list of random questions', async () => { await insertPost(post); await insertPost({ ...post, title: 'title2' }); - const ret = await storage.getPosts('user1', { random: true }); + const ret = await storage.getPosts('user1', { + random: true, + type: 'question', + }); expect(ret?.posts.length).toEqual(2); }); - it('should fetch list of posts based on searchQuery', async () => { + it('should fetch list of questions based on searchQuery', async () => { await insertPost(post); await insertPost({ ...post, @@ -189,27 +193,30 @@ describe.each(databases.eachSupportedId())( }); const ret = await storage.getPosts('user1', { searchQuery: 'to search', + type: 'question', }); expect(ret?.posts.length).toEqual(1); const noPosts = await storage.getPosts('user1', { searchQuery: 'missing', + type: 'question', }); expect(noPosts?.posts.length).toEqual(0); }); - it('should fetch list of posts with special characters in searchQuery', async () => { + it('should fetch list of questions with special characters in searchQuery', async () => { await insertPost({ ...post, content: 'Cannot read config file:', }); const ret = await storage.getPosts('user1', { searchQuery: 'Cannot read config file:', + type: 'question', }); expect(ret?.posts.length).toEqual(1); }); - it('should fetch posts with specific component', async () => { + it('should fetch questions with specific component', async () => { const q1 = await storage.createPost( 'user1', 'title', @@ -229,12 +236,14 @@ describe.each(databases.eachSupportedId())( const ret1 = await storage.getPosts('user1', { entity: 'component:default/comp1', + type: 'question', }); expect(ret1.posts.length).toEqual(1); expect(ret1.posts.at(0)?.id).toEqual(q1.id); const ret2 = await storage.getPosts('user1', { entity: 'component:default/comp2', + type: 'question', }); expect(ret2.posts.length).toEqual(2); @@ -266,6 +275,7 @@ describe.each(databases.eachSupportedId())( const ret1 = await storage.getPosts('user1', { favorite: true, author: 'user1', + type: 'question', }); expect(ret1.posts.length).toEqual(1); @@ -278,6 +288,7 @@ describe.each(databases.eachSupportedId())( const ret2 = await storage.getPosts('user1', { favorite: true, author: 'user1', + type: 'question', }); expect(ret2.posts.length).toEqual(0); }); diff --git a/plugins/qeta-backend/src/database/DatabaseQetaStore.ts b/plugins/qeta-backend/src/database/DatabaseQetaStore.ts index d6451f5a..b4e31c31 100644 --- a/plugins/qeta-backend/src/database/DatabaseQetaStore.ts +++ b/plugins/qeta-backend/src/database/DatabaseQetaStore.ts @@ -24,6 +24,7 @@ import { filterTags, GlobalStat, Post, + PostType, Statistic, StatisticsRequestParameters, UserEntitiesResponse, @@ -198,6 +199,7 @@ export class DatabaseQetaStore implements QetaStore { filters?: PermissionCriteria, ): Promise { const query = this.getPostsBaseQuery(user_ref); + query.where('type', options.type); if (options.fromDate && options.toDate) { query.whereBetween('posts.created', [ @@ -381,6 +383,7 @@ export class DatabaseQetaStore implements QetaStore { entities?: string[], images?: number[], anonymous?: boolean, + type?: PostType, ): Promise { const posts = await this.db .insert( @@ -390,11 +393,20 @@ export class DatabaseQetaStore implements QetaStore { content, created, anonymous: anonymous ?? false, + type: type ?? 'question', }, ['id'], ) .into('posts') - .returning(['id', 'author', 'title', 'content', 'created', 'anonymous']); + .returning([ + 'id', + 'author', + 'title', + 'content', + 'created', + 'anonymous', + 'type', + ]); await Promise.all([ this.addPostTags(posts[0].id, tags), diff --git a/plugins/qeta-backend/src/database/QetaStore.ts b/plugins/qeta-backend/src/database/QetaStore.ts index ced5b0c9..b6c3147b 100644 --- a/plugins/qeta-backend/src/database/QetaStore.ts +++ b/plugins/qeta-backend/src/database/QetaStore.ts @@ -4,6 +4,7 @@ import { Comment, GlobalStat, Post, + PostType, Statistic, StatisticsRequestParameters, UserEntitiesResponse, @@ -36,6 +37,7 @@ export interface Answers { } export interface PostOptions { + type: PostType; limit?: number; offset?: number; author?: string | string[]; diff --git a/plugins/qeta-backend/src/service/router.test.ts b/plugins/qeta-backend/src/service/router.test.ts index 3ea0556c..d8ce0f7d 100644 --- a/plugins/qeta-backend/src/service/router.test.ts +++ b/plugins/qeta-backend/src/service/router.test.ts @@ -115,35 +115,35 @@ describe('createRouter', () => { const qetaStore: jest.Mocked = { commentAnswer: jest.fn(), - commentQuestion: jest.fn(), + commentPost: jest.fn(), deleteAnswerComment: jest.fn(), - deleteQuestionComment: jest.fn(), - getQuestions: jest.fn(), - getQuestion: jest.fn(), + deletePostComment: jest.fn(), + getPosts: jest.fn(), + getPost: jest.fn(), getQuestionByAnswerId: jest.fn(), - postQuestion: jest.fn(), - deleteQuestion: jest.fn(), - answerQuestion: jest.fn(), + createPost: jest.fn(), + deletePost: jest.fn(), + answerPost: jest.fn(), getAnswer: jest.fn(), deleteAnswer: jest.fn(), - voteQuestion: jest.fn(), + votePost: jest.fn(), voteAnswer: jest.fn(), markAnswerCorrect: jest.fn(), markAnswerIncorrect: jest.fn(), getTags: jest.fn(), getEntities: jest.fn(), - updateQuestion: jest.fn(), + updatePost: jest.fn(), updateAnswer: jest.fn(), - favoriteQuestion: jest.fn(), - unfavoriteQuestion: jest.fn(), + favoritePost: jest.fn(), + unfavoritePost: jest.fn(), postAttachment: jest.fn(), getAnswers: jest.fn(), getAttachment: jest.fn(), getMostUpvotedAnswers: jest.fn(), getTotalAnswers: jest.fn(), - getMostUpvotedQuestions: jest.fn(), + getMostUpvotedPosts: jest.fn(), getMostUpvotedCorrectAnswers: jest.fn(), - getTotalQuestions: jest.fn(), + getTotalPosts: jest.fn(), followTag: jest.fn(), unfollowTag: jest.fn(), getUserTags: jest.fn(), @@ -153,7 +153,7 @@ describe('createRouter', () => { getUserEntities: jest.fn(), getUsersForEntities: jest.fn(), getAnswerComment: jest.fn(), - getQuestionComment: jest.fn(), + getPostComment: jest.fn(), } as unknown as jest.Mocked; const mockedAuthorize: jest.MockedFunction = diff --git a/plugins/qeta-backend/src/service/routes/questions.ts b/plugins/qeta-backend/src/service/routes/questions.ts index ee396173..88537150 100644 --- a/plugins/qeta-backend/src/service/routes/questions.ts +++ b/plugins/qeta-backend/src/service/routes/questions.ts @@ -69,10 +69,17 @@ export const questionsRoutes = (router: Router, options: RouteOptions) => { const filter: PermissionCriteria = transformConditions( decision.conditions, ); - const posts = await database.getPosts(username, request.query, filter); + const posts = await database.getPosts( + username, + { ...request.query, type: 'question' }, + filter, + ); response.json({ questions: posts.posts, total: posts.total }); } else { - const posts = await database.getPosts(username, request.query); + const posts = await database.getPosts(username, { + ...request.query, + type: 'question', + }); response.json({ questions: posts.posts, total: posts.total }); } }); @@ -89,7 +96,7 @@ export const questionsRoutes = (router: Router, options: RouteOptions) => { return; } - const optionOverride: PostOptions = {}; + const optionOverride: PostOptions = { type: 'question' }; const type = request.params.type; if (type === 'unanswered') { optionOverride.random = true;