-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathJsonStreamTest.cs
More file actions
204 lines (179 loc) · 7.53 KB
/
JsonStreamTest.cs
File metadata and controls
204 lines (179 loc) · 7.53 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Microsoft.Data.SqlClient.ManualTesting.Tests.SQL.JsonTest;
using Microsoft.Data.SqlClient.Tests.Common.Fixtures.DatabaseObjects;
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public class JsonRecord
{
public int Id { get; set; }
public string Name { get; set; }
}
public class JsonStreamTest
{
private readonly ITestOutputHelper _output;
private static readonly string _jsonFile = DataTestUtility.GetShortName("randomRecords") + ".json";
private static readonly string _outputFile = DataTestUtility.GetShortName("serverRecords") + ".json";
public JsonStreamTest(ITestOutputHelper output)
{
_output = output;
}
private void GenerateJsonFile(int noOfRecords, string filename)
{
DeleteFile(filename);
var random = new Random();
var records = new List<JsonRecord>();
int recordCount = noOfRecords;
for (int i = 0; i < recordCount; i++)
{
records.Add(new JsonRecord
{
Id = i + 1,
Name = "𩸽json" + random.Next(1, noOfRecords),
});
}
string json = JsonSerializer.Serialize(records, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(filename, json);
Assert.True(File.Exists(filename));
_output.WriteLine("Generated JSON file "+filename);
}
private void CompareJsonFiles()
{
using JsonDocument doc1 = JsonDocument.Parse(File.ReadAllText(_jsonFile));
using JsonDocument doc2 = JsonDocument.Parse(File.ReadAllText(_outputFile));
Assert.True(JsonTestHelper.JsonDeepEquals(doc1.RootElement, doc2.RootElement));
}
private void PrintJsonDataToFile(SqlConnection connection, string tableName)
{
DeleteFile(_outputFile);
using (SqlCommand command = new SqlCommand($"SELECT [data] FROM {tableName}", connection))
{
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
{
using (StreamWriter sw = new StreamWriter(_outputFile))
{
while (reader.Read())
{
char[] buffer = new char[4096];
int charsRead = 0;
using (TextReader data = reader.GetTextReader(0))
{
do
{
charsRead = data.Read(buffer, 0, buffer.Length);
sw.Write(buffer, 0, charsRead);
} while (charsRead > 0);
}
_output.WriteLine("Output written to " + _outputFile);
}
}
}
}
}
private async Task PrintJsonDataToFileAsync(SqlConnection connection, string tableName)
{
DeleteFile(_outputFile);
using (SqlCommand command = new SqlCommand($"SELECT [data] FROM {tableName}", connection))
{
using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
{
using (StreamWriter sw = new StreamWriter(_outputFile))
{
while (await reader.ReadAsync())
{
char[] buffer = new char[4096];
int charsRead = 0;
using (TextReader data = reader.GetTextReader(0))
{
do
{
charsRead = await data.ReadAsync(buffer, 0, buffer.Length);
await sw.WriteAsync(buffer, 0, charsRead);
} while (charsRead > 0);
}
_output.WriteLine("Output written to file " + _outputFile);
}
}
}
}
}
private void StreamJsonFileToServer(SqlConnection connection, string tableName)
{
using (SqlCommand cmd = new SqlCommand($"INSERT INTO {tableName} (data) VALUES (@jsondata)", connection))
{
using (StreamReader jsonFile = File.OpenText(_jsonFile))
{
cmd.Parameters.Add("@jsondata", Microsoft.Data.SqlDbTypeExtensions.Json, -1).Value = jsonFile;
cmd.ExecuteNonQuery();
}
}
}
private async Task StreamJsonFileToServerAsync(SqlConnection connection, string tableName)
{
using (SqlCommand cmd = new SqlCommand($"INSERT INTO {tableName} (data) VALUES (@jsondata)", connection))
{
using (StreamReader jsonFile = File.OpenText(_jsonFile))
{
cmd.Parameters.Add("@jsondata", Microsoft.Data.SqlDbTypeExtensions.Json, -1).Value = jsonFile;
await cmd.ExecuteNonQueryAsync();
}
}
}
private void DeleteFile(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
}
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsJsonSupported))]
public void TestJsonStreaming()
{
try
{
GenerateJsonFile(1000, _jsonFile);
using SqlConnection connection = new(DataTestUtility.TCPConnectionString);
connection.Open();
using Table jsonTable = new(connection, "jsonTab", "(data json)");
StreamJsonFileToServer(connection, jsonTable.Name);
PrintJsonDataToFile(connection, jsonTable.Name);
CompareJsonFiles();
}
finally
{
DeleteFile(_jsonFile);
DeleteFile(_outputFile);
}
}
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsJsonSupported))]
public async Task TestJsonStreamingAsync()
{
try
{
GenerateJsonFile(1000, _jsonFile);
using SqlConnection connection = new(DataTestUtility.TCPConnectionString);
await connection.OpenAsync();
using Table jsonTable = new(connection, "jsonTab", "(data json)");
await StreamJsonFileToServerAsync(connection, jsonTable.Name);
await PrintJsonDataToFileAsync(connection, jsonTable.Name);
CompareJsonFiles();
}
finally
{
DeleteFile(_jsonFile);
DeleteFile(_outputFile);
}
}
}
}