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.LocalAccount;
import it.niedermann.owncloud.notes.model.NavigationAdapter;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import android.content.Context;
import android.util.Log;
@ -17,7 +18,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@ -26,6 +29,7 @@ import java.util.TimeZone;
import static org.junit.Assert.*;
/**
* WARNING: for all the test case written by order
* you must run all the test case in the same time
@ -451,21 +455,72 @@ public class NotesDatabaseTest {
}
@Test
public void setCategory() {
// Unable to test with SSO
public void test_12_getNoteFromCursor(){
// pass
}
@Test
public void updateNoteAndSync() {
// Unable to test with SSO
public void test_13_getTitleByCategoryId(){
}
@Test
public void updateNote() {
// can not check
// need remoteNote (note from server)
public void test_14_getCategoryIdsByTitle(){
}
@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() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

View file

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