parent
6643d33480
commit
4209ab8c2c
@ -1,24 +0,0 @@ |
||||
package net.pokeranalytics.android |
||||
|
||||
import androidx.test.InstrumentationRegistry |
||||
import androidx.test.runner.AndroidJUnit4 |
||||
|
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
|
||||
import org.junit.Assert.* |
||||
|
||||
/** |
||||
* Instrumented test, which will execute on an Android device. |
||||
* |
||||
* See [testing documentation](http://d.android.com/tools/testing). |
||||
*/ |
||||
@RunWith(AndroidJUnit4::class) |
||||
class ExampleInstrumentedTest { |
||||
@Test |
||||
fun useAppContext() { |
||||
// Context of the app under test. |
||||
val appContext = InstrumentationRegistry.getTargetContext() |
||||
assertEquals("net.pokeranalytics.android", appContext.packageName) |
||||
} |
||||
} |
||||
@ -0,0 +1,106 @@ |
||||
package net.pokeranalytics.android |
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4 |
||||
import net.pokeranalytics.android.calculus.Calculator |
||||
import net.pokeranalytics.android.calculus.ComputedResults |
||||
import net.pokeranalytics.android.calculus.SessionGroup |
||||
import net.pokeranalytics.android.calculus.Stat |
||||
import net.pokeranalytics.android.model.realm.Session |
||||
import net.pokeranalytics.android.model.realm.TimeFrame |
||||
import org.junit.Assert |
||||
import org.junit.Assert.assertEquals |
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
import java.text.SimpleDateFormat |
||||
import java.util.* |
||||
|
||||
/** |
||||
* Instrumented test, which will execute on an Android device. |
||||
* |
||||
* See [testing documentation](http://d.android.com/tools/testing). |
||||
*/ |
||||
@RunWith(AndroidJUnit4::class) |
||||
|
||||
class ExampleInstrumentedUnitTest : RealmInstrumentedUnitTest() { |
||||
|
||||
// convenience extension |
||||
fun Session.Companion.testInstance(netResult: Double, startDate: Date, endDate: Date?): Session { |
||||
var session: Session = Session.newInstance() |
||||
session.result?.netResult = netResult |
||||
session.timeFrame?.setDate(startDate, endDate) |
||||
return session |
||||
} |
||||
|
||||
@Test |
||||
fun testSessionStats() { |
||||
|
||||
val realm = this.mockRealm |
||||
realm.beginTransaction() |
||||
|
||||
val sdf = SimpleDateFormat("dd/M/yyyy hh:mm") |
||||
|
||||
val sd1 = sdf.parse("01/1/2019 10:00") |
||||
val ed1 = sdf.parse("01/1/2019 11:00") |
||||
val sd2 = sdf.parse("02/1/2019 08:00") |
||||
val ed2 = sdf.parse("02/1/2019 11:00") |
||||
|
||||
var s1 = realm.createObject(Session::class.java, "1") |
||||
var s2 = realm.createObject(Session::class.java, "2") |
||||
|
||||
s1.timeFrame = realm.createObject(TimeFrame::class.java) |
||||
s2.timeFrame = realm.createObject(TimeFrame::class.java) |
||||
|
||||
s1.result = realm.createObject(net.pokeranalytics.android.model.realm.Result::class.java) |
||||
s2.result = realm.createObject(net.pokeranalytics.android.model.realm.Result::class.java) |
||||
|
||||
// var s1: Session = Session.newInstance() |
||||
// var s2: Session = Session.newInstance() |
||||
|
||||
s1.result?.netResult = -100.0 |
||||
s2.result?.netResult = 300.0 |
||||
|
||||
realm.insert(s1) |
||||
realm.insert(s2) |
||||
realm.commitTransaction() |
||||
|
||||
realm.beginTransaction() |
||||
s1.timeFrame?.setDate(sd1, ed1) |
||||
s2.timeFrame?.setDate(sd2, ed2) |
||||
|
||||
realm.copyToRealmOrUpdate(s1) |
||||
realm.copyToRealmOrUpdate(s2) |
||||
|
||||
realm.commitTransaction() |
||||
|
||||
val sessions = realm.where(Session::class.java).findAll() |
||||
val group = SessionGroup(name = "test", sessions = sessions) |
||||
|
||||
val results: ComputedResults = Calculator.compute(group, Calculator.Options()) |
||||
val delta = 0.01 |
||||
|
||||
val sum = results.computedStat(Stat.NETRESULT) |
||||
if (sum != null) { |
||||
assertEquals(200.0, sum.value, delta) |
||||
} else { |
||||
Assert.fail("No Net result stat") |
||||
} |
||||
|
||||
val average = results.computedStat(Stat.AVERAGE) |
||||
if (average != null) { |
||||
assertEquals(100.0, average.value, delta) |
||||
} else { |
||||
Assert.fail("No AVERAGE stat") |
||||
} |
||||
|
||||
val duration = results.computedStat(Stat.DURATION) |
||||
if (duration != null) { |
||||
assertEquals(4.0, duration.value, delta) |
||||
} else { |
||||
Assert.fail("No duration stat") |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,26 @@ |
||||
package net.pokeranalytics.android |
||||
|
||||
import io.realm.Realm |
||||
import io.realm.RealmConfiguration |
||||
import org.junit.After |
||||
import org.junit.Before |
||||
|
||||
open class RealmInstrumentedUnitTest { |
||||
|
||||
lateinit var mockRealm: Realm |
||||
|
||||
@Before |
||||
fun setup() { |
||||
|
||||
val testConfig = RealmConfiguration.Builder().inMemory().name("test-realm").build() |
||||
Realm.setDefaultConfiguration(testConfig) |
||||
mockRealm = Realm.getDefaultInstance() |
||||
} |
||||
|
||||
@After |
||||
@Throws(Exception::class) |
||||
public fun tearDown() { |
||||
mockRealm.close() |
||||
} |
||||
|
||||
} |
||||
@ -1,22 +1,25 @@ |
||||
package net.pokeranalytics.android |
||||
|
||||
//import androidx.test.core.app.ApplicationProvider |
||||
import io.realm.Realm |
||||
import io.realm.RealmConfiguration |
||||
import org.junit.After |
||||
import org.junit.Before |
||||
|
||||
open class RealmUnitTest { |
||||
|
||||
companion object { |
||||
lateinit var mockRealm: Realm |
||||
|
||||
fun realmInstance() : Realm { |
||||
|
||||
// Application |
||||
|
||||
// Realm.init(ApplicationProvider.getApplicationContext<PokerAnalyticsApplication>()) |
||||
@Before |
||||
fun setup() { |
||||
val testConfig = RealmConfiguration.Builder().inMemory().name("test-realm").build() |
||||
return Realm.getInstance(testConfig) |
||||
Realm.setDefaultConfiguration(testConfig) |
||||
mockRealm = Realm.getDefaultInstance() |
||||
} |
||||
|
||||
@After |
||||
@Throws(Exception::class) |
||||
public fun tearDown() { |
||||
mockRealm.close() |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue