2 * Sone - AlbumTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.data
20 import net.pterodactylus.sone.data.impl.*
21 import org.hamcrest.MatcherAssert.*
22 import org.hamcrest.Matchers.*
26 * Unit test for various helper method in `Album.kt`.
31 fun `recursive list of all images for album is returned correctly`() {
32 val sone = IdOnlySone("sone")
33 val album = AlbumImpl(sone)
34 val firstNestedAlbum = AlbumImpl(sone)
35 val secondNestedAlbum = AlbumImpl(sone)
36 firstNestedAlbum.addImage(createImage(sone, "image-1"))
37 firstNestedAlbum.addImage(createImage(sone, "image-2"))
38 secondNestedAlbum.addImage(createImage(sone, "image-3"))
39 album.addImage(createImage(sone, "image-4"))
40 album.addAlbum(firstNestedAlbum)
41 album.addAlbum(secondNestedAlbum)
42 val images = album.allImages
43 assertThat(images.map(Image::id), containsInAnyOrder("image-1", "image-2", "image-3", "image-4"))
46 private fun createImage(sone: IdOnlySone, id: String, key: String? = null) = ImageImpl(id).modify().setSone(sone).setKey(key).update()
49 fun `allAlbums returns itself and all its subalbums`() {
50 val sone = IdOnlySone("sone")
51 val album = AlbumImpl(sone)
52 val firstNestedAlbum = AlbumImpl(sone)
53 val secondNestedAlbum = AlbumImpl(sone)
54 val albumNestedInFirst = AlbumImpl(sone)
55 album.addAlbum(firstNestedAlbum)
56 album.addAlbum(secondNestedAlbum)
57 firstNestedAlbum.addAlbum(albumNestedInFirst)
58 val albums = album.allAlbums
59 assertThat(albums, containsInAnyOrder<Album>(album, firstNestedAlbum, secondNestedAlbum, albumNestedInFirst))
60 assertThat(albums.indexOf(firstNestedAlbum), greaterThan(albums.indexOf(album)))
61 assertThat(albums.indexOf(secondNestedAlbum), greaterThan(albums.indexOf(album)))
62 assertThat(albums.indexOf(albumNestedInFirst), greaterThan(albums.indexOf(firstNestedAlbum)))
66 fun `notEmpty finds album without images is empty`() {
67 val sone = IdOnlySone("sone")
68 val album = AlbumImpl(sone)
69 assertThat(notEmpty(album), equalTo(false))
73 fun `notEmpty finds album with one inserted image is not empty`() {
74 val sone = IdOnlySone("sone")
75 val album = AlbumImpl(sone)
76 album.addImage(createImage(sone, "1", "key"))
77 assertThat(notEmpty(album), equalTo(true))
81 fun `notEmpty finds album with one not-inserted image is empty`() {
82 val sone = IdOnlySone("sone")
83 val album = AlbumImpl(sone)
84 album.addImage(createImage(sone, "1"))
85 assertThat(notEmpty(album), equalTo(false))
89 fun `notEmpty finds album with empty subalbums is empty`() {
90 val sone = IdOnlySone("sone")
91 val album = AlbumImpl(sone)
92 val firstNestedAlbum = AlbumImpl(sone)
93 album.addAlbum(firstNestedAlbum)
94 assertThat(notEmpty(album), equalTo(false))
98 fun `notEmpty finds album with subalbum with not inserted image is empty`() {
99 val sone = IdOnlySone("sone")
100 val album = AlbumImpl(sone)
101 val firstNestedAlbum = AlbumImpl(sone)
102 firstNestedAlbum.addImage(createImage(sone, "1"))
103 album.addAlbum(firstNestedAlbum)
104 assertThat(notEmpty(album), equalTo(false))
108 fun `notEmpty finds album with subalbum with inserted image is not empty`() {
109 val sone = IdOnlySone("sone")
110 val album = AlbumImpl(sone)
111 val firstNestedAlbum = AlbumImpl(sone)
112 firstNestedAlbum.addImage(createImage(sone, "1", "key"))
113 album.addAlbum(firstNestedAlbum)
114 assertThat(notEmpty(album), equalTo(true))
118 fun `allImages returns images from album`() {
119 val sone = IdOnlySone("sone")
120 val album = AlbumImpl(sone)
121 val image1 = createImage(sone, "1").also(album::addImage)
122 val image2 = createImage(sone, "2").also(album::addImage)
123 assertThat(album.allImages, contains(image1, image2))
127 fun `allImages returns images from subalbum`() {
128 val sone = IdOnlySone("sone")
129 val album1 = AlbumImpl(sone)
130 val album2 = AlbumImpl(sone).also(album1::addAlbum)
131 val image1 = createImage(sone, "1").also(album1::addImage)
132 val image2 = createImage(sone, "2").also(album2::addImage)
133 assertThat(album1.allImages, contains(image1, image2))