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().deleteByNoteId(1, LOCAL_DELETED);
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().deleteByNoteId(1, VOID);
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
@ -154,8 +154,8 @@ public class NotesDaoTest {
assertEquals(Integer.valueOf(1), db.getNoteDao().countFavorites(account.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), NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().countFavorites$(secondAccount.getId())));
assertEquals(Integer.valueOf(1), NotesTestingUtil.getOrAwaitValue(db.getNoteDao().countFavorites$(account.getId())));
assertEquals(Integer.valueOf(1), NotesTestingUtil.getOrAwaitValue(db.getNoteDao().countFavorites$(secondAccount.getId())));
}
@Test
@ -165,8 +165,8 @@ public class NotesDaoTest {
assertEquals(Integer.valueOf(7), db.getNoteDao().count(account.getId()));
assertEquals(Integer.valueOf(5), db.getNoteDao().count(secondAccount.getId()));
assertEquals(Integer.valueOf(7), NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().count$(account.getId())));
assertEquals(Integer.valueOf(5), NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().count$(secondAccount.getId())));
assertEquals(Integer.valueOf(7), NotesTestingUtil.getOrAwaitValue(db.getNoteDao().count$(account.getId())));
assertEquals(Integer.valueOf(5), NotesTestingUtil.getOrAwaitValue(db.getNoteDao().count$(secondAccount.getId())));
}
@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);
db.getNoteDao().addNote(note);
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(NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().getContent$(note.getId() + 1)));
assertNull(NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getContent$(note.getId() + 1)));
}
@Test
public void getCategoriesLiveData() throws InterruptedException {
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());
for (CategoryWithNotesCount category : accountCategories) {
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 -> "日记".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());
for (CategoryWithNotesCount category : secondAccountCategories) {
assertEquals(secondAccount.getId(), category.getAccountId());
@ -376,14 +376,14 @@ public class NotesDaoTest {
public void searchCategories() throws InterruptedException {
final Account secondAccount = setupSecondAccountAndTestNotes();
assertEquals(2, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "M%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "Mo%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "MO%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "movie%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "T%")).size());
assertEquals(1, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "日记")).size());
assertEquals(2, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(secondAccount.getId(), "M%")).size());
assertEquals(0, NotesDatabaseTestUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(secondAccount.getId(), "T%")).size());
assertEquals(2, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "M%")).size());
assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "Mo%")).size());
assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "MO%")).size());
assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "movie%")).size());
assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "T%")).size());
assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(account.getId(), "日记")).size());
assertEquals(2, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(secondAccount.getId(), "M%")).size());
assertEquals(0, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().searchCategories$(secondAccount.getId(), "T%")).size());
}
@Test

View file

@ -1,6 +1,7 @@
package it.niedermann.owncloud.notes.persistence;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import androidx.annotation.NonNull;
@ -9,6 +10,8 @@ import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
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.junit.After;
@ -20,6 +23,7 @@ import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
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.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_EDITED;
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
public void testAddNote() {
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;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
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.TimeUnit;
public class NotesDatabaseTestUtil {
public class NotesTestingUtil {
private static long currentLong = 1;
private NotesDatabaseTestUtil() {
private NotesTestingUtil() {
// Util class
}
@ -39,18 +47,14 @@ public class NotesDatabaseTestUtil {
return (T) data[0];
}
public static String randomString(int length) {
final int leftLimit = 48; // numeral '0'
final int rightLimit = 122; // letter 'z'
return new Random().ints(leftLimit, rightLimit + 1)
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
.limit(length)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
public static long uniqueLong() {
return currentLong++;
/**
* Pretends managing {@link SingleSignOnAccount}s by using own private {@link SharedPreferences}.
*
* @param ssoAccount this account will be added
*/
public static void mockSingleSignOn(@NonNull SingleSignOnAccount ssoAccount) throws IOException {
final SharedPreferences sharedPrefs = ApplicationProvider.getApplicationContext().getSharedPreferences("TEMP_SHARED_PREFS_" + currentLong++, Context.MODE_PRIVATE);
sharedPrefs.edit().putString("PREF_ACCOUNT_STRING" + ssoAccount.name, SingleSignOnAccount.toString(ssoAccount)).commit();
AccountImporter.setSharedPreferences(sharedPrefs);
}
}