|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Numerics; |
| 5 | +using System.Text; |
| 6 | +using Cassandra.IntegrationTests.TestBase; |
| 7 | +using Cassandra.Serialization; |
| 8 | +using Cassandra.Tests; |
| 9 | +using Cassandra.Tests.Extensions.Serializers; |
| 10 | +using NUnit.Framework; |
| 11 | + |
| 12 | +namespace Cassandra.IntegrationTests.Core |
| 13 | +{ |
| 14 | + [Category("short")] |
| 15 | + public class TypeSerializersTests : SharedClusterTest |
| 16 | + { |
| 17 | + private const string DecimalInsertQuery = "INSERT INTO tbl_decimal (id, text_value, value1, value2) VALUES (?, ?, ?, ?)"; |
| 18 | + private const string DecimalSelectQuery = "SELECT * FROM tbl_decimal WHERE id = ?"; |
| 19 | + private const string CustomInsertQuery = "INSERT INTO tbl_custom (id, value) VALUES (?, ?)"; |
| 20 | + private const string CustomSelectQuery = "SELECT * FROM tbl_custom WHERE id = ?"; |
| 21 | + |
| 22 | + private const string CustomTypeName = "org.apache.cassandra.db.marshal.DynamicCompositeType(" + |
| 23 | + "s=>org.apache.cassandra.db.marshal.UTF8Type," + |
| 24 | + "i=>org.apache.cassandra.db.marshal.Int32Type)"; |
| 25 | + |
| 26 | + |
| 27 | + protected override string[] SetupQueries |
| 28 | + { |
| 29 | + get |
| 30 | + { |
| 31 | + return new [] |
| 32 | + { |
| 33 | + "CREATE TABLE tbl_decimal (id uuid PRIMARY KEY, text_value text, value1 decimal, value2 decimal)", |
| 34 | + "CREATE TABLE tbl_decimal_key (id decimal PRIMARY KEY)", |
| 35 | + string.Format("CREATE TABLE tbl_custom (id uuid PRIMARY KEY, value '{0}')", CustomTypeName) |
| 36 | + }; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void Should_Throw_When_TypeSerializer_Not_Defined() |
| 42 | + { |
| 43 | + var statement = new SimpleStatement(DecimalInsertQuery, Guid.NewGuid(), null, new BigDecimal(1, 100000909), null); |
| 44 | + var ex = Assert.Throws<InvalidTypeException>(() => Session.Execute(statement)); |
| 45 | + StringAssert.Contains("CLR", ex.Message); |
| 46 | + } |
| 47 | + |
| 48 | + [Test] |
| 49 | + public void Should_Use_Primitive_TypeSerializers_With_BoundStatements() |
| 50 | + { |
| 51 | + var builder = Cluster.Builder() |
| 52 | + .AddContactPoint(TestCluster.InitialContactPoint) |
| 53 | + .WithTypeSerializers(new TypeSerializerDefinitions() |
| 54 | + .Define(new BigDecimalSerializer()) |
| 55 | + .Define(new DummyCustomTypeSerializer())); |
| 56 | + using (var cluster = builder.Build()) |
| 57 | + { |
| 58 | + var session = cluster.Connect(KeyspaceName); |
| 59 | + object[][] values = |
| 60 | + { |
| 61 | + new object[] { Guid.NewGuid(), new BigDecimal(1, BigInteger.Parse("9999999999999999999999999999")), 999999999999999999999999999.9m }, |
| 62 | + new object[] { Guid.NewGuid(), new BigDecimal(6, 367383), 0.367383M }, |
| 63 | + new object[] { Guid.NewGuid(), new BigDecimal(0, 0), 0M }, |
| 64 | + new object[] { Guid.NewGuid(), new BigDecimal(1, -1), -0.1M } |
| 65 | + }; |
| 66 | + var ps = session.Prepare(DecimalInsertQuery); |
| 67 | + foreach (var item in values) |
| 68 | + { |
| 69 | + var id = item[0]; |
| 70 | + //allow inserts as BigDecimal and decimal |
| 71 | + session.Execute(ps.Bind(id, item[2].ToString(), item[1], item[2])); |
| 72 | + var row = session.Execute(new SimpleStatement(DecimalSelectQuery, id)).First(); |
| 73 | + //it allows to retrieve only by 1 type |
| 74 | + Assert.AreEqual(row.GetValue<BigDecimal>("value1"), item[1]); |
| 75 | + Assert.AreEqual(row.GetValue<BigDecimal>("value2"), item[1]); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void Should_Use_Primitive_TypeSerializers_With_SimpleStatements() |
| 82 | + { |
| 83 | + var builder = Cluster.Builder() |
| 84 | + .AddContactPoint(TestCluster.InitialContactPoint) |
| 85 | + .WithTypeSerializers(new TypeSerializerDefinitions() |
| 86 | + .Define(new BigDecimalSerializer()) |
| 87 | + .Define(new DummyCustomTypeSerializer())); |
| 88 | + using (var cluster = builder.Build()) |
| 89 | + { |
| 90 | + var session = cluster.Connect(KeyspaceName); |
| 91 | + object[][] values = |
| 92 | + { |
| 93 | + new object[] { Guid.NewGuid(), new BigDecimal(1, 100000909), 10000090.9M }, |
| 94 | + new object[] { Guid.NewGuid(), new BigDecimal(5, 367383), 3.67383M }, |
| 95 | + new object[] { Guid.NewGuid(), new BigDecimal(0, 0), 0M }, |
| 96 | + new object[] { Guid.NewGuid(), new BigDecimal(0, -1), -1M } |
| 97 | + }; |
| 98 | + foreach (var item in values) |
| 99 | + { |
| 100 | + var id = item[0]; |
| 101 | + //allow inserts as BigDecimal and decimal |
| 102 | + var statement = new SimpleStatement(DecimalInsertQuery, id, item[2].ToString(), item[1], item[2]); |
| 103 | + session.Execute(statement); |
| 104 | + var row = session.Execute(new SimpleStatement(DecimalSelectQuery, id)).First(); |
| 105 | + //it allows to retrieve only by 1 type |
| 106 | + Assert.AreEqual(row.GetValue<BigDecimal>("value1"), item[1]); |
| 107 | + Assert.AreEqual(row.GetValue<BigDecimal>("value2"), item[1]); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public void Should_Use_Primitive_TypeSerializers_With_Simple_BatchStatements() |
| 114 | + { |
| 115 | + var builder = Cluster.Builder() |
| 116 | + .AddContactPoint(TestCluster.InitialContactPoint) |
| 117 | + .WithTypeSerializers(new TypeSerializerDefinitions() |
| 118 | + .Define(new BigDecimalSerializer()) |
| 119 | + .Define(new DummyCustomTypeSerializer())); |
| 120 | + using (var cluster = builder.Build()) |
| 121 | + { |
| 122 | + var session = cluster.Connect(KeyspaceName); |
| 123 | + object[][] values = |
| 124 | + { |
| 125 | + new object[] { Guid.NewGuid(), new BigDecimal(2, 9071), 90.71M }, |
| 126 | + new object[] { Guid.NewGuid(), new BigDecimal(5, 367383), 3.67383M }, |
| 127 | + new object[] { Guid.NewGuid(), new BigDecimal(0, 0), 0M }, |
| 128 | + new object[] { Guid.NewGuid(), new BigDecimal(0, -1), -1M } |
| 129 | + }; |
| 130 | + var batch = new BatchStatement(); |
| 131 | + foreach (var item in values) |
| 132 | + { |
| 133 | + var id = item[0]; |
| 134 | + batch.Add(new SimpleStatement(DecimalInsertQuery, id, item[2].ToString(), item[1], item[2])); |
| 135 | + } |
| 136 | + session.Execute(batch); |
| 137 | + foreach (var item in values) |
| 138 | + { |
| 139 | + var id = item[0]; |
| 140 | + var row = session.Execute(new SimpleStatement(DecimalSelectQuery, id)).First(); |
| 141 | + Assert.AreEqual(row.GetValue<BigDecimal>("value1"), item[1]); |
| 142 | + Assert.AreEqual(row.GetValue<BigDecimal>("value2"), item[1]); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + [Test] |
| 148 | + public void Should_Use_Primitive_TypeSerializers_For_Partition_Key() |
| 149 | + { |
| 150 | + var builder = Cluster.Builder() |
| 151 | + .AddContactPoint(TestCluster.InitialContactPoint) |
| 152 | + .WithTypeSerializers(new TypeSerializerDefinitions() |
| 153 | + .Define(new BigDecimalSerializer()) |
| 154 | + .Define(new DummyCustomTypeSerializer())); |
| 155 | + using (var cluster = builder.Build()) |
| 156 | + { |
| 157 | + var session = cluster.Connect(KeyspaceName); |
| 158 | + var values = new[] |
| 159 | + { |
| 160 | + new BigDecimal(3, 123), |
| 161 | + new BigDecimal(6, 367383), |
| 162 | + new BigDecimal(0, 0), |
| 163 | + new BigDecimal(1, -1) |
| 164 | + }; |
| 165 | + var ps = session.Prepare("INSERT INTO tbl_decimal_key (id) VALUES (?)"); |
| 166 | + foreach (var v in values) |
| 167 | + { |
| 168 | + var statement = ps.Bind(v); |
| 169 | + Assert.NotNull(statement.RoutingKey); |
| 170 | + CollectionAssert.AreEqual(new BigDecimalSerializer().Serialize((ushort) session.BinaryProtocolVersion, v), statement.RoutingKey.RawRoutingKey); |
| 171 | + session.Execute(statement); |
| 172 | + var row = session.Execute(new SimpleStatement("SELECT * FROM tbl_decimal_key WHERE id = ?", v)).First(); |
| 173 | + Assert.AreEqual(row.GetValue<BigDecimal>("id"), v); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + [Test] |
| 179 | + public void Should_Use_Custom_TypeSerializers() |
| 180 | + { |
| 181 | + var builder = Cluster.Builder() |
| 182 | + .AddContactPoint(TestCluster.InitialContactPoint) |
| 183 | + .WithTypeSerializers(new TypeSerializerDefinitions() |
| 184 | + .Define(new DummyCustomTypeSerializer(CustomTypeName))); |
| 185 | + using (var cluster = builder.Build()) |
| 186 | + { |
| 187 | + var session = cluster.Connect(KeyspaceName); |
| 188 | + byte[] buffer = |
| 189 | + { |
| 190 | + 0x80, (byte)'i', 0, 4, 0, 0, 0, 1, 0 |
| 191 | + }; |
| 192 | + object[][] values = |
| 193 | + { |
| 194 | + new object[] { Guid.NewGuid(), new DummyCustomType(buffer) } |
| 195 | + }; |
| 196 | + var ps = session.Prepare(CustomInsertQuery); |
| 197 | + foreach (var item in values) |
| 198 | + { |
| 199 | + var id = item[0]; |
| 200 | + var customValue = (DummyCustomType) item[1]; |
| 201 | + session.Execute(ps.Bind(id, item[1])); |
| 202 | + var row = session.Execute(new SimpleStatement(CustomSelectQuery, id)).First(); |
| 203 | + Assert.AreEqual(row.GetValue<DummyCustomType>("value").Buffer, customValue.Buffer); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments