Skip to content

Commit

Permalink
[Kotlin Migration] CollectionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay0701 authored and mikehardy committed Jun 25, 2022
1 parent 67c1a3e commit 6ee8892
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 143 deletions.
4 changes: 2 additions & 2 deletions AnkiDroid/kotlinMigration.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ permission notice:

// Example of class name: "/com/ichi2/anki/UIUtils.kt"
// Ensure that it starts with '/' (slash)
def source = Source.TEST
def className = "/com/ichi2/libanki/CollectionTest.kt"
def source = Source.MAIN
def className = ""

enum Source {
MAIN("/src/main/java"),
Expand Down
259 changes: 118 additions & 141 deletions AnkiDroid/src/test/java/com/ichi2/libanki/CollectionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,52 @@
* 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 com.ichi2.libanki;

import com.ichi2.anki.CollectionHelper;
import com.ichi2.anki.RobolectricTest;
import com.ichi2.utils.JSONObject;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.Collections;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
public class CollectionTest extends RobolectricTest {

package com.ichi2.libanki

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.CollectionHelper
import com.ichi2.anki.RobolectricTest
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers
import org.hamcrest.Matchers.equalTo
import org.junit.Assert.*
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*

@RunWith(AndroidJUnit4::class)
class CollectionTest : RobolectricTest() {
@Test
public void editClozeGenerateCardsInSameDeck() {
fun editClozeGenerateCardsInSameDeck() {
// #7781
// Technically, editing a card with conditional fields can also cause this, but cloze cards are much more common

Note n = addNoteUsingModelName("Cloze", "{{c1::Hello}} {{c2::World}}", "Extra");

long did = addDeck("Testing");

for (Card c : n.cards()) {
c.setDid(did);
c.flush();
val n = addNoteUsingModelName("Cloze", "{{c1::Hello}} {{c2::World}}", "Extra")
val did = addDeck("Testing")
for (c in n.cards()) {
c.did = did
c.flush()
}

assertThat("two cloze notes should be generated", n.numberOfCards(), is(2));
assertThat("two cloze notes should be generated", n.numberOfCards(), equalTo(2))

// create card 3
n.setField(0, n.getFields()[0] + "{{c3::third}}");

n.flush();

assertThat("A new card should be generated", n.numberOfCards(), is(3));

assertThat("The new card should have the same did as the previous cards", n.cards().get(2).getDid(), is(did));
n.setField(0, n.fields[0].toString() + "{{c3::third}}")
n.flush()
assertThat("A new card should be generated", n.numberOfCards(), equalTo(3))
assertThat("The new card should have the same did as the previous cards", n.cards()[2].did, equalTo(did))
}

@Test
public void beforeUploadClosesCollection() {
Collection col = getCol();
assertThat("db should be open", CollectionHelper.getInstance().colIsOpen(), is(true));
col.beforeUpload();
assertThat("db should be closed", CollectionHelper.getInstance().colIsOpen(), is(false));
fun beforeUploadClosesCollection() {
val col = col
assertThat("db should be open", CollectionHelper.getInstance().colIsOpen(), equalTo(true))
col.beforeUpload()
assertThat("db should be closed", CollectionHelper.getInstance().colIsOpen(), equalTo(false))
}

/*******************
** autogenerated from https://github.com/ankitects/anki/blob/2c73dcb2e547c44d9e02c20a00f3c52419dc277b/pylib/tests/test_cards.py *
*******************/

/*TODO
@Test
public void test_create_open(){
Expand Down Expand Up @@ -112,126 +93,122 @@ public class CollectionTest extends RobolectricTest {
os.unlink(newPath);
} */
@Test
public void test_noteAddDelete() {
Collection col = getCol();
fun test_noteAddDelete() {
val col = col
// add a note
Note note = col.newNote();
note.setItem("Front", "one");
note.setItem("Back", "two");
int n = col.addNote(note);
assertEquals(1, n);
var note = col.newNote()
note.setItem("Front", "one")
note.setItem("Back", "two")
var n = col.addNote(note)
assertEquals(1, n)
// test multiple cards - add another template
Model m = col.getModels().current();
ModelManager mm = col.getModels();
JSONObject t = Models.newTemplate("Reverse");
t.put("qfmt", "{{Back}}");
t.put("afmt", "{{Front}}");
mm.addTemplateModChanged(m, t);
mm.save(m, true); // todo: remove true which is not upstream
assertEquals(2, col.cardCount());
val m = col.models.current()
val mm = col.models
val t = Models.newTemplate("Reverse")
t.put("qfmt", "{{Back}}")
t.put("afmt", "{{Front}}")
mm.addTemplateModChanged(m!!, t)
mm.save(m, true) // todo: remove true which is not upstream
assertEquals(2, col.cardCount())
// creating new notes should use both cards
note = col.newNote();
note.setItem("Front", "three");
note.setItem("Back", "four");
n = col.addNote(note);
assertEquals(2, n);
assertEquals(4, col.cardCount());
note = col.newNote()
note.setItem("Front", "three")
note.setItem("Back", "four")
n = col.addNote(note)
assertEquals(2, n)
assertEquals(4, col.cardCount())
// check q/a generation
Card c0 = note.cards().get(0);
assertThat(c0.q(), containsString("three"));
val c0 = note.cards()[0]
assertThat(c0.q(), Matchers.containsString("three"))
// it should not be a duplicate
assertEquals(note.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT);
assertEquals(note.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT)
// now let's make a duplicate
Note note2 = col.newNote();
note2.setItem("Front", "one");
note2.setItem("Back", "");
assertNotEquals(note2.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT);
val note2 = col.newNote()
note2.setItem("Front", "one")
note2.setItem("Back", "")
assertNotEquals(note2.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT)
// empty first field should not be permitted either
note2.setItem("Front", " ");
assertNotEquals(note2.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT);
note2.setItem("Front", " ")
assertNotEquals(note2.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT)
}


@Test
@Ignore("I don't understand this csum")
public void test_fieldChecksum() {
Collection col = getCol();
Note note = col.newNote();
note.setItem("Front", "new");
note.setItem("Back", "new2");
col.addNote(note);
assertEquals(0xc2a6b03f, col.getDb().queryLongScalar("select csum from notes"));
fun test_fieldChecksum() {
val col = col
val note = col.newNote()
note.setItem("Front", "new")
note.setItem("Back", "new2")
col.addNote(note)
assertEquals(-0xc2a6b03f, col.db.queryLongScalar("select csum from notes"))
// changing the val should change the checksum
note.setItem("Front", "newx");
note.flush();
assertEquals(0x302811ae, col.getDb().queryLongScalar("select csum from notes"));
note.setItem("Front", "newx")
note.flush()
assertEquals(0x302811ae, col.db.queryLongScalar("select csum from notes"))
}


@Test
public void test_addDelTags() {
Collection col = getCol();
Note note = col.newNote();
note.setItem("Front", "1");
col.addNote(note);
Note note2 = col.newNote();
note2.setItem("Front", "2");
col.addNote(note2);
fun test_addDelTags() {
val col = col
val note = col.newNote()
note.setItem("Front", "1")
col.addNote(note)
val note2 = col.newNote()
note2.setItem("Front", "2")
col.addNote(note2)
// adding for a given id
col.getTags().bulkAdd(Collections.singletonList(note.getId()), "foo");
note.load();
note2.load();
assertTrue(note.getTags().contains("foo"));
assertFalse(note2.getTags().contains("foo"));
col.tags.bulkAdd(listOf(note.id), "foo")
note.load()
note2.load()
assertTrue(note.tags.contains("foo"))
assertFalse(note2.tags.contains("foo"))
// should be canonified
col.getTags().bulkAdd(Collections.singletonList(note.getId()), "foo aaa");
note.load();
assertEquals("aaa", note.getTags().get(0));
assertEquals(2, note.getTags().size());
col.tags.bulkAdd(listOf(note.id), "foo aaa")
note.load()
assertEquals("aaa", note.tags[0])
assertEquals(2, note.tags.size)
}


@Test
public void test_timestamps() {
Collection col = getCol();
int stdModelSize = StdModels.STD_MODELS.length;
assertEquals(col.getModels().all().size(), stdModelSize);
for (int i = 0; i < 100; i++) {
StdModels.BASIC_MODEL.add(col);
fun test_timestamps() {
val col = col
val stdModelSize = StdModels.STD_MODELS.size
assertEquals(col.models.all().size, stdModelSize)
for (i in 0..99) {
StdModels.BASIC_MODEL.add(col)
}
assertEquals(col.getModels().all().size(), 100 + stdModelSize);
assertEquals(col.models.all().size, (100 + stdModelSize))
}


@Test
@Ignore("Pending port of media search from Rust code")
public void test_furigana() {
Collection col = getCol();
ModelManager mm = col.getModels();
Model m = mm.current();
fun test_furigana() {
val col = col
val mm = col.models
val m = mm.current()
// filter should work
m.getJSONArray("tmpls").getJSONObject(0).put("qfmt", "{{kana:Front}}");
mm.save(m);
Note n = col.newNote();
n.setItem("Front", "foo[abc]");
col.addNote(n);
Card c = n.cards().get(0);
assertTrue(c.q().endsWith("abc"));
m!!.getJSONArray("tmpls").getJSONObject(0).put("qfmt", "{{kana:Front}}")
mm.save(m)
val n = col.newNote()
n.setItem("Front", "foo[abc]")
col.addNote(n)
val c = n.cards()[0]
assertTrue(c.q().endsWith("abc"))
// and should avoid sound
n.setItem("Front", "foo[sound:abc.mp3]");
n.flush();
String question = c.q(true);
assertThat("Question «" + question + "» does not contains «anki:play».", question, containsString("anki:play"));
n.setItem("Front", "foo[sound:abc.mp3]")
n.flush()
val question = c.q(true)
assertThat("Question «$question» does not contains «anki:play».", question, Matchers.containsString("anki:play"))
// it shouldn't throw an error while people are editing
m.getJSONArray("tmpls").getJSONObject(0).put("qfmt", "{{kana:}}");
mm.save(m);
c.q(true);
m.getJSONArray("tmpls").getJSONObject(0).put("qfmt", "{{kana:}}")
mm.save(m)
c.q(true)
}

@Test
public void test_filterToValidCards() {
Collection col = getCol();
long cid = addNoteUsingBasicModel("foo", "bar").firstCard().getId();
assertEquals( new ArrayList<>(Collections.singleton(cid)), col.filterToValidCards(new long[]{cid, cid + 1}));
fun test_filterToValidCards() {
val col = col
val cid = addNoteUsingBasicModel("foo", "bar").firstCard().id
assertEquals(ArrayList(setOf(cid)), col.filterToValidCards(longArrayOf(cid, cid + 1)))
}
}

0 comments on commit 6ee8892

Please sign in to comment.