find some bugs in NotesDatabase and add some test template

This commit is contained in:
Hui-Ouyang16 2020-05-05 04:52:48 +08:00
parent cc2aaed5eb
commit 4d1ed6bbde
2 changed files with 68 additions and 7 deletions

View file

@ -4,6 +4,7 @@ import it.niedermann.owncloud.notes.model.CloudNote;
import it.niedermann.owncloud.notes.model.DBNote; import it.niedermann.owncloud.notes.model.DBNote;
import it.niedermann.owncloud.notes.model.LocalAccount; import it.niedermann.owncloud.notes.model.LocalAccount;
import it.niedermann.owncloud.notes.model.NavigationAdapter; import it.niedermann.owncloud.notes.model.NavigationAdapter;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
@ -17,7 +18,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -26,6 +29,7 @@ import java.util.TimeZone;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
* WARNING: for all the test case written by order * WARNING: for all the test case written by order
* you must run all the test case in the same time * you must run all the test case in the same time
@ -451,21 +455,72 @@ public class NotesDatabaseTest {
} }
@Test @Test
public void setCategory() { public void test_12_getNoteFromCursor(){
// Unable to test with SSO // pass
} }
@Test @Test
public void updateNoteAndSync() { public void test_13_getTitleByCategoryId(){
// Unable to test with SSO
} }
@Test @Test
public void updateNote() { public void test_14_getCategoryIdsByTitle(){
// can not check
// need remoteNote (note from server)
} }
@Test
public void test_15_getCategoryIdByTitle(){
try {
// TODO: forName error
Class c = Class.forName("NotesDatabase");
Log.i("gogogogo", c.getName());
Method method = Class.forName("NotesDatabase")
.getDeclaredMethod("getCategoryIdByTitle",
Long.class,
String.class,
Boolean.class);
method.setAccessible(true);
List<NavigationAdapter.NavigationItem> categories = db.getCategories(account.getId());
int count = 0;
for (NavigationAdapter.NavigationItem categoryItem : categories) {
Log.i("Test_15_getCategoryIdByTitle", String.format("%s | %s | %d | %d",
categoryItem.id, categoryItem.label, categoryItem.count, categoryItem.icon));
count++;
}
Log.i("Test_15_getCategoryIdByTitle", "count " + count);
int catID = (int)method.invoke(db, account.getId(), "Mike Chester Wang's Diary", false);
assertEquals(catID, -1);
catID = (int)method.invoke(db, account.getId(), "Mike Chester Wang's Diary", true);
assertNotEquals(catID, -1);
catID = (int)method.invoke(db, account.getId(), "hello", true);
assertEquals(catID, -1);
}catch (Exception e){
fail(Arrays.toString(e.getStackTrace()));
Log.e("Test_15_getCategoryIdByTitle_Exception", Arrays.toString(e.getStackTrace()));
}
}
// @Test
// public void setCategory() {
// // Unable to test with SSO
// }
//
// @Test
// public void updateNoteAndSync() {
// // Unable to test with SSO
// }
//
// @Test
// public void updateNote() {
// // can not check
// // need remoteNote (note from server)
// }
public static String getCurDate() { public static String getCurDate() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8")); TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date()); return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

View file

@ -852,6 +852,8 @@ public class NotesDatabase extends AbstractNotesDatabase {
@NonNull @NonNull
@WorkerThread @WorkerThread
private Integer getCategoryIdByTitle(long accountId, @NonNull String categoryTitle, boolean create) { private Integer getCategoryIdByTitle(long accountId, @NonNull String categoryTitle, boolean create) {
// TODO: there is a bug
// only validateAccountId BUT NOT use
if (create) { if (create) {
if (getCategoryIdByTitle(accountId, categoryTitle, false) == -1) { if (getCategoryIdByTitle(accountId, categoryTitle, false) == -1) {
if (addCategory(categoryTitle, accountId) == -1) { if (addCategory(categoryTitle, accountId) == -1) {
@ -881,6 +883,8 @@ public class NotesDatabase extends AbstractNotesDatabase {
@NonNull @NonNull
@WorkerThread @WorkerThread
private String getTitleByCategoryId(long accountId, int id) { private String getTitleByCategoryId(long accountId, int id) {
// TODO: there is a bug
// only validateAccountId BUT NOT use
if (accountId != -1) if (accountId != -1)
validateAccountId(accountId); validateAccountId(accountId);
Cursor cursor = getReadableDatabase().query( Cursor cursor = getReadableDatabase().query(
@ -925,6 +929,8 @@ public class NotesDatabase extends AbstractNotesDatabase {
// TODO: test // TODO: test
private List<Integer> getCategoryIdsByTitle(long accountId, @NonNull String title) { private List<Integer> getCategoryIdsByTitle(long accountId, @NonNull String title) {
// TODO: there is a bug
// only validateAccountId BUT NOT use
validateAccountId(accountId); validateAccountId(accountId);
Cursor cursor = getReadableDatabase().query( Cursor cursor = getReadableDatabase().query(
table_category, table_category,