mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 23:47:02 +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) {
|
suspend fun addPost(post: LobstersPost) = withContext(Dispatchers.IO) {
|
||||||
if (!savedPostsCache.containsKey(post.short_id)) {
|
if (!savedPostsCache.containsKey(post.short_id)) {
|
||||||
savedPostsCache.putIfAbsent(post.short_id, post)
|
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) {
|
suspend fun removePost(post: LobstersPost) = withContext(Dispatchers.IO) {
|
||||||
if (savedPostsCache.containsKey(post.short_id)) {
|
if (savedPostsCache.containsKey(post.short_id)) {
|
||||||
savedPostsCache.remove(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(),
|
emptyList(),
|
||||||
),
|
),
|
||||||
listOf("openbsd", "linux", "containers", "hack the planet", "no thanks"),
|
listOf("openbsd", "linux", "containers", "hack the planet", "no thanks"),
|
||||||
false,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
|
|
@ -14,39 +14,23 @@ CREATE TABLE IF NOT EXISTS LobstersPost(
|
||||||
description TEXT NOT NULL,
|
description TEXT NOT NULL,
|
||||||
comments_url TEXT NOT NULL,
|
comments_url TEXT NOT NULL,
|
||||||
submitter_user TEXT as Submitter NOT NULL,
|
submitter_user TEXT as Submitter NOT NULL,
|
||||||
tags TEXT as List<String> NOT NULL,
|
tags TEXT as List<String> NOT NULL
|
||||||
is_saved INTEGER as Boolean DEFAULT 0
|
|
||||||
);
|
);
|
||||||
|
|
||||||
selectAllPosts:
|
|
||||||
SELECT *
|
|
||||||
FROM LobstersPost;
|
|
||||||
|
|
||||||
selectSavedPosts:
|
|
||||||
SELECT *
|
|
||||||
FROM LobstersPost
|
|
||||||
WHERE is_saved = 1;
|
|
||||||
|
|
||||||
selectPost:
|
selectPost:
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM LobstersPost
|
FROM LobstersPost
|
||||||
WHERE short_id = ?;
|
WHERE short_id = ?;
|
||||||
|
|
||||||
|
selectAllPosts:
|
||||||
|
SELECT *
|
||||||
|
FROM LobstersPost;
|
||||||
|
|
||||||
insertOrReplacePost:
|
insertOrReplacePost:
|
||||||
INSERT OR REPLACE
|
INSERT OR REPLACE
|
||||||
INTO LobstersPost
|
INTO LobstersPost
|
||||||
VALUES ?;
|
VALUES ?;
|
||||||
|
|
||||||
savePost:
|
|
||||||
UPDATE LobstersPost
|
|
||||||
SET is_saved = 1
|
|
||||||
WHERE short_id = ?;
|
|
||||||
|
|
||||||
removeSavedPost:
|
|
||||||
UPDATE LobstersPost
|
|
||||||
SET is_saved = 0
|
|
||||||
WHERE short_id = ?;
|
|
||||||
|
|
||||||
deletePost:
|
deletePost:
|
||||||
DELETE
|
DELETE
|
||||||
FROM LobstersPost
|
FROM LobstersPost
|
||||||
|
@ -56,12 +40,6 @@ deleteAllPosts:
|
||||||
DELETE
|
DELETE
|
||||||
FROM LobstersPost;
|
FROM LobstersPost;
|
||||||
|
|
||||||
isPostSaved:
|
|
||||||
SELECT *
|
|
||||||
FROM LobstersPost
|
|
||||||
WHERE short_id = ?
|
|
||||||
AND is_saved = 1;
|
|
||||||
|
|
||||||
selectCount:
|
selectCount:
|
||||||
SELECT COUNT(*)
|
SELECT COUNT(*)
|
||||||
FROM LobstersPost;
|
FROM LobstersPost;
|
||||||
|
|
|
@ -63,7 +63,7 @@ class SqlDelightQueriesTest {
|
||||||
postQueries.insertOrReplacePost(post)
|
postQueries.insertOrReplacePost(post)
|
||||||
|
|
||||||
// Create a new post and try replacing it
|
// 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)
|
postQueries.insertOrReplacePost(newPost)
|
||||||
|
|
||||||
// Check post count
|
// Check post count
|
||||||
|
@ -72,7 +72,7 @@ class SqlDelightQueriesTest {
|
||||||
|
|
||||||
// Check if post is updated
|
// Check if post is updated
|
||||||
val postFromDb = postQueries.selectPost(post.short_id).executeAsOne()
|
val postFromDb = postQueries.selectPost(post.short_id).executeAsOne()
|
||||||
assertEquals(true, postFromDb.is_saved)
|
assertEquals(100, postFromDb.comment_count)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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
|
@Test
|
||||||
fun deletePost() = runBlocking {
|
fun deletePost() = runBlocking {
|
||||||
// Create 3 posts and insert them to DB
|
// Create 3 posts and insert them to DB
|
||||||
|
@ -155,16 +122,16 @@ class SqlDelightQueriesTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun deleteAllPost() = runBlocking {
|
fun deleteAllPost() = runBlocking {
|
||||||
// Create 3 posts and insert them to DB
|
// Create 5 posts and insert them to DB
|
||||||
val posts = createTestData(5)
|
val posts = createTestData(5)
|
||||||
posts.forEach { postQueries.insertOrReplacePost(it) }
|
posts.forEach { postQueries.insertOrReplacePost(it) }
|
||||||
|
|
||||||
// Delete 2nd post
|
// Delete all posts
|
||||||
postQueries.deleteAllPosts()
|
postQueries.deleteAllPosts()
|
||||||
|
|
||||||
val postsCount = postQueries.selectCount().executeAsOne()
|
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)
|
assertEquals(0, postsCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +163,6 @@ class SqlDelightQueriesTest {
|
||||||
comments_url = "test_comments_url",
|
comments_url = "test_comments_url",
|
||||||
submitter_user = submitter,
|
submitter_user = submitter,
|
||||||
tags = listOf(),
|
tags = listOf(),
|
||||||
is_saved = false
|
|
||||||
)
|
)
|
||||||
|
|
||||||
posts.add(post)
|
posts.add(post)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue