From d8642192891c97d16dd5f4f237ddb38dbc436641 Mon Sep 17 00:00:00 2001 From: Ashley Herrera Date: Tue, 5 Nov 2024 15:13:51 -0500 Subject: [PATCH] In depth testing for similarPosts --- src/tests/PostTest.test.ts | 114 +++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/tests/PostTest.test.ts b/src/tests/PostTest.test.ts index 9fec7d6..c7f6766 100644 --- a/src/tests/PostTest.test.ts +++ b/src/tests/PostTest.test.ts @@ -688,4 +688,118 @@ describe('post tests', () => { expect(getSimilarPostsResponse.posts[0].title).toEqual(similarPost.title); }); + /* + need to create three more in depth tests: + 1. No similar post exists + 2. Multiple similar posts (should retun all of these) + 3. Mulitple unrelated posts (should return none of these) + 4. Checking algorithm for similarity + + + Possible root causes: + - the getLoadedModel is not working + - the embed method is called within a loop + - passing incorrect input when hitting the route on frontend + - algorthim is not working + */ + + test('similar posts - no similar posts exist', async () => { + const user = UserFactory.fakeTemplate(); + const targetPost = PostFactory.fakeTemplate(); + const unrelatedPost1 = PostFactory.fakeTemplate(); + const unrelatedPost2 = PostFactory.fakeTemplate(); + + targetPost.id = '81e6896c-a549-41bf-8851-604e7fbd4f1f'; + unrelatedPost1.id = '81e6896c-a549-41bf-8852-604e7fbd4f1f'; + unrelatedPost2.id = '81e6896c-a549-41bf-8853-604e7fbd4f1f'; + + targetPost.user = user; + unrelatedPost1.user = user; + unrelatedPost2.user = user; + + targetPost.title = 'Post'; + unrelatedPost1.title = 'Completely Different post'; + unrelatedPost2.title = 'Another Unrelated Post'; + + await new DataFactory() + .createUsers(user) + .createPosts(targetPost, unrelatedPost1, unrelatedPost2) + .write(); + + const getSimilarPostsResponse = await postController.similarPosts(user, { id: targetPost.id }); + + expect(getSimilarPostsResponse.posts).toHaveLength(0); + }); + + test('similar posts - multiple similar posts exist', async () => { + const user = UserFactory.fakeTemplate(); + const targetPost = PostFactory.fakeTemplate(); + const similarPost1 = PostFactory.fakeTemplate(); + const similarPost2 = PostFactory.fakeTemplate(); + const unrelatedPost = PostFactory.fakeTemplate(); + + targetPost.id = '81e6896c-a549-41bf-8851-604e7fbd4f1f'; + similarPost1.id = '81e6896c-a549-41bf-8852-604e7fbd4f1f'; + similarPost2.id = '81e6896c-a549-41bf-8853-604e7fbd4f1f'; + unrelatedPost.id = '81e6896c-a549-41bf-8854-604e7fbd4f1f'; + + targetPost.user = user; + similarPost1.user = user; + similarPost2.user = user; + unrelatedPost.user = user; + + targetPost.title = 'Cookbook'; + similarPost1.title = 'Cooking Pan'; + similarPost2.title = 'Baking utensils'; + unrelatedPost.title = 'Airpods'; + + await new DataFactory() + .createUsers(user) + .createPosts(targetPost, similarPost1, similarPost2, unrelatedPost) + .write(); + + const getSimilarPostsResponse = await postController.similarPosts(user, { id: targetPost.id }); + + expect(getSimilarPostsResponse.posts).toHaveLength(2); + const similarTitles = getSimilarPostsResponse.posts.map(post => post.title); + expect(similarTitles).toContain(similarPost1.title); + expect(similarTitles).toContain(similarPost2.title); + }); + + test('similar posts - multiple unrelated posts exist', async () => { + // ... (Similar structure to the previous test, + // but with multiple unrelated posts and only one similar post) + + const user = UserFactory.fakeTemplate(); + const targetPost = PostFactory.fakeTemplate(); + const similarPost1 = PostFactory.fakeTemplate(); + const unrelatedPost1 = PostFactory.fakeTemplate(); + const unrelatedPost2 = PostFactory.fakeTemplate(); + + targetPost.id = '81e6896c-a549-41bf-8851-604e7fbd4f1f'; + similarPost1.id = '81e6896c-a549-41bf-8852-604e7fbd4f1f'; + unrelatedPost1.id = '81e6896c-a549-41bf-8853-604e7fbd4f1f'; + unrelatedPost2.id = '81e6896c-a549-41bf-8854-604e7fbd4f1f'; + + targetPost.user = user; + similarPost1.user = user; + unrelatedPost1.user = user; + unrelatedPost2.user = user; + + targetPost.title = 'Airpods'; + similarPost1.title = 'Headphones'; + unrelatedPost1.title = 'Curling Iron'; + unrelatedPost2.title = 'Shoes'; + + await new DataFactory() + .createUsers(user) + .createPosts(targetPost, similarPost1, unrelatedPost1, unrelatedPost2) + .write(); + + const getSimilarPostsResponse = await postController.similarPosts(user, { id: targetPost.id }); + + expect(getSimilarPostsResponse.posts).toHaveLength(1); + const similarTitles = getSimilarPostsResponse.posts.map(post => post.title); + expect(similarTitles).toContain(similarPost1.title); + }); }); \ No newline at end of file