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
- RecordSet.
  • Loading branch information
JoergHoffmannatGitHub committed Mar 27, 2023
1 parent 827bafd commit 33a9abf
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 39 deletions.
65 changes: 65 additions & 0 deletions Gedcomx.Model.Test/RecordSetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Xml.Serialization;

using Gx.Records;

using Newtonsoft.Json;

using NUnit.Framework;

namespace Gedcomx.Model.Test;

/// <summary>
/// Test calss for <see cref="RecordSet"/>
/// </summary>
[TestFixture]
public class RecordSetTest
{
[Test]
public void RecordSetEmpty()
{
RecordSet sut = new();

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

[Test]
public void RecordSetObjectInitialization()
{
RecordSet sut = new()
{
// ExtensibleData
Id = "F-1",
// HypermediaEnabledData
Links = { new(), { "rel", new Uri("https://www.familysearch.org/platform/collections/tree") }, { "rel", "template" } },
// RecordSet
Lang = "lang",
Metadata = new(),
Records = { new() }
};

VerifyXmlSerialization(sut);
VerifyJsonSerialization(sut);
}

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

Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<RecordSet>(JsonConvert.SerializeObject(sut, jsonSettings), jsonSettings));
}
}
11 changes: 11 additions & 0 deletions Gedcomx.Model.Test/XmlAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ public static void ShouldContain(this string result, RecordDescriptor recordDesc
result.ShouldContain(recordDescriptor as HypermediaEnabledData);
}

public static void ShouldContain(this string result, RecordSet recordSet)
{
Assert.That(result, Does.Contain("<records "));
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.Contains("lang="), Is.EqualTo(recordSet.Lang != null));
Assert.That(result.Contains("<metadata"), Is.EqualTo(recordSet.Metadata != null));
Assert.That(result.Contains("<record "), Is.EqualTo(recordSet.AnyRecords()));
result.ShouldContain(recordSet as HypermediaEnabledData);
}

public static void ShouldContain(this string result, FieldValueDescriptor fieldValueDescriptor)
{
Assert.That(result, Does.Contain("<FieldValueDescriptor "));
Expand Down
17 changes: 9 additions & 8 deletions Gedcomx.Model/RecordSet.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// <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;

namespace Gx.Records
{
Expand Down Expand Up @@ -65,13 +65,18 @@ public System.Collections.Generic.List<Gx.Gedcomx> Records
{
get
{
return this._records;
return this._records ?? (_records = new System.Collections.Generic.List<Gx.Gedcomx>());
}
set
{
this._records = value;
}
}
public bool ShouldSerializeRecords() => AnyRecords();
public bool AnyRecords()
{
return _records?.Any() ?? false;
}

/**
* Build out this record set with a lang.
Expand Down Expand Up @@ -103,11 +108,7 @@ public RecordSet SetMetadata(Gedcomx metadata)
*/
public RecordSet SetRecord(Gedcomx record)
{
if (this._records == null)
{
this._records = new List<Gedcomx>();
}
this._records.Add(record);
Records.Add(record);
return this;
}
}
Expand Down
Loading

0 comments on commit 33a9abf

Please sign in to comment.