X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryDatabaseTest.kt;h=150336f9a553440cf873b40018d1e99308b94e31;hp=1536da41c06b7bbdec18b23ed07dcbefb5ad2291;hb=2ab560633c24940fa49bdaf569635dd673ad7b19;hpb=03cec6a6772c2d836d94864adddaf544cbe9d72f diff --git a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt index 1536da4..150336f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt @@ -1,5 +1,5 @@ /* - * Sone - MemoryDatabaseTest.kt - Copyright © 2013–2019 David Roden + * Sone - MemoryDatabaseTest.kt - Copyright © 2013–2020 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,7 +38,7 @@ import kotlin.test.* */ class MemoryDatabaseTest { - private val configuration = mock() + private val configuration = deepMock() private val memoryDatabase = MemoryDatabase(configuration) private val sone = mock() @@ -50,15 +50,15 @@ class MemoryDatabaseTest { @Test fun `stored sone is made available`() { storeSone() - assertThat(memoryDatabase.getPost("post1"), isPost("post1", 1000L, "post1", absent())) - assertThat(memoryDatabase.getPost("post2"), isPost("post2", 2000L, "post2", of(RECIPIENT_ID))) + assertThat(memoryDatabase.getPost("post1"), isPost("post1", 1000L, "post1", null)) + assertThat(memoryDatabase.getPost("post2"), isPost("post2", 2000L, "post2", RECIPIENT_ID)) assertThat(memoryDatabase.getPost("post3"), nullValue()) assertThat(memoryDatabase.getPostReply("reply1"), isPostReply("reply1", "post1", 3000L, "reply1")) assertThat(memoryDatabase.getPostReply("reply2"), isPostReply("reply2", "post2", 4000L, "reply2")) assertThat(memoryDatabase.getPostReply("reply3"), isPostReply("reply3", "post1", 5000L, "reply3")) assertThat(memoryDatabase.getPostReply("reply4"), nullValue()) - assertThat(memoryDatabase.getAlbum("album1"), isAlbum("album1", null, "album1", "album-description1")) - assertThat(memoryDatabase.getAlbum("album2"), isAlbum("album2", null, "album2", "album-description2")) + assertThat(memoryDatabase.getAlbum("album1"), isAlbum("album1", "root", "album1", "album-description1")) + assertThat(memoryDatabase.getAlbum("album2"), isAlbum("album2", "root", "album2", "album-description2")) assertThat(memoryDatabase.getAlbum("album3"), isAlbum("album3", "album1", "album3", "album-description3")) assertThat(memoryDatabase.getAlbum("album4"), nullValue()) assertThat(memoryDatabase.getImage("image1"), isImage("image1", 1000L, "KSK@image1", "image1", "image-description1", 16, 9)) @@ -123,9 +123,10 @@ class MemoryDatabaseTest { .setDescription("album-description3") .update() firstAlbum.addAlbum(thirdAlbum) - val rootAlbum = mock() - whenever(rootAlbum.id).thenReturn("root") - whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum)) + val rootAlbum = AlbumImpl(sone, "root").also { + it.addAlbum(firstAlbum) + it.addAlbum(secondAlbum) + } whenever(sone.rootAlbum).thenReturn(rootAlbum) val firstImage = TestImageBuilder().withId("image1") .build() @@ -409,6 +410,36 @@ class MemoryDatabaseTest { assertThat(configuration.getStringValue("KnownReplies/1/ID").value, equalTo(null)) } + @Test + @Dirty("the rate limiter should be mocked") + fun `saving the database twice in a row only saves it once`() { + memoryDatabase.save() + memoryDatabase.save() + verify(configuration.getStringValue("KnownPosts/0/ID"), times(1)).value = null + } + + @Test + @Dirty("the rate limiter should be mocked") + fun `setting posts as knows twice in a row only saves the database once`() { + prepareConfigurationValues() + val post = mock() + whenever(post.id).thenReturn("post-id") + memoryDatabase.setPostKnown(post, true) + memoryDatabase.setPostKnown(post, true) + verify(configuration, times(1)).getStringValue("KnownPosts/1/ID") + } + + @Test + @Dirty("the rate limiter should be mocked") + fun `setting post replies as knows twice in a row only saves the database once`() { + prepareConfigurationValues() + val postReply = mock() + whenever(postReply.id).thenReturn("post-reply-id") + memoryDatabase.setPostReplyKnown(postReply, true) + memoryDatabase.setPostReplyKnown(postReply, true) + verify(configuration, times(1)).getStringValue("KnownReplies/1/ID") + } + } private const val SONE_ID = "sone"