Skip to content

Commit 119079b

Browse files
committed
#592 : rebased main branch, added postgres JSON tests
1 parent 5122d43 commit 119079b

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using SqlKata.Compilers;
2+
using SqlKata.Tests.Infrastructure;
3+
using System;
4+
using Xunit;
5+
6+
namespace SqlKata.Tests.PostgreSql
7+
{
8+
public class PostgresJsonTests : TestSupport
9+
{
10+
private readonly PostgresCompiler regularCompiler = new();
11+
private readonly JsonAwarePostgresCompiler jsonCompatibleCompiler = new();
12+
13+
private class JsonAwarePostgresCompiler : PostgresCompiler
14+
{
15+
protected override string parameterPlaceholder { get; set; } = "¤";
16+
}
17+
18+
[Fact]
19+
public void LimitWithCustomPlaceHolder()
20+
{
21+
var query = new Query("test_table").Limit(10);
22+
var result = jsonCompatibleCompiler.Compile(query);
23+
Assert.Equal("""SELECT * FROM "test_table" LIMIT 10""", result.ToString());
24+
}
25+
26+
[Fact]
27+
public void RegularCompilerThrowsExceptionWhereRawJsonContainsQuestionMarkData()
28+
{
29+
Assert.Throws<IndexOutOfRangeException>(() =>
30+
{
31+
Query query = CreateQuestionMarkJsonQuery(out var rawCondition);
32+
SqlResult result = regularCompiler.Compile(query);
33+
Assert.Equal($"""SELECT * FROM "test_table" WHERE {rawCondition}""", result.ToString());
34+
});
35+
}
36+
37+
private Query CreateQuestionMarkJsonQuery(out string rawCondition)
38+
{
39+
rawCondition = "my_json_column @> '{\"json_param\" : \"question mark ? \"}'";
40+
var escapedJsonCondition = rawCondition.Replace("{", "\\{").Replace("}", "\\}");
41+
return new Query("test_table").WhereRaw(escapedJsonCondition);
42+
}
43+
44+
[Fact]
45+
public void WhereRawJsonWithQuestionMarkData()
46+
{
47+
Query query = CreateQuestionMarkJsonQuery(out var rawCondition);
48+
SqlResult result = jsonCompatibleCompiler.Compile(query);
49+
Assert.Equal($"""SELECT * FROM "test_table" WHERE {rawCondition}""", result.ToString());
50+
}
51+
52+
[Fact]
53+
public void UsingJsonArray()
54+
{
55+
var query = new Query("test_table").WhereRaw("[Json]->'address'->>'country' in (¤)", new[] { 1, 2, 3, 4 });
56+
SqlResult result = jsonCompatibleCompiler.Compile(query);
57+
58+
Assert.Equal("""SELECT * FROM "test_table" WHERE "Json"->'address'->>'country' in (1,2,3,4)""", result.ToString());
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)