Adds Unit test for deleting an Account

This commit is contained in:
Stefan Niedermann 2021-05-16 12:49:07 +02:00
parent 17296f0446
commit 8790eeea91
3 changed files with 54 additions and 35 deletions

View file

@ -65,12 +65,12 @@ public class NotesDaoTest {
db.getNoteDao().addNote(new Note(1, 1L, Calendar.getInstance(), "T", "C", "", false, "1", LOCAL_DELETED, account.getId(), "", 0)); db.getNoteDao().addNote(new Note(1, 1L, Calendar.getInstance(), "T", "C", "", false, "1", LOCAL_DELETED, account.getId(), "", 0));
db.getNoteDao().deleteByNoteId(1, LOCAL_DELETED); db.getNoteDao().deleteByNoteId(1, LOCAL_DELETED);
assertNull(db.getNoteDao().getNoteById(1)); assertNull(db.getNoteDao().getNoteById(1));
assertNull(NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().getNoteById$(1))); assertNull(NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getNoteById$(1)));
db.getNoteDao().addNote(new Note(1, 1L, Calendar.getInstance(), "T", "C", "", false, "1", LOCAL_DELETED, account.getId(), "", 0)); db.getNoteDao().addNote(new Note(1, 1L, Calendar.getInstance(), "T", "C", "", false, "1", LOCAL_DELETED, account.getId(), "", 0));
db.getNoteDao().deleteByNoteId(1, VOID); db.getNoteDao().deleteByNoteId(1, VOID);
assertEquals(1, db.getNoteDao().getNoteById(1).getId()); assertEquals(1, db.getNoteDao().getNoteById(1).getId());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().getNoteById$(1)).getId()); assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getNoteById$(1)).getId());
} }
@Test @Test
@ -154,8 +154,8 @@ public class NotesDaoTest {
assertEquals(Integer.valueOf(1), db.getNoteDao().countFavorites(account.getId())); assertEquals(Integer.valueOf(1), db.getNoteDao().countFavorites(account.getId()));
assertEquals(Integer.valueOf(1), db.getNoteDao().countFavorites(secondAccount.getId())); assertEquals(Integer.valueOf(1), db.getNoteDao().countFavorites(secondAccount.getId()));
assertEquals(Integer.valueOf(1), NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().countFavorites$(account.getId()))); assertEquals(Integer.valueOf(1), NotesTestingUtil.getOrAwaitValue(db.getNoteDao().countFavorites$(account.getId())));
assertEquals(Integer.valueOf(1), NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().countFavorites$(secondAccount.getId()))); assertEquals(Integer.valueOf(1), NotesTestingUtil.getOrAwaitValue(db.getNoteDao().countFavorites$(secondAccount.getId())));
} }
@Test @Test
@ -165,8 +165,8 @@ public class NotesDaoTest {
assertEquals(Integer.valueOf(7), db.getNoteDao().count(account.getId())); assertEquals(Integer.valueOf(7), db.getNoteDao().count(account.getId()));
assertEquals(Integer.valueOf(5), db.getNoteDao().count(secondAccount.getId())); assertEquals(Integer.valueOf(5), db.getNoteDao().count(secondAccount.getId()));
assertEquals(Integer.valueOf(7), NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().count$(account.getId()))); assertEquals(Integer.valueOf(7), NotesTestingUtil.getOrAwaitValue(db.getNoteDao().count$(account.getId())));
assertEquals(Integer.valueOf(5), NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().count$(secondAccount.getId()))); assertEquals(Integer.valueOf(5), NotesTestingUtil.getOrAwaitValue(db.getNoteDao().count$(secondAccount.getId())));
} }
@Test @Test
@ -341,16 +341,16 @@ public class NotesDaoTest {
final Note note = new Note(1, 1L, Calendar.getInstance(), "My-Title", "My-Content", "", false, "1", LOCAL_DELETED, account.getId(), "", 0); final Note note = new Note(1, 1L, Calendar.getInstance(), "My-Title", "My-Content", "", false, "1", LOCAL_DELETED, account.getId(), "", 0);
db.getNoteDao().addNote(note); db.getNoteDao().addNote(note);
assertEquals("My-Content", db.getNoteDao().getContent(note.getId())); assertEquals("My-Content", db.getNoteDao().getContent(note.getId()));
assertEquals("My-Content", NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().getContent$(note.getId()))); assertEquals("My-Content", NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getContent$(note.getId())));
assertNull(db.getNoteDao().getContent(note.getId() + 1)); assertNull(db.getNoteDao().getContent(note.getId() + 1));
assertNull(NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().getContent$(note.getId() + 1))); assertNull(NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getContent$(note.getId() + 1)));
} }
@Test @Test
public void getCategoriesLiveData() throws InterruptedException { public void getCategoriesLiveData() throws InterruptedException {
final Account secondAccount = setupSecondAccountAndTestNotes(); final Account secondAccount = setupSecondAccountAndTestNotes();
final List<CategoryWithNotesCount> accountCategories = NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().getCategories$(account.getId())); final List<CategoryWithNotesCount> accountCategories = NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getCategories$(account.getId()));
assertEquals(4, accountCategories.size()); assertEquals(4, accountCategories.size());
for (CategoryWithNotesCount category : accountCategories) { for (CategoryWithNotesCount category : accountCategories) {
assertEquals(account.getId(), category.getAccountId()); assertEquals(account.getId(), category.getAccountId());
@ -361,7 +361,7 @@ public class NotesDaoTest {
assertTrue(accountCategories.stream().anyMatch(cat -> "ToDo".equals(cat.getCategory()) && Integer.valueOf(1).equals(cat.getTotalNotes()))); assertTrue(accountCategories.stream().anyMatch(cat -> "ToDo".equals(cat.getCategory()) && Integer.valueOf(1).equals(cat.getTotalNotes())));
assertTrue(accountCategories.stream().anyMatch(cat -> "日记".equals(cat.getCategory()) && Integer.valueOf(1).equals(cat.getTotalNotes()))); assertTrue(accountCategories.stream().anyMatch(cat -> "日记".equals(cat.getCategory()) && Integer.valueOf(1).equals(cat.getTotalNotes())));
final List<CategoryWithNotesCount> secondAccountCategories = NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().getCategories$(secondAccount.getId())); final List<CategoryWithNotesCount> secondAccountCategories = NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getCategories$(secondAccount.getId()));
assertEquals(2, secondAccountCategories.size()); assertEquals(2, secondAccountCategories.size());
for (CategoryWithNotesCount category : secondAccountCategories) { for (CategoryWithNotesCount category : secondAccountCategories) {
assertEquals(secondAccount.getId(), category.getAccountId()); assertEquals(secondAccount.getId(), category.getAccountId());
@ -376,14 +376,14 @@ public class NotesDaoTest {
public void searchCategories() throws InterruptedException { public void searchCategories() throws InterruptedException {
final Account secondAccount = setupSecondAccountAndTestNotes(); final Account secondAccount = setupSecondAccountAndTestNotes();
assertEquals(2, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "M%")).size()); assertEquals(2, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "M%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "Mo%")).size()); assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "Mo%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "MO%")).size()); assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "MO%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "movie%")).size()); assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "movie%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "T%")).size()); assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "T%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "日记")).size()); assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "日记")).size());
assertEquals(2, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(secondAccount.getId(), "M%")).size()); assertEquals(2, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(secondAccount.getId(), "M%")).size());
assertEquals(0, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(secondAccount.getId(), "T%")).size()); assertEquals(0, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(secondAccount.getId(), "T%")).size());
} }
@Test @Test

View file

@ -1,6 +1,7 @@
package it.niedermann.owncloud.notes.persistence; package it.niedermann.owncloud.notes.persistence;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -9,6 +10,8 @@ import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.MoreExecutors;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import org.json.JSONException; import org.json.JSONException;
import org.junit.After; import org.junit.After;
@ -20,6 +23,7 @@ import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import java.io.IOException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.Arrays;
@ -32,7 +36,7 @@ import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.shared.model.Capabilities; import it.niedermann.owncloud.notes.shared.model.Capabilities;
import it.niedermann.owncloud.notes.shared.model.IResponseCallback; import it.niedermann.owncloud.notes.shared.model.IResponseCallback;
import static it.niedermann.owncloud.notes.persistence.NotesDatabaseTestUtil.getOrAwaitValue; import static it.niedermann.owncloud.notes.persistence.NotesTestingUtil.getOrAwaitValue;
import static it.niedermann.owncloud.notes.shared.model.DBStatus.LOCAL_DELETED; import static it.niedermann.owncloud.notes.shared.model.DBStatus.LOCAL_DELETED;
import static it.niedermann.owncloud.notes.shared.model.DBStatus.LOCAL_EDITED; import static it.niedermann.owncloud.notes.shared.model.DBStatus.LOCAL_EDITED;
import static it.niedermann.owncloud.notes.shared.model.DBStatus.VOID; import static it.niedermann.owncloud.notes.shared.model.DBStatus.VOID;
@ -147,6 +151,17 @@ public class NotesRepositoryTest {
}); });
} }
@Test
public void testDeleteAccount() throws IOException {
NotesTestingUtil.mockSingleSignOn(new SingleSignOnAccount(account.getAccountName(), account.getUserName(), "1337", account.getUrl(), ""));
assertNotNull(repo.getAccountById(account.getId()));
repo.deleteAccount(account);
assertNull(repo.getAccountById(account.getId()));
}
@Test @Test
public void testAddNote() { public void testAddNote() {
final Note localNote = new Note(null, Calendar.getInstance(), "Fancy Title", "MyContent", "Samples", false, "123"); final Note localNote = new Note(null, Calendar.getInstance(), "Fancy Title", "MyContent", "Samples", false, "123");

View file

@ -1,18 +1,26 @@
package it.niedermann.owncloud.notes.persistence; package it.niedermann.owncloud.notes.persistence;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.test.core.app.ApplicationProvider;
import java.util.Random; import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import java.io.IOException;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class NotesDatabaseTestUtil { public class NotesTestingUtil {
private static long currentLong = 1; private static long currentLong = 1;
private NotesDatabaseTestUtil() { private NotesTestingUtil() {
// Util class // Util class
} }
@ -39,18 +47,14 @@ public class NotesDatabaseTestUtil {
return (T) data[0]; return (T) data[0];
} }
public static String randomString(int length) { /**
final int leftLimit = 48; // numeral '0' * Pretends managing {@link SingleSignOnAccount}s by using own private {@link SharedPreferences}.
final int rightLimit = 122; // letter 'z' *
* @param ssoAccount this account will be added
return new Random().ints(leftLimit, rightLimit + 1) */
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97)) public static void mockSingleSignOn(@NonNull SingleSignOnAccount ssoAccount) throws IOException {
.limit(length) final SharedPreferences sharedPrefs = ApplicationProvider.getApplicationContext().getSharedPreferences("TEMP_SHARED_PREFS_" + currentLong++, Context.MODE_PRIVATE);
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) sharedPrefs.edit().putString("PREF_ACCOUNT_STRING" + ssoAccount.name, SingleSignOnAccount.toString(ssoAccount)).commit();
.toString(); AccountImporter.setSharedPreferences(sharedPrefs);
}
public static long uniqueLong() {
return currentLong++;
} }
} }