Skip to content

Commit

Permalink
Add Tests to fix #43 and #48.
Browse files Browse the repository at this point in the history
- Fact with Conclusion.
  • Loading branch information
JoergHoffmannatGitHub committed Jan 23, 2023
1 parent a4df0df commit 14e946b
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 196 deletions.
80 changes: 80 additions & 0 deletions Gedcomx.Model.Test/FactTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Xml.Serialization;

using Gx.Common;
using Gx.Conclusion;
using Gx.Records;
using Gx.Source;
using Gx.Types;

using Newtonsoft.Json;

using NUnit.Framework;

namespace Gedcomx.Model.Test
{
/// <summary>
/// Test calss for <see cref="Fact"/>
/// </summary>
[TestFixture]
public class FactTest
{
[Test]
public void FactEmpty()
{
var sut = new Fact();

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

[Test]
public void FactFilled()
{
var sut = new Fact
{
// 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() },
// Fact
Primary = true,
KnownType = FactType.Adoption,
Date = new DateInfo(),
Place = new PlaceReference(),
Value = "value",
Qualifiers = { new Qualifier() },
Fields = { new Field() }
};

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

private static void VerifyXmlSerialization(Fact sut)
{
var serializer = new XmlSerializer(typeof(Fact));
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(Fact sut)
{
JsonSerializerSettings jsonSettings = new()
{
NullValueHandling = NullValueHandling.Ignore
};

Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<Fact>(JsonConvert.SerializeObject(sut, jsonSettings), jsonSettings));
}
}
}
138 changes: 68 additions & 70 deletions Gedcomx.Model.Test/GedcomxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ public void EmmaBocockExampleTest()
Created = DateTime.Parse("1843-07-27"),
Repository = repository
};
Fact birth = new Fact()
.SetType(FactType.Birth)
.SetDate(new DateInfo().SetOriginal("23 June 1843"))
.SetPlace(new PlaceReference().SetOriginal("Broadfield Bar, Abbeydale Road, Ecclesall-Bierlow, York, England, United Kingdom"));
Fact birth = new()
{
KnownType = FactType.Birth,
Date = new DateInfo().SetOriginal("23 June 1843"),
Place = new PlaceReference().SetOriginal("Broadfield Bar, Abbeydale Road, Ecclesall-Bierlow, York, England, United Kingdom")
};
Person emma = (Person)new Person().SetName("Emma Bocock").SetGender(GenderType.Female).SetFact(birth).SetExtracted(true).SetSource(sourceDescription).SetId("P-1");
Person father = (Person)new Person().SetName("William Bocock").SetFact(new Fact().SetType(FactType.Occupation).SetValue("Toll Collector")).SetExtracted(true).SetSource(sourceDescription).SetId("P-2");
Person mother = (Person)new Person().SetName("Sarah Bocock formerly Brough").SetExtracted(true).SetSource(sourceDescription).SetId("P-3");
Expand Down Expand Up @@ -148,22 +150,28 @@ public void SamuelHamExampleTest()
};

//the marriage fact.
Fact marriage = new Fact()
.SetType(FactType.Marriage)
.SetDate(new DateInfo().SetOriginal("3 November 1828").SetFormal("+1828-11-03"))
.SetPlace(new PlaceReference().SetOriginal("Wilton St George, Wilton, Somerset, England"));
Fact marriage = new()
{
KnownType = FactType.Marriage,
Date = new DateInfo().SetOriginal("3 November 1828").SetFormal("+1828-11-03"),
Place = new PlaceReference().SetOriginal("Wilton St George, Wilton, Somerset, England")
};

//the groom's residence.
Fact samsResidence = new Fact()
.SetType(FactType.Residence)
.SetDate(new DateInfo().SetOriginal("3 November 1828").SetFormal("+1828-11-03"))
.SetPlace(new PlaceReference().SetOriginal("parish of Honiton, Devon, England"));
Fact samsResidence = new()
{
KnownType = FactType.Residence,
Date = new DateInfo().SetOriginal("3 November 1828").SetFormal("+1828-11-03"),
Place = new PlaceReference().SetOriginal("parish of Honiton, Devon, England")
};

//the groom's residence.
Fact lizsResidence = new Fact()
.SetType(FactType.Residence)
.SetDate(new DateInfo().SetOriginal("3 November 1828").SetFormal("+1828-11-03"))
.SetPlace(new PlaceReference().SetOriginal("parish of Wilton, Somerset, England"));
Fact lizsResidence = new()
{
KnownType = FactType.Residence,
Date = new DateInfo().SetOriginal("3 November 1828").SetFormal("+1828-11-03"),
Place = new PlaceReference().SetOriginal("parish of Wilton, Somerset, England")
};

//the groom
Person sam = (Person)new Person().SetName("Samuel Ham").SetGender(GenderType.Male).SetFact(samsResidence).SetExtracted(true).SetSource(transcriptionDescription).SetId("P-1");
Expand Down Expand Up @@ -568,20 +576,26 @@ public void WongAloiauExampleTest()
};

//the birth.
Fact birth = new Fact()
.SetType(FactType.Birth)
.SetDate(new DateInfo().SetOriginal("former Qing 1848 year 11th month 22nd day 23-1 hour").SetFormal("+1848-11-22"))
.SetPlace(new PlaceReference().SetOriginal("Pun Sha Village, See Dai Doo, Chung Shan, Guangdong, China"));
Fact birth = new()
{
KnownType = FactType.Birth,
Date = new DateInfo().SetOriginal("former Qing 1848 year 11th month 22nd day 23-1 hour").SetFormal("+1848-11-22"),
Place = new PlaceReference().SetOriginal("Pun Sha Village, See Dai Doo, Chung Shan, Guangdong, China")
};

//the death.
Fact death = new Fact()
.SetType(FactType.Death)
.SetDate(new DateInfo().SetOriginal("Republic of China year 1920 year 7th mo. 12th day 11-13 hour").SetFormal("+1920-08-03"));
Fact death = new()
{
KnownType = FactType.Death,
Date = new DateInfo().SetOriginal("Republic of China year 1920 year 7th mo. 12th day 11-13 hour").SetFormal("+1920-08-03")
};

//the burial.
Fact burial = new Fact()
.SetType(FactType.Burial)
.SetPlace(new PlaceReference().SetOriginal("Lin Yee Chung Cemetery, Honolulu, Oahu, Hawaii"));
Fact burial = new()
{
KnownType = FactType.Burial,
Place = new PlaceReference().SetOriginal("Lin Yee Chung Cemetery, Honolulu, Oahu, Hawaii")
};

//the principal person
Person aloiau = (Person)new Person().SetName("WONG Aloiau").SetGender(GenderType.Male).SetFact(birth).SetFact(death).SetFact(burial).SetExtracted(true).SetSource(translationDescription).SetId("P-1");
Expand Down Expand Up @@ -711,31 +725,23 @@ private static Person CreateGeorge(PlaceDescription birthPlace, PlaceDescription
Person person = new();
person.SetGender(new Gender(GenderType.Male));

Fact fact = new();
fact.SetId("123");
fact.SetType(FactType.Birth);

fact.SetDate(new DateInfo());
fact.Date.SetOriginal("February 22, 1732");
fact.Date.SetFormal("+1732-02-22");

fact.SetPlace(new PlaceReference());
fact.Place.SetOriginal(birthPlace.Names[0].Value.ToLower());
fact.Place.DescriptionRef = "#" + birthPlace.Id;
Fact fact = new()
{
Id = "123",
KnownType = FactType.Birth,
Date = new DateInfo().SetOriginal("February 22, 1732").SetFormal("+1732-02-22"),
Place = new PlaceReference().SetOriginal(birthPlace.Names[0].Value.ToLower()).SetDescription(birthPlace)
};

person.AddFact(fact);

fact = new Fact();
fact.SetId("456");
fact.SetType(FactType.Death);

fact.SetDate(new DateInfo());
fact.Date.SetOriginal("December 14, 1799");
fact.Date.SetFormal("+1799-12-14T22:00:00");

fact.SetPlace(new PlaceReference());
fact.Place.SetOriginal(deathPlace.Names[0].Value.ToLower());
fact.Place.DescriptionRef = "#" + deathPlace.Id;
fact = new()
{
Id = "456",
KnownType = FactType.Death,
Date = new DateInfo().SetOriginal("December 14, 1799").SetFormal("+1799-12-14T22:00:00"),
Place = new PlaceReference().SetOriginal(deathPlace.Names[0].Value.ToLower()).SetDescription(deathPlace)
};

person.AddFact(fact);

Expand Down Expand Up @@ -768,31 +774,23 @@ private static Person CreateMartha(PlaceDescription birthPlace, PlaceDescription
Person person = new();
person.SetGender(new Gender(GenderType.Male));

Fact fact = new();
fact.SetId("321");
fact.SetType(FactType.Birth);

fact.SetDate(new DateInfo());
fact.Date.SetOriginal("June 2, 1731");
fact.Date.SetFormal("+1731-06-02");

fact.SetPlace(new PlaceReference());
fact.Place.SetOriginal(birthPlace.Names[0].Value.ToLower());
fact.Place.DescriptionRef = "#" + birthPlace.Id;
Fact fact = new()
{
Id = "321",
KnownType = FactType.Birth,
Date = new DateInfo().SetOriginal("June 2, 1731").SetFormal("+1731-06-02"),
Place = new PlaceReference().SetOriginal(birthPlace.Names[0].Value.ToLower()).SetDescription(birthPlace)
};

person.AddFact(fact);

fact = new Fact();
fact.SetId("654");
fact.SetType(FactType.Death);

fact.SetDate(new DateInfo());
fact.Date.SetOriginal("May 22, 1802");
fact.Date.SetFormal("+1802-05-22");

fact.SetPlace(new PlaceReference());
fact.Place.SetOriginal(deathPlace.Names[0].Value.ToLower());
fact.Place.DescriptionRef = "#" + deathPlace.Id;
fact = new()
{
Id = "654",
KnownType = FactType.Death,
Date = new DateInfo().SetOriginal("May 22, 1802").SetFormal("+1802-05-22"),
Place = new PlaceReference().SetOriginal(deathPlace.Names[0].Value.ToLower()).SetDescription(deathPlace)
};

person.AddFact(fact);

Expand Down
28 changes: 28 additions & 0 deletions Gedcomx.Model.Test/XmlAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ public static void ShouldContain(this string result, SourceDescription sourceDes
result.ShouldContain(sourceDescription as HypermediaEnabledData);
}

public static void ShouldContain(this string result, Fact fact)
{
Assert.That(result, Does.Contain("<fact "));
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("primary="), Is.EqualTo(fact.PrimarySpecified));
Assert.That(result.Contains("type="), Is.EqualTo(fact.Type != null));
Assert.That(result.Contains("<date"), Is.EqualTo(fact.Date != null));
Assert.That(result.Contains("<place"), Is.EqualTo(fact.Place != null));
Assert.That(result.Contains("<value"), Is.EqualTo(fact.Value != null));
Assert.That(result.Contains("<qualifier"), Is.EqualTo(fact.AnyQualifiers()));
Assert.That(result.Contains("<field"), Is.EqualTo(fact.AnyFields()));
result.ShouldContain(fact as Conclusion);
}

public static void ShouldContain(this string result, Conclusion conclusion)
{
Assert.That(result.Contains("confidence="), Is.EqualTo(conclusion.Confidence != null));
Assert.That(result.Contains("sortKey="), Is.EqualTo(conclusion.SortKey != null));
Assert.That(result.Contains("lang="), Is.EqualTo(conclusion.Lang != null));
Assert.That(result.Contains("<attribution"), Is.EqualTo(conclusion.Attribution != null));
Assert.That(result.Contains("<source"), Is.EqualTo(conclusion.AnySources()));
Assert.That(result.Contains("<analysis"), Is.EqualTo(conclusion.Analysis != null));
Assert.That(result.Contains("<note"), Is.EqualTo(conclusion.AnyNotes()));
result.ShouldContain(conclusion as HypermediaEnabledData);
}

public static void ShouldContain(this string result, HypermediaEnabledData hypermediaEnabledData)
{
Assert.That(result.Contains("<link"), Is.EqualTo(hypermediaEnabledData.AnyLinks()));
Expand Down
Loading

0 comments on commit 14e946b

Please sign in to comment.