|
| 1 | +package io.sentry.android.sqlite |
| 2 | + |
| 3 | +import android.database.CrossProcessCursor |
| 4 | +import io.sentry.IHub |
| 5 | +import io.sentry.ISpan |
| 6 | +import io.sentry.SentryOptions |
| 7 | +import io.sentry.SentryTracer |
| 8 | +import io.sentry.SpanStatus |
| 9 | +import io.sentry.TransactionContext |
| 10 | +import org.mockito.kotlin.any |
| 11 | +import org.mockito.kotlin.eq |
| 12 | +import org.mockito.kotlin.mock |
| 13 | +import org.mockito.kotlin.verify |
| 14 | +import org.mockito.kotlin.whenever |
| 15 | +import kotlin.test.Test |
| 16 | +import kotlin.test.assertEquals |
| 17 | +import kotlin.test.assertNotNull |
| 18 | +import kotlin.test.assertTrue |
| 19 | + |
| 20 | +class SentryCrossProcessCursorTest { |
| 21 | + private class Fixture { |
| 22 | + private val hub = mock<IHub>() |
| 23 | + private val spanManager = SQLiteSpanManager(hub) |
| 24 | + val mockCursor = mock<CrossProcessCursor>() |
| 25 | + lateinit var options: SentryOptions |
| 26 | + lateinit var sentryTracer: SentryTracer |
| 27 | + |
| 28 | + fun getSut(sql: String, isSpanActive: Boolean = true): SentryCrossProcessCursor { |
| 29 | + options = SentryOptions().apply { |
| 30 | + dsn = "https://[email protected]/proj" |
| 31 | + } |
| 32 | + whenever(hub.options).thenReturn(options) |
| 33 | + sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) |
| 34 | + |
| 35 | + if (isSpanActive) { |
| 36 | + whenever(hub.span).thenReturn(sentryTracer) |
| 37 | + } |
| 38 | + return SentryCrossProcessCursor(mockCursor, spanManager, sql) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private val fixture = Fixture() |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `all calls are propagated to the delegate`() { |
| 46 | + val sql = "sql" |
| 47 | + val cursor = fixture.getSut(sql) |
| 48 | + |
| 49 | + cursor.onMove(0, 1) |
| 50 | + verify(fixture.mockCursor).onMove(eq(0), eq(1)) |
| 51 | + |
| 52 | + cursor.count |
| 53 | + verify(fixture.mockCursor).count |
| 54 | + |
| 55 | + cursor.fillWindow(0, mock()) |
| 56 | + verify(fixture.mockCursor).fillWindow(eq(0), any()) |
| 57 | + |
| 58 | + // Let's verify other methods are delegated, even if not explicitly |
| 59 | + cursor.close() |
| 60 | + verify(fixture.mockCursor).close() |
| 61 | + |
| 62 | + cursor.getString(1) |
| 63 | + verify(fixture.mockCursor).getString(eq(1)) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `getCount creates a span if a span is running`() { |
| 68 | + val sql = "execute" |
| 69 | + val sut = fixture.getSut(sql) |
| 70 | + assertEquals(0, fixture.sentryTracer.children.size) |
| 71 | + sut.count |
| 72 | + val span = fixture.sentryTracer.children.firstOrNull() |
| 73 | + assertSqlSpanCreated(sql, span) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `getCount does not create a span if no span is running`() { |
| 78 | + val sut = fixture.getSut("execute", isSpanActive = false) |
| 79 | + sut.count |
| 80 | + assertEquals(0, fixture.sentryTracer.children.size) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun `onMove creates a span if a span is running`() { |
| 85 | + val sql = "execute" |
| 86 | + val sut = fixture.getSut(sql) |
| 87 | + assertEquals(0, fixture.sentryTracer.children.size) |
| 88 | + sut.onMove(0, 5) |
| 89 | + val span = fixture.sentryTracer.children.firstOrNull() |
| 90 | + assertSqlSpanCreated(sql, span) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `onMove does not create a span if no span is running`() { |
| 95 | + val sut = fixture.getSut("execute", isSpanActive = false) |
| 96 | + sut.onMove(0, 5) |
| 97 | + assertEquals(0, fixture.sentryTracer.children.size) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `fillWindow creates a span if a span is running`() { |
| 102 | + val sql = "execute" |
| 103 | + val sut = fixture.getSut(sql) |
| 104 | + assertEquals(0, fixture.sentryTracer.children.size) |
| 105 | + sut.fillWindow(0, mock()) |
| 106 | + val span = fixture.sentryTracer.children.firstOrNull() |
| 107 | + assertSqlSpanCreated(sql, span) |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `fillWindow does not create a span if no span is running`() { |
| 112 | + val sut = fixture.getSut("execute", isSpanActive = false) |
| 113 | + sut.fillWindow(0, mock()) |
| 114 | + assertEquals(0, fixture.sentryTracer.children.size) |
| 115 | + } |
| 116 | + |
| 117 | + private fun assertSqlSpanCreated(sql: String, span: ISpan?) { |
| 118 | + assertNotNull(span) |
| 119 | + assertEquals("db.sql.query", span.operation) |
| 120 | + assertEquals(sql, span.description) |
| 121 | + assertEquals(SpanStatus.OK, span.status) |
| 122 | + assertTrue(span.isFinished) |
| 123 | + } |
| 124 | +} |
0 commit comments