public void ExtractReadsTypedFile() { const string PATH = "test.csv"; const string CONTENT = "ABC"; try { File.WriteAllLines(PATH, new[] { "\"Test\"", $"\"{CONTENT}\"" }); var extractor = new CsvExtractor(); var extracted = extractor.Extract(new CsvExtractorParams { Files = new[] { new CsvExtractorParams.File(PATH, typeof(CsvClass1)) } }).ToArray(); Assert.NotNull(extracted); Assert.True(extracted.Length == 1); var extractedObj = extracted.First() as CsvClass1; Assert.NotNull(extractedObj); Assert.Equal(CONTENT, extractedObj.Test); } finally { if (File.Exists(PATH)) { File.Delete(PATH); } } }
public void CorrectFilePathWithIncorrectExtensionShouldFail() { // Arrange var filePath = Path.Combine(ResourcesDirectory, "test.txt"); //Act ActualValueDelegate <object> testDelegate = () => CsvExtractor.Extract <Item>(filePath); //Assert Assert.That(testDelegate, Throws.TypeOf <ArgumentException>()); }
public void BadFilePathShouldFail() { // Arrange const string filePath = "randomfilepath"; //Act ActualValueDelegate <object> testDelegate = () => CsvExtractor.Extract <Item>(filePath); //Assert Assert.That(testDelegate, Throws.TypeOf <ArgumentException>()); }
public void CorrectFilePathWithCorrectExtensionAndHeadersShouldSucceed() { // Arrange var filePath = Path.Combine(ResourcesDirectory, "FFP_Sample.csv"); // Act var result = CsvExtractor.Extract <Item>(filePath); // Assert Assert.IsTrue(result.Any()); Assert.AreEqual(result.Count, 5); Assert.AreEqual(result.First().ItemId, "187-300"); Assert.AreEqual(result.Last().FormPartitionId, "187-561"); Assert.IsTrue(result.Count(x => string.IsNullOrEmpty(x.AssociatedStimuliId)) == 1); }
public void ExtractReadsMultipleTypesFromMultipleFiles() { var files = new CsvExtractorParams.File[] { new CsvExtractorParams.File("test.csv", typeof(CsvClass1)), new CsvExtractorParams.File("test2.csv", typeof(CsvClass2)) }; var contents = new Dictionary <string, string> { { "test.csv", "ABC" }, { "test2.csv", "123" } }; try { foreach (var file in files) { File.WriteAllLines(file.Path, new[] { "\"Test\"", $"\"{contents[file.Path]}\"" }); } var extractor = new CsvExtractor(); var extracted = extractor.Extract(new CsvExtractorParams { Files = files }).ToArray(); Assert.NotNull(extracted); Assert.True(extracted.Length == 2); var class1Obj = extracted[0] as CsvClass1; Assert.NotNull(class1Obj); Assert.Equal(contents["test.csv"], class1Obj.Test); var class2Obj = extracted[1] as CsvClass2; Assert.NotNull(class2Obj); Assert.Equal(Convert.ToInt32(contents["test2.csv"]), class2Obj.Test); } finally { foreach (var path in files.Select(x => x.Path)) { if (File.Exists(path)) { File.Delete(path); } } } }
public void ExtractThrowsExceptionIfWrongParameterType() { var extractor = new CsvExtractor(); Assert.Throws <ArgumentException>(() => extractor.Extract(new ParamsStub())); }
private static void Main(string[] args) { Logger.Debug(string.Concat(Enumerable.Repeat("-", 60))); Logger.Debug("Fixed Form Packager Initialized"); try { var options = new Options(); if (Parser.Default.ParseArgumentsStrict(args, options, () => { Logger.Fatal( $"Incorrect parameters provided at the command line [{args.Aggregate((x, y) => $"{x},{y}")}]. Terminating."); })) { ExtractionSettings.GitLabInfo = new GitLabInfo { BaseUrl = options.GitLabBaseUrl, Group = options.GitLabGroup, Password = options.GitLabPassword, Username = options.GitLabUsername }; ExtractionSettings.AssessmentScoring = CsvExtractor.Extract <AssessmentScoringComputationRule>(options.AssessmentScoringInput).ToList(); ExtractionSettings.ItemInput = CsvExtractor.Extract <Item>(options.ItemInput).ToList(); ExtractionSettings.AssessmentInfo = CsvExtractor.Extract <Assessment>(options.AssessmentInput).First(); ExtractionSettings.ItemInput.ForEach( x => { ResourceGenerator.Retrieve(ExtractionSettings.GitLabInfo, $"Item-{x.ItemId}"); // If there is no scoring information provided in the input document, look for it in the item XML if (x.ItemScoringInformation.All(y => string.IsNullOrEmpty(y.MeasurementModel))) { ExtractionSettings.ItemInput[ExtractionSettings.ItemInput.FindIndex( y => y.ItemId.Equals(x.ItemId, StringComparison.OrdinalIgnoreCase))] .ItemScoringInformation = IrtMapper.RetrieveIrtParameters(x.ItemId); } }); var uniqueHash = HashGenerator.Hash(ExtractionSettings.AssessmentInfo.UniqueId.GetHashCode(), ExtractionSettings.ItemInput.First().SegmentId.GetHashCode(), ExtractionSettings.ItemInput.First().FormPartitionId.GetHashCode()); Logger.Debug($"Generated unique hash: {uniqueHash}"); ExtractionSettings.AssessmentInfo.UniqueId += $"{uniqueHash}"; ExtractionSettings.ItemInput = ExtractionSettings.ItemInput.Select(x => new Item { ItemId = x.ItemId, AssociatedStimuliId = x.AssociatedStimuliId, FormPartitionId = x.FormPartitionId + $"{uniqueHash}", FormPartitionPosition = x.FormPartitionPosition, FormPosition = x.FormPosition, ItemScoringInformation = x.ItemScoringInformation, SegmentId = x.SegmentId + $"{uniqueHash}", SegmentPosition = x.SegmentPosition }).ToList(); // Validate that the segment unique IDs and assessment IDs are either the same or different depending on # of segments var segmentIds = ExtractionSettings.ItemInput.Select(x => x.SegmentId).Distinct().ToList(); if (segmentIds.Count() > 1 && segmentIds.Any( x => x.Equals(ExtractionSettings.AssessmentInfo.UniqueId, StringComparison.OrdinalIgnoreCase))) { throw new Exception( "Identifiers of segments and assessments must not match in multi-segmented assessments. Please adjust the assessment and/or item inputs."); } if (segmentIds.Count() == 1 && !segmentIds.First() .Equals(ExtractionSettings.AssessmentInfo.UniqueId, StringComparison.OrdinalIgnoreCase)) { throw new Exception( "Identifiers of segments and assessments must match in single-segmented assessments. Please adjust the assessment and/or item inputs."); } var result = TestSpecification.Construct(); result.ToList().ForEach(x => x.Save( $"{x.XPathSelectElement("./identifier").Attribute("uniqueid")?.Value}_{x.Attribute("purpose")?.Value}.xml")); } } catch (Exception e) { Logger.Error(e); } finally { Console.Write("Press any key to exit."); Console.ReadKey(true); } }