-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathDuckDBDataReaderMapTests.cs
More file actions
150 lines (115 loc) · 4.82 KB
/
Copy pathDuckDBDataReaderMapTests.cs
File metadata and controls
150 lines (115 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
namespace DuckDB.NET.Test;
public class DuckDBDataReaderMapTests(DuckDBDatabaseFixture db) : DuckDBTestBase(db)
{
[Fact]
public void ReadMap()
{
Command.CommandText = "SELECT MAP { 'key1': 1, 'key2': 5, 'key3': 7 }";
var reader = Command.ExecuteReader();
reader.GetFieldType(0).Should().Be(typeof(Dictionary<string, int>));
reader.Read();
var value = reader.GetValue(0);
value.Should().BeOfType<Dictionary<string, int>>();
var expectation = new Dictionary<string, int>() { { "key1", 1 }, { "key2", 5 }, { "key3", 7 } };
value.Should().BeEquivalentTo(expectation);
}
[Fact]
public void ReadMapTwoRows()
{
Command.CommandText = "Select * from (SELECT MAP { 'key1': 1, 'key2': 5, 'key3': 7 } Union SELECT MAP { 'key2': 15, 'key24': 7 }) order by 1";
var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetValue(0);
value.Should().BeOfType<Dictionary<string, int>>();
var expectation = new Dictionary<string, int>() { { "key1", 1 }, { "key2", 5 }, { "key3", 7 } };
value.Should().BeEquivalentTo(expectation);
reader.Read();
value = reader.GetValue(0);
expectation = new Dictionary<string, int>() { { "key2", 15 }, { "key24", 7 } };
value.Should().BeEquivalentTo(expectation);
}
[Fact]
public void ReadMapStronglyTyped()
{
Command.CommandText = "SELECT MAP { 'key1': 1, 'key2': 5, 'key3': 7 }";
var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetFieldValue<Dictionary<string, int>>(0);
var expectation = new Dictionary<string, int>() { { "key1", 1 }, { "key2", 5 }, { "key3", 7 } };
value.Should().BeEquivalentTo(expectation);
}
[Fact]
public void ReadMapWithNullInNullableDictionary()
{
Command.CommandText = "SELECT MAP { 'key1': 1, 'key2': NULL, 'key3': 7 }";
var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetFieldValue<Dictionary<string, int?>>(0);
var expectation = new Dictionary<string, int?>() { { "key1", 1 }, { "key2", null }, { "key3", 7 } };
value.Should().BeEquivalentTo(expectation);
}
[Fact]
public void ReadMapWithNullInReferenceTypeDictionary()
{
Command.CommandText = "SELECT MAP { 'key1': 'abc', 'key2': NULL, 'key3': 'ghi' }";
var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetFieldValue<Dictionary<string, string>>(0);
var expectation = new Dictionary<string, string>() { { "key1", "abc" }, { "key2", null }, { "key3", "ghi" } };
value.Should().BeEquivalentTo(expectation);
}
[Fact]
public void ReadMapWithNullInNotNullableDictionaryThrowsException()
{
Command.CommandText = "SELECT MAP { 'key1': 1, 'key2': NULL, 'key3': 7 }";
var reader = Command.ExecuteReader();
reader.Read();
reader.Invoking(r => r.GetFieldValue<Dictionary<string, int>>(0)).Should().Throw<InvalidCastException>();
}
[Fact]
public void ReadMapOfList()
{
Command.CommandText = "SELECT MAP { ['a', 'b']: [1.1, 2.2], ['c', 'd']: [3.3, 4.4] };";
var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetValue(0) as Dictionary<List<string>, List<decimal>>;
var expectation = new Dictionary<List<string>, List<decimal>>(new ListEqualityClass())
{
{ new List<string>() { "a", "b" }, new List<decimal> { 1.1m, 2.2m } },
{ new List<string>() { "c", "d" }, new List<decimal> { 3.3m, 4.4m } },
};
foreach (var (key, decimals) in value)
{
expectation[key].Should().BeEquivalentTo(decimals);
}
}
[Fact]
public void ReadMapWrongTypeThrowsException()
{
Command.CommandText = "SELECT MAP { ['a', 'b']: [1.1, 2.2], ['c', 'd']: [3.3, 4.4] };";
var reader = Command.ExecuteReader();
reader.Read();
reader.Invoking(r => r.GetFieldValue<List<KeyValuePair<string, int>>>(0)).Should().Throw<InvalidOperationException>();
}
[Fact]
public void GetEmptyMapType()
{
Command.CommandText = "CREATE TABLE tbl (col MAP(INTEGER, DOUBLE));";
Command.ExecuteNonQuery();
Command.CommandText = "SELECT * FROM tbl";
using var reader = Command.ExecuteReader();
var type = reader.GetFieldType(0);
type.Should().Be(typeof(Dictionary<int, double>));
}
class ListEqualityClass : IEqualityComparer<List<string>>
{
public bool Equals(List<string> x, List<string> y)
{
return x.SequenceEqual(y);
}
public int GetHashCode(List<string> obj)
{
return string.Join(",", obj).GetHashCode();
}
}
}