Skip to content

Commit

Permalink
Add Tests to fix FamilySearch#43 and FamilySearch#48.
Browse files Browse the repository at this point in the history
- Document.
  • Loading branch information
JoergHoffmannatGitHub committed Jan 29, 2023
1 parent 37b56bc commit 658c6e4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 21 deletions.
77 changes: 77 additions & 0 deletions Gedcomx.Model.Test/DocumentTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Test calss for <see cref="Document"/>
/// </summary>
[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<Document>(JsonConvert.SerializeObject(sut, jsonSettings), jsonSettings));
}
}
}
47 changes: 26 additions & 21 deletions Gedcomx.Model.Test/GedcomxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions Gedcomx.Model.Test/XmlAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<document "));
Assert.That(result, Does.Contain("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""));
Assert.That(result, Does.Contain("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""));
Assert.That(result, Does.Contain("xmlns=\"http://gedcomx.org/v1/\""));
Assert.That(result.Contains("textType="), Is.EqualTo(document.TextType != null));
Assert.That(result.Contains("extracted="), Is.EqualTo(document.ExtractedSpecified == true));
Assert.That(result.Contains("type="), Is.EqualTo(document.Type != null));
Assert.That(result.Contains("<text"), Is.EqualTo(document.Text != null));
result.ShouldContain(document as Conclusion);
}

public static void ShouldContain(this string result, Fact fact)
{
Assert.That(result, Does.Contain("<fact "));
Expand Down

0 comments on commit 658c6e4

Please sign in to comment.