예제 #1
0
        public void TestRoundtrip(RoundTripObj roundTripObj)
        {
            // This test assumes you have the starting .zip (Filename) included in your project files.
            var pathToStartZip = Path.Combine(Util.AssetsDir, roundTripObj.Filename);

            Assert.IsTrue(File.Exists(pathToStartZip));

            // Roundtrip Part 1

            // Init the project the .zip info is added to.
            var proj1 = Util.RandomProject();

            proj1.VernacularWritingSystem.Bcp47 = roundTripObj.Language;
            proj1 = _projRepo.Create(proj1).Result;

            // Upload the zip file.
            // Generate api parameter with filestream.
            using (var stream = File.OpenRead(pathToStartZip))
            {
                var fileUpload = InitFile(stream, roundTripObj.Filename);

                // Make api call.
                var result = _liftController.UploadLiftFile(proj1 !.Id, fileUpload).Result;
                Assert.That(result is OkObjectResult);
            }

            proj1 = _projRepo.GetProject(proj1.Id).Result;
            if (proj1 is null)
            {
                Assert.Fail();
                return;
            }

            Assert.That(proj1.LiftImported);

            var allWords = _wordRepo.GetAllWords(proj1.Id).Result;

            Assert.AreEqual(allWords.Count, roundTripObj.NumOfWords);

            // We are currently only testing guids on the single-entry data sets.
            if (roundTripObj.EntryGuid != "" && allWords.Count == 1)
            {
                Assert.AreEqual(allWords[0].Guid.ToString(), roundTripObj.EntryGuid);
                if (roundTripObj.SenseGuid != "")
                {
                    Assert.AreEqual(allWords[0].Senses[0].Guid.ToString(), roundTripObj.SenseGuid);
                }
            }

            // Export.
            var exportedFilePath  = _liftController.CreateLiftExport(proj1.Id).Result;
            var exportedDirectory = FileOperations.ExtractZipFile(exportedFilePath, null, false);

            // Assert the file was created with desired hierarchy.
            Assert.That(Directory.Exists(exportedDirectory));
            var sanitizedProjName = Sanitization.MakeFriendlyForPath(proj1.Name, "Lift");
            var exportedProjDir   = Path.Combine(exportedDirectory, sanitizedProjName);

            Assert.That(Directory.Exists(Path.Combine(exportedProjDir, "audio")));
            foreach (var audioFile in roundTripObj.AudioFiles)
            {
                Assert.That(File.Exists(Path.Combine(exportedProjDir, "audio", audioFile)));
            }
            Assert.That(Directory.Exists(Path.Combine(exportedProjDir, "WritingSystems")));
            Assert.That(File.Exists(Path.Combine(
                                        exportedProjDir, "WritingSystems", roundTripObj.Language + ".ldml")));
            Assert.That(File.Exists(Path.Combine(exportedProjDir, sanitizedProjName + ".lift")));
            Directory.Delete(exportedDirectory, true);

            // Clean up.
            _wordRepo.DeleteAllWords(proj1.Id);

            // Roundtrip Part 2

            // Init the project the .zip info is added to.
            var proj2 = Util.RandomProject();

            proj2.VernacularWritingSystem.Bcp47 = roundTripObj.Language;
            proj2 = _projRepo.Create(proj2).Result;

            // Upload the exported words again.
            // Generate api parameter with filestream.
            using (var fstream = File.OpenRead(exportedFilePath))
            {
                var fileUpload = InitFile(fstream, roundTripObj.Filename);

                // Make api call.
                var result2 = _liftController.UploadLiftFile(proj2 !.Id, fileUpload).Result;
                Assert.That(result2 is OkObjectResult);
            }

            proj2 = _projRepo.GetProject(proj2.Id).Result;
            if (proj2 is null)
            {
                Assert.Fail();
                return;
            }

            // Clean up zip file.
            File.Delete(exportedFilePath);

            allWords = _wordRepo.GetAllWords(proj2.Id).Result;
            Assert.That(allWords, Has.Count.EqualTo(roundTripObj.NumOfWords));
            // We are currently only testing guids on the single-entry data sets.
            if (roundTripObj.EntryGuid != "" && allWords.Count == 1)
            {
                Assert.AreEqual(allWords[0].Guid.ToString(), roundTripObj.EntryGuid);
                if (roundTripObj.SenseGuid != "")
                {
                    Assert.AreEqual(allWords[0].Senses[0].Guid.ToString(), roundTripObj.SenseGuid);
                }
            }

            // Export.
            exportedFilePath  = _liftController.CreateLiftExport(proj2.Id).Result;
            exportedDirectory = FileOperations.ExtractZipFile(exportedFilePath, null);

            // Assert the file was created with desired hierarchy.
            Assert.That(Directory.Exists(exportedDirectory));
            sanitizedProjName = Sanitization.MakeFriendlyForPath(proj2.Name, "Lift");
            exportedProjDir   = Path.Combine(exportedDirectory, sanitizedProjName);
            Assert.That(Directory.Exists(Path.Combine(exportedProjDir, "audio")));
            foreach (var audioFile in roundTripObj.AudioFiles)
            {
                var path = Path.Combine(exportedProjDir, "audio", audioFile);
                Assert.That(File.Exists(path),
                            $"The file {audioFile} can not be found at this path: {path}");
            }
            Assert.That(Directory.Exists(Path.Combine(exportedProjDir, "WritingSystems")));
            Assert.That(File.Exists(Path.Combine(
                                        exportedProjDir, "WritingSystems", roundTripObj.Language + ".ldml")));
            Assert.That(File.Exists(Path.Combine(exportedProjDir, sanitizedProjName + ".lift")));
            Directory.Delete(exportedDirectory, true);

            // Clean up.
            _wordRepo.DeleteAllWords(proj2.Id);
            foreach (var project in new List <Project> {
                proj1, proj2
            })
            {
                _projRepo.Delete(project.Id);
            }
        }
        public void TestRoundtrip()
        {
            // This test assumes you have the starting .zip included in your project files.

            // Get path to the starting dir
            var pathToStartZips = Path.Combine(Directory.GetParent(Directory.GetParent(
                                                                       Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString()).ToString(), "Assets");
            var testZips = Directory.GetFiles(pathToStartZips, "*.zip");

            var fileMapping = new Dictionary <string, RoundTripObj>();

            // Add new .zip file information here
            var gusillaay = new RoundTripObj("gsl-Qaaa-x-orth", new List <string>(), 8045 /*number of words*/);

            fileMapping.Add("Gusillaay.zip", gusillaay);
            var lotad = new RoundTripObj("dtr", new List <string>(), 5400);

            fileMapping.Add("Lotad.zip", lotad);
            var natqgu = new RoundTripObj("qaa-x-stc-natqgu", new List <string>(), 11570 /*number of words*/);

            fileMapping.Add("Natqgu.zip", natqgu);
            var resembli = new RoundTripObj("ags", new List <string>(), 255 /*number of words*/);

            fileMapping.Add("Resembli.zip", resembli);
            var rwc = new RoundTripObj("es", new List <string>(), 132 /*number of words*/);

            fileMapping.Add("RWC.zip", rwc);
            var sena = new RoundTripObj("seh", new List <string>(), 1462 /*number of words*/);

            fileMapping.Add("Sena.zip", sena);
            var singleEntryLiftWithSound = new RoundTripObj(
                "ptn", new List <string> {
                "short.mp3"
            }, 1 /*number of words*/);

            fileMapping.Add("SingleEntryLiftWithSound.zip", singleEntryLiftWithSound);
            var singleEntryLiftWithTwoSound = new RoundTripObj(
                "ptn",
                new List <string> {
                "short.mp3", "short1.mp3"
            }, 1 /*number of words*/);

            fileMapping.Add("SingleEntryLiftWithTwoSound.zip", singleEntryLiftWithTwoSound);

            foreach (var dataSet in fileMapping)
            {
                var actualFilename = dataSet.Key;
                var pathToStartZip = Path.Combine(pathToStartZips, actualFilename);

                // Upload the zip file

                // Init the project the .zip info is added to
                var proj = RandomProject();
                _projServ.Create(proj);

                // Generate api parameter with filestream
                if (File.Exists(pathToStartZip))
                {
                    var fstream    = File.OpenRead(pathToStartZip);
                    var fileUpload = InitFile(fstream, actualFilename);

                    // Make api call
                    var result = _liftController.UploadLiftFile(proj.Id, fileUpload).Result;
                    Assert.That(!(result is BadRequestObjectResult));

                    proj = _projServ.GetProject(proj.Id).Result;
                    Assert.AreEqual(proj.VernacularWritingSystem, dataSet.Value.Language);

                    fstream.Close();

                    var allWords = _wordrepo.GetAllWords(proj.Id);
                    // Export
                    var exportedFilePath  = _liftController.CreateLiftExport(proj.Id);
                    var exportedDirectory = Path.GetDirectoryName(exportedFilePath);

                    // Assert the file was created with desired heirarchy
                    Assert.That(Directory.Exists(exportedDirectory));
                    Assert.That(Directory.Exists(Path.Combine(exportedDirectory, "LiftExport", "Lift", "audio")));
                    foreach (var audioFile in dataSet.Value.AudioFiles)
                    {
                        Assert.That(File.Exists(Path.Combine(
                                                    exportedDirectory, "LiftExport", "Lift", "audio", audioFile)));
                    }
                    Assert.That(Directory.Exists(Path.Combine(exportedDirectory, "LiftExport", "Lift", "WritingSystems")));
                    Assert.That(File.Exists(Path.Combine(
                                                exportedDirectory,
                                                "LiftExport", "Lift", "WritingSystems", dataSet.Value.Language + ".ldml")));
                    Assert.That(File.Exists(Path.Combine(exportedDirectory, "LiftExport", "Lift", "NewLiftFile.lift")));
                    var dirList = new List <string>(
                        Directory.GetDirectories(Path.GetDirectoryName(exportedDirectory)));
                    dirList.Remove(exportedDirectory);
                    Assert.That(Directory.Exists(Path.Combine(Path.GetDirectoryName(exportedDirectory), dirList.Single())));

                    _wordrepo.DeleteAllWords(proj.Id);

                    // Roundtrip Part 2

                    // Upload the exported words again
                    // Init the project the .zip info is added to
                    var proj2 = RandomProject();
                    _projServ.Create(proj2);

                    // Generate api parameter with filestream
                    fstream    = File.OpenRead(exportedFilePath);
                    fileUpload = InitFile(fstream, actualFilename);

                    // Make api call
                    var result2 = _liftController.UploadLiftFile(proj2.Id, fileUpload).Result;
                    Assert.That(!(result is BadRequestObjectResult));

                    proj2 = _projServ.GetProject(proj2.Id).Result;
                    Assert.AreEqual(proj2.VernacularWritingSystem, dataSet.Value.Language);

                    fstream.Close();

                    allWords = _wordrepo.GetAllWords(proj2.Id);
                    Assert.AreEqual(allWords.Result.Count, dataSet.Value.NumOfWords);

                    // Export
                    exportedFilePath  = _liftController.CreateLiftExport(proj2.Id);
                    exportedDirectory = Path.GetDirectoryName(exportedFilePath);

                    // Assert the file was created with desired hierarchy
                    Assert.That(Directory.Exists(exportedDirectory));
                    Assert.That(Directory.Exists(Path.Combine(exportedDirectory, "LiftExport", "Lift", "audio")));
                    foreach (var audioFile in dataSet.Value.AudioFiles)
                    {
                        var path = Path.Combine(exportedDirectory, "LiftExport", "Lift", "audio", audioFile);
                        Assert.That(File.Exists(path),
                                    "The file " + audioFile + " can not be found at this path: " + path);
                    }
                    Assert.That(Directory.Exists(Path.Combine(exportedDirectory, "LiftExport", "Lift", "WritingSystems")));
                    Assert.That(File.Exists(Path.Combine(
                                                exportedDirectory,
                                                "LiftExport", "Lift", "WritingSystems", dataSet.Value.Language + ".ldml")));
                    Assert.That(File.Exists(Path.Combine(exportedDirectory, "LiftExport", "Lift", "NewLiftFile.lift")));
                    dirList = new List <string>(Directory.GetDirectories(Path.GetDirectoryName(exportedDirectory)));
                    dirList.Remove(exportedDirectory);
                    Assert.That(Directory.Exists(Path.Combine(Path.GetDirectoryName(exportedDirectory), dirList.Single())));

                    _wordrepo.DeleteAllWords(proj.Id);
                }
            }
        }