Skip to content

Commit

Permalink
adding and refactoring InMemory Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calebjenkins committed Jan 8, 2024
1 parent 0bda86d commit 5cb08b8
Showing 1 changed file with 101 additions and 37 deletions.
138 changes: 101 additions & 37 deletions src/KeyValueRepoTests/InMemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,46 @@

using System.Security.Principal;

[Collection("RepoTests")]
public class InMemoryTests{
namespace Calebs.Data.KeyValueRepoTests;

[Collection("RepoTests")]
public class InMemoryTests
{
public virtual IKeyValueRepo GetNewRepo()
{
return new KeyValueInMemory();
}

private async Task<IKeyValueRepo> getRepoWithPerson(Person p, string TestName)
private async Task<IKeyValueRepo> getRepoWithRecords(Person p, string TestName)
{
var people = new List<Person>() { p };
return await getRepoWithRecords(people, TestName);
}

private async Task<IKeyValueRepo> getRepoWithRecords(IList<Person> People, string TestName)
{
IKeyValueRepo repo = GetNewRepo();

var TEST_Name = TestName;
IIdentity ident = new GenericIdentity(TEST_Name);
IPrincipal princ = new GenericPrincipal(ident, null);
Thread.CurrentPrincipal = princ;

await repo.Update<Person>(p.Id, p);
foreach (Person p in People)
{
await repo.Update(p.Id, p);
}

return repo;
}

[Fact]
public async Task Updates_Populate_Meta_Info()
public async Task Update_PopulatesMetaInfo()
{
var p = new Person("Test1", "Last", 1);
var TEST_Name = "test name";

IKeyValueRepo repo = await getRepoWithPerson(p, TEST_Name);
IKeyValueRepo repo = await getRepoWithRecords(p, TEST_Name);

var result = await repo.GetMeta<Person>(p.Id);
result?.CreatedBy.Should().Be(TEST_Name);
Expand All @@ -44,43 +55,35 @@ public async Task Updates_Populate_Meta_Info()
[Fact]
public async Task GetMetaAll_ReturnsAllForType()
{
var p = new Person("Test1", "Last", 1);
var people = new List<Person>()
{
new Person("Test1", "Last", 1),
new Person("Test2", "Last2", 2),
new Person("Test3", "Last3", 3)
};
var TEST_Name = "test name";

IKeyValueRepo repo = await getRepoWithPerson(p, TEST_Name);
var p2 = new Person("Test2", "Last2", 2);
await repo.Update<Person>(p2.Id, p2);
IKeyValueRepo repo = await getRepoWithRecords(people, TEST_Name);


var results = await repo.GetMetaAll<Person>();
results.Count.Should().BeGreaterThan(1);
results.Count.Should().BeGreaterThan(2);
}

[Fact]
public async Task GetHistoryShouldReturnMetaList()
public async Task GetHistory_ShouldReturnMetaList()
{
var p = new Person("Test1", "Last", 1);
var TEST_Name = "test name";

IKeyValueRepo repo = await getRepoWithPerson(p, TEST_Name);
IKeyValueRepo repo = await getRepoWithRecords(p, TEST_Name);

var results = await repo.GetHistory<Person>(p.Id);
results?.Count.Should().Be(1);
results?.First()?.Value?.First.Should().Be(p.First);
results?.First()?.CreatedBy.Should().Be(TEST_Name);
}

[Fact]
public async Task GetMetaAll()
{
var p = new Person("Test1", "Last", 1);
var TEST_Name = "test name";

IKeyValueRepo repo = await getRepoWithPerson(p, TEST_Name);

var results = await repo. GetHistory<Person>(p.Id);
results?.Count.Should().Be(1);
results?.Count.Should().BeGreaterThan(0);
results?.First()?.Value?.First.Should().Be(p.First);
results?.First()?.Value?.Last.Should().Be(p.Last);
results?.First()?.CreatedBy.Should().Be(TEST_Name);
results?.First()?.UpdatedBy.Should().Be(TEST_Name);
}

[Fact]
Expand All @@ -93,26 +96,36 @@ public async Task RepoShouldHoldValues()

var p2 = await repo.Get<Person>("1");
p2.Should().NotBeNull();
p2?.First.Should().Be("Test");
p2?.First.Should().Be(p.First);
p2?.Last.Should().Be(p.Last);

var p3 = p2 with { First = "Test2" };

Check warning on line 102 in src/KeyValueRepoTests/InMemoryTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 102 in src/KeyValueRepoTests/InMemoryTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 102 in src/KeyValueRepoTests/InMemoryTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
await repo.Update(p3.Id.ToString(), p3);

var p4 = await repo.Get<Person>(1);
p4?.First.Should().Be("Test2");
p4?.First.Should().Be(p3.First);
}

[Fact]
public async Task Get_ShouldReturnVoidForUnknownTypes()
{
IKeyValueRepo repo = GetNewRepo();
var p = await repo.Get<UnusedType>("1");

p.Should().BeNull();
}

[Fact]
public async Task ShouldReturnVoidForUnknownTypes()
public async Task GetMeta_ShouldReturnVoidForUnknownTypes()
{
IKeyValueRepo repo = GetNewRepo();
var p = await repo.Get<Person>("1");
var p = await repo.GetMeta<UnusedType>("1");

p.Should().BeNull();
}

[Fact]
public async Task MissingIdShouldReturnNull()
public async Task Get_ShouldReturnNullForMissingIds()
{
IKeyValueRepo repo = GetNewRepo();
var p1 = new Person("Kelly", "Burkhardt", 1);
Expand All @@ -130,7 +143,25 @@ public async Task MissingIdShouldReturnNull()
}

[Fact]
public async Task GetAllShouldReturnAllInstances()
public async Task GetMeta_ShouldReturnNullForMissingIds()
{
IKeyValueRepo repo = GetNewRepo();
var p1 = new Person("Kelly", "Burkhardt", 1);
await repo.Update(p1.Id, p1);

var p2 = await repo.GetMeta<Person>(1);
p2.Should().NotBeNull();

var nope = await repo.GetMeta<UnusedType>(1);
nope.Should().BeNull();

var randomId = Guid.NewGuid().ToString().Substring(0, 8);
var p3 = await repo.GetMeta<Person>(randomId);
p3.Should().BeNull();
}

[Fact]
public async Task GetAll_ShouldReturnAllInstances()
{
IKeyValueRepo repo = GetNewRepo();
var p1 = new Person("Kelly", "Burkhardt", 1);
Expand Down Expand Up @@ -160,8 +191,41 @@ public async Task GetAllShouldReturnAllInstances()
people.Count().Should().BeGreaterThanOrEqualTo(4);
locals.Count().Should().BeGreaterThanOrEqualTo(2);
}

[Fact]
public async Task GetMetaAll_ShouldReturnAllInstances()
{
IKeyValueRepo repo = GetNewRepo();
var p1 = new Person("Kelly", "Burkhardt", 1);
var p2 = new Person("Drew", "Wu", 2);
var p3 = new Person("Monroe", "", 3);

await repo.Update(p1.Id, p1);
await repo.Update(p2.Id, p2);
await repo.Update(p3.Id.ToString(), p3);

var people = await repo.GetMetaAll<Person>();
people.Count.Should().BeGreaterThanOrEqualTo(3);
// Repo holds records from previous test runs
// Either need to fully reset DB each time or maybe add a Remove / RemoveAll methods

var l1 = new Location("1", "123 Main", "Dallas");
var l2 = new Location("2", "456 Front St.", "Tulsa");

await repo.Update(l1.Id, l1);
await repo.Update(l2.Id, l2);

var p4 = new Person("Nick", "Burkhardt", 4);
await repo.Update(p4.Id.ToString(), p4);

people = await repo.GetMetaAll<Person>();
var locals = await repo.GetMetaAll<Location>();

people.Count().Should().BeGreaterThanOrEqualTo(4);
locals.Count().Should().BeGreaterThanOrEqualTo(2);
}
}

public record Person (string First, string Last, int Id);
public record Location (string Id, string Street, string City);
public record Person(string First, string Last, int Id);
public record Location(string Id, string Street, string City);
public record UnusedType();

0 comments on commit 5cb08b8

Please sign in to comment.