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
- Collection.
  • Loading branch information
JoergHoffmannatGitHub committed Mar 11, 2023
1 parent cd03f6e commit ebd2f48
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 111 deletions.
139 changes: 68 additions & 71 deletions Gedcomx.Model.Test/AgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,92 @@

using Gx.Agent;
using Gx.Common;
using Gx.Conclusion;
using Gx.Links;

using Newtonsoft.Json;

using NUnit.Framework;

namespace Gedcomx.Model.Test
namespace Gedcomx.Model.Test;

/// <summary>
/// Test calss for <see cref="Agent"/>
/// </summary>
[TestFixture]
public class AgentTest
{
/// <summary>
/// Test calss for <see cref="Agent"/>
/// </summary>
[TestFixture]
public class AgentTest
[Test]
public void AgentEmpty()
{
[Test]
public void AgentEmpty()
{
var sut = new Agent();
Agent sut = new();

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}
VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

[Test]
public void AgentObjectInitialization()
[Test]
public void AgentObjectInitialization()
{
Agent sut = new()
{
var sut = new Agent
{
// ExtensibleData
Id = "A-1",
// HypermediaEnabledData
Links = { new Link(), { "rel", new Uri("https://www.familysearch.org/platform/collections/tree") }, { "rel", "template" } },
// Agent
Accounts = { new OnlineAccount() },
Addresses = { new Address() },
Emails = { "[email protected]" },
Homepage = new ResourceReference(),
Identifiers = { new Identifier() },
Names = { "Jane Doe" },
Openid = new ResourceReference(),
Phones = { new ResourceReference() },
};
// ExtensibleData
Id = "A-1",
// HypermediaEnabledData
Links = { new(), { "rel", new Uri("https://www.familysearch.org/platform/collections/tree") }, { "rel", "template" } },
// Agent
Accounts = { new() },
Addresses = { new() },
Emails = { "[email protected]" },
Homepage = new(),
Identifiers = { new() },
Names = { "Jane Doe" },
Openid = new(),
Phones = { new() }
};

Assert.That(sut.Names[0].Value, Is.EqualTo("Jane Doe"));
Assert.That(sut.Emails[0].Resource, Is.EqualTo("mailto:[email protected]"));
Assert.That(sut.Names[0].Value, Is.EqualTo("Jane Doe"));
Assert.That(sut.Emails[0].Resource, Is.EqualTo("mailto:[email protected]"));

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}
VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

[Test]
public void SetAccountTest()
{
var agent = new Agent();
agent.Accounts.Add(new OnlineAccount());
agent.Addresses.Add(new Address());
agent.Emails.Add(new ResourceReference());
agent.Homepage = new ResourceReference();
agent.Identifiers.Add(new Identifier());
agent.Names.Add(new TextValue());
agent.Openid = new ResourceReference();
agent.Phones.Add(new ResourceReference());
agent.Id = "id";
agent.Links.Add(new Link());
[Test]
public void SetAccountTest()
{
Agent agent = new();
agent.Accounts.Add(new());
agent.Addresses.Add(new());
agent.Emails.Add(new ResourceReference());
agent.Homepage = new();
agent.Identifiers.Add(new());
agent.Names.Add(new TextValue());
agent.Openid = new();
agent.Phones.Add(new());
agent.Id = "id";
agent.Links.Add(new());

VerifyXmlSerialization(agent);
VerifyJsonSerialization(agent);
}
VerifyXmlSerialization(agent);
VerifyJsonSerialization(agent);
}

private static void VerifyXmlSerialization(Agent sut)
{
var serializer = new XmlSerializer(typeof(Agent));
using var stream = new MemoryStream();
serializer.Serialize(stream, sut);
private static void VerifyXmlSerialization(Agent sut)
{
XmlSerializer serializer = new(typeof(Agent));
using MemoryStream stream = new();
serializer.Serialize(stream, sut);

stream.Seek(0, SeekOrigin.Begin);
var result = new StreamReader(stream).ReadToEnd();
result.ShouldContain(sut);
}
stream.Seek(0, SeekOrigin.Begin);
var result = new StreamReader(stream).ReadToEnd();
result.ShouldContain(sut);
}

private static void VerifyJsonSerialization(Agent sut)
private static void VerifyJsonSerialization(Agent sut)
{
JsonSerializerSettings jsonSettings = new()
{
JsonSerializerSettings jsonSettings = new()
{
NullValueHandling = NullValueHandling.Ignore
};
NullValueHandling = NullValueHandling.Ignore
};

Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<Agent>(JsonConvert.SerializeObject(sut, jsonSettings), jsonSettings));
}
Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<Agent>(JsonConvert.SerializeObject(sut, jsonSettings), jsonSettings));
}
}
67 changes: 67 additions & 0 deletions Gedcomx.Model.Test/CollectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Xml.Serialization;

using Gx.Records;

using Newtonsoft.Json;

using NUnit.Framework;

namespace Gedcomx.Model.Test;

/// <summary>
/// Test calss for <see cref="Collection"/>
/// </summary>
[TestFixture]
public class CollectionTest
{
[Test]
public void CollectionEmpty()
{
Collection sut = new();

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

[Test]
public void CollectionObjectInitialization()
{
Collection sut = new()
{
// ExtensibleData
Id = "A-1",
// HypermediaEnabledData
Links = { new(), { "rel", new Uri("https://www.familysearch.org/platform/collections/tree") }, { "rel", "template" } },
// Collection
Lang = "en",
Title = "title",
Size = 5,
Content = { new() },
Attribution = new()
};

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

private static void VerifyXmlSerialization(Collection sut)
{
XmlSerializer serializer = new(typeof(Collection));
using MemoryStream stream = new();
serializer.Serialize(stream, sut);

stream.Seek(0, SeekOrigin.Begin);
var result = new StreamReader(stream).ReadToEnd();
result.ShouldContain(sut);
}

private static void VerifyJsonSerialization(Collection sut)
{
JsonSerializerSettings jsonSettings = new()
{
NullValueHandling = NullValueHandling.Ignore
};

Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<Collection>(JsonConvert.SerializeObject(sut, jsonSettings), jsonSettings));
}
}
21 changes: 9 additions & 12 deletions Gedcomx.Model.Test/NameTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Xml.Serialization;

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

using Newtonsoft.Json;
Expand All @@ -21,7 +18,7 @@ public class NameTest
[Test]
public void NameEmpty()
{
var sut = new Name();
Name sut = new();

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
Expand All @@ -38,15 +35,15 @@ public void NameFilled()
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() },
Attribution = new(),
Sources = { new(), new() { Id = "S-1" } },
Analysis = new(),
Notes = { new() },
// Name
KnownType = NameType.BirthName,
Preferred = true,
Date = new DateInfo(),
NameForms = { new NameForm() }
Date = new(),
NameForms = { new() }
};

VerifyXmlSerialization(sut);
Expand All @@ -55,8 +52,8 @@ public void NameFilled()

private static void VerifyXmlSerialization(Name sut)
{
var serializer = new XmlSerializer(typeof(Name));
using var stream = new MemoryStream();
XmlSerializer serializer = new(typeof(Name));
using MemoryStream stream = new();
serializer.Serialize(stream, sut);

stream.Seek(0, SeekOrigin.Begin);
Expand Down
15 changes: 15 additions & 0 deletions Gedcomx.Model.Test/XmlAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Gx.Common;
using Gx.Conclusion;
using Gx.Links;
using Gx.Records;
using Gx.Source;

using NUnit.Framework;
Expand Down Expand Up @@ -134,6 +135,20 @@ public static void ShouldContain(this string result, SourceDescription sourceDes
result.ShouldContain(sourceDescription as HypermediaEnabledData);
}

public static void ShouldContain(this string result, Collection collection)
{
Assert.That(result, Does.Contain("<collection "));
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/\""));
result.ShouldContainAttribute("lang", collection.Lang);
result.ShouldContainElement("title", collection.Title);
Assert.That(result.Contains("<size"), Is.EqualTo(collection.SizeSpecified));
Assert.That(result.Contains("<content "), Is.EqualTo(collection.AnyContent()));
result.ShouldContainElement("attribution", collection.Attribution);
result.ShouldContain(collection as HypermediaEnabledData);
}

public static void ShouldContain(this string result, Document document)
{
Assert.That(result, Does.Contain("<document "));
Expand Down
2 changes: 0 additions & 2 deletions Gedcomx.Model/Attribution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

using Gedcomx.Model.Util;

using Gx.Model;

using Newtonsoft.Json;

namespace Gx.Common
Expand Down
38 changes: 16 additions & 22 deletions Gedcomx.Model/Collection.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Gedcomx.Model.Rt;
using Gx.Common;
// <auto-generated>
// <auto-generated>
//
//
// Generated by <a href="http://enunciate.codehaus.org">Enunciate</a>.
// </auto-generated>
using System;
using System.Collections.Generic;
using System.Linq;

using Gedcomx.Model.Rt;

using Gx.Common;

namespace Gx.Records
{
Expand Down Expand Up @@ -106,13 +109,18 @@ public System.Collections.Generic.List<Gx.Records.CollectionContent> Content
{
get
{
return this._content;
return this._content ?? (_content = new System.Collections.Generic.List<Gx.Records.CollectionContent>());
}
set
{
this._content = value;
}
}
public bool ShouldSerializeContent() => AnyContent();
public bool AnyContent()
{
return _content?.Any() ?? false;
}
/// <summary>
/// Attribution metadata for this collection.
/// </summary>
Expand Down Expand Up @@ -169,7 +177,10 @@ public Collection SetTitle(String title)
*/
public Collection SetContent(CollectionContent content)
{
AddContent(content);
if (content != null)
{
Content.Add(content);
}
return this;
}

Expand All @@ -195,22 +206,5 @@ public Collection SetSize(Int32 size)
Size = size;
return this;
}

/**
* Add a reference to the record content values being used as evidence.
*
* @param content The content to be added.
*/
public void AddContent(CollectionContent content)
{
if (content != null)
{
if (this._content == null)
{
this._content = new List<CollectionContent>();
}
this._content.Add(content);
}
}
}
}
Loading

0 comments on commit ebd2f48

Please sign in to comment.