Skip to content

Commit

Permalink
FbBlobStream
Browse files Browse the repository at this point in the history
  • Loading branch information
la-we committed Dec 6, 2023
1 parent 867fa99 commit 06ba286
Show file tree
Hide file tree
Showing 10 changed files with 797 additions and 46 deletions.
86 changes: 86 additions & 0 deletions src/FirebirdSql.Data.FirebirdClient.Tests/FbBlobStreamTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.IO;
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 FbBlobStreamTests : FbTestsBase
{
public FbBlobStreamTests(FbServerType serverType, bool compression, FbWireCrypt wireCrypt)
: base(serverType, compression, wireCrypt)
{ }

[Test]
public async Task FbBlobStreamReadTest()
{
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))
{
await using var reader = await select.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
await using var output = new MemoryStream();
await using (var stream = reader.GetStream(0))
{
await stream.CopyToAsync(output);
}

var select_values = output.ToArray();
CollectionAssert.AreEqual(insert_values, select_values);
}
}
}

[Test]
public async Task FbBlobStreamWriteTest()
{
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 using (var select = new FbCommand($"SELECT blob_field FROM TEST WHERE int_field = {id_value}", Connection, transaction))
{
await using var reader = await select.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
await using var stream = reader.GetStream(0);
await stream.WriteAsync(insert_values);

break;
}
}
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);
}
}
}
1 change: 1 addition & 0 deletions src/FirebirdSql.Data.FirebirdClient.Tests/FbBlobTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public async Task BinaryBlobTest()
insert.Parameters.Add("@blob_field", FbDbType.Binary).Value = insert_values;
await insert.ExecuteNonQueryAsync();
}

await transaction.CommitAsync();
}

Expand Down
Loading

0 comments on commit 06ba286

Please sign in to comment.