|
| 1 | +package org.schabi.newpipe.database |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.room.Room |
| 5 | +import androidx.test.core.app.ApplicationProvider |
| 6 | +import io.reactivex.rxjava3.core.Single |
| 7 | +import org.junit.After |
| 8 | +import org.junit.Assert.assertEquals |
| 9 | +import org.junit.Assert.assertNotNull |
| 10 | +import org.junit.Before |
| 11 | +import org.junit.Test |
| 12 | +import org.schabi.newpipe.database.feed.dao.FeedDAO |
| 13 | +import org.schabi.newpipe.database.feed.model.FeedEntity |
| 14 | +import org.schabi.newpipe.database.feed.model.FeedGroupEntity |
| 15 | +import org.schabi.newpipe.database.stream.StreamWithState |
| 16 | +import org.schabi.newpipe.database.stream.dao.StreamDAO |
| 17 | +import org.schabi.newpipe.database.stream.model.StreamEntity |
| 18 | +import org.schabi.newpipe.database.subscription.SubscriptionDAO |
| 19 | +import org.schabi.newpipe.database.subscription.SubscriptionEntity |
| 20 | +import org.schabi.newpipe.extractor.ServiceList |
| 21 | +import org.schabi.newpipe.extractor.channel.ChannelInfo |
| 22 | +import org.schabi.newpipe.extractor.stream.StreamType |
| 23 | +import java.io.IOException |
| 24 | +import java.time.OffsetDateTime |
| 25 | +import kotlin.streams.toList |
| 26 | + |
| 27 | +class FeedDAOTest { |
| 28 | + private lateinit var db: AppDatabase |
| 29 | + private lateinit var feedDAO: FeedDAO |
| 30 | + private lateinit var streamDAO: StreamDAO |
| 31 | + private lateinit var subscriptionDAO: SubscriptionDAO |
| 32 | + |
| 33 | + private val serviceId = ServiceList.YouTube.serviceId |
| 34 | + |
| 35 | + private val stream1 = StreamEntity(1, serviceId, "https://youtube.com/watch?v=1", "stream 1", StreamType.VIDEO_STREAM, 1000, "channel-1", "https://youtube.com/channel/1", "https://i.ytimg.com/vi/1/hqdefault.jpg", 100, "2023-01-01", OffsetDateTime.parse("2023-01-01T00:00:00Z")) |
| 36 | + private val stream2 = StreamEntity(2, serviceId, "https://youtube.com/watch?v=2", "stream 2", StreamType.VIDEO_STREAM, 1000, "channel-1", "https://youtube.com/channel/1", "https://i.ytimg.com/vi/1/hqdefault.jpg", 100, "2023-01-02", OffsetDateTime.parse("2023-01-02T00:00:00Z")) |
| 37 | + private val stream3 = StreamEntity(3, serviceId, "https://youtube.com/watch?v=3", "stream 3", StreamType.LIVE_STREAM, 1000, "channel-1", "https://youtube.com/channel/1", "https://i.ytimg.com/vi/1/hqdefault.jpg", 100, "2023-01-03", OffsetDateTime.parse("2023-01-03T00:00:00Z")) |
| 38 | + private val stream4 = StreamEntity(4, serviceId, "https://youtube.com/watch?v=4", "stream 4", StreamType.VIDEO_STREAM, 1000, "channel-2", "https://youtube.com/channel/2", "https://i.ytimg.com/vi/1/hqdefault.jpg", 100, "2023-08-10", OffsetDateTime.parse("2023-08-10T00:00:00Z")) |
| 39 | + private val stream5 = StreamEntity(5, serviceId, "https://youtube.com/watch?v=5", "stream 5", StreamType.VIDEO_STREAM, 1000, "channel-2", "https://youtube.com/channel/2", "https://i.ytimg.com/vi/1/hqdefault.jpg", 100, "2023-08-20", OffsetDateTime.parse("2023-08-20T00:00:00Z")) |
| 40 | + private val stream6 = StreamEntity(6, serviceId, "https://youtube.com/watch?v=6", "stream 6", StreamType.VIDEO_STREAM, 1000, "channel-3", "https://youtube.com/channel/3", "https://i.ytimg.com/vi/1/hqdefault.jpg", 100, "2023-09-01", OffsetDateTime.parse("2023-09-01T00:00:00Z")) |
| 41 | + private val stream7 = StreamEntity(7, serviceId, "https://youtube.com/watch?v=7", "stream 7", StreamType.VIDEO_STREAM, 1000, "channel-4", "https://youtube.com/channel/4", "https://i.ytimg.com/vi/1/hqdefault.jpg", 100, "2023-08-10", OffsetDateTime.parse("2023-08-10T00:00:00Z")) |
| 42 | + |
| 43 | + private val allStreams = listOf( |
| 44 | + stream1, stream2, stream3, stream4, stream5, stream6, stream7 |
| 45 | + ) |
| 46 | + |
| 47 | + @Before |
| 48 | + fun createDb() { |
| 49 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 50 | + db = Room.inMemoryDatabaseBuilder( |
| 51 | + context, AppDatabase::class.java |
| 52 | + ).build() |
| 53 | + feedDAO = db.feedDAO() |
| 54 | + streamDAO = db.streamDAO() |
| 55 | + subscriptionDAO = db.subscriptionDAO() |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + @Throws(IOException::class) |
| 60 | + fun closeDb() { |
| 61 | + db.close() |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun testUnlinkStreamsOlderThan_KeepOne() { |
| 66 | + setupUnlinkDelete("2023-08-15T00:00:00Z") |
| 67 | + val streams = feedDAO.getStreams( |
| 68 | + FeedGroupEntity.GROUP_ALL_ID, includePlayed = true, includePartiallyPlayed = true, null |
| 69 | + ) |
| 70 | + .blockingGet() |
| 71 | + val allowedStreams = listOf(stream3, stream5, stream6, stream7) |
| 72 | + assertEqual(streams, allowedStreams) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun testUnlinkStreamsOlderThan_KeepMultiple() { |
| 77 | + setupUnlinkDelete("2023-08-01T00:00:00Z") |
| 78 | + val streams = feedDAO.getStreams( |
| 79 | + FeedGroupEntity.GROUP_ALL_ID, includePlayed = true, includePartiallyPlayed = true, null |
| 80 | + ) |
| 81 | + .blockingGet() |
| 82 | + val allowedStreams = listOf(stream3, stream4, stream5, stream6, stream7) |
| 83 | + assertEqual(streams, allowedStreams) |
| 84 | + } |
| 85 | + |
| 86 | + private fun assertEqual(streams: List<StreamWithState>?, allowedStreams: List<StreamEntity>) { |
| 87 | + assertNotNull(streams) |
| 88 | + assertEquals(allowedStreams, streams!!.stream().map { it.stream }.toList().sortedBy { it.uid }) |
| 89 | + } |
| 90 | + |
| 91 | + private fun setupUnlinkDelete(time: String) { |
| 92 | + clearAndFillTables() |
| 93 | + Single.fromCallable { |
| 94 | + feedDAO.unlinkStreamsOlderThan(OffsetDateTime.parse(time)) |
| 95 | + }.blockingSubscribe() |
| 96 | + Single.fromCallable { |
| 97 | + streamDAO.deleteOrphans() |
| 98 | + }.blockingSubscribe() |
| 99 | + } |
| 100 | + |
| 101 | + private fun clearAndFillTables() { |
| 102 | + db.clearAllTables() |
| 103 | + streamDAO.insertAll(allStreams) |
| 104 | + subscriptionDAO.insertAll( |
| 105 | + listOf( |
| 106 | + SubscriptionEntity.from(ChannelInfo(serviceId, "1", "https://youtube.com/channel/1", "https://youtube.com/channel/1", "channel-1")), |
| 107 | + SubscriptionEntity.from(ChannelInfo(serviceId, "2", "https://youtube.com/channel/2", "https://youtube.com/channel/2", "channel-2")), |
| 108 | + SubscriptionEntity.from(ChannelInfo(serviceId, "3", "https://youtube.com/channel/3", "https://youtube.com/channel/3", "channel-3")), |
| 109 | + SubscriptionEntity.from(ChannelInfo(serviceId, "4", "https://youtube.com/channel/4", "https://youtube.com/channel/4", "channel-4")), |
| 110 | + ) |
| 111 | + ) |
| 112 | + feedDAO.insertAll( |
| 113 | + listOf( |
| 114 | + FeedEntity(1, 1), |
| 115 | + FeedEntity(2, 1), |
| 116 | + FeedEntity(3, 1), |
| 117 | + FeedEntity(4, 2), |
| 118 | + FeedEntity(5, 2), |
| 119 | + FeedEntity(6, 3), |
| 120 | + FeedEntity(7, 4), |
| 121 | + ) |
| 122 | + ) |
| 123 | + } |
| 124 | +} |
0 commit comments