Npgsql: Improve test coverage for json and jsonb types

Created on 22 Jul 2020  路  3Comments  路  Source: npgsql/npgsql

There is a lack of tests for JSON type handlers. The built in and plugin are verified only for string, POCO and documents, but not for additional types which the text handler supports (like char[], ArraySegment<char> and so on).

test

Most helpful comment

Thanks @YohDeadfall, makes sense (: I'll have a go at this and try and push something through when I get a chance.

All 3 comments

Hi @YohDeadfall , I've been looking at adding these tests but am getting some errors when trying to execute the Reader for the additional types such as char, char[] etc.

Below is a snippet of the code I鈥檝e been playing with for the proposed tests, I鈥檝e stepped through the code and can see the changes you鈥檝e made to fix ValidateandGetLength methods are now working as expected (and not entering the infinite loop) but when the executeReader is run I get the following error: 22P02: invalid input syntax for type json (17612)

var expected1 = 'a';
var expected2 = new char[] { 'a', 'b', 'c' };

using var conn = OpenConnection();

using var cmd = new NpgsqlCommand(@"SELECT @p1, @p2", conn);

cmd.Parameters.Add(new NpgsqlParameter<char>("p1", NpgsqlDbType.Json) { Value = expected1 });
cmd.Parameters.Add(new NpgsqlParameter<char[]>("p2", NpgsqlDbType.Json) { Value = expected2 });

try
{
    using var reader = cmd.ExecuteReader();
    reader.Read();
    Assert.That(reader.GetFieldValue<char>(0), Is.EqualTo(expected1));
}

I鈥檝e gone through the notes on using Json.Net here but I鈥檓 not sure if there is something else I need to do to get this working. Any help here is appreciated. Thanks

Thank you for taking this!

Even if a parameter has json as its type, the value must be a valid json string. The same applies for jsonb.

var expected = new char[] { '{', '"', 'p', '"', '1', '}' }; //  or use "{\"p\":1}".ToCharArray() instead

A single character isn't a valid json, but without that change stack overflow happens (: Everything except char should work.

Thanks @YohDeadfall, makes sense (: I'll have a go at this and try and push something through when I get a chance.

Was this page helpful?
0 / 5 - 0 ratings