From fee88d7e27ecf112d8d4ed23995c08db6d760fe6 Mon Sep 17 00:00:00 2001 From: "FUJI Goro (gfx)" Date: Mon, 22 Feb 2016 09:07:53 +0900 Subject: [PATCH 1/4] use direct associations with Orma v2 --- app/build.gradle | 4 +- .../confsched/activity/SearchActivity.java | 4 +- .../droidkaigi/confsched/dao/SessionDao.java | 28 ++++++-------- .../confsched/fragment/SessionsFragment.java | 4 +- .../droidkaigi/confsched/model/Category.java | 5 ++- .../confsched/model/Contributor.java | 7 ++-- .../droidkaigi/confsched/model/Place.java | 5 ++- .../droidkaigi/confsched/model/Session.java | 37 +++++-------------- .../droidkaigi/confsched/model/Speaker.java | 7 +++- 9 files changed, 44 insertions(+), 57 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9ef8c38..b8d5330 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -146,8 +146,8 @@ dependencies { compile "org.parceler:parceler-api:1.0.4" apt "org.parceler:parceler:1.0.4" - apt 'com.github.gfx.android.orma:orma-processor:1.3.0' - compile 'com.github.gfx.android.orma:orma:1.3.0' + apt 'com.github.gfx.android.orma:orma-processor:2.0.5' + compile 'com.github.gfx.android.orma:orma:2.0.5' compile 'com.github.hotchemi:permissionsdispatcher:2.0.6' apt 'com.github.hotchemi:permissionsdispatcher-processor:2.0.6' diff --git a/app/src/main/java/io/github/droidkaigi/confsched/activity/SearchActivity.java b/app/src/main/java/io/github/droidkaigi/confsched/activity/SearchActivity.java index cbe771d..39bff82 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/activity/SearchActivity.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/activity/SearchActivity.java @@ -1,5 +1,7 @@ package io.github.droidkaigi.confsched.activity; +import org.parceler.Parcels; + import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -26,8 +28,6 @@ import android.widget.Filterable; import android.widget.TextView; -import org.parceler.Parcels; - import java.util.ArrayList; import java.util.List; diff --git a/app/src/main/java/io/github/droidkaigi/confsched/dao/SessionDao.java b/app/src/main/java/io/github/droidkaigi/confsched/dao/SessionDao.java index 8eea629..68be016 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/dao/SessionDao.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/dao/SessionDao.java @@ -1,9 +1,9 @@ package io.github.droidkaigi.confsched.dao; -import android.support.annotation.NonNull; - import com.github.gfx.android.orma.TransactionTask; +import android.support.annotation.NonNull; + import java.util.List; import javax.inject.Inject; @@ -52,7 +52,6 @@ public void insertAll(@NonNull List sessions) { @Override public void execute() throws Exception { for (Session session : sessions) { - session.prepareSave(); insertSpeaker(session.speaker); insertCategory(session.category); insertPlace(session.place); @@ -64,44 +63,40 @@ public void execute() throws Exception { } private void insertSpeaker(Speaker speaker) { - if (speaker != null && speakerRelation().selector().idEq(speaker.id).count() == 0) { + if (speaker != null && speakerRelation().selector().idEq(speaker.id).isEmpty()) { speakerRelation().inserter().execute(speaker); } } private void insertPlace(Place place) { - if (place != null && placeRelation().selector().idEq(place.id).count() == 0) { + if (place != null && placeRelation().selector().idEq(place.id).isEmpty()) { placeRelation().inserter().execute(place); } } private void insertCategory(Category category) { - if (category != null && categoryRelation().selector().idEq(category.id).count() == 0) { + if (category != null && categoryRelation().selector().idEq(category.id).isEmpty()) { categoryRelation().inserter().execute(category); } } public Observable> findAll() { return sessionRelation().selector().executeAsObservable() - .map(session -> session.initAssociations(orma)) .toList(); } public Observable> findByChecked() { return sessionRelation().selector().checkedEq(true).executeAsObservable() - .map(session -> session.initAssociations(orma)) .toList(); } public Observable> findByPlace(int placeId) { - return sessionRelation().selector().placeIdEq(placeId).executeAsObservable() - .map(session -> session.initAssociations(orma)) + return sessionRelation().selector().placeEq(placeId).executeAsObservable() .toList(); } public Observable> findByCategory(int categoryId) { - return sessionRelation().selector().categoryIdEq(categoryId).executeAsObservable() - .map(session -> session.initAssociations(orma)) + return sessionRelation().selector().categoryEq(categoryId).executeAsObservable() .toList(); } @@ -118,11 +113,10 @@ public void updateAllSync(List sessions) { placeRelation().deleter().execute(); for (Session session : sessions) { - session.prepareSave(); insertSpeaker(session.speaker); insertCategory(session.category); insertPlace(session.place); - if (sessionRelation().idEq(session.id).count() == 0) { + if (sessionRelation().idEq(session.id).isEmpty()) { sessionRelation().inserter().execute(session); } else { update(session); @@ -145,17 +139,17 @@ private void update(Session session) { .idEq(session.id) .title(session.title) .description(session.description) - .speakerId(session.speaker.id) + .speaker(session.speaker) .stime(session.stime) .etime(session.etime) - .placeId(session.place.id) + .place(session.place) .languageId(session.languageId) .slideUrl(session.slideUrl) .movieUrl(session.movieUrl) .shareUrl(session.shareUrl); if (session.category != null) { - updater.categoryId(session.category.id); + updater.category(session.category); } updater.execute(); diff --git a/app/src/main/java/io/github/droidkaigi/confsched/fragment/SessionsFragment.java b/app/src/main/java/io/github/droidkaigi/confsched/fragment/SessionsFragment.java index bb471ab..5148e03 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/fragment/SessionsFragment.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/fragment/SessionsFragment.java @@ -1,5 +1,7 @@ package io.github.droidkaigi.confsched.fragment; +import org.parceler.Parcels; + import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -19,8 +21,6 @@ import android.view.View; import android.view.ViewGroup; -import org.parceler.Parcels; - import java.util.ArrayList; import java.util.List; import java.util.Map; diff --git a/app/src/main/java/io/github/droidkaigi/confsched/model/Category.java b/app/src/main/java/io/github/droidkaigi/confsched/model/Category.java index ea58eb7..b4e3dc9 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/model/Category.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/model/Category.java @@ -1,8 +1,10 @@ package io.github.droidkaigi.confsched.model; +import com.google.gson.annotations.SerializedName; + import com.github.gfx.android.orma.annotation.Column; +import com.github.gfx.android.orma.annotation.PrimaryKey; import com.github.gfx.android.orma.annotation.Table; -import com.google.gson.annotations.SerializedName; import org.parceler.Parcel; @@ -12,6 +14,7 @@ @Table public class Category implements SearchGroup { + @PrimaryKey(auto = false) @Column(indexed = true) @SerializedName("id") public int id; diff --git a/app/src/main/java/io/github/droidkaigi/confsched/model/Contributor.java b/app/src/main/java/io/github/droidkaigi/confsched/model/Contributor.java index 00508dc..1e13106 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/model/Contributor.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/model/Contributor.java @@ -1,16 +1,17 @@ package io.github.droidkaigi.confsched.model; -import android.support.annotation.Nullable; +import com.google.gson.annotations.SerializedName; import com.github.gfx.android.orma.annotation.Column; import com.github.gfx.android.orma.annotation.PrimaryKey; import com.github.gfx.android.orma.annotation.Table; -import com.google.gson.annotations.SerializedName; + +import android.support.annotation.Nullable; @Table public class Contributor { - @PrimaryKey + @PrimaryKey(auto = false) @Column("name") @SerializedName("login") public String name; diff --git a/app/src/main/java/io/github/droidkaigi/confsched/model/Place.java b/app/src/main/java/io/github/droidkaigi/confsched/model/Place.java index 000b5fd..f8d4d40 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/model/Place.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/model/Place.java @@ -1,8 +1,10 @@ package io.github.droidkaigi.confsched.model; +import com.google.gson.annotations.SerializedName; + import com.github.gfx.android.orma.annotation.Column; +import com.github.gfx.android.orma.annotation.PrimaryKey; import com.github.gfx.android.orma.annotation.Table; -import com.google.gson.annotations.SerializedName; import org.parceler.Parcel; @@ -10,6 +12,7 @@ @Table public class Place implements SearchGroup { + @PrimaryKey(auto = false) @Column(indexed = true) @SerializedName("id") public int id; diff --git a/app/src/main/java/io/github/droidkaigi/confsched/model/Session.java b/app/src/main/java/io/github/droidkaigi/confsched/model/Session.java index ec81d25..1a85a21 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/model/Session.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/model/Session.java @@ -1,15 +1,17 @@ package io.github.droidkaigi.confsched.model; -import android.content.Context; -import android.support.annotation.Nullable; -import android.text.TextUtils; +import com.google.gson.annotations.SerializedName; import com.github.gfx.android.orma.annotation.Column; +import com.github.gfx.android.orma.annotation.PrimaryKey; import com.github.gfx.android.orma.annotation.Table; -import com.google.gson.annotations.SerializedName; import org.parceler.Parcel; +import android.content.Context; +import android.support.annotation.Nullable; +import android.text.TextUtils; + import java.util.Date; import io.github.droidkaigi.confsched.R; @@ -19,6 +21,7 @@ @Table public class Session { + @PrimaryKey(auto = false) @Column(indexed = true) @SerializedName("id") public int id; @@ -31,9 +34,7 @@ public class Session { @SerializedName("description") public String description; - @Column - public int speakerId; - + @Column(value = "speakerId", indexed = true) @SerializedName("speaker") public Speaker speaker; @@ -45,16 +46,12 @@ public class Session { @SerializedName("etime") public Date etime; - @Column(indexed = true) - public int categoryId; - + @Column(value = "categoryId", indexed = true) @Nullable @SerializedName("category") public Category category; - @Column(indexed = true) - public int placeId; - + @Column(value = "placeId", indexed = true) @SerializedName("place") public Place place; @@ -91,20 +88,6 @@ public Date getDisplayETime(Context context) { return LocaleUtil.getDisplayDate(etime, context); } - public void prepareSave() { - speakerId = speaker.id; - if (category != null) categoryId = category.id; - placeId = place.id; - } - - public Session initAssociations(OrmaDatabase orma) { - if (category == null) category = orma.selectFromCategory().idEq(categoryId).value(); - if (place == null) place = orma.selectFromPlace().idEq(placeId).value(); - if (speaker == null) speaker = orma.selectFromSpeaker().idEq(speakerId).value(); - - return this; - } - public int getLanguageResId() { switch (languageId) { case LocaleUtil.LANG_EN_ID: diff --git a/app/src/main/java/io/github/droidkaigi/confsched/model/Speaker.java b/app/src/main/java/io/github/droidkaigi/confsched/model/Speaker.java index 1b81873..6b5fb9a 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/model/Speaker.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/model/Speaker.java @@ -1,17 +1,20 @@ package io.github.droidkaigi.confsched.model; -import android.support.annotation.Nullable; +import com.google.gson.annotations.SerializedName; import com.github.gfx.android.orma.annotation.Column; +import com.github.gfx.android.orma.annotation.PrimaryKey; import com.github.gfx.android.orma.annotation.Table; -import com.google.gson.annotations.SerializedName; import org.parceler.Parcel; +import android.support.annotation.Nullable; + @Parcel @Table public class Speaker { + @PrimaryKey(auto = false) @Column(indexed = true) @SerializedName("id") public int id; From c3f5614a3b4e6031b9f6dda44986c29099e247ed Mon Sep 17 00:00:00 2001 From: "FUJI Goro (gfx)" Date: Mon, 22 Feb 2016 21:14:33 +0900 Subject: [PATCH 2/4] rewrite SessionDaoTest.java not to use mockito --- .../confsched/dao/SessionDaoTest.java | 89 +++++-------------- 1 file changed, 24 insertions(+), 65 deletions(-) diff --git a/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java b/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java index 42e7bac..2370bb9 100644 --- a/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java +++ b/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java @@ -1,40 +1,29 @@ package io.github.droidkaigi.confsched.dao; -import com.github.gfx.android.orma.Inserter; -import com.github.gfx.android.orma.TransactionTask; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import java.util.Date; import java.util.ArrayList; import java.util.List; import io.github.droidkaigi.confsched.model.Category; -import io.github.droidkaigi.confsched.model.Category_Relation; -import io.github.droidkaigi.confsched.model.Category_Selector; import io.github.droidkaigi.confsched.model.OrmaDatabase; import io.github.droidkaigi.confsched.model.Place; -import io.github.droidkaigi.confsched.model.Place_Relation; -import io.github.droidkaigi.confsched.model.Place_Selector; import io.github.droidkaigi.confsched.model.Session; -import io.github.droidkaigi.confsched.model.Session_Relation; import io.github.droidkaigi.confsched.model.Speaker; -import io.github.droidkaigi.confsched.model.Speaker_Relation; -import io.github.droidkaigi.confsched.model.Speaker_Selector; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.assertThat; /** * Unit tests for {@link SessionDao}. */ -@RunWith(JUnit4.class) +@RunWith(AndroidJUnit4.class) public class SessionDaoTest { private static final int SESSION_ID1 = 11; @@ -55,68 +44,38 @@ public class SessionDaoTest { PLACE1.id = PLACE_ID1; PLACE1.name = "place1"; SESSION1.id = SESSION_ID1; + SESSION1.title = "title1"; + SESSION1.description = "description1"; + SESSION1.stime = new Date(); + SESSION1.etime = new Date(); + SESSION1.languageId = "ja"; SESSION1.category = CATEGORY1; SESSION1.place = PLACE1; SESSION1.speaker = SPEAKER1; SESSIONS.add(SESSION1); } - private @Mock OrmaDatabase mockOrmaDatabase; - - private @Mock Speaker_Relation mockSpeakerRelation; - private @Mock Category_Relation mockCategoryRelation; - private @Mock Place_Relation mockPlaceRelation; - private @Mock Session_Relation mockSessionRelation; - - private @Mock Speaker_Selector mockSpeakerSelector; - private @Mock Place_Selector mockPlaceSelector; - private @Mock Category_Selector mockCategorySelector; - - private @Mock Inserter mockSpeakerInserter; - private @Mock Inserter mockPlaceInserter; - private @Mock Inserter mockCategoryInserter; - private @Mock Inserter mockSessionInserter; - - private @Captor ArgumentCaptor transactionTaskCaptor; + private OrmaDatabase orma; private SessionDao sessionDao; + private Context getContext() { + return InstrumentationRegistry.getTargetContext(); + } + @Before public void setUp() { - MockitoAnnotations.initMocks(this); - sessionDao = new SessionDao(mockOrmaDatabase); - - when(mockOrmaDatabase.relationOfCategory()).thenReturn(mockCategoryRelation); - when(mockOrmaDatabase.relationOfSpeaker()).thenReturn(mockSpeakerRelation); - when(mockOrmaDatabase.relationOfPlace()).thenReturn(mockPlaceRelation); - when(mockOrmaDatabase.relationOfSession()).thenReturn(mockSessionRelation); - when(mockSpeakerRelation.selector()).thenReturn(mockSpeakerSelector); - when(mockCategoryRelation.selector()).thenReturn(mockCategorySelector); - when(mockPlaceRelation.selector()).thenReturn(mockPlaceSelector); - when(mockSpeakerRelation.inserter()).thenReturn(mockSpeakerInserter); - when(mockPlaceRelation.inserter()).thenReturn(mockPlaceInserter); - when(mockCategoryRelation.inserter()).thenReturn(mockCategoryInserter); - when(mockSessionRelation.inserter()).thenReturn(mockSessionInserter); + orma = OrmaDatabase.builder(getContext()).name(null).build(); + sessionDao = new SessionDao(orma); } @Test public void testInsertAll() throws Exception { - // This test assumes existing rows don't exist - when(mockPlaceSelector.idEq(PLACE_ID1)).thenReturn(mockPlaceSelector); - when(mockCategorySelector.idEq(CATEGORY_ID1)).thenReturn(mockCategorySelector); - when(mockSpeakerSelector.idEq(SPEAKER_ID1)).thenReturn(mockSpeakerSelector); - when(mockPlaceSelector.count()).thenReturn(0); - when(mockCategorySelector.count()).thenReturn(0); - when(mockSpeakerSelector.count()).thenReturn(0); - - sessionDao.insertAll(SESSIONS); - - verify(mockOrmaDatabase).transactionAsync(transactionTaskCaptor.capture()); - transactionTaskCaptor.getValue().execute(); + sessionDao.updateAllSync(SESSIONS); - verify(mockSpeakerInserter).execute(SPEAKER1); - verify(mockPlaceInserter).execute(PLACE1); - verify(mockCategoryInserter).execute(CATEGORY1); - verify(mockSessionInserter).executeAll(SESSIONS); + assertThat(orma.selectFromSpeaker().idEq(SPEAKER_ID1)).hasSize(1); + assertThat(orma.selectFromPlace().idEq(PLACE_ID1)).hasSize(1); + assertThat(orma.selectFromCategory().idEq(CATEGORY_ID1)).hasSize(1); + assertThat(orma.selectFromSession().idEq(SESSION_ID1)).hasSize(1); } } From 1b24d69647f6d6a6288d0da6e6c8e8b383171cd8 Mon Sep 17 00:00:00 2001 From: "FUJI Goro (gfx)" Date: Mon, 22 Feb 2016 21:21:18 +0900 Subject: [PATCH 3/4] merge SesionTest into SessionDaoTest --- .../droidkaigi/confsched/SessionTest.java | 73 ------------------- .../confsched/dao/SessionDaoTest.java | 38 +++++++++- 2 files changed, 37 insertions(+), 74 deletions(-) delete mode 100644 app/src/test/java/io/github/droidkaigi/confsched/SessionTest.java diff --git a/app/src/test/java/io/github/droidkaigi/confsched/SessionTest.java b/app/src/test/java/io/github/droidkaigi/confsched/SessionTest.java deleted file mode 100644 index a795bea..0000000 --- a/app/src/test/java/io/github/droidkaigi/confsched/SessionTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package io.github.droidkaigi.confsched; - -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -import android.annotation.TargetApi; -import android.app.Application; -import android.content.Context; -import android.os.Build; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.reflect.Type; -import java.util.Collection; -import java.util.List; - -import io.github.droidkaigi.confsched.api.DroidKaigiClient; -import io.github.droidkaigi.confsched.dao.SessionDao; -import io.github.droidkaigi.confsched.di.AppModule; -import io.github.droidkaigi.confsched.model.OrmaDatabase; -import io.github.droidkaigi.confsched.model.Session; - -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(AndroidJUnit4.class) -@TargetApi(Build.VERSION_CODES.KITKAT) -public class SessionTest { - - AppModule appModule; - - Context getContext() { - return InstrumentationRegistry.getTargetContext(); - } - - List loadSessions() { - try (InputStream is = getContext().getResources().openRawResource(R.raw.sessions_ja)) { - - Gson gson = DroidKaigiClient.createGson(); - Type t = new TypeToken>() { - }.getType(); - return gson.fromJson(new InputStreamReader(is), t); - - } catch (IOException e) { - throw new AssertionError(e); - } - } - - @Before - public void setUp() throws Exception { - appModule = new AppModule((Application)getContext().getApplicationContext()); - } - - @Test - public void testLoadingSessions() throws Exception { - OrmaDatabase orma = appModule.provideOrmaDatabase(appModule.provideContext()); - SessionDao dao = new SessionDao(orma); - dao.updateAllSync(loadSessions()); - - assertThat(dao.sessionRelation().selector().toList()).isNotEmpty(); - - Session session = dao.findAll().toBlocking().first().get(0); - assertThat(session.category).isNotNull(); - assertThat(session.speaker).isNotNull(); - assertThat(session.place).isNotNull(); - } -} diff --git a/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java b/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java index 2370bb9..27e9c3e 100644 --- a/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java +++ b/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java @@ -1,17 +1,29 @@ package io.github.droidkaigi.confsched.dao; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import android.annotation.TargetApi; import android.content.Context; +import android.os.Build; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; -import java.util.Date; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; import java.util.List; +import io.github.droidkaigi.confsched.R; +import io.github.droidkaigi.confsched.api.DroidKaigiClient; import io.github.droidkaigi.confsched.model.Category; import io.github.droidkaigi.confsched.model.OrmaDatabase; import io.github.droidkaigi.confsched.model.Place; @@ -24,6 +36,7 @@ * Unit tests for {@link SessionDao}. */ @RunWith(AndroidJUnit4.class) +@TargetApi(Build.VERSION_CODES.KITKAT) // to use Java7 try-with-resource syntax public class SessionDaoTest { private static final int SESSION_ID1 = 11; @@ -59,6 +72,17 @@ public class SessionDaoTest { private SessionDao sessionDao; + List loadSessions() { + try (InputStream is = getContext().getResources().openRawResource(R.raw.sessions_ja)) { + Gson gson = DroidKaigiClient.createGson(); + Type t = new TypeToken>() { + }.getType(); + return gson.fromJson(new InputStreamReader(is), t); + } catch (IOException e) { + throw new AssertionError(e); + } + } + private Context getContext() { return InstrumentationRegistry.getTargetContext(); } @@ -78,4 +102,16 @@ public void testInsertAll() throws Exception { assertThat(orma.selectFromCategory().idEq(CATEGORY_ID1)).hasSize(1); assertThat(orma.selectFromSession().idEq(SESSION_ID1)).hasSize(1); } + + @Test + public void testLoadingSessions() throws Exception { + sessionDao.updateAllSync(loadSessions()); + + assertThat(sessionDao.sessionRelation().selector().toList()).isNotEmpty(); + + Session session = sessionDao.findAll().toBlocking().first().get(0); + assertThat(session.category).isNotNull(); + assertThat(session.speaker).isNotNull(); + assertThat(session.place).isNotNull(); + } } From ccb8ee6194fc77e9d74612ef7175e40870285056 Mon Sep 17 00:00:00 2001 From: "FUJI Goro (gfx)" Date: Mon, 22 Feb 2016 21:52:25 +0900 Subject: [PATCH 4/4] use upsert in SessionDao --- .../droidkaigi/confsched/dao/SessionDao.java | 45 +------------------ .../confsched/dao/SessionDaoTest.java | 10 +++++ 2 files changed, 11 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/io/github/droidkaigi/confsched/dao/SessionDao.java b/app/src/main/java/io/github/droidkaigi/confsched/dao/SessionDao.java index 68be016..5607365 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/dao/SessionDao.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/dao/SessionDao.java @@ -2,8 +2,6 @@ import com.github.gfx.android.orma.TransactionTask; -import android.support.annotation.NonNull; - import java.util.List; import javax.inject.Inject; @@ -16,7 +14,6 @@ import io.github.droidkaigi.confsched.model.Place_Relation; import io.github.droidkaigi.confsched.model.Session; import io.github.droidkaigi.confsched.model.Session_Relation; -import io.github.droidkaigi.confsched.model.Session_Updater; import io.github.droidkaigi.confsched.model.Speaker; import io.github.droidkaigi.confsched.model.Speaker_Relation; import rx.Observable; @@ -47,21 +44,6 @@ private Category_Relation categoryRelation() { return orma.relationOfCategory(); } - public void insertAll(@NonNull List sessions) { - orma.transactionAsync(new TransactionTask() { - @Override - public void execute() throws Exception { - for (Session session : sessions) { - insertSpeaker(session.speaker); - insertCategory(session.category); - insertPlace(session.place); - } - - sessionRelation().inserter().executeAll(sessions); - } - }); - } - private void insertSpeaker(Speaker speaker) { if (speaker != null && speakerRelation().selector().idEq(speaker.id).isEmpty()) { speakerRelation().inserter().execute(speaker); @@ -116,11 +98,7 @@ public void updateAllSync(List sessions) { insertSpeaker(session.speaker); insertCategory(session.category); insertPlace(session.place); - if (sessionRelation().idEq(session.id).isEmpty()) { - sessionRelation().inserter().execute(session); - } else { - update(session); - } + sessionRelation().upserter().execute(session); } } @@ -134,27 +112,6 @@ public void execute() throws Exception { }); } - private void update(Session session) { - Session_Updater updater = sessionRelation().updater() - .idEq(session.id) - .title(session.title) - .description(session.description) - .speaker(session.speaker) - .stime(session.stime) - .etime(session.etime) - .place(session.place) - .languageId(session.languageId) - .slideUrl(session.slideUrl) - .movieUrl(session.movieUrl) - .shareUrl(session.shareUrl); - - if (session.category != null) { - updater.category(session.category); - } - - updater.execute(); - } - public void updateChecked(Session session) { sessionRelation().updater() .idEq(session.id) diff --git a/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java b/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java index 27e9c3e..5073d4c 100644 --- a/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java +++ b/app/src/test/java/io/github/droidkaigi/confsched/dao/SessionDaoTest.java @@ -103,6 +103,16 @@ public void testInsertAll() throws Exception { assertThat(orma.selectFromSession().idEq(SESSION_ID1)).hasSize(1); } + @Test + public void testInsertAllWithUpdate() throws Exception { + sessionDao.updateAllSync(SESSIONS); + SESSIONS.get(0).title = "updated title"; + sessionDao.updateAllSync(SESSIONS); + + assertThat(orma.selectFromSession().idEq(SESSION_ID1).value().title) + .isEqualTo("updated title"); + } + @Test public void testLoadingSessions() throws Exception { sessionDao.updateAllSync(loadSessions());