Skip to content

Commit

Permalink
Un-parameterize three time-sensitive tests
Browse files Browse the repository at this point in the history
This means the argument to the RetryTestRule is no longer in use, so remove it.
  • Loading branch information
amberin committed Mar 20, 2024
1 parent 0989c67 commit 13dc5e7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/src/androidTest/java/com/orgzly/android/RetryTestRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.junit.runners.model.Statement
/**
* Retry test rule used to retry test that failed. Retry failed test 3 times
*/
class RetryTestRule(val retryCount: Int = 3, val sleepBetweenAttemptsMs: Long = 1000) : TestRule {
class RetryTestRule(val retryCount: Int = 3) : TestRule {

private val TAG = RetryTestRule::class.java.simpleName

Expand All @@ -30,7 +30,7 @@ class RetryTestRule(val retryCount: Int = 3, val sleepBetweenAttemptsMs: Long =
} catch (t: Throwable) {
caughtThrowable = t
Log.e(TAG, description.displayName + ": run " + (i + 1) + " failed")
Thread.sleep(sleepBetweenAttemptsMs)
Thread.sleep(1000)
}
}

Expand Down
19 changes: 1 addition & 18 deletions app/src/androidTest/java/com/orgzly/android/query/QueryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class QueryTest(private val param: Parameter) : OrgzlyTest() {

@Rule
@JvmField
// Avoid sleeping between test attempts, as parameter creation vs test run
// timing is sensitive for many of these tests.
val mRetryTestRule: TestRule = RetryTestRule(3, 0)
val mRetryTestRule: TestRule = RetryTestRule(3)

data class Parameter(
val queryString: String,
Expand Down Expand Up @@ -194,16 +192,6 @@ class QueryTest(private val param: Parameter) : OrgzlyTest() {
expectedQueryString = "s.3d",
expectedSqlSelection = "((scheduled_is_active = 1 AND scheduled_time_timestamp != 0 AND scheduled_time_timestamp < " + TimeUtils.timeFromNow(Calendar.DAY_OF_MONTH, 3+1) + "))"
),
Parameter(
queryString = "s.le.2h",
expectedQueryString = "s.2h",
expectedSqlSelection = "((scheduled_is_active = 1 AND scheduled_time_timestamp != 0 AND scheduled_time_timestamp < " + TimeUtils.timeFromNow(Calendar.HOUR_OF_DAY, 2+1) + "))"
),
Parameter(
queryString = "s.le.+2h",
expectedQueryString = "s.2h",
expectedSqlSelection = "((scheduled_is_active = 1 AND scheduled_time_timestamp != 0 AND scheduled_time_timestamp < " + TimeUtils.timeFromNow(Calendar.HOUR_OF_DAY, 2+1) + "))"
),
Parameter(
queryString = "d.tom",
expectedQueryString = "d.tomorrow",
Expand All @@ -219,11 +207,6 @@ class QueryTest(private val param: Parameter) : OrgzlyTest() {
expectedQueryString = "c.ge.yesterday",
expectedSqlSelection = "((closed_time_timestamp != 0 AND ${TimeUtils.timeFromNow(Calendar.DAY_OF_MONTH, -1)} <= closed_time_timestamp))"
),
Parameter(
queryString = "c.gt.-1h",
expectedQueryString = "c.gt.-1h",
expectedSqlSelection = "((closed_time_timestamp != 0 AND ${TimeUtils.timeFromNow(Calendar.HOUR_OF_DAY, 0)} <= closed_time_timestamp))"
),
Parameter(
queryString = "p.a",
expectedQueryString = "p.a",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.orgzly.android.query

import androidx.test.espresso.matcher.ViewMatchers
import com.orgzly.android.OrgzlyTest
import com.orgzly.android.query.sql.SqliteQueryBuilder
import com.orgzly.android.query.user.DottedQueryParser
import org.hamcrest.Matchers
import org.junit.Test
import java.util.Calendar

/**
* Parameterization works poorly for these cases, because when there are many parameters,
* there may be a too long delay between parameter creation and test run, so that the
* timestamp is too old when the test is run.
*/
class TimeSensitiveQueryTest : OrgzlyTest() {
@Test
fun testScheduledWithinHours1() {
// Parse query
val queryString = "s.le.2h"
val expectedSqlSelection = "((scheduled_is_active = 1 AND scheduled_time_timestamp != 0 AND scheduled_time_timestamp < " + TimeUtils.timeFromNow(
Calendar.HOUR_OF_DAY, 2+1) + "))"
val parser = DottedQueryParser()
val query = parser.parse(queryString)

// Build SQL
val sqlBuilder = SqliteQueryBuilder(context)
val sqlQuery = sqlBuilder.build(query)

// Build query
val actualSqlSelection = sqlQuery.selection

expectedSqlSelection.let {
ViewMatchers.assertThat(
queryString,
actualSqlSelection,
Matchers.`is`(expectedSqlSelection)
)
}
}

@Test
fun testScheduledWithinHours2() {
// Parse query
val queryString = "s.le.+2h"
val expectedSqlSelection = "((scheduled_is_active = 1 AND scheduled_time_timestamp != 0 AND scheduled_time_timestamp < " + TimeUtils.timeFromNow(
Calendar.HOUR_OF_DAY, 2+1) + "))"
val parser = DottedQueryParser()
val query = parser.parse(queryString)

// Build SQL
val sqlBuilder = SqliteQueryBuilder(context)
val sqlQuery = sqlBuilder.build(query)

// Build query
val actualSqlSelection = sqlQuery.selection

expectedSqlSelection.let {
ViewMatchers.assertThat(
queryString,
actualSqlSelection,
Matchers.`is`(expectedSqlSelection)
)
}
}

@Test
fun testClosedRecently() {
// Parse query
val queryString = "c.gt.-1h"
val expectedSqlSelection = "((closed_time_timestamp != 0 AND ${TimeUtils.timeFromNow(Calendar.HOUR_OF_DAY, 0)} <= closed_time_timestamp))"
val parser = DottedQueryParser()
val query = parser.parse(queryString)

// Build SQL
val sqlBuilder = SqliteQueryBuilder(context)
val sqlQuery = sqlBuilder.build(query)

// Build query
val actualSqlSelection = sqlQuery.selection

expectedSqlSelection.let {
ViewMatchers.assertThat(
queryString,
actualSqlSelection,
Matchers.`is`(expectedSqlSelection)
)
}
}
}

0 comments on commit 13dc5e7

Please sign in to comment.