Skip to content

Commit

Permalink
Added support for DataSet/DataTable DiffGram (to transfer RowStates a…
Browse files Browse the repository at this point in the history
…nd row versions) serialization with BSON serializer
  • Loading branch information
theRainbird committed Nov 24, 2024
1 parent ad8f8ef commit ad3f486
Show file tree
Hide file tree
Showing 9 changed files with 786 additions and 1 deletion.
37 changes: 37 additions & 0 deletions CoreRemoting.Tests/BsonSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data;
using CoreRemoting.RpcMessaging;
using CoreRemoting.Serialization.Bson;
using CoreRemoting.Tests.Tools;
Expand Down Expand Up @@ -192,5 +193,41 @@ public void BsonSerializerAdapter_should_deserialize_Int32_value_in_envelope_cor
Assert.Equal(envelope.Value, deserializedValue.Value);
Assert.IsType<Int32>(envelope.Value);
}

[Fact]
public void BsonSerializerAdapter_should_serialize_DataSet_as_Diffgram()
{
var originalTable = new DataTable("TestTable");
originalTable.Columns.Add("UserName", typeof(string));
originalTable.Columns.Add("Age", typeof(short));
var originalDataSet = new DataSet("TestDataSet");
originalDataSet.Tables.Add(originalTable);

var originalRow = originalTable.NewRow();
originalRow["UserName"] = "Tester";
originalRow["Age"] = 44;
originalTable.Rows.Add(originalRow);

originalTable.AcceptChanges();

originalRow["Age"] = 43;

var envelope = new Envelope(originalDataSet);

var serializer = new BsonSerializerAdapter();
var raw = serializer.Serialize(envelope);
var deserializedEnvelope = serializer.Deserialize<Envelope>(raw);

var deserializedDataSet = (DataSet)deserializedEnvelope.Value;
var deserializedTable = deserializedDataSet.Tables["TestTable"];
var deserializedRow = deserializedTable!.Rows[0];

Assert.Equal(originalDataSet.DataSetName, deserializedDataSet.DataSetName);
Assert.Equal(originalTable.TableName, deserializedTable.TableName);
Assert.Equal(originalRow.RowState, deserializedRow.RowState);
Assert.Equal(originalRow["Age", DataRowVersion.Original], deserializedRow["Age", DataRowVersion.Original]);
Assert.Equal(originalRow["Age", DataRowVersion.Current], deserializedRow["Age", DataRowVersion.Current]);
Assert.Equal(originalRow["UserName", DataRowVersion.Current], deserializedRow["UserName", DataRowVersion.Current]);
}
}
}
152 changes: 152 additions & 0 deletions CoreRemoting.Tests/DataSetSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System.Data;
using System.IO;
using System.Reflection;
using CoreRemoting.Serialization.Bson.DataSetDiffGramSupport;
using Newtonsoft.Json;
using Xunit;

namespace CoreRemoting.Tests;

public class DataSetSerializationTests
{
[Fact]
public void DataSetDiffGramJsonConverter_should_serialize_typed_DataSet_to_DiffGram()
{
var originalDataSet = new TestDataSet();
var originalRow = originalDataSet.TestTable.NewTestTableRow();
originalRow.UserName = "TestUser";
originalRow.Age = 44;
originalDataSet.TestTable.AddTestTableRow(originalRow);
originalDataSet.AcceptChanges();

originalRow.Age = 43;

var json =
JsonConvert.SerializeObject(
value: originalDataSet,
formatting: Formatting.Indented,
converters: new DataSetDiffGramJsonConverter());

var deserializedDataSet =
JsonConvert.DeserializeObject<TestDataSet>(
value: json,
converters: new DataSetDiffGramJsonConverter());

var deserializedRow = deserializedDataSet.TestTable[0];

Assert.Equal(originalDataSet.DataSetName, deserializedDataSet.DataSetName);
Assert.Equal(originalDataSet.Tables.Count, deserializedDataSet.Tables.Count);
Assert.Equal(originalRow.RowState, deserializedRow.RowState);
Assert.Equal(originalRow["Age", DataRowVersion.Original], deserializedRow["Age", DataRowVersion.Original]);
Assert.Equal(originalRow["Age", DataRowVersion.Current], deserializedRow["Age", DataRowVersion.Current]);
Assert.Equal(originalRow["UserName", DataRowVersion.Current], deserializedRow["UserName", DataRowVersion.Current]);
}

[Fact]
public void DataSetDiffGramJsonConverter_should_serialize_typed_DataTable_to_DiffGram()
{
var originalTable = new TestDataSet.TestTableDataTable();
var originalRow = originalTable.NewTestTableRow();
originalRow.UserName = "TestUser";
originalRow.Age = 44;
originalTable.AddTestTableRow(originalRow);
originalTable.AcceptChanges();

originalRow.Age = 43;

var json =
JsonConvert.SerializeObject(
value: originalTable,
formatting: Formatting.Indented,
converters: new DataSetDiffGramJsonConverter());

var deserializedTable =
JsonConvert.DeserializeObject<TestDataSet.TestTableDataTable>(
value: json,
converters: new DataSetDiffGramJsonConverter());

var deserializedRow = deserializedTable[0];

Assert.Equal(originalRow.RowState, deserializedRow.RowState);
Assert.Equal(originalRow["Age", DataRowVersion.Original], deserializedRow["Age", DataRowVersion.Original]);
Assert.Equal(originalRow["Age", DataRowVersion.Current], deserializedRow["Age", DataRowVersion.Current]);
Assert.Equal(originalRow["UserName", DataRowVersion.Current], deserializedRow["UserName", DataRowVersion.Current]);
}

[Fact]
public void DataSetDiffGramJsonConverter_should_serialize_untyped_DataSet_to_DiffGram()
{
var originalTable = new DataTable("TestTable");
originalTable.Columns.Add("UserName", typeof(string));
originalTable.Columns.Add("Age", typeof(short));
var originalDataSet = new DataSet("TestDataSet");
originalDataSet.Tables.Add(originalTable);

var originalRow = originalTable.NewRow();
originalRow["UserName"] = "Tester";
originalRow["Age"] = 44;
originalTable.Rows.Add(originalRow);

originalTable.AcceptChanges();

originalRow["Age"] = 43;

var json =
JsonConvert.SerializeObject(
value: originalDataSet,
formatting: Formatting.Indented,
converters: new DataSetDiffGramJsonConverter());

var deserializedDataSet =
JsonConvert.DeserializeObject<DataSet>(
value: json,
converters: new DataSetDiffGramJsonConverter());

var deserializedTable = deserializedDataSet.Tables["TestTable"];
var deserializedRow = deserializedTable!.Rows[0];

Assert.Equal(originalDataSet.DataSetName, deserializedDataSet.DataSetName);
Assert.Equal(originalTable.TableName, deserializedTable.TableName);
Assert.Equal(originalRow.RowState, deserializedRow.RowState);
Assert.Equal(originalRow["Age", DataRowVersion.Original], deserializedRow["Age", DataRowVersion.Original]);
Assert.Equal(originalRow["Age", DataRowVersion.Current], deserializedRow["Age", DataRowVersion.Current]);
Assert.Equal(originalRow["UserName", DataRowVersion.Current], deserializedRow["UserName", DataRowVersion.Current]);
}

[Fact]
public void DataSetDiffGramJsonConverter_should_serialize_untyped_DataTable_to_DiffGram()
{
var originalTable = new DataTable("TestTable");
originalTable.Columns.Add("UserName", typeof(string));
originalTable.Columns.Add("Age", typeof(short));

var originalRow = originalTable.NewRow();
originalRow["UserName"] = "Tester";
originalRow["Age"] = 44;
originalTable.Rows.Add(originalRow);

originalTable.AcceptChanges();

originalRow["Age"] = 43;

var json =
JsonConvert.SerializeObject(
value: originalTable,
formatting: Formatting.Indented,
converters: new DataSetDiffGramJsonConverter());

var deserializedDataSet =
JsonConvert.DeserializeObject<DataSet>(
value: json,
converters: new DataSetDiffGramJsonConverter());

var deserializedTable = deserializedDataSet.Tables["TestTable"];
var deserializedRow = deserializedTable!.Rows[0];

Assert.Equal(originalTable.TableName, deserializedTable.TableName);
Assert.Equal(originalRow.RowState, deserializedRow.RowState);
Assert.Equal(originalRow["Age", DataRowVersion.Original], deserializedRow["Age", DataRowVersion.Original]);
Assert.Equal(originalRow["Age", DataRowVersion.Current], deserializedRow["Age", DataRowVersion.Current]);
Assert.Equal(originalRow["UserName", DataRowVersion.Current], deserializedRow["UserName", DataRowVersion.Current]);
}
}
Loading

0 comments on commit ad3f486

Please sign in to comment.