Skip to content

Commit

Permalink
Merge pull request #71 from jinaga:compare-proxy
Browse files Browse the repository at this point in the history
Improved comparison semantics with proxies
  • Loading branch information
michaellperry authored Feb 15, 2024
2 parents 712f54a + 1c840d4 commit b8d4ecb
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"michaelperry"
]
}
15 changes: 15 additions & 0 deletions Jinaga.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jinaga.Tool", "Jinaga.Tool\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jinaga.SourceGenerator", "Jinaga.SourceGenerator\Jinaga.SourceGenerator.csproj", "{5A6E4D77-8EE6-4145-884B-4AA0165BC226}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jinaga.SourceGenerator.Test", "Jinaga.SourceGenerator.Test\Jinaga.SourceGenerator.Test.csproj", "{829E3DB2-6C03-4070-B404-72DC7F44F2A3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -145,11 +147,24 @@ Global
{5A6E4D77-8EE6-4145-884B-4AA0165BC226}.Release|iPhone.Build.0 = Release|Any CPU
{5A6E4D77-8EE6-4145-884B-4AA0165BC226}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{5A6E4D77-8EE6-4145-884B-4AA0165BC226}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Debug|iPhone.Build.0 = Debug|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Release|Any CPU.Build.0 = Release|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Release|iPhone.ActiveCfg = Release|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Release|iPhone.Build.0 = Release|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{829E3DB2-6C03-4070-B404-72DC7F44F2A3}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{829E3DB2-6C03-4070-B404-72DC7F44F2A3} = {9928AE02-0EF9-41A8-B439-9D5BD0003B43}
{5A6E4D77-8EE6-4145-884B-4AA0165BC226} = {9928AE02-0EF9-41A8-B439-9D5BD0003B43}
{08D2D6FD-7E46-4B9C-8EBB-BF8EF1D0974B} = {554E5584-2CBE-4BCE-91C7-717994FAAE3A}
{B907D523-F870-40D2-A40E-C704E38AC42C} = {554E5584-2CBE-4BCE-91C7-717994FAAE3A}
Expand Down
2 changes: 2 additions & 0 deletions Jinaga.SourceGenerator.Test/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
global using FluentAssertions;
global using Xunit;
27 changes: 27 additions & 0 deletions Jinaga.SourceGenerator.Test/Jinaga.SourceGenerator.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Jinaga\Jinaga.csproj" />
<ProjectReference Include="..\Jinaga.UnitTest\Jinaga.UnitTest.csproj" />
<ProjectReference Include="..\Jinaga.SourceGenerator\Jinaga.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
21 changes: 21 additions & 0 deletions Jinaga.SourceGenerator.Test/Model/Blog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Jinaga.SourceGenerator.Test.Model;

[FactType("Blog.Site")]
public partial record Site(User creator, string identifier) { }

[FactType("Blog.GuestBlogger")]
public partial record GuestBlogger(Site site, User guest) { }

[FactType("Blog.Content")]
public partial record Content(Site site, string path) { }

[FactType("Blog.Content")]
public partial record ContentV2(Site site, DateTime? createdAt) { }

[FactType("Blog.Comment")]
public partial record Comment(Content content, Guid uniqueId, User author) { }

[FactType("Blog.Content.Publish")]
public partial record Publish(Content content, DateTime date) { }
109 changes: 109 additions & 0 deletions Jinaga.SourceGenerator.Test/Specifications/ProjectionVersioningTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using Jinaga.UnitTest;

namespace Jinaga.SourceGenerator.Test.Specifications;

public class ProjectionVersioningTest
{
[Fact]
public async Task CanReadVersionOneFieldFromVersionOneFact()
{
var jinagaClient = JinagaTest.Create();
var site = await jinagaClient.Fact(new Model.Site(new User("Michael"), "michaelperry.net"));
var content = await jinagaClient.Fact(new Model.Content(site, "index.html"));
var originalHash = jinagaClient.Hash(content);

var contentPathForSite = Given<Model.Site>.Match((site, facts) =>
from content in facts.OfType<Model.Content>()
where content.site == site
select new
{
content,
content.path,
hash = jinagaClient.Hash(content)
}
);
var contentPath = await jinagaClient.Query(contentPathForSite, site);

var projection = contentPath.Should().ContainSingle().Subject;
projection.path.Should().Be("index.html");
projection.hash.Should().Be(originalHash);
bool creatorEqual = projection.content.site.creator == content.site.creator;
creatorEqual.Should().BeTrue();
bool siteEqual = projection.content.site == content.site;
siteEqual.Should().BeTrue();
projection.content.Should().Be(content);
}

[Fact]
public async Task CanReadVersionTwoFieldFromVersionOneFact()
{
var jinagaClient = JinagaTest.Create();
var site = await jinagaClient.Fact(new Model.Site(new User("Michael"), "michaelperry.net"));
var contentV1 = await jinagaClient.Fact(new Model.Content(site, "index.html"));
var originalHash = jinagaClient.Hash(contentV1);

var contentCreatedAtForSite = Given<Model.Site>.Match((site, facts) =>
from content in facts.OfType<Model.ContentV2>()
where content.site == site
select new
{
content.createdAt,
hash = jinagaClient.Hash(content),
content
}
);
var contentCreatedAt = await jinagaClient.Query(contentCreatedAtForSite, site);

var projection = contentCreatedAt.Should().ContainSingle().Subject;
projection.createdAt.Should().BeNull();
projection.hash.Should().Be(originalHash);
jinagaClient.Hash(projection.content).Should().Be(originalHash);
}

[Fact]
public async Task CanReadVersionTwoFieldFromVersionTwoFact()
{
var jinagaClient = JinagaTest.Create();
var site = await jinagaClient.Fact(new Model.Site(new User("Michael"), "michaelperry.net"));
var contentV2 = await jinagaClient.Fact(new Model.ContentV2(site, new DateTime(2021, 1, 1).ToUniversalTime()));
var originalHash = jinagaClient.Hash(contentV2);

var contentCreatedAtForSite = Given<Model.Site>.Match((site, facts) =>
from content in facts.OfType<Model.ContentV2>()
where content.site == site
select new
{
content.createdAt,
hash = jinagaClient.Hash(content)
}
);
var contentCreatedAt = await jinagaClient.Query(contentCreatedAtForSite, site);

contentCreatedAt.Should().ContainSingle().Which.createdAt.Should().Be(new DateTime(2021, 1, 1).ToUniversalTime());
contentCreatedAt.Should().ContainSingle().Which.hash.Should().Be(originalHash);
}

[Fact]
public async Task CanReadVersionOneFieldFromVersionTwoFact()
{
var jinagaClient = JinagaTest.Create();
var site = await jinagaClient.Fact(new Model.Site(new User("Michael"), "michaelperry.net"));
var contentV2 = await jinagaClient.Fact(new Model.ContentV2(site, new DateTime(2021, 1, 1)));
var originalHash = jinagaClient.Hash(contentV2);

var contentPathForSite = Given<Model.Site>.Match((site, facts) =>
from content in facts.OfType<Model.Content>()
where content.site == site
select new
{
content.path,
hash = jinagaClient.Hash(content)
}
);
var contentPath = await jinagaClient.Query(contentPathForSite, site);

var projection = contentPath.Should().ContainSingle().Subject;
projection.path.Should().Be("");
projection.hash.Should().Be(originalHash);
}
}
20 changes: 20 additions & 0 deletions Jinaga/Facts/FactGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,25 @@ public FactGraph GetSubgraph(FactReference reference)
subgraph = subgraph.Add(fact);
return subgraph;
}

public override bool Equals(object obj)
{
// Two graphs are equal if they contain the same fact references.
if (obj is FactGraph other)
{
return topologicalOrder.SequenceEqual(other.topologicalOrder);
}
else
{
return false;
}
}

public override int GetHashCode()
{
return topologicalOrder.Any()
? Last.Hash.GetHashCode()
: 0;
}
}
}

0 comments on commit b8d4ecb

Please sign in to comment.