Skip to content

Commit

Permalink
Add property value tests for SaveRequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanceylan committed Jan 27, 2025
1 parent c9fd3eb commit 117c800
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,8 @@ public void Create_Allows_If_User_Has_TheInsertPermission()

[InsertPermission(insertPermission)]
[UpdatePermission(updatePermission)]
private class TestRow : Row<TestRow.RowFields>, IIdRow
private class TestRow : IdNameRow<TestRow.RowFields>
{
[IdProperty, Identity]
public int? Id { get => fields.Id[this]; set => fields.Id[this] = value; }

[NameProperty]
public string Name { get => fields.Name[this]; set => fields.Name[this] = value; }
public class RowFields : RowFieldsBase
{
public Int32Field Id;
public StringField Name;
}
public class RowFields : IdNameRowFields {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace Serenity.Services;

public partial class SaveRequestHandler_Create_PropertyValue_Tests
{
[Fact]
public void Public_PropertyValues_AreAsExpected()
{
var context = new NullRequestContext().WithPermissions(x => true);
var handler = new SaveRequestHandler<TestRow>(context);
using var connection = new MockDbConnection();
var uow = new MockUnitOfWork(connection);
connection.InterceptManipulateRow(args => 123L);
var row = new TestRow() { Name = "Test" };
var request = new SaveRequest<TestRow>()
{
Entity = row
};
handler.Create(uow, request);

Assert.True(handler.IsCreate);
Assert.False(handler.IsUpdate);
Assert.NotNull(handler.Row);
Assert.Null(handler.Old);

Assert.Equal(context, handler.Context);
Assert.Equal(context.Cache, handler.Cache);
Assert.Equal(context.Localizer, handler.Localizer);
Assert.Equal(context.Permissions, handler.Permissions);
Assert.Equal(context.User, handler.User);
Assert.Equal(connection, handler.Connection);
Assert.Equal(uow, handler.UnitOfWork);
Assert.Equal(request, handler.Request);
Assert.NotNull(handler.Response);
Assert.Equal(123L, handler.Response.EntityId);
Assert.NotNull(handler.StateBag);

ISaveRequestHandler ihandler = handler;
Assert.True(ihandler.IsCreate);
Assert.False(ihandler.IsUpdate);
Assert.Equal(handler.Row, ihandler.Row);
Assert.Null(ihandler.Old);

Assert.Equal(handler.Context, ihandler.Context);
Assert.Equal(handler.Connection, ihandler.Connection);
Assert.Equal(handler.UnitOfWork, ihandler.UnitOfWork);
Assert.Equal(handler.Request, ihandler.Request);
Assert.Equal(handler.Response, ihandler.Response);
Assert.Equal(handler.StateBag, ihandler.StateBag);
}

private class TestRow : IdNameRow<TestRow.RowFields>
{
public class RowFields : IdNameRowFields { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace Serenity.Services;

public partial class SaveRequestHandler_Update_PropertyValue_Tests
{
[Fact]
public void Public_PropertyValues_AreAsExpected()
{
var context = new NullRequestContext().WithPermissions(x => true);
var handler = new SaveRequestHandler<TestRow>(context);
using var connection = new MockDbConnection();
var uow = new MockUnitOfWork(connection);
var existing = new TestRow() { Id = 123, Name = "OldTest" };
connection.InterceptExecuteReader(args => new MockDbDataReader(new
{
Id = 123,
Name = "OldTest"
}));
connection.InterceptManipulateRow(args => 1);
var row = new TestRow() { Id = 123, Name = "NewTest" };
var request = new SaveRequest<TestRow>()
{
Entity = row
};
handler.Update(uow, request);

Assert.False(handler.IsCreate);
Assert.True(handler.IsUpdate);

Assert.NotNull(handler.Row);
Assert.Equal(123, handler.Row.Id);
Assert.Equal("NewTest", handler.Row.Name);

Assert.NotNull(handler.Old);
Assert.Equal(123, handler.Old.Id);
Assert.Equal("OldTest", handler.Old.Name);

Assert.Equal(context, handler.Context);
Assert.Equal(context.Cache, handler.Cache);
Assert.Equal(context.Localizer, handler.Localizer);
Assert.Equal(context.Permissions, handler.Permissions);
Assert.Equal(context.User, handler.User);
Assert.Equal(connection, handler.Connection);
Assert.Equal(uow, handler.UnitOfWork);
Assert.Equal(request, handler.Request);
Assert.NotNull(handler.Response);
Assert.Equal(123, handler.Response.EntityId);
Assert.NotNull(handler.StateBag);

ISaveRequestHandler ihandler = handler;
Assert.False(ihandler.IsCreate);
Assert.True(ihandler.IsUpdate);
Assert.Equal(handler.Row, ihandler.Row);
Assert.Equal(handler.Old, ihandler.Old);

Assert.Equal(handler.Context, ihandler.Context);
Assert.Equal(handler.Connection, ihandler.Connection);
Assert.Equal(handler.UnitOfWork, ihandler.UnitOfWork);
Assert.Equal(handler.Request, ihandler.Request);
Assert.Equal(handler.Response, ihandler.Response);
Assert.Equal(handler.StateBag, ihandler.StateBag);
}

private class TestRow : IdNameRow<TestRow.RowFields>
{
public class RowFields : IdNameRowFields { }
}
}
19 changes: 19 additions & 0 deletions tests/Serenity.Net.Tests/testutils/testrows/IdNameRowBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Serenity.TestUtils;

public abstract class IdNameRow<TFields> : Row<TFields>, IIdRow, INameRow
where TFields : IdNameRowFields
{
[IdProperty, Identity]
public int? Id { get => fields.Id[this]; set => fields.Id[this] = value; }

[NameProperty]
public string Name { get => fields.Name[this]; set => fields.Name[this] = value; }
}

public abstract class IdNameRowFields() : RowFieldsBase()
{
#pragma warning disable CS0649
public Int32Field Id;
public StringField Name;
#pragma warning restore CS0649
}

0 comments on commit 117c800

Please sign in to comment.