Last active
March 14, 2024 12:49
-
-
Save lucax88x/69c6deb14b80c856271534de5c10e1ad to your computer and use it in GitHub Desktop.
bug.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data; | |
using System.Text.Json.Serialization; | |
using Dapper; | |
using Newtonsoft.Json; | |
using Npgsql; | |
namespace TestProject1; | |
public class UnitTest1 | |
{ | |
private string _query = """ | |
select metadata | |
from rnc_sequence_features | |
limit 1; | |
"""; | |
public UnitTest1() | |
{ | |
// SqlMapper.AddTypeMap(typeof(string), DbType.String, true); | |
} | |
[Fact] | |
public async Task dapper_should_work_with_json_net() | |
{ | |
await using var connection = await CreateConnection(c => c.UseJsonNet()); | |
var result = await connection.QueryAsync<Dto>(_query); | |
Assert.True(result.ToList().First()!.Metadata.Length > 0); | |
} | |
[Fact] | |
public async Task dapper_should_work_without_json_net() | |
{ | |
await using var connection = await CreateConnection(); | |
var result = await connection.QueryAsync<Dto>(_query); | |
Assert.True(result.ToList().First()!.Metadata.Length > 0); | |
} | |
[Fact] | |
public async Task ado_should_work_with_json_net() | |
{ | |
await using var connection = await CreateConnection(c => c.UseJsonNet()); | |
await using var cmd = new NpgsqlCommand(_query, connection); | |
await using var reader = await cmd.ExecuteReaderAsync(); | |
while (await reader.ReadAsync()) | |
{ | |
var metadata = reader.GetFieldValue<Metadata>(0); | |
Assert.True(metadata.Url.Length > 0); | |
} | |
} | |
[Fact] | |
public async Task ado_should_work_without_json_net() | |
{ | |
await using var connection = await CreateConnection(); | |
await using var cmd = new NpgsqlCommand(_query, connection); | |
await using var reader = await cmd.ExecuteReaderAsync(); | |
while (await reader.ReadAsync()) | |
{ | |
var metadata = reader.GetFieldValue<Metadata>(0); | |
Assert.True(metadata.Url.Length > 0); | |
} | |
} | |
private static async Task<NpgsqlConnection> CreateConnection(Action<NpgsqlDataSourceBuilder>? configure = null) | |
{ | |
var dataSourceBuilder = new NpgsqlDataSourceBuilder( | |
"Host=hh-pgsql-public.ebi.ac.uk;Port=5432;Database=pfmegrnargs;Username=reader;Password=NWDMCE5xdipIjRrp;Include Error Detail=true;"); | |
configure?.Invoke(dataSourceBuilder); | |
dataSourceBuilder.EnableDynamicJson(); | |
var npgsqlDataSource = dataSourceBuilder.Build(); | |
var connection = await npgsqlDataSource.OpenConnectionAsync(); | |
return connection; | |
} | |
} | |
public class Dto | |
{ | |
// public Metadata? Metadata { get; set; } | |
public string Metadata { get; set; } | |
} | |
public class Metadata | |
{ | |
[JsonPropertyName("url")] | |
[JsonProperty("url")] | |
public string Url { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment