|
| 1 | +using System.Data.Common; |
| 2 | +using FireboltDotNetSdk.Client; |
| 3 | +using FireboltDotNetSdk.Utils; |
| 4 | + |
| 5 | +namespace FireboltDotNetSdk.Tests |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + [Category("v2")] |
| 9 | + [Category("engine-v2")] |
| 10 | + internal class StructMappingTest : IntegrationTest |
| 11 | + { |
| 12 | + private const string TableName = "struct_test"; |
| 13 | + |
| 14 | + private static readonly string[] SetupSql = |
| 15 | + { |
| 16 | + "SET advanced_mode=1", |
| 17 | + "SET enable_create_table_v2=true", |
| 18 | + "SET enable_struct_syntax=true", |
| 19 | + "SET prevent_create_on_information_schema=true", |
| 20 | + "SET enable_create_table_with_struct_type=true", |
| 21 | + $"DROP TABLE IF EXISTS {TableName}", |
| 22 | + $"CREATE TABLE IF NOT EXISTS {TableName} (" + |
| 23 | + "id numeric(5,0), " + |
| 24 | + "plain struct(int_val int, str_val text, arr_val array(numeric(5,3)))" + |
| 25 | + ")" |
| 26 | + }; |
| 27 | + |
| 28 | + private const string CleanupSql = $"DROP TABLE IF EXISTS {TableName}"; |
| 29 | + |
| 30 | + [OneTimeSetUp] |
| 31 | + public void GlobalSetup() |
| 32 | + { |
| 33 | + using var conn = new FireboltConnection(ConnectionString()); |
| 34 | + conn.Open(); |
| 35 | + SetupStructTable(conn); |
| 36 | + } |
| 37 | + |
| 38 | + [OneTimeTearDown] |
| 39 | + public void GlobalTearDown() |
| 40 | + { |
| 41 | + using var conn = new FireboltConnection(ConnectionString()); |
| 42 | + conn.Open(); |
| 43 | + CreateCommand(conn, CleanupSql).ExecuteNonQuery(); |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + public void InsertAndSelectStruct_PocoWithAttributesMapping_Sync() |
| 48 | + { |
| 49 | + using var conn = new FireboltConnection(ConnectionString()); |
| 50 | + conn.Open(); |
| 51 | + |
| 52 | + var select = CreateCommand(conn, $"SELECT id, plain FROM {TableName} ORDER BY id ASC"); |
| 53 | + using var reader = select.ExecuteReader(); |
| 54 | + Assert.Multiple(() => |
| 55 | + { |
| 56 | + Assert.That(reader.Read(), Is.True); |
| 57 | + Assert.That(reader.GetFieldValue<decimal>(0), Is.EqualTo(1)); |
| 58 | + }); |
| 59 | + var plain = reader.GetFieldValue<PlainStructWithAttributes>(1); |
| 60 | + AssertPocoWithAttributes(plain); |
| 61 | + Assert.That(reader.Read(), Is.False); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public async Task InsertAndSelectStruct_PocoWithAttributesMapping_Async() |
| 66 | + { |
| 67 | + await using var conn = new FireboltConnection(ConnectionString()); |
| 68 | + await conn.OpenAsync(); |
| 69 | + |
| 70 | + var select = CreateCommand(conn, $"SELECT id, plain FROM {TableName} ORDER BY id ASC"); |
| 71 | + await using var reader = await select.ExecuteReaderAsync(); |
| 72 | + Assert.Multiple(async () => |
| 73 | + { |
| 74 | + Assert.That(await reader.ReadAsync(), Is.True); |
| 75 | + Assert.That(await reader.GetFieldValueAsync<decimal>(0), Is.EqualTo(1)); |
| 76 | + }); |
| 77 | + var plain = await reader.GetFieldValueAsync<PlainStructWithAttributes>(1); |
| 78 | + AssertPocoWithAttributes(plain); |
| 79 | + Assert.That(await reader.ReadAsync(), Is.False); |
| 80 | + } |
| 81 | + |
| 82 | + [Test] |
| 83 | + public void InsertAndSelectStruct_PocoWithoutAttributesMapping_Sync() |
| 84 | + { |
| 85 | + using var conn = new FireboltConnection(ConnectionString()); |
| 86 | + conn.Open(); |
| 87 | + |
| 88 | + var select = CreateCommand(conn, $"SELECT id, plain FROM {TableName} ORDER BY id ASC"); |
| 89 | + using var reader = select.ExecuteReader(); |
| 90 | + Assert.Multiple(() => |
| 91 | + { |
| 92 | + Assert.That(reader.Read(), Is.True); |
| 93 | + Assert.That(reader.GetFieldValue<decimal>(0), Is.EqualTo(1)); |
| 94 | + }); |
| 95 | + var plain = reader.GetFieldValue<PlainStructWithoutAttributes>(1); |
| 96 | + AssertPocoWithoutAttributes(plain); |
| 97 | + Assert.That(reader.Read(), Is.False); |
| 98 | + } |
| 99 | + |
| 100 | + [Test] |
| 101 | + public async Task InsertAndSelectStruct_PocoWithoutAttributesMapping_Async() |
| 102 | + { |
| 103 | + await using var conn = new FireboltConnection(ConnectionString()); |
| 104 | + await conn.OpenAsync(); |
| 105 | + |
| 106 | + var select = CreateCommand(conn, $"SELECT id, plain FROM {TableName} ORDER BY id ASC"); |
| 107 | + await using var reader = await select.ExecuteReaderAsync(); |
| 108 | + Assert.Multiple(async () => |
| 109 | + { |
| 110 | + Assert.That(await reader.ReadAsync(), Is.True); |
| 111 | + Assert.That(await reader.GetFieldValueAsync<decimal>(0), Is.EqualTo(1)); |
| 112 | + }); |
| 113 | + var plain = await reader.GetFieldValueAsync<PlainStructWithoutAttributes>(1); |
| 114 | + AssertPocoWithoutAttributes(plain); |
| 115 | + Assert.That(await reader.ReadAsync(), Is.False); |
| 116 | + } |
| 117 | + |
| 118 | + private static void AssertPocoWithAttributes(PlainStructWithAttributes plain) |
| 119 | + { |
| 120 | + Assert.Multiple(() => |
| 121 | + { |
| 122 | + Assert.That(plain.IntVal, Is.EqualTo(1)); |
| 123 | + Assert.That(plain.StrVal, Is.EqualTo("test")); |
| 124 | + Assert.That(plain.ArrVal, Is.Not.Null); |
| 125 | + Assert.That(plain.ArrVal, Has.Length.EqualTo(2)); |
| 126 | + Assert.That(plain.ArrVal[0], Is.EqualTo(12.34f).Within(1e-4)); |
| 127 | + Assert.That(plain.ArrVal[1], Is.EqualTo(56.789f).Within(1e-4)); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + private static void AssertPocoWithoutAttributes(PlainStructWithoutAttributes plain) |
| 132 | + { |
| 133 | + Assert.Multiple(() => |
| 134 | + { |
| 135 | + Assert.That(plain.IntVal, Is.EqualTo(1)); |
| 136 | + Assert.That(plain.StrVal, Is.EqualTo("test")); |
| 137 | + Assert.That(plain.ArrVal, Is.Not.Null); |
| 138 | + Assert.That(plain.ArrVal, Has.Length.EqualTo(2)); |
| 139 | + Assert.That(plain.ArrVal[0], Is.EqualTo(12.34f).Within(1e-4)); |
| 140 | + Assert.That(plain.ArrVal[1], Is.EqualTo(56.789f).Within(1e-4)); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + private static void SetupStructTable(FireboltConnection conn) |
| 145 | + { |
| 146 | + foreach (var sql in SetupSql) |
| 147 | + { |
| 148 | + CreateCommand(conn, sql).ExecuteNonQuery(); |
| 149 | + } |
| 150 | + |
| 151 | + // Insert a struct row |
| 152 | + const string insert = $"INSERT INTO {TableName} (id, plain) VALUES (1, struct(1, 'test', [12.34, 56.789]))"; |
| 153 | + CreateCommand(conn, insert).ExecuteNonQuery(); |
| 154 | + } |
| 155 | + |
| 156 | + private class PlainStructWithAttributes |
| 157 | + { |
| 158 | + |
| 159 | + [FireboltStructName("int_val")] |
| 160 | + public int IntVal { get; init; } |
| 161 | + |
| 162 | + [FireboltStructName("str_val")] |
| 163 | + public string StrVal { get; init; } = null!; |
| 164 | + |
| 165 | + [FireboltStructName("arr_val")] |
| 166 | + public float[] ArrVal { get; init; } = null!; |
| 167 | + } |
| 168 | + |
| 169 | + private class PlainStructWithoutAttributes |
| 170 | + { |
| 171 | + |
| 172 | + public int IntVal { get; init; } |
| 173 | + |
| 174 | + public string StrVal { get; init; } = null!; |
| 175 | + |
| 176 | + public float[] ArrVal { get; init; } = null!; |
| 177 | + } |
| 178 | + |
| 179 | + private static DbCommand CreateCommand(DbConnection conn, string query) |
| 180 | + { |
| 181 | + var command = conn.CreateCommand(); |
| 182 | + command.CommandText = query; |
| 183 | + return command; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | + |
0 commit comments