From 658c6e4840268ce302ec543107205a5c33676604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hoffmann?= Date: Sun, 29 Jan 2023 15:39:03 +0100 Subject: [PATCH] Add Tests to fix #43 and #48. - Document. --- Gedcomx.Model.Test/DocumentTest.cs | 77 +++++++++++++++++++++++++++++ Gedcomx.Model.Test/GedcomxTest.cs | 47 ++++++++++-------- Gedcomx.Model.Test/XmlAssertions.cs | 13 +++++ 3 files changed, 116 insertions(+), 21 deletions(-) create mode 100644 Gedcomx.Model.Test/DocumentTest.cs diff --git a/Gedcomx.Model.Test/DocumentTest.cs b/Gedcomx.Model.Test/DocumentTest.cs new file mode 100644 index 0000000..a22499d --- /dev/null +++ b/Gedcomx.Model.Test/DocumentTest.cs @@ -0,0 +1,77 @@ +using System.Xml.Serialization; + +using Gx.Common; +using Gx.Conclusion; +using Gx.Source; + +using Gx.Types; + +using Newtonsoft.Json; + +using NUnit.Framework; + +namespace Gedcomx.Model.Test +{ + /// + /// Test calss for + /// + [TestFixture] + public class DocumentTest + { + [Test] + public void DocumentEmpty() + { + var sut = new Document(); + + VerifyXmlSerialization(sut); + VerifyJsonSerialization(sut); + } + + [Test] + public void DocumentFilled() + { + var sut = new Document + { + // ExtensibleData + Id = "F-1", + // Conclusion + KnownConfidence = ConfidenceLevel.Medium, + SortKey = "sort key", + Lang = "lang", + Attribution = new Attribution(), + Sources = { new SourceReference(), new SourceDescription() { Id = "S-1" } }, + Analysis = new ResourceReference(), + Notes = { new Note() }, + // Document + TextType = "text type", + Extracted = true, + KnownType = DocumentType.Transcription, + Text = "text" + }; + + VerifyXmlSerialization(sut); + VerifyJsonSerialization(sut); + } + + private static void VerifyXmlSerialization(Document sut) + { + var serializer = new XmlSerializer(typeof(Document)); + using var stream = new MemoryStream(); + serializer.Serialize(stream, sut); + + stream.Seek(0, SeekOrigin.Begin); + var result = new StreamReader(stream).ReadToEnd(); + result.ShouldContain(sut); + } + + private static void VerifyJsonSerialization(Document sut) + { + JsonSerializerSettings jsonSettings = new() + { + NullValueHandling = NullValueHandling.Ignore + }; + + Assert.DoesNotThrow(() => JsonConvert.DeserializeObject(JsonConvert.SerializeObject(sut, jsonSettings), jsonSettings)); + } + } +} diff --git a/Gedcomx.Model.Test/GedcomxTest.cs b/Gedcomx.Model.Test/GedcomxTest.cs index a3be283..43f892b 100644 --- a/Gedcomx.Model.Test/GedcomxTest.cs +++ b/Gedcomx.Model.Test/GedcomxTest.cs @@ -88,7 +88,7 @@ public void EmmaBocockExampleTest() }; Relationship fatherRelationship = new() { KnownType = RelationshipType.ParentChild, Person1 = father, Person2 = emma }; Relationship motherRelationship = new() { KnownType = RelationshipType.ParentChild, Person1 = mother, Person2 = emma }; - Document analysis = (Document)new Document().SetText("...Jane Doe's analysis document...").SetId("D-1"); + Document analysis = new() { Id = "D-1", Text = "...Jane Doe's analysis document..." }; Person emmaConclusion = new() { Id = "C-1", @@ -150,18 +150,19 @@ public void SamuelHamExampleTest() }; //The transcription of the grave stone. - Document transcription = (Document)new Document() - .SetType(DocumentType.Transcription) - .SetText("Samuel Ham of the parish of Honiton and Elizabeth Spiller\n" + + Document transcription = new() + { + Id = "D-1", + Text = "Samuel Ham of the parish of Honiton and Elizabeth Spiller\n" + "were married this 3rd day of November 1828 by David Smith\n" + "Stone, Pl Curate,\n" + "In the Presence of\n" + "Jno Pain.\n" + "R.G. Halls. Peggy Hammet.\n" + - "No. 86.") - .SetSource(recordDescription) - .SetLang("en") - .SetId("D-1"); + "No. 86.", + Sources = { recordDescription }, + Lang = "en" + }; //The transcription described as a source. SourceDescription transcriptionDescription = new() @@ -268,7 +269,7 @@ public void SamuelHamExampleTest() .SetId("E-1"); //Jane Doe's analysis. - Document analysis = (Document)new Document().SetText("...Jane Doe's analysis document...").SetId("D-2"); + Document analysis = new() { Id = "D-2", Text = "...Jane Doe's analysis document..." }; //Jane Doe's conclusions about a person. Person samConclusion = new() @@ -607,16 +608,18 @@ public void WongAloiauExampleTest() }; //The transcription of the grave stone. - Document transcription = (Document)new Document() - .SetText("WONG ALOIAU\n" + + Document transcription = new() + { + Id = "D-1", + Text = "WONG ALOIAU\n" + "NOV. 22, 1848 – AUG. 3, 1920\n" + "中山 大字都 泮沙鄉\n" + "生 於 前 清 戊申 年 十一 月 廿二(日)子 時\n" + "終 於 民國 庚申 年 七月 十二 (日) 午 時\n" + - "先考 諱 羅有 字 容康 王 府 君 之 墓") - .SetSource(gravestoneImageDescription) - .SetLang("zh") - .SetId("D-1"); + "先考 諱 羅有 字 容康 王 府 君 之 墓", + Sources = { gravestoneImageDescription }, + Lang = "zh", + }; //The transcription described as a source. SourceDescription transcriptionDescription = new() @@ -630,15 +633,17 @@ public void WongAloiauExampleTest() }; //The translation of the grave stone. - Document translation = (Document)new Document() - .SetText("WONG ALOIAU\n" + + Document translation = new() + { + Id = "D-2", + Text = "WONG ALOIAU\n" + "NOV. 22, 1848 – AUG. 3, 1920 [lunar dates]\n" + "[Birthplace] [China, Guandong, ]Chung Shan, See Dai Doo, Pun Sha village\n" + "[Date of birth] Born at former Qing 1848 year 11th month 22nd day 23-1 hour.\n" + "[Life] ended at Republic of China year 1920 year 7th mo. 12th day 11-13 hour.\n" + - "Deceased father avoid [mention of] Lo Yau also known as Young Hong Wong [noble]residence ruler’s grave.") - .SetSource(transcriptionDescription) - .SetId("D-2"); + "Deceased father avoid [mention of] Lo Yau also known as Young Hong Wong [noble]residence ruler’s grave.", + Sources = { transcriptionDescription } + }; //The translation described as a source. SourceDescription translationDescription = new() @@ -698,7 +703,7 @@ public void WongAloiauExampleTest() Relationship fatherRelationship = new() { KnownType = RelationshipType.ParentChild, Person1 = father, Person2 = aloiau }; //Jane Doe's analysis. - Document analysis = (Document)new Document().SetText("...Jane Doe's analysis document...").SetId("D-3"); + Document analysis = new() { Id = "D-3", Text = "...Jane Doe's analysis document..." }; //Jane Doe's conclusions about a person. Person aloiauConclusion = new() diff --git a/Gedcomx.Model.Test/XmlAssertions.cs b/Gedcomx.Model.Test/XmlAssertions.cs index 5e8ec73..87f9bda 100644 --- a/Gedcomx.Model.Test/XmlAssertions.cs +++ b/Gedcomx.Model.Test/XmlAssertions.cs @@ -81,6 +81,19 @@ public static void ShouldContain(this string result, SourceDescription sourceDes result.ShouldContain(sourceDescription as HypermediaEnabledData); } + public static void ShouldContain(this string result, Document document) + { + Assert.That(result, Does.Contain("