Skip to content

Commit

Permalink
fix: posts tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drodil committed Oct 19, 2024
1 parent 7a17516 commit 4626388
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
27 changes: 19 additions & 8 deletions plugins/qeta-backend/src/database/DatabaseQetaStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down
14 changes: 13 additions & 1 deletion plugins/qeta-backend/src/database/DatabaseQetaStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
filterTags,
GlobalStat,
Post,
PostType,
Statistic,
StatisticsRequestParameters,
UserEntitiesResponse,
Expand Down Expand Up @@ -198,6 +199,7 @@ export class DatabaseQetaStore implements QetaStore {
filters?: PermissionCriteria<QetaFilters>,
): Promise<Posts> {
const query = this.getPostsBaseQuery(user_ref);
query.where('type', options.type);

if (options.fromDate && options.toDate) {
query.whereBetween('posts.created', [
Expand Down Expand Up @@ -381,6 +383,7 @@ export class DatabaseQetaStore implements QetaStore {
entities?: string[],
images?: number[],
anonymous?: boolean,
type?: PostType,
): Promise<Post> {
const posts = await this.db
.insert(
Expand All @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions plugins/qeta-backend/src/database/QetaStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Comment,
GlobalStat,
Post,
PostType,
Statistic,
StatisticsRequestParameters,
UserEntitiesResponse,
Expand Down Expand Up @@ -36,6 +37,7 @@ export interface Answers {
}

export interface PostOptions {
type: PostType;
limit?: number;
offset?: number;
author?: string | string[];
Expand Down
28 changes: 14 additions & 14 deletions plugins/qeta-backend/src/service/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,35 @@ describe('createRouter', () => {

const qetaStore: jest.Mocked<QetaStore> = {
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(),
Expand All @@ -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<QetaStore>;

const mockedAuthorize: jest.MockedFunction<PermissionEvaluator['authorize']> =
Expand Down
13 changes: 10 additions & 3 deletions plugins/qeta-backend/src/service/routes/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ export const questionsRoutes = (router: Router, options: RouteOptions) => {
const filter: PermissionCriteria<QetaFilters> = 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 });
}
});
Expand All @@ -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;
Expand Down

0 comments on commit 4626388

Please sign in to comment.