-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathTimeUuidSerializationTests.cs
More file actions
183 lines (162 loc) · 8.03 KB
/
TimeUuidSerializationTests.cs
File metadata and controls
183 lines (162 loc) · 8.03 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Copyright (C) DataStax Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cassandra.IntegrationTests.TestBase;
using Cassandra.IntegrationTests.TestClusterManagement;
using Cassandra.Tests;
using NUnit.Framework;
namespace Cassandra.IntegrationTests.Core
{
[Category(TestCategory.Short), Category(TestCategory.RealCluster), Category(TestCategory.ServerApi)]
public class TimeUuidSerializationTests : SharedClusterTest
{
private const string AllTypesTableName = "all_formats_table";
private const string MinMaxTimeUuidTable = "min_max_timeuuid";
private PreparedStatement _insertPrepared;
private PreparedStatement _selectPrepared;
protected override string[] SetupQueries => new[]
{
$"CREATE TABLE {MinMaxTimeUuidTable} (id uuid, timeuuid_sample timeuuid, PRIMARY KEY((id), timeuuid_sample))",
string.Format(TestUtils.CreateTableAllTypes, AllTypesTableName)
};
private static string GetToDateFunction()
{
if (TestClusterManager.CheckCassandraVersion(true, new Version(2, 2), Comparison.GreaterThanOrEqualsTo))
{
return "toDate";
}
else
{
return "dateOf";
}
}
public override void OneTimeSetUp()
{
base.OneTimeSetUp();
var insertQuery = $"INSERT INTO {AllTypesTableName} (id, timeuuid_sample) VALUES (?, ?)";
var selectQuery = $"SELECT id, timeuuid_sample, {GetToDateFunction()}(timeuuid_sample) FROM {AllTypesTableName} WHERE id = ?";
if (TestClusterManager.CheckCassandraVersion(false, new Version(2, 2), Comparison.GreaterThanOrEqualsTo))
{
selectQuery =
$"SELECT id, timeuuid_sample, toTimestamp(timeuuid_sample) as timeuuid_date_value FROM {AllTypesTableName} WHERE id = ?";
}
_insertPrepared = Session.Prepare(insertQuery);
_selectPrepared = Session.Prepare(selectQuery);
}
[Test]
public void DeserializationTests()
{
var rowIdArray = new[] { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
var ticksArray = new[] { 35465410426481591, 63546500026481590, 70011723893288L, 1723893288L };
const string format = "yyyy-MM-dd HH:mm:ss.SSS";
for (var i = 0; i < rowIdArray.Length; i++)
{
var rowId = rowIdArray[i];
var ticks = ticksArray[i];
var timeUuid = TimeUuid.NewId(new DateTimeOffset(ticks, TimeSpan.Zero));
Session.Execute(_insertPrepared.Bind(rowId, timeUuid));
var row = Session.Execute(_selectPrepared.Bind(rowId)).FirstOrDefault();
Assert.NotNull(row);
var resultTimeUuidValue = row.GetValue<TimeUuid>("timeuuid_sample");
Assert.AreEqual(timeUuid, resultTimeUuidValue);
Assert.AreEqual(timeUuid.GetDate(), resultTimeUuidValue.GetDate());
if (row.GetColumn("timeuuid_date_value") != null)
{
// the timestamp retrieved by the cql function has lower precision than timeuuid
const long precision = 10000L;
Assert.AreEqual(
timeUuid.GetDate().Ticks / precision,
row.GetValue<DateTimeOffset>("timeuuid_date_value").Ticks / precision);
}
//Still defaults to Guid
var boxedValue = row.GetValue<object>("timeuuid_sample");
Assert.IsInstanceOf<Guid>(boxedValue);
Assert.AreEqual(resultTimeUuidValue, (TimeUuid)(Guid)boxedValue);
//The precision is lost, up to milliseconds is fine
Assert.AreEqual(timeUuid.GetDate().ToString(format), row.GetValue<DateTimeOffset>(2).ToString(format));
}
}
[Test]
public void TimeUuid_Should_Execute_TimeUuid_CQL_Functions()
{
var guid = Guid.NewGuid();
var timeuuidSample = TimeUuid.NewId();
var dateOffset = timeuuidSample.GetDate();
var selectMinMaxTimeuuidPrepared = Session.Prepare($"select * from {MinMaxTimeUuidTable} where id = ? " +
"and timeuuid_sample < ? and timeuuid_sample > ?");
var insertMinMaxTimeuuidPrepared = Session.Prepare($"insert into {MinMaxTimeUuidTable} (id, timeuuid_sample) values (?, ?)");
Session.Execute(insertMinMaxTimeuuidPrepared.Bind(guid, timeuuidSample));
var row = Session.Execute(selectMinMaxTimeuuidPrepared.Bind(guid, TimeUuid.Max(dateOffset), TimeUuid.Min(dateOffset))).FirstOrDefault();
Assert.NotNull(row);
}
[Test]
public void RandomValuesTest()
{
var tasks = new List<Task>();
for (var i = 0; i < 500; i++)
{
tasks.Add(
Session.ExecuteAsync(_insertPrepared.Bind(Guid.NewGuid(), TimeUuid.NewId())));
}
Assert.DoesNotThrow(() => Task.WaitAll(tasks.ToArray()));
var selectQuery = $"SELECT id, timeuuid_sample, {GetToDateFunction()}(timeuuid_sample) FROM {AllTypesTableName} LIMIT 10000";
Assert.DoesNotThrow(() =>
Enumerable.Select(Session.Execute(selectQuery), r => r.GetValue<TimeUuid>("timeuuid_sample")).ToArray());
}
[Test]
public void ComparisonTest()
{
var rowIdBefore = Guid.NewGuid();
var rowIdAfter = Guid.NewGuid();
var dt1 = new DateTime(2016, 1, 1, 4, 55, 00);
var dt2 = new DateTime(2016, 1, 1, 5, 55, 00);
var ctimes = dt1.CompareTo(dt2);
//base check
Assert.That(ctimes < 0);
var timeuuidBefore = TimeUuid.NewId(dt1);
var timeuuidAfter = TimeUuid.NewId(dt2);
Session.Execute(_insertPrepared.Bind(rowIdBefore, timeuuidBefore));
Session.Execute(_insertPrepared.Bind(rowIdAfter, timeuuidAfter));
var cuuids = timeuuidBefore.CompareTo(timeuuidAfter);
Assert.That(cuuids < 0); //Double checking Timeuuid comparison
var row1 = Session.Execute(_selectPrepared.Bind(rowIdBefore)).FirstOrDefault();
Assert.NotNull(row1);
var row2 = Session.Execute(_selectPrepared.Bind(rowIdAfter)).FirstOrDefault();
Assert.NotNull(row2);
if (row1.GetColumn("timeuuid_date_value") != null
&& row2.GetColumn("timeuuid_date_value") != null)
{
//checking the comparison of (de)serialized timeuuid timestamps
var ticks1 = row1.GetValue<DateTimeOffset>("timeuuid_date_value").Ticks;
var ticks2 = row2.GetValue<DateTimeOffset>("timeuuid_date_value").Ticks;
Assert.Greater(ticks2, ticks1);
}
}
[Test]
public void SerializationTests()
{
//TimeUuid and Guid are valid values for a timeuuid column value
Assert.DoesNotThrow(() =>
Session.Execute(_insertPrepared.Bind(Guid.NewGuid(), TimeUuid.NewId())));
var validUuidV1Bytes = TimeUuid.NewId().ToByteArray();
Assert.DoesNotThrow(() =>
Session.Execute(_insertPrepared.Bind(Guid.NewGuid(), new Guid(validUuidV1Bytes))));
}
}
}