Skip to content
Merged
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
61 changes: 25 additions & 36 deletions test/Npgsql.PluginTests/JsonNetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,85 +15,79 @@ namespace Npgsql.PluginTests;
/// <summary>
/// Tests for the Npgsql.Json.NET mapping plugin
/// </summary>
[TestFixture(NpgsqlDbType.Jsonb)]
[TestFixture(NpgsqlDbType.Json)]
public class JsonNetTests(NpgsqlDbType npgsqlDbType) : TestBase
[TestFixture("jsonb")]
[TestFixture("json")]
public class JsonNetTests(string dataTypeName) : TestBase
{
[Test]
public Task Roundtrip_object()
=> AssertType(
JsonDataSource,
new Foo { Bar = 8 },
IsJsonb ? @"{""Bar"": 8}" : @"{""Bar"":8}",
_pgTypeName,
npgsqlDbType,
dataTypeName,
isDefault: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);
Comment thread
NinoFloris marked this conversation as resolved.
Resolved

[Test, IssueLink("https://github.com/npgsql/npgsql/issues/3085")]
public Task Roundtrip_string()
=> AssertType(
JsonDataSource,
@"{""p"": 1}",
@"{""p"": 1}",
_pgTypeName,
npgsqlDbType,
dataTypeName,
isDefaultForWriting: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);

[Test, IssueLink("https://github.com/npgsql/npgsql/issues/3085")]
public Task Roundtrip_char_array()
=> AssertType(
JsonDataSource,
@"{""p"": 1}".ToCharArray(),
@"{""p"": 1}",
_pgTypeName,
npgsqlDbType,
dataTypeName,
isDefault: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);

[Test, IssueLink("https://github.com/npgsql/npgsql/issues/3085")]
public Task Roundtrip_byte_array()
=> AssertType(
JsonDataSource,
Encoding.ASCII.GetBytes(@"{""p"": 1}"),
@"{""p"": 1}",
_pgTypeName,
npgsqlDbType,
dataTypeName,
isDefault: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);

[Test]
public Task Roundtrip_JObject()
=> AssertType(
JsonDataSource,
new JObject { ["Bar"] = 8 },
IsJsonb ? @"{""Bar"": 8}" : @"{""Bar"":8}",
_pgTypeName,
npgsqlDbType,
dataTypeName,
// By default we map JObject to jsonb
isDefaultForWriting: IsJsonb,
isDefaultForReading: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);

[Test]
public Task Roundtrip_JArray()
=> AssertType(
JsonDataSource,
new JArray(new[] { 1, 2, 3 }),
IsJsonb ? "[1, 2, 3]" : "[1,2,3]",
_pgTypeName,
npgsqlDbType,
dataTypeName,
// By default we map JArray to jsonb
isDefaultForWriting: IsJsonb,
isDefaultForReading: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);

[Test]
public async Task Deserialize_failure()
{
await using var conn = await JsonDataSource.OpenConnectionAsync();
await using var cmd = new NpgsqlCommand($@"SELECT '[1, 2, 3]'::{_pgTypeName}", conn);
await using var cmd = new NpgsqlCommand($@"SELECT '[1, 2, 3]'::{dataTypeName}", conn);
await using var reader = await cmd.ExecuteReaderAsync();
await reader.ReadAsync();
// Attempt to deserialize JSON array into object
Expand All @@ -117,10 +111,9 @@ await AssertType(
dataSource,
new Foo { Bar = 8 },
IsJsonb ? @"{""Bar"": 8}" : @"{""Bar"":8}",
_pgTypeName,
npgsqlDbType,
dataTypeName,
isDefaultForReading: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);
}

[Test]
Expand All @@ -137,10 +130,9 @@ await AssertType(
dataSource,
new[] { 1, 2, 3 },
IsJsonb ? "[1, 2, 3]" : "[1,2,3]",
_pgTypeName,
npgsqlDbType,
dataTypeName,
isDefaultForReading: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);
}

class DateWrapper
Expand All @@ -166,10 +158,9 @@ await AssertType(
dataSource,
new DateWrapper { Date = new DateTime(2018, 04, 20) },
IsJsonb ? "{\"Date\": \"The 20th of April, 2018\"}" : "{\"Date\":\"The 20th of April, 2018\"}",
_pgTypeName,
npgsqlDbType,
dataTypeName,
isDefault: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);
}

[Test]
Expand All @@ -183,8 +174,8 @@ public async Task Bug3464()
await using var conn = await dataSource.OpenConnectionAsync();
await using var cmd = new NpgsqlCommand(@"SELECT @p1, @p2", conn);

cmd.Parameters.AddWithValue("p1", expected).NpgsqlDbType = npgsqlDbType;
cmd.Parameters.AddWithValue("p2", expected).NpgsqlDbType = npgsqlDbType;
cmd.Parameters.AddWithValue("p1", expected).DataTypeName = dataTypeName;
cmd.Parameters.AddWithValue("p2", expected).DataTypeName = dataTypeName;

await using var reader = cmd.ExecuteReader();
}
Expand Down Expand Up @@ -261,8 +252,6 @@ class Foo
public override int GetHashCode() => Bar.GetHashCode();
}

readonly string _pgTypeName = npgsqlDbType.ToString().ToLower();

[OneTimeSetUp]
public void SetUp()
{
Expand All @@ -275,7 +264,7 @@ public void SetUp()
public async Task Teardown()
=> await JsonDataSource.DisposeAsync();

bool IsJsonb => npgsqlDbType == NpgsqlDbType.Jsonb;
bool IsJsonb => dataTypeName == "jsonb";

NpgsqlDataSource JsonDataSource = default!;
}
15 changes: 5 additions & 10 deletions test/Npgsql.PluginTests/LegacyNodaTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using System.Data;
using System.Threading.Tasks;
using NodaTime;
using Npgsql.NodaTime.Internal;
using Npgsql.Tests;
using NpgsqlTypes;
using NUnit.Framework;
using Npgsql.NodaTime.Internal;

namespace Npgsql.PluginTests;

Expand All @@ -20,41 +19,37 @@ public async Task Timestamp_as_ZonedDateTime()
new LocalDateTime(1998, 4, 12, 13, 26, 38, 789).InZoneLeniently(DateTimeZoneProviders.Tzdb[TimeZone]),
"1998-04-12 13:26:38.789+02",
"timestamp with time zone",
NpgsqlDbType.TimestampTz,
DbType.DateTimeOffset,
isNpgsqlDbTypeInferredFromClrType: false, isDefault: false);
isDataTypeInferredFromValue: false, isDefault: false);

[Test]
public Task Timestamp_as_Instant()
=> AssertType(
new LocalDateTime(1998, 4, 12, 13, 26, 38, 789).InUtc().ToInstant(),
"1998-04-12 13:26:38.789",
"timestamp without time zone",
NpgsqlDbType.Timestamp,
DbType.DateTime,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);

[Test]
public Task Timestamp_as_LocalDateTime()
=> AssertType(
new LocalDateTime(1998, 4, 12, 13, 26, 38, 789),
"1998-04-12 13:26:38.789",
"timestamp without time zone",
NpgsqlDbType.Timestamp,
DbType.DateTime,
isDefaultForReading: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);

[Test]
public Task Timestamptz_as_Instant()
=> AssertType(
new LocalDateTime(1998, 4, 12, 13, 26, 38, 789).InUtc().ToInstant(),
"1998-04-12 15:26:38.789+02",
"timestamp with time zone",
NpgsqlDbType.TimestampTz,
DbType.DateTimeOffset,
isDefaultForWriting: false,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);

[Test]
public async Task Timestamptz_ZonedDateTime_infinite_values_are_not_supported()
Expand Down
3 changes: 1 addition & 2 deletions test/Npgsql.PluginTests/NetTopologySuiteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ await AssertType(
new Geometry[] { point },
'{' + GetSqlLiteral(point) + '}',
"geometry[]",
NpgsqlDbType.Geometry | NpgsqlDbType.Array,
isNpgsqlDbTypeInferredFromClrType: false);
isDataTypeInferredFromValue: false);
}

[Test]
Expand Down
7 changes: 2 additions & 5 deletions test/Npgsql.PluginTests/NodaTimeInfinityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ await AssertType(
new DateInterval(LocalDate.MinIsoValue, LocalDate.MaxIsoValue),
"[-infinity,infinity]",
"daterange",
NpgsqlDbType.DateRange,
isNpgsqlDbTypeInferredFromClrType: false, skipArrayCheck: true); // NpgsqlRange<T>[] is mapped to multirange by default, not array; test separately
isDataTypeInferredFromValue: false, skipArrayCheck: true); // NpgsqlRange<T>[] is mapped to multirange by default, not array; test separately

await AssertType(
new [] {new DateInterval(LocalDate.MinIsoValue, LocalDate.MaxIsoValue)},
"""{"[-infinity,infinity]"}""",
"daterange[]",
NpgsqlDbType.DateRange | NpgsqlDbType.Array,
isDefault: false, skipArrayCheck: true);

await using var conn = await OpenConnectionAsync();
Expand All @@ -43,8 +41,7 @@ await AssertType(
await AssertType(
new [] {new DateInterval(LocalDate.MinIsoValue, LocalDate.MaxIsoValue)},
"""{[-infinity,infinity]}""",
"datemultirange",
NpgsqlDbType.DateMultirange, isNpgsqlDbTypeInferredFromClrType: false, skipArrayCheck: true);
"datemultirange", isDataTypeInferredFromValue: false, skipArrayCheck: true);
}

[Test]
Expand Down
Loading