Merge pull request #3045 from kiwix/Issue#3044

Convert KiwixDatabaseTest.java into kotlin
This commit is contained in:
Kelson 2022-11-10 09:48:02 +01:00 committed by GitHub
commit ef545094a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 90 deletions

View File

@ -1,89 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.data.local;
import android.content.Context;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import com.yahoo.squidb.data.SquidCursor;
import com.yahoo.squidb.sql.Query;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kiwix.kiwixmobile.core.data.local.KiwixDatabase;
import org.kiwix.kiwixmobile.core.data.local.entity.Bookmark;
import static org.junit.Assert.assertArrayEquals;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class KiwixDatabaseTest {
private final Context context;
public KiwixDatabaseTest() {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
}
@Test
@SuppressWarnings("CharsetObjectCanBeUsed") // Standard charset throws exception on < API19
public void testMigrateDatabase() throws IOException {
KiwixDatabase kiwixDatabase = new KiwixDatabase(context, null, null, null, null);
kiwixDatabase.recreate();
String testId = "8ce5775a-10a9-bbf3-178a-9df69f23263c";
String[] testBookmarks = new String[] { "Test1", "Test2", "Test3" };
String fileName = context.getFilesDir().getAbsolutePath() + File.separator + testId + ".txt";
File f = new File(fileName);
if (!f.createNewFile()) {
throw new IOException("Unable to create file for testing migration");
}
Writer writer =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName),
"UTF-8"));
for (String bookmark : testBookmarks) {
writer.write(bookmark + "\n");
}
writer.close();
kiwixDatabase.migrateBookmarksVersion6();
ArrayList<String> bookmarkTitles = new ArrayList<>();
try {
SquidCursor<Bookmark> bookmarkCursor = kiwixDatabase.query(Bookmark.class,
Query.selectDistinct(Bookmark.BOOKMARK_TITLE)
.where(Bookmark.ZIM_ID.eq(testId)
.or(Bookmark.ZIM_NAME.eq("")))
.orderBy(Bookmark.BOOKMARK_TITLE.asc()));
while (bookmarkCursor.moveToNext()) {
bookmarkTitles.add(bookmarkCursor.get(Bookmark.BOOKMARK_TITLE));
}
} catch (Exception exception) {
exception.printStackTrace();
}
assertArrayEquals(testBookmarks, bookmarkTitles.toArray());
// TODO Add new migration test for version 16
}
}

View File

@ -0,0 +1,96 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.data.local
import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
import com.yahoo.squidb.sql.Query
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.kiwix.kiwixmobile.core.data.local.KiwixDatabase
import org.kiwix.kiwixmobile.core.data.local.entity.Bookmark
import java.io.BufferedWriter
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStreamWriter
import java.io.Writer
@RunWith(AndroidJUnit4::class)
@SmallTest
class KiwixDatabaseTest {
private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
@Test @Throws(IOException::class) // Standard charset throws exception on < API19
fun testMigrateDatabase() {
val kiwixDatabase = KiwixDatabase(
context,
null,
null,
null,
null
)
kiwixDatabase.recreate()
val testId = "8ce5775a-10a9-bbf3-178a-9df69f23263c"
val testBookmarks = arrayOf("Test1", "Test2", "Test3")
val fileName = context.filesDir.absolutePath + File.separator + testId + ".txt"
val f = File(fileName)
if (!f.createNewFile()) {
throw IOException("Unable to create file for testing migration")
}
val writer: Writer = BufferedWriter(
OutputStreamWriter(
FileOutputStream(fileName),
"UTF-8"
)
)
for (bookmark in testBookmarks) {
writer.write(
"""
$bookmark
""".trimIndent()
)
}
writer.close()
kiwixDatabase.migrateBookmarksVersion6()
val bookmarkTitles = ArrayList<String?>()
try {
val bookmarkCursor = kiwixDatabase.query(
Bookmark::class.java,
Query.selectDistinct(Bookmark.BOOKMARK_TITLE)
.where(
Bookmark.ZIM_ID.eq(testId)
.or(Bookmark.ZIM_NAME.eq(""))
)
.orderBy(Bookmark.BOOKMARK_TITLE.asc())
)
while (bookmarkCursor.moveToNext()) {
bookmarkTitles.add(bookmarkCursor.get(Bookmark.BOOKMARK_TITLE))
}
} catch (exception: Exception) {
exception.printStackTrace()
}
Assert.assertArrayEquals(testBookmarks, bookmarkTitles.toTypedArray())
// TODO Add new migration test for version 16
}
}

View File

@ -63,7 +63,6 @@ class TopLevelDestinationRobot : BaseRobot() {
fun clickDownloadOnBottomNav(func: OnlineLibraryRobot.() -> Unit) {
clickOn(ViewId(R.id.downloadsFragment))
onlineLibrary(func)
pressBack()
}
private fun inNavDrawer(navDrawerAction: () -> Unit) {