package io.explod.testable.data.local;

import android.annotation.SuppressLint;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import java.util.concurrent.atomic.AtomicInteger;

import io.reactivex.Observable;
import meta.BaseRoboTest;
import meta.rx.ImmediateSchedulerRule;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;

@SuppressLint("DefaultLocale")
@RunWith(RobolectricTestRunner.class)
public class AppLocalRepoTest extends BaseRoboTest {

	@Rule
	public ImmediateSchedulerRule rule = new ImmediateSchedulerRule();

	AppLocalRepo repo;

	@Before
	public void createRepo() {
		repo = new AppLocalRepo();
	}

	@Test
	public void insertAndGetGithubRepositories() throws Exception {
		AtomicInteger count = new AtomicInteger(0);

		repo
			.Users.insert("explodes")
			.flatMap(user -> repo.Repositories.insert(user.getId(), "test-repo1-" + user).map(x -> user))
			.flatMap(user -> repo.Repositories.insert(user.getId(), "test-repo2-" + user).map(x -> user))
			.flatMapObservable(user -> repo.Repositories.getAll())
			.subscribe(repo -> count.addAndGet(1), e -> fail(e.toString()));

		assertEquals(2, count.get());
	}

	@Test
	public void insertAndGetGithubUsers() throws Exception {
		AtomicInteger count = new AtomicInteger(0);

		Observable.merge(repo.Users.insert("abc").toObservable(), repo.Users.insert("123").toObservable()).subscribe(x -> {
		}, e -> fail(e.toString()));
		// subscribe in separate observable to make this test slightly different than the
		// equivalent github repository test
		repo.Users.getAll().subscribe(repo -> count.addAndGet(1), e -> fail(e.toString()));

		assertEquals(2, count.get());
	}

}
@@ -74,16 +74,40 @@
 sLint;%0A%0A
+import org.junit.After;%0A
 import o
@@ -150,31 +150,8 @@
 le;%0A
-import org.junit.Test;%0A
 impo
@@ -229,250 +229,115 @@
 er;%0A
-%0A
 import 
-java.util.concurrent.atomic.AtomicInteger;%0A%0Aimport io.reactivex.Observable;%0Aimport meta.BaseRoboTest;%0Aimport meta.rx.ImmediateSchedulerRule;%0A%0Aimport static junit.framework.Assert.assertEquals;%0Aimport static junit.framework.Assert.fail
+org.robolectric.RuntimeEnvironment;%0A%0Aimport meta.BaseRoboTest;%0Aimport meta.rx.ImmediateSchedulerRule
 ;%0A%0A@
@@ -417,19 +417,16 @@
 c class 
-App
 LocalRep
@@ -536,25 +536,24 @@
 ;%0A%0A%09
-App
 Local
-Repo repo
+Database db
 ;%0A%0A%09
@@ -594,544 +594,61 @@
 %7B%0A%09%09
-repo
+db
  = new 
-App
 Local
-Repo();%0A%09%7D%0A%0A%09@Test%0A%09public void insertAndGetGithubRepositories() throws Exception %7B%0A%09%09AtomicInteger count = new AtomicInteger(0);%0A%0A%09%09repo%0A%09%09%09.Users.insert(%22explodes%22)%0A%09%09%09.flatMap(user -%3E repo.Repositories.insert(user.getId(), %22test-repo1-%22 + user).map(x -%3E user))%0A%09%09%09.flatMap(user -%3E repo.Repositories.insert(user.getId(), %22test-repo2-%22 + user).map(x -%3E user))%0A%09%09%09.flatMapObservable(user -%3E repo.Repositories.getAll())%0A%09%09%09.subscribe(repo -%3E count.addAndGet(1), e -%3E fail(e.toString()));%0A%0A%09%09assertEquals(2, count.get()
+Database(RuntimeEnvironment.application
 );%0A%09
@@ -652,20 +652,21 @@
 ;%0A%09%7D%0A%0A%09@
-Test
+After
 %0A%09public
@@ -675,488 +675,37 @@
 oid 
-insertAndGetGithubUsers() throws Exception %7B%0A%09%09AtomicInteger count = new AtomicInteger(0);%0A%0A%09%09Observable.merge(repo.Users.insert(%22abc%22).toObservable(), repo.Users.insert(%22123%22).toObservable()).subscribe(x -%3E %7B%0A%09%09%7D, e -%3E fail(e.toString()));%0A%09%09// subscribe in separate observable to make this test slightly different than the%0A%09%09// equivalent github repository test%0A%09%09repo.Users.getAll().subscribe(repo -%3E count.addAndGet(1), e -%3E fail(e.toString()));%0A%0A%09%09assertEquals(2, count.get()
+closeDatabase() %7B%0A%09%09db.close(
 );%0A%09

remove tests for code that was wiped out during refactor