-
-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathSentrySQLiteDriverTest.kt
More file actions
156 lines (128 loc) · 5.31 KB
/
Copy pathSentrySQLiteDriverTest.kt
File metadata and controls
156 lines (128 loc) · 5.31 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
package io.sentry.sqlite
import androidx.sqlite.SQLiteConnection
import androidx.sqlite.SQLiteDriver
import androidx.sqlite.SQLiteStatement
import androidx.sqlite.driver.SupportSQLiteDriver
import io.sentry.IScopes
import io.sentry.Sentry
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.SentryOptions
import io.sentry.SentryTracer
import io.sentry.SpanDataConvention
import io.sentry.TransactionContext
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import kotlin.test.assertSame
import kotlin.test.assertTrue
import org.junit.Before
import org.mockito.Mockito
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
class SentrySQLiteDriverTest {
private class Fixture {
val mockDriver = mock<SQLiteDriver>()
val mockConnection = mock<SQLiteConnection>()
fun getSut(fileName: String): SentrySQLiteDriver {
whenever(mockDriver.open(fileName)).thenReturn(mockConnection)
return SentrySQLiteDriver.create(mockDriver) as SentrySQLiteDriver
}
}
private val fixture = Fixture()
@Before
fun setup() {
SentryIntegrationPackageStorage.getInstance().clearStorage()
}
@Test
fun `create registers SQLiteDriver integration`() {
assertFalse(SentryIntegrationPackageStorage.getInstance().integrations.contains("SQLiteDriver"))
SentrySQLiteDriver.create(fixture.mockDriver)
assertTrue(SentryIntegrationPackageStorage.getInstance().integrations.contains("SQLiteDriver"))
}
@Test
fun `create with non-wrapped driver returns SentrySQLiteDriver`() {
val result = SentrySQLiteDriver.create(fixture.mockDriver)
assertIs<SentrySQLiteDriver>(result)
}
@Test
fun `create with already-wrapped driver returns same instance without re-wrapping`() {
val wrapped = SentrySQLiteDriver.create(fixture.mockDriver)
val doubleWrapped = SentrySQLiteDriver.create(wrapped)
assertSame(wrapped, doubleWrapped)
}
@Test
fun `create with SupportSQLiteDriver bridge returns same instance without wrapping`() {
val bridge = SupportSQLiteDriver()
val result = SentrySQLiteDriver.create(bridge)
assertSame(bridge, result)
assertFalse(result is SentrySQLiteDriver)
}
@Test
fun `hasConnectionPool forwards delegate value when supported`() {
whenever(fixture.mockDriver.hasConnectionPool).thenReturn(true)
val sut = SentrySQLiteDriver.create(fixture.mockDriver) as SentrySQLiteDriver
assertTrue(sut.hasConnectionPool)
}
@Test
fun `hasConnectionPool returns false when delegate throws LinkageError`() {
whenever(fixture.mockDriver.hasConnectionPool).thenThrow(AbstractMethodError())
val sut = SentrySQLiteDriver.create(fixture.mockDriver) as SentrySQLiteDriver
assertFalse(sut.hasConnectionPool)
}
@Test
fun `hasConnectionPool does not catch non-LinkageErrors`() {
whenever(fixture.mockDriver.hasConnectionPool).thenThrow(IllegalStateException())
val sut = SentrySQLiteDriver.create(fixture.mockDriver) as SentrySQLiteDriver
assertFailsWith<IllegalStateException> { sut.hasConnectionPool }
}
@Test
fun `open returns SentrySQLiteConnection wrapping delegate if wrapping succeeds`() {
val driver = fixture.getSut("myapp.db")
val connection = driver.open("myapp.db")
assertIs<SentrySQLiteConnection>(connection)
}
@Test
fun `open returns the unwrapped delegate if wrapping fails`() {
val brokenScopes = mock<IScopes>()
val validOptions = SentryOptions().apply { dsn = "https://key@sentry.io/proj" }
whenever(brokenScopes.options)
.thenThrow(RuntimeException("Sentry options unavailable"))
.thenReturn(validOptions)
Mockito.mockStatic(Sentry::class.java).use { mockedSentry ->
mockedSentry.`when`<Any> { Sentry.getCurrentScopes() }.thenReturn(brokenScopes)
val driver = fixture.getSut("myapp.db")
val result = driver.open("myapp.db")
assertSame(fixture.mockConnection, result)
verify(fixture.mockDriver).open("myapp.db")
}
}
// Smoke test ensuring all layers are properly wired up.
@Test
fun `full stack produces a span with correct metadata`() {
val scopes = mock<IScopes>()
val options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" }
whenever(scopes.options).thenReturn(options)
val tracer = SentryTracer(TransactionContext("name", "op"), scopes)
whenever(scopes.span).thenReturn(tracer)
val mockStatement = mock<SQLiteStatement>()
whenever(fixture.mockConnection.prepare("SELECT * FROM users")).thenReturn(mockStatement)
whenever(mockStatement.step()).thenReturn(true, false)
Mockito.mockStatic(Sentry::class.java).use { mockedSentry ->
mockedSentry.`when`<Any> { Sentry.getCurrentScopes() }.thenReturn(scopes)
val driver = fixture.getSut("/data/data/com.example/databases/myapp.db")
val connection = driver.open("/data/data/com.example/databases/myapp.db")
val statement = connection.prepare("SELECT * FROM users")
assertIs<SentrySQLiteConnection>(connection)
assertIs<SentrySQLiteStatement>(statement)
statement.step()
statement.step()
val span = tracer.children.firstOrNull()
assertNotNull(span)
assertEquals("myapp.db", span.data[SpanDataConvention.DB_NAME_KEY])
}
}
}