package net.pokeranalytics.android.unitTests

import net.pokeranalytics.android.components.RealmInstrumentedUnitTest
import net.pokeranalytics.android.model.realm.Bankroll
import net.pokeranalytics.android.model.realm.Currency
import net.pokeranalytics.android.util.extensions.findById
import org.junit.Assert
import org.junit.Test

class DeleteInstrumentedUnitTest : RealmInstrumentedUnitTest() {

    @Test
    fun testDeleteValidation() {

        val realm = this.mockRealm
        realm.beginTransaction()

        val s1 = newSessionInstance(realm)
        val s2 = newSessionInstance(realm)

        val br1 = realm.createObject(Bankroll::class.java, "1")
        br1.live = false
        val br2 = realm.createObject(Bankroll::class.java, "2")
        br2.live = false

        val c1 = realm.createObject(Currency::class.java, "1")
        val c2 = realm.createObject(Currency::class.java, "2")
        c1.rate = 0.1
        c2.rate = 2.0
        br1.currency = c1
        br2.currency = c2

        s1.bankroll = br1
        s2.bankroll = br2

        s1.result?.netResult = 100.0
        s2.result?.netResult = 200.0

        realm.commitTransaction()

        var isValidForDelete = br1.isValidForDelete(realm)
        Assert.assertEquals(false, isValidForDelete)

        realm.findById(Bankroll::class.java, "1")?.let {
            isValidForDelete = it.isValidForDelete(realm)
            Assert.assertEquals(false, isValidForDelete)

            isValidForDelete = realm.copyFromRealm(it).isValidForDelete(realm)
            Assert.assertEquals(false, isValidForDelete)
        }
    }

}