Skip to content

Commit 37362db

Browse files
authored
Fix event frequency condition to sum counts instead of counting buckets (#74)
1 parent 30fb3e2 commit 37362db

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

  • nubrick/src
    • androidTest/kotlin/app/nubrick/nubrick/data/database
    • main/kotlin/app/nubrick/nubrick/data/database
Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
package app.nubrick.nubrick.data.database
22

3+
import android.database.sqlite.SQLiteDatabase
4+
import app.nubrick.nubrick.schema.ConditionOperator
5+
import app.nubrick.nubrick.schema.FrequencyUnit
6+
import app.nubrick.nubrick.schema.UserEventFrequencyCondition
37
import org.junit.Assert
8+
import org.junit.After
9+
import org.junit.Before
410
import org.junit.Test
511

6-
class ExampleUnitTest {
12+
class DatabaseRepositoryAndroidTest {
13+
private lateinit var db: SQLiteDatabase
14+
private lateinit var repository: DatabaseRepositoryImpl
15+
16+
@Before
17+
fun setUp() {
18+
db = SQLiteDatabase.create(null)
19+
db.execSQL(SQL_CREATE_EXPERIMENT_HISTORY_TABLE)
20+
db.execSQL(SQL_CREATE_USER_EVENT_TABLE)
21+
repository = DatabaseRepositoryImpl(db)
22+
}
23+
24+
@After
25+
fun tearDown() {
26+
db.close()
27+
}
28+
729
@Test
8-
fun addition_isCorrect() {
9-
Assert.assertEquals(4, 2 + 2)
30+
fun eventFrequencyConditionCountsEventsInSameBucket() {
31+
repository.appendUserEvent("purchase")
32+
repository.appendUserEvent("purchase")
33+
34+
val matched = repository.isMatchedToUserEventFrequencyCondition(
35+
UserEventFrequencyCondition(
36+
eventName = "purchase",
37+
lookbackPeriod = 1,
38+
unit = FrequencyUnit.DAY,
39+
comparison = ConditionOperator.GreaterThanOrEqual,
40+
threshold = 2,
41+
)
42+
)
43+
44+
Assert.assertTrue(matched)
1045
}
1146
}

nubrick/src/main/kotlin/app/nubrick/nubrick/data/database/database.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal class DatabaseRepositoryImpl(private val db: SQLiteDatabase): DatabaseR
7878
lookbackPeriod = condition.lookbackPeriod,
7979
since = condition.since
8080
)
81-
val total = counts.values.count()
81+
val total = counts.values.sum()
8282
return compareInteger(total, listOf(threshold), comparison)
8383
}
84-
}
84+
}

0 commit comments

Comments
 (0)