-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathFetch.cs
More file actions
583 lines (525 loc) · 26.4 KB
/
Fetch.cs
File metadata and controls
583 lines (525 loc) · 26.4 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//
// 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.Data.Linq;
using Cassandra.IntegrationTests.Mapping.Structures;
using Cassandra.IntegrationTests.TestBase;
using Cassandra.IntegrationTests.TestClusterManagement;
using Cassandra.Mapping;
using Cassandra.Tests;
using Cassandra.Tests.Mapping.FluentMappings;
using Cassandra.Tests.Mapping.Pocos;
using NUnit.Framework;
using HairColor = Cassandra.Tests.Mapping.Pocos.HairColor;
namespace Cassandra.IntegrationTests.Mapping.Tests
{
[Category(TestCategory.Short), Category(TestCategory.RealCluster)]
public class Fetch : SharedClusterTest
{
ISession _session;
private string _uniqueKsName;
public override void OneTimeSetUp()
{
base.OneTimeSetUp();
_session = Session;
}
[SetUp]
public void TestSetup()
{
_uniqueKsName = TestUtils.GetUniqueKeyspaceName();
_session.CreateKeyspace(_uniqueKsName);
_session.ChangeKeyspace(_uniqueKsName);
}
/// <summary>
/// Successfully Fetch mapped records by passing in a static query string
/// </summary>
[Test]
public void Fetch_UsingSelectCqlString()
{
Table<Author> table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
List<string> followersForAuthor = new List<string>() { "follower1", "follower2", "" };
Author expectedAuthor = new Author
{
AuthorId = Guid.NewGuid().ToString(),
Followers = followersForAuthor,
};
mapper.Insert(expectedAuthor);
List<Author> authors = mapper.Fetch<Author>("SELECT * from " + table.Name).ToList();
Assert.AreEqual(1, authors.Count);
expectedAuthor.AssertEquals(authors[0]);
}
/// <summary>
/// Successfully Fetch mapped records by passing in a static query string
/// </summary>
[Test]
public void FetchAsync_Using_Select_Cql_And_PageSize()
{
var table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
var ids = new[] {Guid.NewGuid().ToString(), Guid.NewGuid().ToString()};
mapper.Insert(new Author { AuthorId = ids[0] });
mapper.Insert(new Author { AuthorId = ids[1] });
List<Author> authors = null;
mapper.FetchAsync<Author>(Cql.New("SELECT * from " + table.Name).WithOptions(o => o.SetPageSize(int.MaxValue))).ContinueWith(t =>
{
authors = t.Result.ToList();
}).Wait();
Assert.AreEqual(2, authors.Count);;
CollectionAssert.AreEquivalent(ids, authors.Select(a => a.AuthorId));
}
#if !NETFRAMEWORK
[Test]
public async Task FetchAsAsyncEnumerable_Using_Select_Cql_And_PageSize()
{
var table = new Table<Author>(_session, new MappingConfiguration());
await table.CreateAsync();
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
var ids = new[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() };
await mapper.InsertAsync(new Author { AuthorId = ids[0] });
await mapper.InsertAsync(new Author { AuthorId = ids[1] });
var authors = new List<Author>();
await foreach (var author in mapper.FetchAsAsyncEnumerable<Author>(Cql.New("SELECT * FROM " + table.Name)
.WithOptions(o => o.SetPageSize(int.MaxValue))))
{
authors.Add(author);
}
Assert.AreEqual(2, authors.Count);
CollectionAssert.AreEquivalent(ids, authors.Select(a => a.AuthorId));
}
#endif
/// <summary>
/// Successfully Fetch mapped records by passing in a Cql Object
/// </summary>
[Test]
public void Fetch_UsingCqlObject()
{
Table<Author> table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
int totalInserts = 10;
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
List<Author> expectedAuthors = Author.GetRandomList(totalInserts);
foreach (Author expectedAuthor in expectedAuthors)
mapper.Insert(expectedAuthor);
Cql cql = new Cql("SELECT * from " + table.Name);
List<Author> actualAuthors = mapper.Fetch<Author>(cql).ToList();
Assert.AreEqual(totalInserts, actualAuthors.Count);
Author.AssertListsContainTheSame(expectedAuthors, actualAuthors);
}
/// <summary>
/// Successfully Fetch mapped records by passing in a Cql Object,
/// using all available acceptable consistency levels
/// </summary>
[Test]
public void Fetch_WithConsistencyLevel_Valids()
{
Table<Author> table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
int totalInserts = 10;
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
List<Author> expectedAuthors = Author.GetRandomList(totalInserts);
foreach (Author expectedAuthor in expectedAuthors)
mapper.Insert(expectedAuthor);
var consistencyLevels = new[]
{
ConsistencyLevel.All,
ConsistencyLevel.LocalOne,
ConsistencyLevel.LocalQuorum,
ConsistencyLevel.Quorum,
ConsistencyLevel.One,
};
foreach (var consistencyLevel in consistencyLevels)
{
Cql cql = new Cql("SELECT * from " + table.Name).WithOptions(c => c.SetConsistencyLevel(consistencyLevel));
List<Author> actualAuthors = mapper.Fetch<Author>(cql).ToList();
Assert.AreEqual(totalInserts, actualAuthors.Count);
Author.AssertListsContainTheSame(expectedAuthors, actualAuthors);
}
}
/// <summary>
/// Attempte to Fetch mapped records by passing in a Cql Object,
/// using consistency levels that are only valid for writes
/// </summary>
[Test]
public void Fetch_WithConsistencyLevel_Invalids_OnlySupportedForWrites()
{
Table<Author> table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
int totalInserts = 10;
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
List<Author> expectedAuthors = Author.GetRandomList(totalInserts);
foreach (Author expectedAuthor in expectedAuthors)
mapper.Insert(expectedAuthor);
Cql cql = new Cql("SELECT * from " + table.Name).WithOptions(c => c.SetConsistencyLevel(ConsistencyLevel.Any));
var err = Assert.Throws<InvalidQueryException>(() => mapper.Fetch<Author>(cql).ToList());
Assert.AreEqual("ANY ConsistencyLevel is only supported for writes", err.Message);
if (TestClusterManager.CheckCassandraVersion(false, Version.Parse("3.0"), Comparison.LessThan))
{
cql = new Cql("SELECT * from " + table.Name).WithOptions(c => c.SetConsistencyLevel(ConsistencyLevel.EachQuorum));
err = Assert.Throws<InvalidQueryException>(() => mapper.Fetch<Author>(cql).ToList());
Assert.AreEqual("EACH_QUORUM ConsistencyLevel is only supported for writes", err.Message);
}
}
/// <summary>
/// Successfully Fetch mapped records by passing in a Cql Object,
/// with consistency level set larger than available node count.
/// Assert expected failure message.
/// </summary>
[Test]
public void Fetch_WithConsistencyLevel_Invalids_NotEnoughReplicas()
{
Table<Author> table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
int totalInserts = 10;
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
List<Author> expectedAuthors = Author.GetRandomList(totalInserts);
foreach (Author expectedAuthor in expectedAuthors)
mapper.Insert(expectedAuthor);
Cql cql = new Cql("SELECT * from " + table.Name).WithOptions(c => c.SetConsistencyLevel(ConsistencyLevel.Two));
var err = Assert.Throws<UnavailableException>(() => mapper.Fetch<Author>(cql));
Assert.AreEqual("Not enough replicas available for query at consistency Two (2 required but only 1 alive)", err.Message);
cql = new Cql("SELECT * from " + table.Name).WithOptions(c => c.SetConsistencyLevel(ConsistencyLevel.Three));
err = Assert.Throws<UnavailableException>(() => mapper.Fetch<Author>(cql));
Assert.AreEqual("Not enough replicas available for query at consistency Three (3 required but only 1 alive)", err.Message);
}
/// <summary>
/// When a List of strings is uploaded as null, it is downloaded as an empty list
/// </summary>
[Test]
public void Fetch_ListUploadedAsNull()
{
Table<Author> table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
Author expectedAuthor = Author.GetRandom();
expectedAuthor.Followers = null;
mapper.Insert(expectedAuthor);
// Assert values from cql query
Author expectedAuthorFromQuery = new Author
{
Followers = new List<string>(), // not null
AuthorId = expectedAuthor.AuthorId,
};
Cql cql = new Cql("SELECT * from " + table.Name);
List<Author> actualAuthors = mapper.Fetch<Author>(cql).ToList();
Assert.AreEqual(1, actualAuthors.Count);
expectedAuthorFromQuery.AssertEquals(actualAuthors[0]);
}
/// <summary>
/// Page through results from a mapped Fetch request
///
/// @Jira CSHARP-215 https://datastax-oss.atlassian.net/browse/CSHARP-215
/// </summary>
[Test]
public void Fetch_Lazy()
{
Table<Author> table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
List<Author> expectedAuthors = Author.GetRandomList(100);
foreach (Author expectedAuthor in expectedAuthors)
mapper.Insert(expectedAuthor);
// wait until all records are available to be fetched:
List<Author> authors = mapper.Fetch<Author>("SELECT * from " + table.Name).ToList();
DateTime futureDateTime = DateTime.Now.AddSeconds(10);
while (authors.Count < expectedAuthors.Count && DateTime.Now < futureDateTime)
authors = mapper.Fetch<Author>("SELECT * from " + table.Name).ToList();
Assert.AreEqual(expectedAuthors.Count, authors.Count, "Setup FAIL: Less than expected number of authors uploaded");
Cql cql = new Cql("SELECT * from " + table.Name).WithOptions(o => o.SetConsistencyLevel(ConsistencyLevel.Quorum));
List<Author> authorsFetchedAndSaved = new List<Author>();
var authorsFetched = mapper.Fetch<Author>(cql).GetEnumerator();
while (authorsFetched.MoveNext())
authorsFetchedAndSaved.Add(authorsFetched.Current);
Assert.AreEqual(expectedAuthors.Count, authorsFetchedAndSaved.Count);
foreach (var authorFetched in authorsFetchedAndSaved)
{
Author.AssertListContains(expectedAuthors, authorFetched);
}
}
/// <summary>
/// Page through results from a mapped FetchPage request
///
/// @Jira CSHARP-262 https://datastax-oss.atlassian.net/browse/CSHARP-262
/// </summary>
[Test]
public void FetchPage_Manual_Explicit()
{
const int totalLength = 100;
Table<Author> table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
List<Author> expectedAuthors = Author.GetRandomList(totalLength);
foreach (Author expectedAuthor in expectedAuthors)
{
mapper.Insert(expectedAuthor);
}
var ids = new HashSet<string>();
byte[] pagingState = null;
var safeCounter = 0;
do
{
IPage<Author> authors = mapper.FetchPage<Author>(10, pagingState, "SELECT * from " + table.Name);
foreach (var a in authors)
{
ids.Add(a.AuthorId);
}
pagingState = authors.PagingState;
} while (pagingState != null && safeCounter++ < 100);
Assert.AreEqual(totalLength, ids.Count);
}
/// <summary>
/// Page through results from a mapped FetchPage with query options
///
/// @Jira CSHARP-262 https://datastax-oss.atlassian.net/browse/CSHARP-262
/// </summary>
[Test]
public void FetchPage_Manual_WithQueryOptions()
{
const int totalLength = 100;
const int pageSize = 10;
Table<Author> table = new Table<Author>(_session, new MappingConfiguration());
table.Create();
var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
List<Author> expectedAuthors = Author.GetRandomList(totalLength);
foreach (Author expectedAuthor in expectedAuthors)
{
mapper.Insert(expectedAuthor);
}
var ids = new HashSet<string>();
byte[] pagingState = null;
var safeCounter = 0;
do
{
var state = pagingState;
IPage<Author> authors = mapper.FetchPage<Author>(Cql.New("SELECT * from " + table.Name).WithOptions(opt => opt.SetPageSize(pageSize).SetPagingState(state)));
foreach (var a in authors)
{
ids.Add(a.AuthorId);
}
Assert.LessOrEqual(authors.Count, pageSize);
pagingState = authors.PagingState;
} while (pagingState != null && safeCounter++ < 100);
Assert.AreEqual(totalLength, ids.Count);
}
[Test, TestCassandraVersion(2, 1, 0)]
public void Fetch_With_Udt()
{
var mapper = new Mapper(_session, new MappingConfiguration());
_session.Execute("CREATE TYPE song (id uuid, title text, artist text)");
_session.Execute("CREATE TABLE albums (id uuid primary key, name text, songs list<frozen<song>>)");
_session.UserDefinedTypes.Define(UdtMap.For<Song2>("song"));
_session.Execute("INSERT INTO albums (id, name, songs) VALUES (uuid(), 'Legend', [{id: uuid(), title: 'Africa Unite', artist: 'Bob Marley'}])");
var result = mapper.Fetch<Album>("SELECT * from albums LIMIT 1").ToList();
Assert.AreEqual(1, result.Count);
var album = result[0];
Assert.AreEqual("Legend", album.Name);
Assert.AreEqual(1, album.Songs.Count);
var song = album.Songs[0];
Assert.AreEqual("Bob Marley", song.Artist);
Assert.AreEqual("Africa Unite", song.Title);
}
[Test]
public void Fetch_Struct_Value_Should_Default()
{
_session.Execute("CREATE TABLE tbl_with_structs (id uuid primary key, bool_sample boolean, timestamp_sample timestamp)");
var map = new Map<AllTypesEntity>()
.Column(p => p.BooleanValue, c => c.WithName("bool_sample"))
.Column(p => p.UuidValue, c => c.WithName("id"))
.PartitionKey(p => p.UuidValue)
.ExplicitColumns();
var id = Guid.NewGuid();
var query = string.Format("INSERT INTO tbl_with_structs (id, bool_sample, timestamp_sample) VALUES ({0}, null, null)", id);
_session.Execute(query);
var mapper = new Mapper(_session, new MappingConfiguration().Define(map));
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
var row = mapper.Fetch<AllTypesEntity>("SELECT * FROM tbl_with_structs WHERE id = ?", id).First();
Assert.NotNull(row);
Assert.AreEqual(row.UuidValue, id);
Assert.AreEqual(row.BooleanValue, default(bool));
Assert.AreEqual(row.DateTimeValue, default(DateTime));
}
[Test]
public void Fetch_TimeUuid_Set()
{
_session.Execute("CREATE TABLE tbl_timeuuid_sortedset (id int primary key, my_set set<timeuuid>)");
var map = new Map<PocoWithCollections<TimeUuid>>()
.Column(p => p.Id)
.Column(p => p.SortedSet, c => c.WithName("my_set"))
.PartitionKey(p => p.Id)
.TableName("tbl_timeuuid_sortedset")
.ExplicitColumns();
var mapper = new Mapper(_session, new MappingConfiguration().Define(map));
var inserted = new PocoWithCollections<TimeUuid>
{
Id = 1,
SortedSet = new SortedSet<TimeUuid> { TimeUuid.NewId(), TimeUuid.NewId() }
};
mapper.Insert(inserted);
var retrieved = mapper.Fetch<PocoWithCollections<TimeUuid>>("WHERE id = ?", inserted.Id).First();
CollectionAssert.AreEqual(inserted.SortedSet, retrieved.SortedSet);
}
[Test]
public void Fetch_TimeUuid_Map_Key_Sorted()
{
_session.Execute("CREATE TABLE tbl_timeuuid_map (id int primary key, my_map map<timeuuid, text>)");
var map = new Map<PocoWithCollections<TimeUuid>>()
.Column(p => p.Id)
.Column(p => p.SortedDictionaryTKeyString, c => c.WithName("my_map"))
.PartitionKey(p => p.Id)
.TableName("tbl_timeuuid_map")
.ExplicitColumns();
var mapper = new Mapper(_session, new MappingConfiguration().Define(map));
var inserted = new PocoWithCollections<TimeUuid>
{
Id = 1,
SortedDictionaryTKeyString = new SortedDictionary<TimeUuid, string> { { TimeUuid.NewId(), "one" } }
};
mapper.Insert(inserted);
var retrieved = mapper.Fetch<PocoWithCollections<TimeUuid>>("WHERE id = ?", inserted.Id).First();
CollectionAssert.AreEqual(inserted.SortedDictionaryTKeyString, retrieved.SortedDictionaryTKeyString);
}
[Test]
public void Fetch_TimeUuid_Map_Key()
{
_session.Execute("CREATE TABLE tbl_timeuuid_map2 (id int primary key, my_map map<timeuuid, text>)");
var map = new Map<PocoWithCollections<TimeUuid>>()
.Column(p => p.Id)
.Column(p => p.DictionaryTKeyString, c => c.WithName("my_map"))
.PartitionKey(p => p.Id)
.TableName("tbl_timeuuid_map2")
.ExplicitColumns();
var mapper = new Mapper(_session, new MappingConfiguration().Define(map));
var inserted = new PocoWithCollections<TimeUuid>
{
Id = 1,
DictionaryTKeyString = new Dictionary<TimeUuid, string> { { TimeUuid.NewId(), "one" } }
};
mapper.Insert(inserted);
var retrieved = mapper.Fetch<PocoWithCollections<TimeUuid>>("WHERE id = ?", inserted.Id).First();
CollectionAssert.AreEqual(inserted.DictionaryTKeyString, retrieved.DictionaryTKeyString);
}
[Test]
public void Fetch_TimeUuid_HashSet_Key()
{
_session.Execute("CREATE TABLE tbl_timeuuid_hashset (id int primary key, my_set set<timeuuid>)");
var map = new Map<PocoWithCollections<TimeUuid>>()
.Column(p => p.Id)
.Column(p => p.HashSet, c => c.WithName("my_set"))
.PartitionKey(p => p.Id)
.TableName("tbl_timeuuid_hashset")
.ExplicitColumns();
var mapper = new Mapper(_session, new MappingConfiguration().Define(map));
var inserted = new PocoWithCollections<TimeUuid>
{
Id = 1,
HashSet = new HashSet<TimeUuid> { TimeUuid.NewId(), TimeUuid.NewId() }
};
mapper.Insert(inserted);
var retrieved = mapper.Fetch<PocoWithCollections<TimeUuid>>("WHERE id = ?", inserted.Id).First();
CollectionAssert.AreEqual(inserted.HashSet, retrieved.HashSet);
}
[Test]
public void Fetch_TimeUuid_List()
{
_session.Execute("CREATE TABLE tbl_timeuuid_list (id int primary key, my_list list<timeuuid>)");
var map = new Map<PocoWithCollections<TimeUuid>>()
.Column(p => p.Id)
.Column(p => p.List, c => c.WithName("my_list"))
.PartitionKey(p => p.Id)
.TableName("tbl_timeuuid_list")
.ExplicitColumns();
var mapper = new Mapper(_session, new MappingConfiguration().Define(map));
var inserted = new PocoWithCollections<TimeUuid>
{
Id = 1,
List = new List<TimeUuid> { TimeUuid.NewId(), TimeUuid.NewId() }
};
mapper.Insert(inserted);
var retrieved = mapper.Fetch<PocoWithCollections<TimeUuid>>("WHERE id = ?", inserted.Id).First();
var listRetrieved = retrieved.List.OrderBy(x => x);
var listInserted = inserted.List.OrderBy(x => x);
CollectionAssert.AreEqual(listInserted, listRetrieved);
}
[Test]
public void Fetch_Guid_HashSet_Key()
{
_session.Execute("CREATE TABLE tbl_uuid_hashset (id int primary key, my_set set<uuid>)");
var map = new Map<PocoWithCollections<Guid>>()
.Column(p => p.Id)
.Column(p => p.HashSet, c => c.WithName("my_set"))
.PartitionKey(p => p.Id)
.TableName("tbl_uuid_hashset")
.ExplicitColumns();
var mapper = new Mapper(_session, new MappingConfiguration().Define(map));
var inserted = new PocoWithCollections<Guid>
{
Id = 1,
HashSet = new HashSet<Guid> { Guid.NewGuid(), Guid.NewGuid() }
};
mapper.Insert(inserted);
var retrieved = mapper.Fetch<PocoWithCollections<Guid>>("WHERE id = ?", inserted.Id).First();
var hashes = inserted.HashSet.OrderBy(x => x);
var hashesRetrieved = retrieved.HashSet.OrderBy(x => x);
CollectionAssert.AreEqual(hashes, hashesRetrieved);
}
[Test]
public void Fetch_Poco_With_Enum_Collections_Test()
{
_session.Execute(string.Format(PocoWithEnumCollections.DefaultCreateTableCql, "tbl_with_enum_collections"));
var expectedCollection = new[]{ HairColor.Blonde, HairColor.Gray };
var expectedMap = new SortedDictionary<HairColor, TimeUuid>
{
{ HairColor.Brown, TimeUuid.NewId() },
{ HairColor.Red, TimeUuid.NewId() }
};
var collectionValues = expectedCollection.Select(x => (int)x).ToArray();
var mapValues =
new SortedDictionary<int, Guid>(expectedMap.ToDictionary(kv => (int) kv.Key, kv => (Guid) kv.Value));
const string insertQuery =
"INSERT INTO tbl_with_enum_collections (id, list1, list2, array1, set1, set2, set3, map1, map2, map3)" +
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
_session.Execute(new SimpleStatement(insertQuery, 2000L, collectionValues, collectionValues,
collectionValues, collectionValues, collectionValues, collectionValues, mapValues, mapValues,
mapValues));
_session.Execute(new SimpleStatement(insertQuery, 2001L, null, null, null, null, null, null, null, null,
null));
var config =
new MappingConfiguration().Define(
PocoWithEnumCollections.DefaultMapping.TableName("tbl_with_enum_collections"));
var mapper = new Mapper(_session, config);
var result = mapper.Fetch<PocoWithEnumCollections>("WHERE id = ?", 2000L);
Assert.NotNull(result);
var rows = result.ToArray();
Assert.AreEqual(1, rows.Length);
var poco = rows[0];
Assert.AreEqual(2000L, poco.Id);
Assert.AreEqual(expectedCollection, poco.List1);
Assert.AreEqual(expectedCollection, poco.List2);
Assert.AreEqual(expectedCollection, poco.Array1);
Assert.AreEqual(expectedCollection, poco.Set1);
Assert.AreEqual(expectedCollection, poco.Set2);
Assert.AreEqual(expectedCollection, poco.Set3);
Assert.AreEqual(expectedMap, poco.Dictionary1);
Assert.AreEqual(expectedMap, poco.Dictionary2);
Assert.AreEqual(expectedMap, poco.Dictionary3);
}
}
}