-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathFbBlobTests.cs
More file actions
161 lines (138 loc) · 5.55 KB
/
FbBlobTests.cs
File metadata and controls
161 lines (138 loc) · 5.55 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
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* All Rights Reserved.
*/
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
using System.Security.Cryptography;
using System.Threading.Tasks;
using FirebirdSql.Data.TestsBase;
using NUnit.Framework;
namespace FirebirdSql.Data.FirebirdClient.Tests;
[TestFixtureSource(typeof(FbServerTypeTestFixtureSource), nameof(FbServerTypeTestFixtureSource.Default))]
[TestFixtureSource(typeof(FbServerTypeTestFixtureSource), nameof(FbServerTypeTestFixtureSource.Embedded))]
public class FbBlobTests : FbTestsBase
{
public FbBlobTests(FbServerType serverType, bool compression, FbWireCrypt wireCrypt)
: base(serverType, compression, wireCrypt)
{ }
[Test]
public async Task BinaryBlobTest()
{
var id_value = RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);
var insert_values = RandomNumberGenerator.GetBytes(100000 * 4);
await using (var transaction = await Connection.BeginTransactionAsync())
{
await using (var insert = new FbCommand("INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)", Connection, transaction))
{
insert.Parameters.Add("@int_field", FbDbType.Integer).Value = id_value;
insert.Parameters.Add("@blob_field", FbDbType.Binary).Value = insert_values;
await insert.ExecuteNonQueryAsync();
}
await transaction.CommitAsync();
}
await using (var select = new FbCommand($"SELECT blob_field FROM TEST WHERE int_field = {id_value}", Connection))
{
var select_values = (byte[])await select.ExecuteScalarAsync();
CollectionAssert.AreEqual(insert_values, select_values);
}
}
[Test]
public async Task ReaderGetBytes()
{
var id_value = RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);
var insert_values = RandomNumberGenerator.GetBytes(100000 * 4);
await using (var transaction = await Connection.BeginTransactionAsync())
{
await using (var insert = new FbCommand("INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)", Connection, transaction))
{
insert.Parameters.Add("@int_field", FbDbType.Integer).Value = id_value;
insert.Parameters.Add("@blob_field", FbDbType.Binary).Value = insert_values;
await insert.ExecuteNonQueryAsync();
}
await transaction.CommitAsync();
}
await using (var select = new FbCommand($"SELECT blob_field FROM TEST WHERE int_field = {id_value}", Connection))
{
var select_values = new byte[100000 * 4];
using (var reader = await select.ExecuteReaderAsync())
{
var index = 0;
var segmentSize = 1000;
while (await reader.ReadAsync())
{
while (index < 400000)
{
reader.GetBytes(0, index, select_values, index, segmentSize);
index += segmentSize;
}
}
}
CollectionAssert.AreEqual(insert_values, select_values);
}
}
[Test]
public async Task BinaryBlobTestWithLargeSegmentSize()
{
var id_value = RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);
var insert_values = RandomNumberGenerator.GetBytes(200000);
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
csb.BlobSegmentSize = 65535;
await using (var conn = new FbConnection(csb.ToString()))
{
await conn.OpenAsync();
await using (var transaction = await conn.BeginTransactionAsync())
{
await using (var insert = new FbCommand("INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)", conn, transaction))
{
insert.Parameters.Add("@int_field", FbDbType.Integer).Value = id_value;
insert.Parameters.Add("@blob_field", FbDbType.Binary).Value = insert_values;
await insert.ExecuteNonQueryAsync();
}
await transaction.CommitAsync();
}
await using (var select = new FbCommand($"SELECT blob_field FROM TEST WHERE int_field = {id_value}", conn))
{
var select_values = (byte[])await select.ExecuteScalarAsync();
CollectionAssert.AreEqual(insert_values, select_values);
}
}
}
[Test]
public async Task BinaryBlobTestWithMaxSegmentSize()
{
var id_value = RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);
var insert_values = RandomNumberGenerator.GetBytes(100000);
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
csb.BlobSegmentSize = 65535;
await using (var conn = new FbConnection(csb.ToString()))
{
await conn.OpenAsync();
Assert.AreEqual(65535, conn.BlobSegmentSize);
await using (var transaction = await conn.BeginTransactionAsync())
{
await using (var insert = new FbCommand("INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)", conn, transaction))
{
insert.Parameters.Add("@int_field", FbDbType.Integer).Value = id_value;
insert.Parameters.Add("@blob_field", FbDbType.Binary).Value = insert_values;
await insert.ExecuteNonQueryAsync();
}
await transaction.CommitAsync();
}
await using (var select = new FbCommand($"SELECT blob_field FROM TEST WHERE int_field = {id_value}", conn))
{
var select_values = (byte[])await select.ExecuteScalarAsync();
CollectionAssert.AreEqual(insert_values, select_values);
}
}
}
}