Skip to content

Commit

Permalink
fix: broken PK detection for StandaloneEntities
Browse files Browse the repository at this point in the history
  • Loading branch information
ascott18 committed Jul 24, 2024
1 parent c046362 commit 2d6d9dd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using IntelliTect.Coalesce.Api;
using IntelliTect.Coalesce.Models;
using IntelliTect.Coalesce.Tests.Fixtures;
using IntelliTect.Coalesce.Tests.TargetClasses;
using IntelliTect.Coalesce.Tests.TargetClasses.TestDbContext;
using IntelliTect.Coalesce.Tests.Util;
using Microsoft.EntityFrameworkCore;
Expand All @@ -20,11 +22,11 @@ public StandardBehaviorsTests() : base()
}

private StandardDataSource<T, AppDbContext> Source<T>()
where T : class, new()
where T : class
=> new StandardDataSource<T, AppDbContext>(CrudContext);

private StandardBehaviors<T, AppDbContext> Behaviors<T>()
where T : class, new()
where T : class
=> new StandardBehaviors<T, AppDbContext>(CrudContext);

private class TransformDs : StandardDataSource<Case, AppDbContext>
Expand Down Expand Up @@ -80,5 +82,49 @@ public async Task Save_WhenDataSourceIsUntracked_RetracksEntityAndSaves()
ds.Db.ChangeTracker.Clear();
Assert.Equal("new desc", ds.Db.Cases.Single().Description);
}

[Fact]
public async Task DetermineSaveKindAsync_CanDeterminePkFromEfEntity()
{
// Arrange
var behaviors = Behaviors<RequiredAndInitModel>();

// Act
var result = await behaviors.DetermineSaveKindAsync(new RequiredAndInitModelParameterDto { Id = 42 }, Source<RequiredAndInitModel>(), new DataSourceParameters());

// Assert
Assert.Equal((SaveKind.Update, 42), result);
}

class RequiredAndInitModelParameterDto : GeneratedParameterDto<RequiredAndInitModel>
{
public int Id { get; set; }

public override void MapTo(RequiredAndInitModel obj, IMappingContext context) => throw new System.NotImplementedException();

public override RequiredAndInitModel MapToNew(IMappingContext context) => throw new System.NotImplementedException();
}

[Fact]
public async Task DetermineSaveKindAsync_CanDeterminePkFromStandaloneEntity()
{
// Arrange
var behaviors = Behaviors<StandaloneReadWrite>();

// Act
var result = await behaviors.DetermineSaveKindAsync(new StandaloneReadWriteDto { Id = 42 }, Source<StandaloneReadWrite>(), new DataSourceParameters());

// Assert
Assert.Equal((SaveKind.Update, 42), result);
}

class StandaloneReadWriteDto : GeneratedParameterDto<StandaloneReadWrite>
{
public int Id { get; set; }

public override void MapTo(StandaloneReadWrite obj, IMappingContext context) => throw new System.NotImplementedException();

public override StandaloneReadWrite MapToNew(IMappingContext context) => throw new System.NotImplementedException();
}
}
}
19 changes: 16 additions & 3 deletions src/IntelliTect.Coalesce/Api/Behaviors/StandardBehaviors`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using System.Security.Claims;
using System.Threading.Tasks;

Expand Down Expand Up @@ -95,11 +96,23 @@ IDataSourceParameters parameters
where TDto : class, IParameterDto<T>, new()
{
var dtoClassViewModel = Context.ReflectionRepository.GetClassViewModel<TDto>()!;
var dtoPkInfo = dtoClassViewModel.PrimaryKey ?? throw new InvalidOperationException("Data sources cannot save items that lack a PK");
var pk = ClassViewModel.PrimaryKey ?? throw new InvalidOperationException("Data sources cannot save items that lack a PK");
PropertyViewModel dtoPkInfo;

if (incomingDto is GeneratedParameterDto<T> genDto)
{
// For generated DTOs, simplify by matching the PK name since it will always match.
dtoPkInfo = dtoClassViewModel.PropertyByName(pk.Name)!;
}
else
{
// This path is really only for custom DTOs
dtoPkInfo = dtoClassViewModel.PrimaryKey ?? throw new InvalidOperationException("Data sources cannot save items that lack a PK");
}

object? idValue = dtoPkInfo.PropertyInfo.GetValue(incomingDto);

if (ClassViewModel.PrimaryKey?.DatabaseGenerated == DatabaseGeneratedOption.None)
if (pk.DatabaseGenerated == DatabaseGeneratedOption.None)
{
// PK is not database generated.
// We have to look for the existing object to determine if
Expand Down Expand Up @@ -129,7 +142,7 @@ IDataSourceParameters parameters
// !IsNullable handles non-Nullable<T> value types.
if (dtoPkInfo.Type.IsReferenceOrNullableValue
? idValue == null
: idValue!.Equals(Activator.CreateInstance(dtoClassViewModel.PrimaryKey.Type.TypeInfo)))
: idValue!.Equals(Activator.CreateInstance(dtoPkInfo.Type.TypeInfo)))
{
return (SaveKind.Create, null);
}
Expand Down

0 comments on commit 2d6d9dd

Please sign in to comment.