package net.pokeranalytics.android

import io.realm.Realm
import io.realm.RealmConfiguration
import net.pokeranalytics.android.model.realm.ComputableResult
import net.pokeranalytics.android.model.realm.Result
import net.pokeranalytics.android.model.realm.Session
import org.junit.After
import org.junit.Before
import java.util.*


open class RealmInstrumentedUnitTest {

    lateinit var mockRealm: Realm

    companion object {

        fun newSessionInstance(realm: Realm) : Session {
            val session = realm.createObject(Session::class.java, UUID.randomUUID().toString())
            val computableResult = realm.createObject(ComputableResult::class.java)
            computableResult.session = session
            session.result = realm.createObject(Result::class.java)
            return session
        }

    }

    @Before
    fun setup() {

        val testConfig = RealmConfiguration.Builder().inMemory().name("test-realm").build()
        Realm.setDefaultConfiguration(testConfig)
        this.mockRealm = Realm.getDefaultInstance()

        this.mockRealm.executeTransaction {
            this.mockRealm.deleteAll()
        }

    }

    @After
    @Throws(Exception::class)
    public fun tearDown() {
        this.mockRealm.close()
    }

}