Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/Postgrest/Postgrest.Tests/Linq/LinqQueryTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -166,6 +167,43 @@ public async Task Set_ShouldUpdateOnlyTheAssignedColumns()
record.IntValue.Should().Be(original.IntValue + 1);
}

[TestMethod]
public async Task Set_ShouldClearOnlyTheMatchingRowsJsonColumn_GivenNull()
{
var client = LocalStack.Client();
var targetUsername = $"set-null-{Guid.NewGuid():N}";
var neighborUsername = $"{targetUsername}-neighbor";
await client.Table<UserWithJsonData>().Insert(new List<UserWithJsonData>
{
new() { Username = targetUsername, Data = new JsonObject { ["value"] = "target" } },
new() { Username = neighborUsername, Data = new JsonObject { ["value"] = "neighbor" } }
});

try
{
var updated = await client.Table<UserWithJsonData>()
.Where(user => user.Username == targetUsername)
.Set(user => user.Data!, null)
.Update(new QueryOptions { Returning = QueryOptions.ReturnType.Representation });
updated.Models.Should().ContainSingle().Which.Data.Should().BeNull();

var cleared = await client.Table<UserWithJsonData>()
.Where(user => user.Username == targetUsername)
.Filter("data", Operator.Is, "null").Single();
cleared.Should().NotBeNull();

var neighbor = await client.Table<UserWithJsonData>()
.Where(user => user.Username == neighborUsername).Single();
neighbor.Should().NotBeNull();
neighbor!.Data!["value"]!.GetValue<string>().Should().Be("neighbor");
}
finally
{
await client.Table<UserWithJsonData>()
.Where(user => user.Username == targetUsername || user.Username == neighborUsername).Delete();
}
}

[TestMethod]
public async Task Delete_ShouldRemoveRowsMatchingThePredicate()
{
Expand Down
43 changes: 39 additions & 4 deletions packages/Postgrest/Postgrest.Tests/Linq/SetClauseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,66 @@ public class SetClauseTests
private const string BaseUrl = "http://localhost:54321/rest/v1";
private readonly Client client = new(BaseUrl);

[TestMethod]
public void Set_ShouldAcceptNull_GivenAReferenceTypeColumn()
{
var act = () => this.client.Table<KitchenSink>().Set(model => model.ListOfStrings!, null);
act.Should().NotThrow();
}

[TestMethod]
public void Set_ShouldAcceptNull_GivenAJsonObjectColumn()
{
var act = () => this.client.Table<UserWithJsonData>().Set(user => user.Data!, null);
act.Should().NotThrow();
}

[TestMethod]
public void Set_ShouldAcceptNull_GivenAStringColumn()
{
var act = () => this.client.Table<KitchenSink>().Set(model => model.StringValue!, null);
act.Should().NotThrow();
}

[TestMethod]
public void Set_ShouldAcceptNull_GivenANullableValueTypeColumn()
{
var act = () => this.client.Table<KitchenSink>().Set(model => model.IntValue!, null);
act.Should().NotThrow();
}

[TestMethod]
public void Set_ShouldThrow_GivenNullForANonNullableValueTypeColumn()
{
var act = () => this.client.Table<KitchenSink>().Set(model => model.BooleanValue, null);
act.Should().Throw<ArgumentException>();
}

[TestMethod]
public void Set_ShouldThrow_GivenAValueOfTheWrongType()
{
var act = () => client.Table<Movie>().Set(x => x.Name!, DateTime.Now);
var act = () => this.client.Table<Movie>().Set(x => x.Name!, DateTime.Now);
act.Should().Throw<ArgumentException>();
}

[TestMethod]
public void Set_ShouldThrow_GivenAKeyThatIsNotAColumn()
{
var act = () => client.Table<Movie>().Set(x => DateTime.Now, "value");
var act = () => this.client.Table<Movie>().Set(x => DateTime.Now, "value");
act.Should().Throw<ArgumentException>();
}

[TestMethod]
public void Set_ShouldThrow_GivenAKeyValuePairWithAMismatchedValueType()
{
var act = () => client.Table<Movie>().Set(x => new KeyValuePair<object, object?>(x.Name!, DateTime.Now));
var act = () => this.client.Table<Movie>().Set(x => new KeyValuePair<object, object?>(x.Name!, DateTime.Now));
act.Should().Throw<ArgumentException>();
}

[TestMethod]
public void Set_ShouldThrow_GivenAKeyValuePairWhoseKeyIsNotAColumn()
{
var act = () => client.Table<Movie>().Set(x => new KeyValuePair<object, object?>(DateTime.Now, "value"));
var act = () => this.client.Table<Movie>().Set(x => new KeyValuePair<object, object?>(DateTime.Now, "value"));
act.Should().Throw<ArgumentException>();
}
}
15 changes: 15 additions & 0 deletions packages/Postgrest/Postgrest.Tests/Models/UserWithJsonData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Nodes;
using Supabase.Postgrest.Attributes;
using Supabase.Postgrest.Models;

namespace Postgrest.Tests.Models;

[Table("users")]
public class UserWithJsonData : BaseModel
{
[PrimaryKey("username", true)]
public string? Username { get; set; }

[Column("data")]
public JsonObject? Data { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":null}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ await this.Client.Table<Todo>().Filter("id", Operator.Equals, "1")
.Update();
await this.Verify(this.EmittedRequestBody).UseDirectory("Data");
}

[TestMethod]
public async Task UpdateRequest_ShouldIncludeExplicitNull_GivenAJsonColumnSetToNull()
{
await this.Client.Table<UserWithJsonData>().Filter("username", Operator.Equals, "null-set-target")
.Set(user => user.Data!, null)
.Update();
await this.Verify(this.EmittedRequestBody).UseDirectory("Data");
}
}
2 changes: 1 addition & 1 deletion packages/Postgrest/Postgrest/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public IPostgrestTable<TModel> Set(Expression<Func<TModel, object>> keySelector,
throw new ArgumentException(
"Expression should return a KeyValuePair with a key of a Model Property and a value.");

if (value == null && visitor.ExpectedType != typeof(string))
if (value == null && visitor.ExpectedType.IsValueType)
{
if (Nullable.GetUnderlyingType(visitor.ExpectedType) == null)
throw new ArgumentException(
Expand Down