Exemplo n.º 1
0
        public async Task TestExportAnkiDue()
        {
            sourceCollection.Crt -= 86400 * 10;
            sourceCollection.Sched.Reset();
            var c = sourceCollection.Sched.PopCard();

            sourceCollection.Sched.AnswerCard(c, Sched.AnswerEase.Hard);
            sourceCollection.Sched.AnswerCard(c, Sched.AnswerEase.Hard);

            //Should have ivl of 1, due on day 11
            Assert.AreEqual(1, c.Interval);
            Assert.AreEqual(11, c.Due);
            Assert.AreEqual(10, sourceCollection.Sched.Today);

            //Export
            var export = new AnkiExporter(sourceCollection);

            export.IncludeSched = true;
            await export.ExportInto(tempExport, "ankitest.anki2");

            //Importing into a new col, the due date should be equivalent
            var tempfolder2 = await Utils.localFolder.CreateFolderAsync("tempfolder2");

            using (Collection col2 = await Utils.GetEmptyCollection(tempfolder2))
            {
                var imp = new Anki2Importer(col2, tempExport, "ankitest.anki2");
                await imp.Run();

                c = col2.GetCard(c.Id);
                col2.Sched.Reset();
                Assert.AreEqual(1, c.Due - col2.Sched.Today);
            }
            await tempfolder2.DeleteAsync();
        }
Exemplo n.º 2
0
        public async Task TestAnki2MediaDupes()
        {
            long   mid;
            string exportFileName = "testAnki2MediaDupe.apkg";

            using (Collection col = await Utils.GetEmptyCollection(tempFolder))
            {
                //Add a note
                Note n = col.NewNote();
                n.SetItem("Front", "[sound:foo.mp3]");
                mid = (long)JsonHelper.GetNameNumber(n.Model, "id");
                col.AddNote(n);
                var folder = await col.Media.MediaFolder.CreateFolderAsync(defaultDeckId);

                //Add that sound to the media folder
                using (FileStream file = new FileStream(folder.Path + "\\" + "foo.mp3", FileMode.Create))
                {
                    byte[] buf = Encoding.UTF8.GetBytes("foo");
                    file.Write(buf, 0, buf.Length);
                }
                AnkiU.AnkiCore.Exporter.AnkiPackageExporter exporter = new AnkiU.AnkiCore.Exporter.AnkiPackageExporter(col);
                await exporter.ExportInto(tempFolder, exportFileName);
            }

            //It should be imported correctly into an empty deck
            StorageFile exportedFiles = await tempFolder.GetFileAsync(exportFileName);

            StorageFolder tempFolder2 = await Utils.localFolder.CreateFolderAsync("tempFolder2");

            using (Collection empty = await Utils.GetEmptyCollection(tempFolder2))
            {
                Importer imp = new AnkiPackageImporter(empty, exportedFiles);
                await imp.Run();

                var expected = new List <string> {
                    "foo.mp3"
                };
                var folder = await empty.Media.MediaFolder.GetFolderAsync(defaultDeckId);

                var storageFiles = await folder.GetFilesAsync();

                var actual = (from s in storageFiles select s.Name).ToList();
                Assert.IsTrue(actual.SequenceEqual(expected), actual.PrintArray());

                //And importing again will not duplicate, as the file content matches
                var cardList = empty.Database.QueryColumn <CardIdOnlyTable>("select id from cards");
                empty.RemoveCardsAndNoteIfNoCardsLeft((from c in cardList select c.Id).ToArray());
                imp = new Anki2Importer(empty, tempFolder, "test.anki2");
                await imp.Run();

                storageFiles = await folder.GetFilesAsync();

                actual = (from s in storageFiles select s.Name).ToList();
                Assert.IsTrue(actual.SequenceEqual(expected), actual.PrintArray());

                var n = empty.GetNote(empty.Database.QueryScalar <long>("select id from notes"));
                Assert.IsTrue(n.Fields[0].Contains("foo.mp3"), n.Fields[0]);

                //If the local file content is different, an import should trigger a rename
                cardList = empty.Database.QueryColumn <CardIdOnlyTable>("select id from cards");
                empty.RemoveCardsAndNoteIfNoCardsLeft((from c in cardList select c.Id).ToArray());

                using (FileStream file = new FileStream(empty.Media.MediaFolder.Path + "\\" + "foo.mp3", FileMode.Create))
                {
                    byte[] buf = Encoding.UTF8.GetBytes("bar");
                    file.Write(buf, 0, buf.Length);
                }
                imp = new Anki2Importer(empty, tempFolder, "test.anki2");
                await imp.Run();

                storageFiles = await empty.Media.MediaFolder.GetFilesAsync();

                actual   = (from s in storageFiles select s.Name).ToList();
                expected = new List <string> {
                    "foo.mp3", String.Format("foo_{0}.mp3", mid)
                };
                Assert.IsTrue(actual.SequenceEqual(expected), actual.PrintArray());

                n = empty.GetNote(empty.Database.QueryScalar <long>("select id from notes"));
                Assert.IsTrue(n.Fields[0].Contains("_"), n.Fields[0]);

                //If the localized media file already exists, we rewrite the note and media
                cardList = empty.Database.QueryColumn <CardIdOnlyTable>("select id from cards");
                empty.RemoveCardsAndNoteIfNoCardsLeft((from c in cardList select c.Id).ToArray());

                using (FileStream file = new FileStream(empty.Media.MediaFolder.Path + "\\" + "foo.mp3", FileMode.Open))
                {
                    byte[] buf = Encoding.UTF8.GetBytes("bar");
                    file.Write(buf, 0, buf.Length);
                }
                imp = new Anki2Importer(empty, tempFolder, "test.anki2");
                await imp.Run();

                storageFiles = await empty.Media.MediaFolder.GetFilesAsync();

                actual   = (from s in storageFiles select s.Name).ToList();
                expected = new List <string> {
                    "foo.mp3", String.Format("foo_{0}.mp3", mid)
                };
                Assert.IsTrue(actual.SequenceEqual(expected), actual.PrintArray());

                n = empty.GetNote(empty.Database.QueryScalar <long>("select id from notes"));
                Assert.IsTrue(n.Fields[0].Contains("_"), n.Fields[0]);
            }

            await tempFolder2.DeleteAsync();
        }