mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 22:37:03 +05:30
src: remove is_saved from LobstersPost and fix tests
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
This commit is contained in:
parent
6b3b35d4df
commit
1251ae3bc0
4 changed files with 12 additions and 69 deletions
|
@ -45,14 +45,14 @@ class LobstersRepository @Inject constructor(private val lobstersDatabase: Lobst
|
|||
suspend fun addPost(post: LobstersPost) = withContext(Dispatchers.IO) {
|
||||
if (!savedPostsCache.containsKey(post.short_id)) {
|
||||
savedPostsCache.putIfAbsent(post.short_id, post)
|
||||
lobstersDatabase.postQueries.insertOrReplacePost(post.copy(is_saved = true))
|
||||
lobstersDatabase.postQueries.insertOrReplacePost(post)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun removePost(post: LobstersPost) = withContext(Dispatchers.IO) {
|
||||
if (savedPostsCache.containsKey(post.short_id)) {
|
||||
savedPostsCache.remove(post.short_id)
|
||||
lobstersDatabase.postQueries.removeSavedPost(post.short_id)
|
||||
lobstersDatabase.postQueries.deletePost(post.short_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,6 @@ val TEST_POST = LobstersPost(
|
|||
emptyList(),
|
||||
),
|
||||
listOf("openbsd", "linux", "containers", "hack the planet", "no thanks"),
|
||||
false,
|
||||
)
|
||||
|
||||
@Composable
|
||||
|
|
|
@ -14,39 +14,23 @@ CREATE TABLE IF NOT EXISTS LobstersPost(
|
|||
description TEXT NOT NULL,
|
||||
comments_url TEXT NOT NULL,
|
||||
submitter_user TEXT as Submitter NOT NULL,
|
||||
tags TEXT as List<String> NOT NULL,
|
||||
is_saved INTEGER as Boolean DEFAULT 0
|
||||
tags TEXT as List<String> NOT NULL
|
||||
);
|
||||
|
||||
selectAllPosts:
|
||||
SELECT *
|
||||
FROM LobstersPost;
|
||||
|
||||
selectSavedPosts:
|
||||
SELECT *
|
||||
FROM LobstersPost
|
||||
WHERE is_saved = 1;
|
||||
|
||||
selectPost:
|
||||
SELECT *
|
||||
FROM LobstersPost
|
||||
WHERE short_id = ?;
|
||||
|
||||
selectAllPosts:
|
||||
SELECT *
|
||||
FROM LobstersPost;
|
||||
|
||||
insertOrReplacePost:
|
||||
INSERT OR REPLACE
|
||||
INTO LobstersPost
|
||||
VALUES ?;
|
||||
|
||||
savePost:
|
||||
UPDATE LobstersPost
|
||||
SET is_saved = 1
|
||||
WHERE short_id = ?;
|
||||
|
||||
removeSavedPost:
|
||||
UPDATE LobstersPost
|
||||
SET is_saved = 0
|
||||
WHERE short_id = ?;
|
||||
|
||||
deletePost:
|
||||
DELETE
|
||||
FROM LobstersPost
|
||||
|
@ -56,12 +40,6 @@ deleteAllPosts:
|
|||
DELETE
|
||||
FROM LobstersPost;
|
||||
|
||||
isPostSaved:
|
||||
SELECT *
|
||||
FROM LobstersPost
|
||||
WHERE short_id = ?
|
||||
AND is_saved = 1;
|
||||
|
||||
selectCount:
|
||||
SELECT COUNT(*)
|
||||
FROM LobstersPost;
|
||||
|
|
|
@ -63,7 +63,7 @@ class SqlDelightQueriesTest {
|
|||
postQueries.insertOrReplacePost(post)
|
||||
|
||||
// Create a new post and try replacing it
|
||||
val newPost = post.copy(is_saved = true)
|
||||
val newPost = post.copy(comment_count = 100)
|
||||
postQueries.insertOrReplacePost(newPost)
|
||||
|
||||
// Check post count
|
||||
|
@ -72,7 +72,7 @@ class SqlDelightQueriesTest {
|
|||
|
||||
// Check if post is updated
|
||||
val postFromDb = postQueries.selectPost(post.short_id).executeAsOne()
|
||||
assertEquals(true, postFromDb.is_saved)
|
||||
assertEquals(100, postFromDb.comment_count)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -103,39 +103,6 @@ class SqlDelightQueriesTest {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun savePost() = runBlocking {
|
||||
// Insert post to DB
|
||||
val post = createTestData(1)[0]
|
||||
postQueries.insertOrReplacePost(post)
|
||||
|
||||
// Update is_saved property of post
|
||||
postQueries.savePost(post.short_id)
|
||||
|
||||
// Get the post and check if is_saved is true
|
||||
val postFromDB = postQueries.selectPost(post.short_id).executeAsOne()
|
||||
assertEquals(true, postFromDB.is_saved)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun removeSavedPost() = runBlocking {
|
||||
val post = createTestData(1)[0]
|
||||
postQueries.insertOrReplacePost(post)
|
||||
|
||||
postQueries.savePost(post.short_id)
|
||||
|
||||
// Get the post and check if is_saved is true
|
||||
val postFromDB = postQueries.selectPost(post.short_id).executeAsOne()
|
||||
assertEquals(true, postFromDB.is_saved)
|
||||
|
||||
postQueries.removeSavedPost(post.short_id)
|
||||
|
||||
// Get the post and check if is_saved is false
|
||||
val updatedPostFromDB = postQueries.selectPost(post.short_id).executeAsOne()
|
||||
assertEquals(false, updatedPostFromDB.is_saved)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deletePost() = runBlocking {
|
||||
// Create 3 posts and insert them to DB
|
||||
|
@ -155,16 +122,16 @@ class SqlDelightQueriesTest {
|
|||
|
||||
@Test
|
||||
fun deleteAllPost() = runBlocking {
|
||||
// Create 3 posts and insert them to DB
|
||||
// Create 5 posts and insert them to DB
|
||||
val posts = createTestData(5)
|
||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||
|
||||
// Delete 2nd post
|
||||
// Delete all posts
|
||||
postQueries.deleteAllPosts()
|
||||
|
||||
val postsCount = postQueries.selectCount().executeAsOne()
|
||||
|
||||
// Check if size is 2, and only the correct post is deleted
|
||||
// Check if db is empty
|
||||
assertEquals(0, postsCount)
|
||||
}
|
||||
|
||||
|
@ -196,7 +163,6 @@ class SqlDelightQueriesTest {
|
|||
comments_url = "test_comments_url",
|
||||
submitter_user = submitter,
|
||||
tags = listOf(),
|
||||
is_saved = false
|
||||
)
|
||||
|
||||
posts.add(post)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue