-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
CoreTests.cs
7126 lines (5643 loc) · 253 KB
/
CoreTests.cs
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Dapper;
using Microsoft.AspNetCore.Hosting.StaticWebAssets;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using YesSql.Commands;
using YesSql.Filters.Query;
using YesSql.Indexes;
using YesSql.Services;
using YesSql.Sql;
using YesSql.Tests.Commands;
using YesSql.Tests.CompiledQueries;
using YesSql.Tests.Indexes;
using YesSql.Tests.Models;
namespace YesSql.Tests
{
public abstract class CoreTests : IAsyncLifetime
{
protected virtual string TablePrefix => "tp";
protected virtual string DecimalColumnDefinitionFormatString => "DECIMAL({0},{1})";
protected IStore _store;
protected static IConfiguration _configuration;
protected ITestOutputHelper _output;
protected abstract IConfiguration CreateConfiguration();
public CoreTests(ITestOutputHelper output)
{
_output = output;
}
public async Task InitializeAsync()
{
// Create the tables only once
if (_configuration == null)
{
_configuration = CreateConfiguration();
await CleanDatabaseAsync(_configuration, false);
_store = await StoreFactory.CreateAndInitializeAsync(_configuration);
await _store.InitializeCollectionAsync("Col1");
_store.TypeNames[typeof(Person)] = "People";
await CoreTests.CreateTablesAsync(_configuration);
}
else
{
_store = await StoreFactory.CreateAndInitializeAsync(_configuration);
await _store.InitializeCollectionAsync("Col1");
_store.TypeNames[typeof(Person)] = "People";
}
// Clear the tables for each new test
await ClearTablesAsync(_configuration);
}
public virtual Task DisposeAsync()
=> Task.CompletedTask;
protected void EnableLogging()
{
var logger = new ConsoleLogger(_output);
_store.Configuration.Logger = logger;
}
//[DebuggerNonUserCode]
protected virtual async Task CleanDatabaseAsync(IConfiguration configuration, bool throwOnError)
{
// Remove existing tables
await using var connection = configuration.ConnectionFactory.CreateConnection();
await connection.OpenAsync();
await using var transaction = await connection.BeginTransactionAsync(configuration.IsolationLevel);
var builder = new SchemaBuilder(configuration, transaction, throwOnError);
await builder.DropReduceIndexTableAsync<ArticlesByDay>();
await builder.DropReduceIndexTableAsync<AttachmentByDay>();
await builder.DropMapIndexTableAsync<ArticleByPublishedDate>();
await builder.DropMapIndexTableAsync<PersonByName>();
await builder.DropMapIndexTableAsync<CarIndex>();
await builder.DropMapIndexTableAsync<PersonByNameCol>();
await builder.DropMapIndexTableAsync<PersonIdentity>();
await builder.DropMapIndexTableAsync<EmailByAttachment>();
await builder.DropMapIndexTableAsync<TypesIndex>();
await builder.DropMapIndexTableAsync<ShapeIndex>();
await builder.DropMapIndexTableAsync<PersonByAge>();
await builder.DropMapIndexTableAsync<PersonByNullableAge>();
await builder.DropMapIndexTableAsync<Binary>();
await builder.DropMapIndexTableAsync<PublishedArticle>();
await builder.DropMapIndexTableAsync<PropertyIndex>();
await builder.DropReduceIndexTableAsync<UserByRoleNameIndex>();
await builder.DropMapIndexTableAsync<PersonByName>("Col1");
await builder.DropMapIndexTableAsync<PersonByNameCol>("Col1");
await builder.DropMapIndexTableAsync<PersonByBothNamesCol>("Col1");
await builder.DropReduceIndexTableAsync<PersonsByNameCol>("Col1");
await builder.DropReduceIndexTableAsync<PersonsByNameCol>("LongCollection");
await builder.DropTableAsync(configuration.TableNameConvention.GetDocumentTable("Col1"));
await builder.DropTableAsync(configuration.TableNameConvention.GetDocumentTable(""));
await builder.DropTableAsync(DbBlockIdGenerator.TableName);
await OnCleanDatabaseAsync(builder, transaction);
await transaction.CommitAsync();
}
protected virtual async Task ClearTablesAsync(IConfiguration configuration)
{
async Task DeleteReduceIndexTableAsync<IndexType>(DbConnection connection, string collection = "")
{
var indexTable = configuration.TableNameConvention.GetIndexTable(typeof(IndexType), collection);
var documentTable = configuration.TableNameConvention.GetDocumentTable(collection);
var bridgeTableName = indexTable + "_" + documentTable;
try
{
await connection.ExecuteAsync($"DELETE FROM {configuration.SqlDialect.QuoteForTableName(TablePrefix + bridgeTableName, configuration.Schema)}");
await connection.ExecuteAsync($"DELETE FROM {configuration.SqlDialect.QuoteForTableName(TablePrefix + indexTable, configuration.Schema)}");
}
catch
{
}
}
async Task DeleteMapIndexTableAsync<IndexType>(DbConnection connection, string collection = "")
{
var indexName = typeof(IndexType).Name;
var indexTable = configuration.TableNameConvention.GetIndexTable(typeof(IndexType), collection);
try
{
await connection.ExecuteAsync($"DELETE FROM {configuration.SqlDialect.QuoteForTableName(TablePrefix + indexTable, configuration.Schema)}");
}
catch { }
}
async Task DeleteDocumentTableAsync(DbConnection connection, string collection = "")
{
var tableName = configuration.TableNameConvention.GetDocumentTable(collection);
try
{
await connection.ExecuteAsync($"DELETE FROM {configuration.SqlDialect.QuoteForTableName(TablePrefix + tableName, configuration.Schema)}");
}
catch { }
}
// Remove existing tables
await using (var connection = configuration.ConnectionFactory.CreateConnection())
{
await connection.OpenAsync();
await DeleteReduceIndexTableAsync<ArticlesByDay>(connection);
await DeleteReduceIndexTableAsync<AttachmentByDay>(connection);
await DeleteMapIndexTableAsync<ArticleByPublishedDate>(connection);
await DeleteMapIndexTableAsync<PersonByName>(connection);
await DeleteMapIndexTableAsync<CarIndex>(connection);
await DeleteMapIndexTableAsync<PersonByNameCol>(connection);
await DeleteMapIndexTableAsync<PersonIdentity>(connection);
await DeleteMapIndexTableAsync<EmailByAttachment>(connection);
await DeleteMapIndexTableAsync<TypesIndex>(connection);
await DeleteMapIndexTableAsync<ShapeIndex>(connection);
await DeleteMapIndexTableAsync<PersonByAge>(connection);
await DeleteMapIndexTableAsync<PersonByNullableAge>(connection);
await DeleteMapIndexTableAsync<Binary>(connection);
await DeleteMapIndexTableAsync<PublishedArticle>(connection);
await DeleteMapIndexTableAsync<PropertyIndex>(connection);
await DeleteReduceIndexTableAsync<UserByRoleNameIndex>(connection);
await DeleteMapIndexTableAsync<PersonByName>(connection, "Col1");
await DeleteMapIndexTableAsync<PersonByNameCol>(connection, "Col1");
await DeleteMapIndexTableAsync<PersonByBothNamesCol>(connection, "Col1");
await DeleteReduceIndexTableAsync<PersonsByNameCol>(connection, "Col1");
await DeleteDocumentTableAsync(connection, "Col1");
await DeleteDocumentTableAsync(connection, "");
//connection.Execute($"DELETE FROM {TablePrefix}{DbBlockIdGenerator.TableName}");
await OnClearTablesAsync(connection);
}
}
protected virtual Task OnCleanDatabaseAsync(SchemaBuilder builder, DbTransaction transaction)
=> Task.CompletedTask;
protected virtual Task OnClearTablesAsync(DbConnection connection)
=> Task.CompletedTask;
public static async Task CreateTablesAsync(IConfiguration configuration)
{
await using var connection = configuration.ConnectionFactory.CreateConnection();
await connection.OpenAsync();
await using var transaction = await connection.BeginTransactionAsync(configuration.IsolationLevel);
var builder = new SchemaBuilder(configuration, transaction);
await builder.CreateReduceIndexTableAsync<ArticlesByDay>(column => column
.Column<int>(nameof(ArticlesByDay.Count))
.Column<int>(nameof(ArticlesByDay.DayOfYear))
);
await builder.CreateReduceIndexTableAsync<AttachmentByDay>(column => column
.Column<int>(nameof(AttachmentByDay.Count))
.Column<int>(nameof(AttachmentByDay.Date))
);
await builder.CreateReduceIndexTableAsync<UserByRoleNameIndex>(column => column
.Column<int>(nameof(UserByRoleNameIndex.Count))
.Column<string>(nameof(UserByRoleNameIndex.RoleName))
);
await builder.CreateMapIndexTableAsync<ArticleByPublishedDate>(column => column
.Column<DateTime>(nameof(ArticleByPublishedDate.PublishedDateTime))
.Column<string>(nameof(ArticleByPublishedDate.Title))
);
await builder.CreateMapIndexTableAsync<PersonByName>(column => column
.Column<string>(nameof(PersonByName.SomeName))
);
await builder.CreateMapIndexTableAsync<CarIndex>(column => column
.Column<string>(nameof(CarIndex.Name))
.Column<Categories>(nameof(CarIndex.Category))
);
await builder.CreateMapIndexTableAsync<PersonByNameCol>(column => column
.Column<string>(nameof(PersonByNameCol.Name))
);
await builder.CreateMapIndexTableAsync<PersonIdentity>(column => column
.Column<string>(nameof(PersonIdentity.Identity))
);
await builder.CreateMapIndexTableAsync<PersonByAge>(column => column
.Column<int>(nameof(PersonByAge.Age))
.Column<bool>(nameof(PersonByAge.Adult))
.Column<string>(nameof(PersonByAge.Name))
);
await builder.CreateMapIndexTableAsync<ShapeIndex>(column => column
.Column<string>(nameof(ShapeIndex.Name))
);
await builder.CreateMapIndexTableAsync<PersonByNullableAge>(column => column
.Column<int?>(nameof(PersonByAge.Age), c => c.Nullable())
);
await builder.CreateMapIndexTableAsync<PublishedArticle>(column => { });
await builder.CreateMapIndexTableAsync<EmailByAttachment>(column => column
.Column<DateTime>(nameof(EmailByAttachment.Date))
.Column<string>(nameof(EmailByAttachment.AttachmentName))
);
await builder.CreateMapIndexTableAsync<PropertyIndex>(column => column
.Column<string>(nameof(PropertyIndex.Name), col => col.WithLength(767))
.Column<bool>(nameof(PropertyIndex.ForRent))
.Column<bool>(nameof(PropertyIndex.IsOccupied))
.Column<string>(nameof(PropertyIndex.Location), col => col.WithLength(1000))
);
await builder.CreateMapIndexTableAsync<Binary>(column => column
.Column<byte[]>(nameof(Binary.Content1), c => c.WithLength(255))
.Column<byte[]>(nameof(Binary.Content2), c => c.WithLength(8000))
.Column<byte[]>(nameof(Binary.Content3), c => c.WithLength(65535))
.Column<byte[]>(nameof(Binary.Content4), c => c.WithLength(1))
);
await builder.CreateMapIndexTableAsync<TypesIndex>(column => column
.Column<bool>(nameof(TypesIndex.ValueBool))
//.Column<char>(nameof(TypesIndex.ValueChar))
.Column<DateTime>(nameof(TypesIndex.ValueDateTime))
.Column<DateTimeOffset>(nameof(TypesIndex.ValueDateTimeOffset))
.Column<decimal>(nameof(TypesIndex.ValueDecimal))
.Column<double>(nameof(TypesIndex.ValueDouble))
.Column<float>(nameof(TypesIndex.ValueFloat))
.Column<Guid>(nameof(TypesIndex.ValueGuid))
.Column<int>(nameof(TypesIndex.ValueInt))
.Column<long>(nameof(TypesIndex.ValueLong))
//.Column<sbyte>(nameof(TypesIndex.ValueSByte))
.Column<short>(nameof(TypesIndex.ValueShort))
.Column<TimeSpan>(nameof(TypesIndex.ValueTimeSpan))
//.Column<uint>(nameof(TypesIndex.ValueUInt))
//.Column<ulong>(nameof(TypesIndex.ValueULong))
//.Column<ushort>(nameof(TypesIndex.ValueUShort))
.Column<bool?>(nameof(TypesIndex.NullableBool), c => c.Nullable())
//.Column<char?>(nameof(TypesIndex.NullableChar), c => c.Nullable())
.Column<DateTime?>(nameof(TypesIndex.NullableDateTime), c => c.Nullable())
.Column<DateTimeOffset?>(nameof(TypesIndex.NullableDateTimeOffset), c => c.Nullable())
.Column<decimal?>(nameof(TypesIndex.NullableDecimal), c => c.Nullable())
.Column<double?>(nameof(TypesIndex.NullableDouble), c => c.Nullable())
.Column<float?>(nameof(TypesIndex.NullableFloat), c => c.Nullable())
.Column<Guid?>(nameof(TypesIndex.NullableGuid), c => c.Nullable())
.Column<int?>(nameof(TypesIndex.NullableInt), c => c.Nullable())
.Column<long?>(nameof(TypesIndex.NullableLong), c => c.Nullable())
//.Column<sbyte?>(nameof(TypesIndex.NullableSByte), c => c.Nullable())
.Column<short?>(nameof(TypesIndex.NullableShort), c => c.Nullable())
.Column<TimeSpan?>(nameof(TypesIndex.NullableTimeSpan), c => c.Nullable())
//.Column<uint?>(nameof(TypesIndex.NullableUInt), c => c.Nullable())
//.Column<ulong?>(nameof(TypesIndex.NullableULong), c => c.Nullable())
//.Column<ushort?>(nameof(TypesIndex.NullableUShort), c => c.Nullable())
);
await builder.CreateMapIndexTableAsync<PersonByName>(column => column
.Column<string>(nameof(PersonByName.SomeName)),
"Col1"
);
await builder.CreateMapIndexTableAsync<PersonByNameCol>(column => column
.Column<string>(nameof(PersonByNameCol.Name)),
"Col1"
);
await builder.CreateMapIndexTableAsync<PersonByBothNamesCol>(column => column
.Column<string>(nameof(PersonByBothNamesCol.Firstname))
.Column<string>(nameof(PersonByBothNamesCol.Lastname)),
"Col1"
);
await builder.CreateReduceIndexTableAsync<PersonsByNameCol>(column => column
.Column<string>(nameof(PersonsByNameCol.Name))
.Column<int>(nameof(PersonsByNameCol.Count)),
"Col1"
);
await transaction.CommitAsync();
}
[Fact]
public async Task ShouldCreateDatabase()
{
await using var session = _store.CreateSession();
var doc = new Product();
await session.SaveAsync(doc);
}
[Fact]
public async Task ShouldSaveCustomObject()
{
await using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
await session.SaveAsync(bill);
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
Assert.Equal(1, await session.Query<Person>().CountAsync());
}
}
[Fact]
public async Task NotCallingCommitShouldCancelTransaction()
{
await using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
await session.SaveAsync(bill);
}
await using (var session = _store.CreateSession())
{
Assert.Equal(0, await session.Query<Person>().CountAsync());
}
}
[Fact]
public async Task ShouldCancelTransactionAfterFlush()
{
await using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
await session.SaveAsync(bill);
await session.FlushAsync();
var steve = new Person
{
Firstname = "Steve",
Lastname = "Balmer"
};
await session.SaveAsync(steve);
}
await using (var session = _store.CreateSession())
{
var total = await session.Query<Person>().CountAsync();
Assert.Equal(0, total);
}
}
[Fact]
public async Task ShouldSaveSeveralObjects()
{
await using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
var steve = new Person
{
Firstname = "Steve",
Lastname = "Balmer"
};
await session.SaveAsync(bill);
await session.SaveAsync(steve);
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
Assert.Equal(2, await session.Query<Person>().CountAsync());
}
}
[Fact]
public async Task ShouldSaveAnonymousObject()
{
await using var session = _store.CreateSession();
var bill = new
{
Firstname = "Bill",
Lastname = "Gates"
};
var steve = new
{
Firstname = "Steve",
Lastname = "Balmer"
};
await session.SaveAsync(bill);
await session.SaveAsync(steve);
await session.SaveChangesAsync();
}
[Fact]
public async Task ShouldLoadAnonymousDocument()
{
await using (var session = _store.CreateSession())
{
var bill = new
{
Firstname = "Bill",
Lastname = "Gates",
Address = new
{
Street = "1 Microsoft Way",
City = "Redmond"
}
};
await session.SaveAsync(bill);
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
dynamic person = await session.Query().Any().FirstOrDefaultAsync();
Assert.NotNull(person);
Assert.Equal("Bill", (string)person.Firstname);
Assert.Equal("Gates", (string)person.Lastname);
Assert.NotNull(person.Address);
Assert.Equal("1 Microsoft Way", (string)person.Address.Street);
Assert.Equal("Redmond", (string)person.Address.City);
}
}
[Fact]
public async Task ShouldQueryNonExistentResult()
{
await using var session = _store.CreateSession();
var person = await session.Query<Person>().FirstOrDefaultAsync();
Assert.Null(person);
}
[Fact]
public async Task ShouldUpdateNewDocument()
{
await using (var session = _store.CreateSession())
{
var bill = new Person { Firstname = "Bill" };
await session.SaveAsync(bill);
bill.Lastname = "Gates";
await session.SaveAsync(bill);
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
Assert.Equal(1, await session.Query<Person>().CountAsync());
var person = await session.Query<Person>().FirstOrDefaultAsync();
Assert.Equal("Bill", person.Firstname);
Assert.Equal("Gates", person.Lastname);
}
}
[Fact]
public async Task ShouldQueryIndexWithParameter()
{
_store.RegisterIndexes<PersonIndexProvider>();
await using (var session = _store.CreateSession())
{
var bill = new Person { Firstname = "Bill" };
await session.SaveAsync(bill);
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
var person = await session.QueryIndex<PersonByName>().Where(d => d.QuoteForColumnName(nameof(PersonByName.SomeName)) + " = @Name").WithParameter("Name", "Bill").FirstOrDefaultAsync();
Assert.NotNull(person);
Assert.Equal("Bill", (string)person.SomeName);
}
}
[Fact]
public async Task ShouldMapEnums()
{
_store.RegisterIndexes<CarIndexProvider>();
await using (var session = _store.CreateSession())
{
await session.SaveAsync(new Car { Name = "Truck", Category = Categories.Truck });
await session.SaveAsync(new Car { Name = "Van", Category = Categories.Van });
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
Assert.Equal("Van", (await session.QueryIndex<CarIndex>(x => x.Category == Categories.Van).FirstOrDefaultAsync()).Name);
Assert.Equal("Truck", (await session.QueryIndex<CarIndex>(x => x.Category == Categories.Truck).FirstOrDefaultAsync()).Name);
Assert.Equal("Van", (await session.Query<Car, CarIndex>(x => x.Category == Categories.Van).FirstOrDefaultAsync()).Name);
Assert.Equal("Truck", (await session.Query<Car, CarIndex>(x => x.Category == Categories.Truck).FirstOrDefaultAsync()).Name);
}
}
[Fact]
public async Task ShouldApplyIndexFilter()
{
_store.RegisterIndexes<PersonWithAIndexProvider>();
await using (var session = _store.CreateSession())
{
await session.SaveAsync(new Person { Firstname = "Alex" });
await session.SaveAsync(new Person { Firstname = "Bill" });
await session.SaveAsync(new Person { Firstname = "assan" });
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
Assert.Equal(2, await session.QueryIndex<PersonByName>().CountAsync());
}
}
[Fact]
public async Task ShouldMapAsyncIndex()
{
_store.RegisterIndexes<PersonAsyncIndexProvider>();
await using (var session = _store.CreateSession())
{
var bill = new Person { Firstname = "Bill" };
await session.SaveAsync(bill);
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
var person = await session.QueryIndex<PersonByName>().FirstOrDefaultAsync();
Assert.NotNull(person);
Assert.Equal("Bill", person.SomeName);
}
}
[Fact]
public async Task ShouldResolveScopedIndexProviders()
{
await using (var session = _store.CreateSession())
{
session.RegisterIndexes(new ScopedPersonAsyncIndexProvider(1));
await session.SaveAsync(new Person { Firstname = "Bill" });
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
session.RegisterIndexes(new ScopedPersonAsyncIndexProvider(2));
await session.SaveAsync(new Person { Firstname = "Bill" });
await session.SaveAsync(new Person { Firstname = "Steve" });
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
var count = await session.QueryIndex<PersonByName>().CountAsync();
var bill1 = await session.QueryIndex<PersonByName>(x => x.SomeName == "Bill1").FirstOrDefaultAsync();
var bill2 = await session.QueryIndex<PersonByName>(x => x.SomeName == "Bill2").FirstOrDefaultAsync();
var steve2 = await session.QueryIndex<PersonByName>(x => x.SomeName == "Steve2").FirstOrDefaultAsync();
Assert.Equal(3, count);
Assert.NotNull(bill1);
Assert.NotNull(bill2);
Assert.NotNull(steve2);
}
}
[Fact]
public async Task ShouldQueryNullValues()
{
_store.RegisterIndexes<PersonIndexProvider>();
await using (var session = _store.CreateSession())
{
await session.SaveAsync(new Person { Firstname = null });
await session.SaveAsync(new Person { Firstname = "a" });
await session.SaveAsync(new Person { Firstname = "b" });
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
Assert.Equal(1, await session.QueryIndex<PersonByName>(x => x.SomeName == null).CountAsync());
Assert.Equal(2, await session.QueryIndex<PersonByName>(x => x.SomeName != null).CountAsync());
Assert.Equal(1, await session.QueryIndex<PersonByName>(x => null == x.SomeName).CountAsync());
Assert.Equal(2, await session.QueryIndex<PersonByName>(x => null != x.SomeName).CountAsync());
Assert.Equal(3, await session.QueryIndex<PersonByName>(x => null == null).CountAsync());
Assert.Equal(0, await session.QueryIndex<PersonByName>(x => null != null).CountAsync());
}
}
[Fact]
public async Task ShouldQueryNullVariables()
{
_store.RegisterIndexes<PersonIndexProvider>();
await using (var session = _store.CreateSession())
{
await session.SaveAsync(new Person { Firstname = null });
await session.SaveAsync(new Person { Firstname = "a" });
await session.SaveAsync(new Person { Firstname = "b" });
await session.SaveChangesAsync();
}
string nullVariable = null;
await using (var session = _store.CreateSession())
{
Assert.Equal(1, await session.QueryIndex<PersonByName>(x => x.SomeName == nullVariable).CountAsync());
Assert.Equal(2, await session.QueryIndex<PersonByName>(x => x.SomeName != nullVariable).CountAsync());
Assert.Equal(1, await session.QueryIndex<PersonByName>(x => nullVariable == x.SomeName).CountAsync());
Assert.Equal(2, await session.QueryIndex<PersonByName>(x => nullVariable != x.SomeName).CountAsync());
}
}
[Fact]
public async Task ShouldCompareWithConstants()
{
_store.RegisterIndexes<ArticleBydPublishedDateProvider>();
await using (var session = _store.CreateSession())
{
await session.SaveAsync(new Article { Title = TestConstants.Strings.SomeString, PublishedUtc = new DateTime(2011, 11, 1) });
await session.SaveAsync(new Article { Title = TestConstants.Strings.SomeOtherString, PublishedUtc = new DateTime(2011, 11, 1) });
await session.SaveAsync(new Article { Title = TestConstants.Strings.SomeString, PublishedUtc = new DateTime(2011, 11, 2) });
await session.SaveAsync(new Article { Title = TestConstants.Strings.SomeOtherString, PublishedUtc = new DateTime(2011, 11, 2) });
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
Assert.Equal(2, await session.Query<Article, ArticleByPublishedDate>(x => x.Title == TestConstants.Strings.SomeString).CountAsync());
Assert.Equal(2, await session.Query<Article, ArticleByPublishedDate>(x => x.Title != TestConstants.Strings.SomeString).CountAsync());
Assert.Equal(4, await session.Query<Article, ArticleByPublishedDate>(x => x.PublishedDateTime < DateTime.UtcNow).CountAsync());
Assert.Equal(2, await session.Query<Article, ArticleByPublishedDate>(x => x.Title != TestConstants.Strings.SomeString && x.PublishedDateTime < DateTime.UtcNow).CountAsync());
}
}
[Fact]
public async Task ShouldQueryDocumentWithParameter()
{
_store.RegisterIndexes<PersonIndexProvider>();
await using (var session = _store.CreateSession())
{
var bill = new Person { Firstname = "Bill" };
await session.SaveAsync(bill);
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
var person = await session.Query<Person, PersonByName>().Where(d => d.QuoteForColumnName(nameof(PersonByName.SomeName)) + " = @Name").WithParameter("Name", "Bill").FirstOrDefaultAsync();
Assert.NotNull(person);
Assert.Equal("Bill", person.Firstname);
}
}
[Fact]
public async Task ShouldQuoteString()
{
_store.RegisterIndexes<PersonIndexProvider>();
await using (var session = _store.CreateSession())
{
var bill = new Person { Firstname = "Bill" };
var steve = new Person { Firstname = "Steve" };
await session.SaveAsync(bill);
await session.SaveAsync(steve);
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
var connection = await session.CreateConnectionAsync();
var dialect = _store.Configuration.SqlDialect;
var sql = dialect.QuoteForColumnName(nameof(PersonByName.SomeName)) + " = " + dialect.GetSqlValue("Bill");
var person = await session.Query<Person, PersonByName>().Where(sql).FirstOrDefaultAsync();
Assert.NotNull(person);
Assert.Equal("Bill", person.Firstname);
}
}
[Fact]
public async Task ShouldSerializeComplexObject()
{
long productId;
await using (var session = _store.CreateSession())
{
var product = new Product
{
Cost = 3.99m,
Name = "Milk",
};
await session.SaveAsync(product);
await session.FlushAsync();
productId = product.Id;
await session.SaveAsync(new Order
{
Customer = "customers/microsoft",
OrderLines =
{
new OrderLine
{
ProductId = product.Id,
Quantity = 3
},
}
});
await session.SaveChangesAsync();
}
await using (var session = _store.CreateSession())
{
var prod = await session.GetAsync<Product>(productId);
Assert.NotNull(prod);
Assert.Equal("Milk", prod.Name);
}
}
[Fact]
public async Task ShouldAssignIdWhenSaved()
{
await using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
Assert.Equal(0, bill.Id);
await session.SaveAsync(bill);
Assert.NotEqual(0, bill.Id);
await session.SaveChangesAsync();
}
}
[Fact]
public async Task ShouldAutoFlushOnGet()
{
await using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
await session.SaveAsync(bill);
var newBill = await session.GetAsync<Person>(bill.Id);
Assert.Same(newBill, bill);
await session.SaveChangesAsync();
}
}
[Fact]
public async Task ShouldKeepTrackedOnAutoFlush()
{
await using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
await session.SaveAsync(bill);
var newBill = await session.GetAsync<Person>(bill.Id);
Assert.Same(newBill, bill);
await session.SaveChangesAsync();
}
}
[Fact]
public async Task NoSavingChangesShouldRollbackAutoFlush()
{
await using (var session = _store.CreateSession())
{
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
await session.SaveAsync(bill);
var newBill = await session.GetAsync<Person>(bill.Id);
Assert.Same(newBill, bill);
}
await using (var session = _store.CreateSession())
{
Assert.Equal(0, await session.Query<Person>().CountAsync());
}
}
[Fact]
public async Task IdentityMapShouldBeClearedAfterCommit()
{
await using var session = _store.CreateSession();
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
await session.SaveAsync(bill);
var newBill = await session.GetAsync<Person>(bill.Id);
Assert.Equal(bill, newBill);
await session.SaveChangesAsync();
newBill = await session.GetAsync<Person>(bill.Id);
Assert.NotEqual(bill, newBill);
}
[Fact]
public async Task ShouldNotKeepIdentityMapOnCommitAsync()
{
await using var session = _store.CreateSession(false);
var bill = new Person
{
Firstname = "Bill",
Lastname = "Gates"
};
await session.SaveAsync(bill);
await session.SaveChangesAsync();
var newBill = await session.GetAsync<Person>(bill.Id);
Assert.NotEqual(bill, newBill);
}
[Fact]
public async Task DetachAllRemoveAllEntitiesFromIdentityMap()
{
await using var session = _store.CreateSession();
var p1 = new Person();
var p2 = new Person();
var p3 = new Person();
await session.SaveAsync(p1);
await session.SaveAsync(p2);
await session.SaveAsync(p3);
await session.SaveChangesAsync();
// SaveChangesAsync should clear the identity mao
var p1a = await session.GetAsync<Person>(p1.Id);
var p2a = await session.GetAsync<Person>(p2.Id);
var p3a = await session.GetAsync<Person>(p3.Id);
Assert.NotEqual(p1, p1a);
Assert.NotEqual(p2, p2a);
Assert.NotEqual(p3, p3a);
// The identity should be valid as we do only reads
var p1b = await session.GetAsync<Person>(p1.Id);
var p2b = await session.GetAsync<Person>(p2.Id);
var p3b = await session.GetAsync<Person>(p3.Id);
Assert.Equal(p1a, p1b);
Assert.Equal(p2a, p2b);
Assert.Equal(p3a, p3b);
// Detach should clear specific items
session.Detach(p1b);
var p1c = await session.GetAsync<Person>(p1.Id);
var p2c = await session.GetAsync<Person>(p2.Id);
var p3c = await session.GetAsync<Person>(p3.Id);
Assert.NotEqual(p1a, p1c);