-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoadCsvTest.cs
160 lines (136 loc) · 7.94 KB
/
LoadCsvTest.cs
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
namespace RelationalAI.Test
{
public class LoadCsvTests : UnitTest
{
public static string Uuid = Guid.NewGuid().ToString();
public static string Dbname = $"csharp-sdk-{Uuid}";
public static string EngineName = $"csharp-sdk-{Uuid}";
private const string Sample = "" +
"cocktail,quantity,price,date\n" +
"\"martini\",2,12.50,\"2020-01-01\"\n" +
"\"sazerac\",4,14.25,\"2020-02-02\"\n" +
"\"cosmopolitan\",4,11.00,\"2020-03-03\"\n" +
"\"bellini\",3,12.25,\"2020-04-04\"\n";
[Fact]
public async Task LoadCsvTest()
{
var client = CreateClient();
await client.CreateEngineWaitAsync(EngineName);
await client.CreateDatabaseAsync(Dbname, EngineName);
var loadRsp = await client.LoadCsvAsync(Dbname, EngineName, "sample", Sample);
Assert.Equal(TransactionAsyncState.Completed, loadRsp.Transaction.State);
Assert.Empty(loadRsp.Problems);
var rsp = await client.ExecuteWaitAsync(Dbname, EngineName, "def output = sample");
Assert.Equal(rsp.Results[0].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[1].Table, new List<object> { "martini", "sazerac", "cosmopolitan", "bellini" });
Assert.Equal(rsp.Results[2].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[3].Table, new List<object> { "2020-01-01", "2020-02-02", "2020-03-03", "2020-04-04" });
Assert.Equal(rsp.Results[4].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[5].Table, new List<object> { "12.50", "14.25", "11.00", "12.25" });
Assert.Equal(rsp.Results[6].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[7].Table, new List<object> { "2", "4", "4", "3" });
}
private const string SampleNoHeader = "" +
"\"martini\",2,12.50,\"2020-01-01\"\n" +
"\"sazerac\",4,14.25,\"2020-02-02\"\n" +
"\"cosmopolitan\",4,11.00,\"2020-03-03\"\n" +
"\"bellini\",3,12.25,\"2020-04-04\"\n";
[Fact]
public async Task LoadCsvNoHeaderTest()
{
var client = CreateClient();
await client.CreateEngineWaitAsync(EngineName);
await client.CreateDatabaseAsync(Dbname, EngineName);
var opts = new CsvOptions().WithHeaderRow(0);
var loadRsp = await client.LoadCsvAsync(Dbname, EngineName, "sample_no_header", SampleNoHeader, opts);
Assert.Equal(TransactionAsyncState.Completed, loadRsp.Transaction.State);
Assert.Empty(loadRsp.Problems);
var rsp = await client.ExecuteWaitAsync(Dbname, EngineName, "def output = sample_no_header");
Assert.Equal(rsp.Results[0].Table, new List<object> { 1L, 2L, 3L, 4L });
Assert.Equal(rsp.Results[1].Table, new List<object> { "martini", "sazerac", "cosmopolitan", "bellini" });
Assert.Equal(rsp.Results[2].Table, new List<object> { 1L, 2L, 3L, 4L });
Assert.Equal(rsp.Results[3].Table, new List<object> { "2", "4", "4", "3" });
Assert.Equal(rsp.Results[4].Table, new List<object> { 1L, 2L, 3L, 4L });
Assert.Equal(rsp.Results[5].Table, new List<object> { "12.50", "14.25", "11.00", "12.25" });
Assert.Equal(rsp.Results[6].Table, new List<object> { 1L, 2L, 3L, 4L });
Assert.Equal(rsp.Results[7].Table, new List<object> { "2020-01-01", "2020-02-02", "2020-03-03", "2020-04-04" });
}
private const string SampleAltSyntax = "" +
"cocktail|quantity|price|date\n" +
"'martini'|2|12.50|'2020-01-01'\n" +
"'sazerac'|4|14.25|'2020-02-02'\n" +
"'cosmopolitan'|4|11.00|'2020-03-03'\n" +
"'bellini'|3|12.25|'2020-04-04'\n";
[Fact]
public async Task LoadCsvAltSyntaxTest()
{
var client = CreateClient();
await client.CreateEngineWaitAsync(EngineName);
await client.CreateDatabaseAsync(Dbname, EngineName);
var opts = new CsvOptions().WithDelim('|').WithQuoteChar('\'');
var loadRsp = await client.LoadCsvAsync(Dbname, EngineName, "sample_alt_syntax", SampleAltSyntax, opts);
Assert.Equal(TransactionAsyncState.Completed, loadRsp.Transaction.State);
Assert.Empty(loadRsp.Problems);
var rsp = await client.ExecuteWaitAsync(Dbname, EngineName, "def output = sample_alt_syntax");
Assert.Equal(rsp.Results[0].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[1].Table, new List<object> { "martini", "sazerac", "cosmopolitan", "bellini" });
Assert.Equal(rsp.Results[2].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[3].Table, new List<object> { "2020-01-01", "2020-02-02", "2020-03-03", "2020-04-04" });
Assert.Equal(rsp.Results[4].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[5].Table, new List<object> { "12.50", "14.25", "11.00", "12.25" });
Assert.Equal(rsp.Results[6].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[7].Table, new List<object> { "2", "4", "4", "3" });
}
[Fact]
public async Task LoadCsvWithSchemaTest()
{
var client = CreateClient();
await client.CreateEngineWaitAsync(EngineName);
await client.CreateDatabaseAsync(Dbname, EngineName);
var schema = new Dictionary<string, string>
{
{ "cocktail", "string" },
{ "quantity", "int" },
{ "price", "decimal(64,2)" },
{ "date", "date" }
};
var opts = new CsvOptions().WithSchema(schema);
var loadRsp = await client.LoadCsvAsync(Dbname, EngineName, "sample", Sample, opts);
Assert.Equal(TransactionAsyncState.Completed, loadRsp.Transaction.State);
Assert.Empty(loadRsp.Problems);
var rsp = await client.ExecuteWaitAsync(Dbname, EngineName, "def output = sample");
Assert.Equal(rsp.Results[0].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[1].Table, new List<object> { "martini", "sazerac", "cosmopolitan", "bellini" });
Assert.Equal(rsp.Results[2].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[3].Table, new List<object> { 737425L, 737457L, 737487L, 737519L });
Assert.Equal(rsp.Results[4].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[5].Table, new List<object> { 1250L, 1425L, 1100L, 1225L });
Assert.Equal(rsp.Results[6].Table, new List<object> { 2L, 3L, 4L, 5L });
Assert.Equal(rsp.Results[7].Table, new List<object> { 2L, 4L, 4L, 3L });
}
public override async Task DisposeAsync()
{
var client = CreateClient();
try
{
await client.DeleteDatabaseAsync(Dbname);
}
catch (Exception e)
{
await Console.Error.WriteLineAsync(e.ToString());
}
try
{
await client.DeleteEngineWaitAsync(EngineName);
}
catch (Exception e)
{
await Console.Error.WriteLineAsync(e.ToString());
}
}
}
}