-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding AfterModel action for two sided relationships
- Loading branch information
Raphael Hoppe
committed
Oct 22, 2024
1 parent
c367f97
commit 41b1d4b
Showing
13 changed files
with
296 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/Commons.Builders.Model.Tests/AfterModel/AfterModelTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using NUnit.Framework; | ||
|
||
using FluentAssertions; | ||
|
||
using Queo.Commons.Builders.Model.Examples; | ||
using Queo.Commons.Builders.Model.Examples.Relations; | ||
|
||
namespace Queo.Commons.Builders.Model.Tests.AfterModel; | ||
|
||
[TestFixture] | ||
public class AfterModelTests | ||
{ | ||
[Test] | ||
public void CreateCountry_WithPresident() | ||
{ | ||
Country c = Create.Country().WithName("USA") | ||
.WithPresident(p => p.WithName("Roosevelt")); | ||
|
||
c.Name.Should().Be("USA"); | ||
c.President?.Name.Should().Be("Roosevelt"); | ||
c.Should().Be(c.President?.Country); | ||
} | ||
|
||
[Test] | ||
public void CreatePresident_WithCountry() | ||
{ | ||
President p = Create.President().WithName("Roosevelt") | ||
.WithCountry(c => c.WithName("USA")); | ||
|
||
p.Name.Should().Be("Roosevelt"); | ||
p.Country.Name.Should().Be("USA"); | ||
p.Country.President.Should().Be(p); | ||
} | ||
|
||
[Test] | ||
public void CreateUser_WithOrg() | ||
{ | ||
User u = Create.User().WithName("George") | ||
.WithOrg(o => o.WithName("GOrg")); | ||
|
||
u.Name.Should().Be("George"); | ||
u.Org?.Name.Should().Be("GOrg"); | ||
u.Org?.Members.Should().Contain(u); | ||
} | ||
|
||
[Test] | ||
public void CreateOrg_WithUsers() | ||
{ | ||
Org o = Create.Org().WithName("GOrg") | ||
.WithAdmin(u => u.WithName("George")) | ||
.AddMember(u => u.WithName("Other")); | ||
|
||
o.Name.Should().Be("GOrg"); | ||
o.Admin.Name.Should().Be("George"); | ||
o.Admin.Org.Should().Be(o); | ||
o.Members.Should().Contain(m => m.Name.Equals("Other")); | ||
o.Members.Should().Contain(m => m.Name.Equals("George")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Queo.Commons.Builders.Model.Examples.Relations; | ||
|
||
public class Country(string name) | ||
{ | ||
public string Name => name; | ||
public President? President { set; get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
using System; | ||
|
||
using Queo.Commons.Builders.Model.Builder; | ||
using Queo.Commons.Builders.Model.Factory; | ||
|
||
namespace Queo.Commons.Builders.Model.Examples.Relations; | ||
|
||
public class CountryBuilder : ModelBuilder<Country> | ||
{ | ||
private string _name; | ||
private IBuilder<President>? _president; | ||
|
||
public CountryBuilder(IBuilderFactory factory) : base(factory) | ||
{ | ||
_name = $"Country-{BuilderIndex}"; | ||
_president = null; | ||
} | ||
|
||
public CountryBuilder WithName(string name) => Set(() => _name = name); | ||
public CountryBuilder WithPresident(Action<PresidentBuilder> buildAction) => Set(() => | ||
{ | ||
var builder = FromAction<PresidentBuilder, President>(buildAction); | ||
builder.WithCountry(this); | ||
_president = builder; | ||
}); | ||
|
||
protected override Country BuildModel() => new(_name); | ||
|
||
protected override void AfterModel(Country model) | ||
{ | ||
if (_president is not null) | ||
{ | ||
// President can only be build, if the country is already available | ||
model.President = _president.Build(); | ||
} | ||
} | ||
|
||
protected override CountryBuilder Set(Action action) => Set<CountryBuilder>(action); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Queo.Commons.Builders.Model.Examples.Relations; | ||
|
||
public class Org(string name, User admin) | ||
{ | ||
public string Name => name; | ||
public User Admin => admin; | ||
|
||
public ICollection<User> Members { get; } = []; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
|
||
using Queo.Commons.Builders.Model.Builder; | ||
using Queo.Commons.Builders.Model.Factory; | ||
|
||
namespace Queo.Commons.Builders.Model.Examples.Relations; | ||
|
||
public class OrgBuilder : ModelBuilder<Org> | ||
{ | ||
public string _name; | ||
public IBuilder<User> _admin; | ||
public BuilderCollection<UserBuilder, User> _members; | ||
|
||
public OrgBuilder(IBuilderFactory factory) : base(factory) | ||
{ | ||
_name = $"Org-{BuilderIndex}"; | ||
_admin = factory.Create<UserBuilder>(); | ||
|
||
_members = new(factory); | ||
} | ||
|
||
public OrgBuilder WithName(string name) => Set(() => _name = name); | ||
|
||
public OrgBuilder WithAdmin(IBuilder<User> admin) => Set(() => _admin = admin); | ||
public OrgBuilder WithAdmin(Action<UserBuilder> buildAction) => Set(() => | ||
{ | ||
_admin = FromAction<UserBuilder, User>(buildAction); | ||
}); | ||
|
||
public OrgBuilder AddMember(IBuilder<User> member) => Set(() => _members.Add(member)); | ||
public OrgBuilder AddMember(Action<UserBuilder> buildAction) => Set(() => | ||
{ | ||
_members.Add(buildAction); | ||
}); | ||
|
||
protected override Org BuildModel() | ||
{ | ||
User admin = _admin.Build(); | ||
Org o = new Org(_name, _admin.Build()); | ||
|
||
admin.Org = o; | ||
o.Members.Add(admin); | ||
|
||
foreach (var member in _members.BuildModels()) | ||
{ | ||
member.Org = o; | ||
o.Members.Add(member); | ||
} | ||
|
||
return o; | ||
} | ||
|
||
protected override OrgBuilder Set(Action action) => Set<OrgBuilder>(action); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Queo.Commons.Builders.Model.Examples.Relations; | ||
|
||
public class President(string name, Country country) | ||
{ | ||
public string Name => name; | ||
public Country Country => country; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
using System; | ||
|
||
using Queo.Commons.Builders.Model.Builder; | ||
using Queo.Commons.Builders.Model.Factory; | ||
|
||
namespace Queo.Commons.Builders.Model.Examples.Relations; | ||
|
||
public class PresidentBuilder : ModelBuilder<President> | ||
{ | ||
private string _name; | ||
private IBuilder<Country> _country; | ||
|
||
public PresidentBuilder(IBuilderFactory factory) : base(factory) | ||
{ | ||
_name = $"President-{BuilderIndex}"; | ||
_country = factory.Create<CountryBuilder>(); | ||
} | ||
|
||
public PresidentBuilder WithName(string name) => Set(() => _name = name); | ||
public PresidentBuilder WithCountry(IBuilder<Country> country) => Set(() => _country = country); | ||
public PresidentBuilder WithCountry(Action<CountryBuilder> buildAction) => Set(() => | ||
{ | ||
_country = FromAction<CountryBuilder, Country>(buildAction); | ||
}); | ||
|
||
protected override President BuildModel() | ||
{ | ||
President p = new(_name, _country.Build()); | ||
_country.Build().President = p; | ||
|
||
return p; | ||
} | ||
protected override PresidentBuilder Set(Action action) => Set<PresidentBuilder>(action); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Queo.Commons.Builders.Model.Examples.Relations; | ||
|
||
public class User(string name) | ||
{ | ||
public string Name = name; | ||
public Org? Org { set; get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
using System; | ||
|
||
using Queo.Commons.Builders.Model.Builder; | ||
using Queo.Commons.Builders.Model.Factory; | ||
|
||
namespace Queo.Commons.Builders.Model.Examples.Relations; | ||
|
||
public class UserBuilder : ModelBuilder<User> | ||
{ | ||
private string _name; | ||
private IBuilder<Org>? _org; | ||
|
||
public UserBuilder(IBuilderFactory factory) : base(factory) | ||
{ | ||
_name = $"User-{BuilderIndex}"; | ||
_org = null; | ||
} | ||
|
||
public UserBuilder WithName(string name) => Set(() => _name = name); | ||
public UserBuilder WithOrg(IBuilder<Org> org) => Set(() => _org = org); | ||
public UserBuilder WithOrg(Action<OrgBuilder> buildAction) => Set(() => | ||
{ | ||
_org = FromAction<OrgBuilder, Org>(buildAction); | ||
}); | ||
|
||
protected override User BuildModel() => new(_name); | ||
|
||
protected override void AfterModel(User model) | ||
{ | ||
if (_org is not null) | ||
{ | ||
var org = _org.Build(); | ||
model.Org = org; | ||
org.Members.Add(model); | ||
} | ||
} | ||
|
||
protected override UserBuilder Set(Action action) => Set<UserBuilder>(action); | ||
} |