-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathDuckDBDataReaderStructTests.cs
More file actions
169 lines (145 loc) · 4.95 KB
/
Copy pathDuckDBDataReaderStructTests.cs
File metadata and controls
169 lines (145 loc) · 4.95 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
namespace DuckDB.NET.Test;
public class DuckDBDataReaderStructTests(DuckDBDatabaseFixture db) : DuckDBTestBase(db)
{
[Fact]
public void ReadBasicStruct()
{
Command.CommandText = "SELECT {'x': 1, 'y': 2, 'z': 'test'};";
using var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetFieldValue<Struct1>(0);
value.Should().BeEquivalentTo(new Struct1
{
X = 1,
Y = 2,
Z = "test"
});
}
[Fact]
public void ReadBasicStructWithGetValue()
{
Command.CommandText = "SELECT {'x': 1, 'y': 2, 'z': 'test', 'xy': null};";
using var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetValue(0);
value.Should().BeEquivalentTo(new Dictionary<string, object> { { "x", 1 }, { "y", 2 }, { "z", "test" }, {"xy", null} });
}
[Fact]
public void ReadBasicStructList()
{
Command.CommandText = "SELECT [{'x': 1, 'y': 2, 'z': 'test'}, {'x': 4, 'y': 3, 'z': 'tset'}, NULL];";
using var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetFieldValue<List<Struct1>>(0);
value.Should().BeEquivalentTo(new List<Struct1>
{
new() { X = 1, Y = 2, Z = "test" },
new() { X = 4, Y = 3, Z = "tset" },
null
});
}
[Fact]
public void ReadBasicStructListWithGetValue()
{
Command.CommandText = "SELECT [{'x': 1, 'y': 2, 'z': 'test'}, {'x': 4, 'y': 3, 'z': 'tset'}, NULL];";
using var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetValue(0);
value.Should().BeEquivalentTo(new List<Dictionary<string, object>>
{
new() { { "x", 1 }, { "y", 2 }, { "z", "test" } },
new() { { "x", 4 }, { "y", 3 }, { "z", "tset" } },
null
});
}
[Fact]
public void ReadStructWithNull()
{
Command.CommandText = "SELECT {'yes': 'duck', 'maybe': 'goose', 'huh': NULL, 'no': 'heron', 'type': 0} Union All " +
"SELECT {'yes': 'duck', 'maybe': 'goose', 'huh': 'bird', 'no': 'heron', 'type':1};";
using var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetFieldValue<Struct2>(0);
value.Should().BeEquivalentTo(new Struct2
{
Huh = null,
Maybe = "goose",
No = "heron",
Yes = "duck",
Type = 0
});
reader.Read();
value = reader.GetFieldValue<Struct2>(0);
value.Should().BeEquivalentTo(new Struct2
{
Huh = "bird",
Maybe = "goose",
No = "heron",
Yes = "duck",
Type = 1
});
}
[Fact]
public void ReadStructWithNestedStruct()
{
Command.CommandText = "SELECT {'birds': {'yes': 'duck', 'maybe': 'goose', 'huh': NULL, 'no': 'heron', 'type': 0}, " +
"'aliens': NULL, " +
"'amphibians': {'yes':'frog', 'maybe': 'salamander', 'huh': 'dragon', 'no':'toad', 'type':1} };";
using var reader = Command.ExecuteReader();
reader.Read();
var value = reader.GetFieldValue<Struct3>(0);
value.Aliens.Should().BeNull();
value.Birds.Should().BeEquivalentTo(new Struct2
{
Huh = null,
Maybe = "goose",
No = "heron",
Yes = "duck",
Type = 0
});
value.Amphibians.Should().BeEquivalentTo(new Struct2
{
Huh = "dragon",
Maybe = "salamander",
No = "toad",
Yes = "frog",
Type = 1
});
}
[Theory]
//[InlineData("SELECT {'b': null};", $"Property '{nameof(Struct4.A)}' not found in struct")]
[InlineData("SELECT {'b': null, 'a': 4};", $"Property '{nameof(Struct4.B)}' is not nullable but struct contains null value")]
public void ReadStructWithMissingDataThrowsException(string query, string error)
{
Command.CommandText = query;
using var reader = Command.ExecuteReader();
reader.Read();
reader.Invoking(r => r.GetFieldValue<Struct4>(0)).Should().Throw<InvalidCastException>().WithMessage(error);
}
class Struct1
{
public int X { get; set; }
public int Y { get; set; }
public string Z { get; set; }
public int XY { get; }
}
class Struct2
{
public string Yes { get; set; }
public string Maybe { get; set; }
public string Huh { get; set; }
public string No { get; set; }
public int Type { get; set; }
}
class Struct3
{
public Struct2 Birds { get; set; }
public Struct2 Aliens { get; set; }
public Struct2 Amphibians { get; set; }
}
class Struct4
{
public int A { get; set; }
public int B { get; set; }
}
}