aboutsummaryrefslogtreecommitdiff
path: root/app/src/androidTest/kotlin/com/pitchedapps/frost/db/NotificationDbTest.kt
blob: 9167c7bfc0e6566a38b2bed5bae7d1563a36ae04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright 2019 Allan Wang
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.db

import com.pitchedapps.frost.services.NOTIF_CHANNEL_GENERAL
import com.pitchedapps.frost.services.NOTIF_CHANNEL_MESSAGES
import com.pitchedapps.frost.services.NotificationContent
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class NotificationDbTest : BaseDbTest() {

    private val dao get() = db.notifDao()

    private fun cookie(id: Long) = CookieEntity(id, "name$id", "cookie$id")

    private fun notifContent(id: Long, cookie: CookieEntity, time: Long = id) = NotificationContent(
        data = cookie,
        id = id,
        href = "",
        title = null,
        text = "",
        timestamp = time,
        profileUrl = null
    )

    @Test
    fun saveAndRetrieve() {
        val cookie = cookie(12345L)
        // Unique unsorted ids
        val notifs = listOf(0L, 4L, 2L, 6L, 99L, 3L).map { notifContent(it, cookie) }
        runBlocking {
            db.cookieDao().save(cookie)
            dao.saveNotifications(NOTIF_CHANNEL_GENERAL, notifs)
            val dbNotifs = dao.selectNotifications(cookie.id, NOTIF_CHANNEL_GENERAL)
            assertEquals(notifs.sortedByDescending { it.timestamp }, dbNotifs, "Incorrect notification list received")
        }
    }

    @Test
    fun selectConditions() {
        runBlocking {
            val cookie1 = cookie(12345L)
            val cookie2 = cookie(12L)
            val notifs1 = (0L..2L).map { notifContent(it, cookie1) }
            val notifs2 = (5L..10L).map { notifContent(it, cookie2) }
            db.cookieDao().save(cookie1)
            db.cookieDao().save(cookie2)
            dao.saveNotifications(NOTIF_CHANNEL_GENERAL, notifs1)
            dao.saveNotifications(NOTIF_CHANNEL_MESSAGES, notifs2)
            assertEquals(
                emptyList(),
                dao.selectNotifications(cookie1.id, NOTIF_CHANNEL_MESSAGES),
                "Filtering by type did not work for cookie1"
            )
            assertEquals(
                notifs1.sortedByDescending { it.timestamp },
                dao.selectNotifications(cookie1.id, NOTIF_CHANNEL_GENERAL),
                "Selection for cookie1 failed"
            )
            assertEquals(
                emptyList(),
                dao.selectNotifications(cookie2.id, NOTIF_CHANNEL_GENERAL),
                "Filtering by type did not work for cookie2"
            )
            assertEquals(
                notifs2.sortedByDescending { it.timestamp },
                dao.selectNotifications(cookie2.id, NOTIF_CHANNEL_MESSAGES),
                "Selection for cookie2 failed"
            )
        }
    }

    /**
     * Primary key is both id and userId, in the event that the same notification to multiple users has the same id
     */
    @Test
    fun primaryKeyCheck() {
        runBlocking {
            val cookie1 = cookie(12345L)
            val cookie2 = cookie(12L)
            val notifs1 = (0L..2L).map { notifContent(it, cookie1) }
            val notifs2 = notifs1.map { it.copy(data = cookie2) }
            db.cookieDao().save(cookie1)
            db.cookieDao().save(cookie2)
            assertTrue(dao.saveNotifications(NOTIF_CHANNEL_GENERAL, notifs1), "Notif1 save failed")
            assertTrue(dao.saveNotifications(NOTIF_CHANNEL_GENERAL, notifs2), "Notif2 save failed")
        }
    }

    @Test
    fun cascadeDeletion() {
        val cookie = cookie(12345L)
        // Unique unsorted ids
        val notifs = listOf(0L, 4L, 2L, 6L, 99L, 3L).map { notifContent(it, cookie) }
        runBlocking {
            db.cookieDao().save(cookie)
            dao.saveNotifications(NOTIF_CHANNEL_GENERAL, notifs)
            db.cookieDao().deleteById(cookie.id)
            val dbNotifs = dao.selectNotifications(cookie.id, NOTIF_CHANNEL_GENERAL)
            assertTrue(dbNotifs.isEmpty(), "Cascade deletion failed")
        }
    }

    @Test
    fun latestEpoch() {
        val cookie = cookie(12345L)
        // Unique unsorted ids
        val notifs = listOf(0L, 4L, 2L, 6L, 99L, 3L).map { notifContent(it, cookie) }
        runBlocking {
            assertEquals(-1L, dao.latestEpoch(cookie.id, NOTIF_CHANNEL_GENERAL), "Default epoch failed")
            db.cookieDao().save(cookie)
            dao.saveNotifications(NOTIF_CHANNEL_GENERAL, notifs)
            assertEquals(99L, dao.latestEpoch(cookie.id, NOTIF_CHANNEL_GENERAL), "Latest epoch failed")
        }
    }

    @Test
    fun insertionWithInvalidCookies() {
        runBlocking {
            assertFalse(
                dao.saveNotifications(NOTIF_CHANNEL_GENERAL, listOf(notifContent(1L, cookie(2L)))),
                "Notif save should not have passed without relevant cookie entries"
            )
        }
    }
}