Skip to content

Commit 3b227f0

Browse files
committed
Add Constellation to the universe model, we need a many-to-many relationship for tests
1 parent b97fdd4 commit 3b227f0

File tree

7 files changed

+52
-0
lines changed

7 files changed

+52
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using JetBrains.Annotations;
3+
using JsonApiDotNetCore.Resources;
4+
using JsonApiDotNetCore.Resources.Annotations;
5+
6+
namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceDefinitions.Reading;
7+
8+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
9+
[Resource(ControllerNamespace = "JsonApiDotNetCoreTests.IntegrationTests.ResourceDefinitions.Reading")]
10+
public sealed class Constellation : Identifiable<int>
11+
{
12+
[Attr]
13+
public string Name { get; set; } = null!;
14+
15+
[Attr]
16+
[Required]
17+
public Season? VisibleDuring { get; set; }
18+
19+
[HasMany]
20+
public ISet<Star> Stars { get; set; } = new HashSet<Star>();
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using JetBrains.Annotations;
2+
using JsonApiDotNetCore.Configuration;
3+
4+
namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceDefinitions.Reading;
5+
6+
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
7+
public sealed class ConstellationDefinition(IResourceGraph resourceGraph, ResourceDefinitionHitCounter hitCounter)
8+
: HitCountingResourceDefinition<Moon, int>(resourceGraph, hitCounter)
9+
{
10+
protected override ResourceDefinitionExtensibilityPoints ExtensibilityPointsToTrack => ResourceDefinitionExtensibilityPoints.Reading;
11+
}

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceDefinitions/Reading/ResourceDefinitionReadTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public ResourceDefinitionReadTests(IntegrationTestContext<TestableStartup<Univer
2323

2424
testContext.ConfigureServices(services =>
2525
{
26+
services.AddResourceDefinition<ConstellationDefinition>();
2627
services.AddResourceDefinition<StarDefinition>();
2728
services.AddResourceDefinition<PlanetDefinition>();
2829
services.AddResourceDefinition<MoonDefinition>();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceDefinitions.Reading;
2+
3+
public enum Season
4+
{
5+
Winter,
6+
Spring,
7+
Summer,
8+
Fall
9+
}

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceDefinitions/Reading/Star.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ public sealed class Star : Identifiable<int>
2525

2626
[HasMany]
2727
public ISet<Planet> Planets { get; set; } = new HashSet<Planet>();
28+
29+
[HasMany]
30+
public ISet<Constellation> IsPartOf { get; set; } = new HashSet<Constellation>();
2831
}

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceDefinitions/Reading/UniverseDbContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceDefinitions.Reading;
88
public sealed class UniverseDbContext(DbContextOptions<UniverseDbContext> options)
99
: TestableDbContext(options)
1010
{
11+
public DbSet<Constellation> Constellations => Set<Constellation>();
1112
public DbSet<Star> Stars => Set<Star>();
1213
public DbSet<Planet> Planets => Set<Planet>();
1314
public DbSet<Moon> Moons => Set<Moon>();

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceDefinitions/Reading/UniverseFakers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace JsonApiDotNetCoreTests.IntegrationTests.ResourceDefinitions.Reading;
88

99
internal sealed class UniverseFakers
1010
{
11+
private readonly Lazy<Faker<Constellation>> _lazyConstellationFaker = new(() => new Faker<Constellation>()
12+
.MakeDeterministic()
13+
.RuleFor(constellation => constellation.Name, faker => faker.Random.Word())
14+
.RuleFor(constellation => constellation.VisibleDuring, faker => faker.PickRandom<Season>()));
15+
1116
private readonly Lazy<Faker<Star>> _lazyStarFaker = new(() => new Faker<Star>()
1217
.MakeDeterministic()
1318
.RuleFor(star => star.Name, faker => faker.Random.Word())
@@ -27,6 +32,7 @@ internal sealed class UniverseFakers
2732
.RuleFor(moon => moon.Name, faker => faker.Random.Word())
2833
.RuleFor(moon => moon.SolarRadius, faker => faker.Random.Decimal(.01M, 1000M)));
2934

35+
public Faker<Constellation> Constellation => _lazyConstellationFaker.Value;
3036
public Faker<Star> Star => _lazyStarFaker.Value;
3137
public Faker<Planet> Planet => _lazyPlanetFaker.Value;
3238
public Faker<Moon> Moon => _lazyMoonFaker.Value;

0 commit comments

Comments
 (0)