-
-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathDocumentMappingTests.cs
More file actions
848 lines (693 loc) · 25.1 KB
/
Copy pathDocumentMappingTests.cs
File metadata and controls
848 lines (693 loc) · 25.1 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
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
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using JasperFx.Core.Reflection;
using JasperFx.Events;
using JasperFx.Events.Projections;
using Marten;
using Marten.Events.Projections;
using Marten.Exceptions;
using Marten.Linq.Members;
using Marten.Linq.Parsing;
using Marten.Schema;
using Marten.Schema.Identity;
using Marten.Schema.Identity.Sequences;
using Marten.Storage;
using Marten.Storage.Metadata;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using NpgsqlTypes;
using Shouldly;
using Weasel.Core;
using Weasel.Postgresql.Tables;
using Xunit;
using Marten.Newtonsoft;
namespace DocumentDbTests.Configuration;
public class DocumentMappingTests
{
[Fact]
public void can_replace_hilo_def_settings()
{
var mapping = DocumentMapping.For<LongId>();
var newDef = new HiloSettings { MaxLo = 33 };
mapping.HiloSettings = newDef;
var sequence = mapping.IdStrategy.ShouldBeOfType<HiloIdGeneration>();
sequence.MaxLo.ShouldBe(newDef.MaxLo);
}
[Fact]
public void disable_partitioning_is_false_by_default()
{
var mapping = DocumentMapping.For<LongId>();
mapping.DisablePartitioningIfAny = true;
}
[Fact]
public void default_PrimaryKeyTenancyOrdering_mode()
{
var mapping = DocumentMapping.For<Target>();
mapping.PrimaryKeyTenancyOrdering.ShouldBe(PrimaryKeyTenancyOrdering.TenantId_Then_Id);
}
[Fact]
public void use_revision_from_stream_is_false_by_default()
{
var mapping = DocumentMapping.For<LongId>();
mapping.UseVersionFromMatchingStream.ShouldBeFalse();
}
[Fact]
public void concrete_type_with_subclasses_is_hierarchy()
{
var mapping = DocumentMapping.For<User>();
mapping.SubClasses.Add(typeof(SuperUser));
mapping.IsHierarchy().ShouldBeTrue();
}
[Fact]
public void default_alias_for_a_type_that_is_not_nested()
{
var mapping = DocumentMapping.For<User>();
mapping.Alias.ShouldBe("user");
}
[Fact]
public void default_search_mode_is_jsonb_to_record()
{
var mapping = DocumentMapping.For<User>();
mapping.PropertySearching.ShouldBe(PropertySearching.JSON_Locator_Only);
}
[Fact]
public void default_table_name()
{
var mapping = DocumentMapping.For<User>();
mapping.TableName.Name.ShouldBe("mt_doc_user");
}
[Fact]
public void default_table_name_2()
{
var mapping = DocumentMapping.For<User>();
mapping.TableName.QualifiedName.ShouldBe("public.mt_doc_user");
}
[Fact]
public void default_table_name_on_other_schema()
{
var mapping = DocumentMapping.For<User>("other");
mapping.TableName.QualifiedName.ShouldBe("other.mt_doc_user");
}
[Fact]
public void default_table_name_on_overriden_schema()
{
var mapping = DocumentMapping.For<User>("other");
mapping.DatabaseSchemaName = "overriden";
mapping.TableName.QualifiedName.ShouldBe("overriden.mt_doc_user");
}
[Fact]
public void default_table_name_with_different_schema()
{
var mapping = DocumentMapping.For<User>("other");
mapping.TableName.QualifiedName.ShouldBe("other.mt_doc_user");
}
[Fact]
public void default_table_name_with_schema()
{
var mapping = DocumentMapping.For<User>();
mapping.TableName.QualifiedName.ShouldBe("public.mt_doc_user");
}
[Theory]
[InlineData(EnumStorage.AsInteger)]
[InlineData(EnumStorage.AsString)]
public void enum_storage_should_be_taken_from_store_options(EnumStorage enumStorage)
{
var storeOptions = new StoreOptions();
storeOptions.UseNewtonsoftForSerialization(enumStorage);
var mapping = new DocumentMapping<User>(storeOptions);
mapping.EnumStorage.ShouldBe(enumStorage);
}
[Fact]
public void doc_type_with_use_optimistic_concurrency_attribute()
{
DocumentMapping.For<VersionedDoc>()
.UseOptimisticConcurrency.ShouldBeTrue();
}
[Fact]
public void duplicate_a_field()
{
var mapping = DocumentMapping.For<User>();
mapping.DuplicateField(nameof(User.FirstName));
mapping.QueryMembers.MemberFor(nameof(User.FirstName)).ShouldBeOfType<DuplicatedField>();
// other fields are still the same
mapping.QueryMembers.MemberFor(nameof(User.LastName)).ShouldNotBeOfType<DuplicatedField>();
}
[Theory]
[InlineData(EnumStorage.AsInteger, NpgsqlDbType.Integer)]
[InlineData(EnumStorage.AsString, NpgsqlDbType.Varchar)]
public void duplicated_field_enum_storage_should_be_taken_from_store_options_enum_storage_by_default(
EnumStorage enumStorage, NpgsqlDbType expectedNpgsqlDbType)
{
var storeOptions = new StoreOptions();
storeOptions.UseSystemTextJsonForSerialization(enumStorage);
var mapping = new DocumentMapping<Target>(storeOptions);
var duplicatedField = mapping.DuplicateField(nameof(Target.Color));
duplicatedField.DbType.ShouldBe(expectedNpgsqlDbType);
}
[Theory]
[InlineData(EnumStorage.AsInteger, NpgsqlDbType.Integer)]
[InlineData(EnumStorage.AsString, NpgsqlDbType.Varchar)]
public void
duplicated_field_enum_storage_should_be_taken_from_store_options_duplicated_field_enum_storage_when_it_was_changed(
EnumStorage enumStorage, NpgsqlDbType expectedNpgsqlDbType)
{
var storeOptions = new StoreOptions();
storeOptions.Advanced.DuplicatedFieldEnumStorage = enumStorage;
var mapping = new DocumentMapping<Target>(storeOptions);
var duplicatedField = mapping.DuplicateField(nameof(Target.Color));
duplicatedField.DbType.ShouldBe(expectedNpgsqlDbType);
}
[Theory]
[InlineData(true, NpgsqlDbType.Timestamp)]
[InlineData(false, NpgsqlDbType.TimestampTz)]
public void
duplicated_field_date_time_db_type_should_be_taken_from_store_options_useTimestampWithoutTimeZoneForDateTime(
bool useTimestampWithoutTimeZoneForDateTime, NpgsqlDbType expectedNpgsqlDbType)
{
var storeOptions = new StoreOptions();
storeOptions.Advanced.DuplicatedFieldUseTimestampWithoutTimeZoneForDateTime =
useTimestampWithoutTimeZoneForDateTime;
var mapping = new DocumentMapping<Target>(storeOptions);
var duplicatedField = mapping.DuplicateField(nameof(Target.Date));
duplicatedField.DbType.ShouldBe(expectedNpgsqlDbType);
}
[Fact]
public void find_field_for_immediate_field_that_is_not_duplicated()
{
var mapping = DocumentMapping.For<UpperCaseField>();
var field = mapping.QueryMembers.MemberFor("Id");
field.As<IdMember>().Member.ShouldBeAssignableTo<FieldInfo>()
.Name.ShouldBe("Id");
}
[Fact]
public void find_field_for_immediate_property_that_is_not_duplicated()
{
var mapping = DocumentMapping.For<UpperCaseProperty>();
var field = mapping.QueryMembers.MemberFor("Id");
field.As<IdMember>().Member.ShouldBeAssignableTo<PropertyInfo>()
.Name.ShouldBe("Id");
}
[Fact]
public void get_the_sql_locator_for_the_Id_member()
{
DocumentMapping.For<User>().QueryMembers.MemberFor("Id")
.TypedLocator.ShouldBe("d.id");
DocumentMapping.For<FieldId>().QueryMembers.MemberFor("id")
.TypedLocator.ShouldBe("d.id");
}
[Fact]
public void is_hierarchy__is_false_for_concrete_type_with_no_subclasses()
{
DocumentMapping.For<User>().IsHierarchy().ShouldBeFalse();
}
[Fact]
public void is_hierarchy_always_true_for_abstract_type()
{
DocumentMapping.For<AbstractDoc>()
.IsHierarchy().ShouldBeTrue();
}
[Fact]
public void is_hierarchy_always_true_for_interface()
{
DocumentMapping.For<IDoc>().IsHierarchy()
.ShouldBeTrue();
}
[Fact]
public void optimistic_versioning_is_turned_off_by_default()
{
var mapping = DocumentMapping.For<User>();
mapping.UseOptimisticConcurrency.ShouldBeFalse();
}
[Fact]
public void override_the_alias()
{
var mapping = DocumentMapping.For<User>();
mapping.Alias = "users";
mapping.TableName.Name.ShouldBe("mt_doc_users");
}
[Fact]
public void override_the_alias_converts_alias_to_lowercase()
{
var mapping = DocumentMapping.For<User>();
mapping.Alias = "Users";
mapping.Alias.ShouldBe("users");
}
[Fact]
public void override_the_alias_converts_table_name_to_lowercase()
{
var mapping = DocumentMapping.For<User>();
mapping.Alias = "Users";
mapping.TableName.Name.ShouldBe("mt_doc_users");
}
[Fact]
public void override_the_alias_converts_tablename_with_different_schema_to_lowercase()
{
var mapping = DocumentMapping.For<User>("OTHER");
mapping.Alias = "Users";
mapping.TableName.QualifiedName.ShouldBe("other.mt_doc_users");
}
[Fact]
public void override_the_alias_converts_tablename_with_schema_to_lowercase()
{
var mapping = DocumentMapping.For<User>();
mapping.Alias = "Users";
mapping.TableName.QualifiedName.ShouldBe("public.mt_doc_users");
}
[Fact]
public void pick_up_lower_case_field_id()
{
var mapping = DocumentMapping.For<LowerCaseField>();
mapping.IdMember.ShouldBeAssignableTo<FieldInfo>()
.Name.ShouldBe(nameof(LowerCaseField.id));
}
[Fact]
public void pick_up_lower_case_property_id()
{
var mapping = DocumentMapping.For<LowerCaseProperty>();
mapping.IdMember.ShouldBeAssignableTo<PropertyInfo>()
.Name.ShouldBe(nameof(LowerCaseProperty.id));
}
[Fact]
public void pick_up_upper_case_field_id()
{
var mapping = DocumentMapping.For<UpperCaseField>();
mapping.IdMember.ShouldBeAssignableTo<FieldInfo>()
.Name.ShouldBe(nameof(UpperCaseField.Id));
}
[Fact]
public void pick_up_upper_case_property_id()
{
var mapping = DocumentMapping.For<UpperCaseProperty>();
mapping.IdMember.ShouldBeAssignableTo<PropertyInfo>()
.Name.ShouldBe(nameof(UpperCaseProperty.Id));
}
[Fact]
public void picks_up_marten_attribute_on_document_type()
{
var mapping = DocumentMapping.For<Organization>();
mapping.PropertySearching.ShouldBe(PropertySearching.JSON_Locator_Only);
}
[Fact]
public void picks_up_marten_ginindexed_attribute_on_document_type()
{
var mapping = DocumentMapping.For<BaseDocumentWithAttribute>();
var indexDefinition = mapping.Indexes.Cast<DocumentIndex>().Single(x => x.Columns.First() == "data");
indexDefinition.Method.ShouldBe(IndexMethod.gin);
var mappingSub = DocumentMapping.For<BaseDocumentSubClass>();
indexDefinition = mappingSub.Indexes.Cast<DocumentIndex>().Single(x => x.Columns.First() == "data");
indexDefinition.Method.ShouldBe(IndexMethod.gin);
}
[Fact]
public void picks_up_searchable_attribute_on_fields()
{
var mapping = DocumentMapping.For<Organization>();
mapping.QueryMembers.MemberFor("OtherName").ShouldBeOfType<DuplicatedField>();
mapping.QueryMembers.MemberFor(nameof(Organization.OtherField)).ShouldNotBeOfType<DuplicatedField>();
}
[Fact]
public void picks_up_searchable_attribute_on_properties()
{
var mapping = DocumentMapping.For<Organization>();
mapping.QueryMembers.MemberFor(nameof(Organization.Name)).ShouldBeOfType<DuplicatedField>();
mapping.QueryMembers.MemberFor(nameof(Organization.OtherProp)).ShouldNotBeOfType<DuplicatedField>();
}
[Fact]
public void table_name_for_document()
{
DocumentMapping.For<MySpecialDocument>().TableName.Name
.ShouldBe("mt_doc_documentmappingtests_myspecialdocument");
}
[Fact]
public void table_name_with_schema_for_document()
{
DocumentMapping.For<MySpecialDocument>().TableName.QualifiedName
.ShouldBe("public.mt_doc_documentmappingtests_myspecialdocument");
}
[Fact]
public void table_name_with_schema_for_document_on_other_schema()
{
DocumentMapping.For<MySpecialDocument>("other").TableName.QualifiedName
.ShouldBe("other.mt_doc_documentmappingtests_myspecialdocument");
}
[Fact]
public void table_name_with_schema_for_document_on_overriden_schema()
{
var documentMapping = DocumentMapping.For<MySpecialDocument>("other");
documentMapping.DatabaseSchemaName = "overriden";
documentMapping.TableName.QualifiedName
.ShouldBe("overriden.mt_doc_documentmappingtests_myspecialdocument");
}
[Fact]
public void to_table_columns_with_duplicated_fields()
{
var mapping = DocumentMapping.For<User>();
mapping.DuplicateField(nameof(User.FirstName));
var table = new DocumentTable(mapping);
table.Columns.Select(x => x.Name)
.ShouldHaveTheSameElementsAs("id", "data", SchemaConstants.LastModifiedColumn,
SchemaConstants.VersionColumn, SchemaConstants.DotNetTypeColumn, "first_name");
}
[Fact]
public void to_table_columns_with_subclasses()
{
var mapping = DocumentMapping.For<Squad>();
mapping.SubClasses.Add(typeof(BaseballTeam));
var table = new DocumentTable(mapping);
var typeColumn = table.Columns[table.Columns.Count - 1];
typeColumn.Name.ShouldBe(SchemaConstants.DocumentTypeColumn);
typeColumn.Type.ShouldBe("varchar");
}
[Fact]
public void to_table_without_subclasses_and_no_duplicated_fields()
{
var mapping = DocumentMapping.For<IntDoc>();
var table = new DocumentTable(mapping);
table.Columns.Select(x => x.Name)
.ShouldHaveTheSameElementsAs("id", "data", SchemaConstants.LastModifiedColumn,
SchemaConstants.VersionColumn, SchemaConstants.DotNetTypeColumn);
}
[Fact]
public void trying_to_replace_the_hilo_settings_when_not_using_hilo_for_the_sequence_throws()
{
Should.Throw<InvalidOperationException>(
() => { DocumentMapping.For<StringId>().HiloSettings = new HiloSettings(); });
}
[Fact]
public void use_custom_id_generation_on_mapping_shoudl_be_settable()
{
var mapping = DocumentMapping.For<LongId>();
mapping.IdStrategy = new CustomIdGeneration();
mapping.IdStrategy.ShouldBeOfType<CustomIdGeneration>();
}
[Fact]
public void use_guid_id_generation_for_guid_id()
{
var mapping = DocumentMapping.For<UpperCaseProperty>();
mapping.IdStrategy.ShouldBeOfType<SequentialGuidIdGeneration>();
}
[Fact]
public void use_hilo_id_generation_for_int_id()
{
DocumentMapping.For<IntId>()
.IdStrategy.ShouldBeOfType<HiloIdGeneration>();
}
[Fact]
public void use_hilo_id_generation_for_long_id()
{
DocumentMapping.For<LongId>()
.IdStrategy.ShouldBeOfType<HiloIdGeneration>();
}
[Fact]
public void use_string_id_generation_for_string()
{
var mapping = DocumentMapping.For<StringId>();
mapping.IdStrategy.ShouldBeOfType<StringIdGeneration>();
}
[Fact]
public void uses_ConfigureMarten_method_to_alter_mapping_upon_construction()
{
var mapping = DocumentMapping.For<ConfiguresItself>();
mapping.Alias.ShouldBe("different");
}
[Fact]
public void uses_ConfigureMarten_method_to_alter_mapping_upon_construction_with_the_generic_signature()
{
var mapping = DocumentMapping.For<ConfiguresItselfSpecifically>();
mapping.DuplicatedFields.Single().MemberName.ShouldBe(nameof(ConfiguresItselfSpecifically.Name));
}
[Fact]
public void trying_to_index_deleted_at_when_not_soft_deleted_document_throws()
{
Should.Throw<InvalidOperationException>(() => DocumentMapping.For<IntId>().AddDeletedAtIndex());
}
[Fact]
public void no_tenant_id_column_when_not_conjoined_tenancy()
{
var mapping = DocumentMapping.For<ConfiguresItselfSpecifically>();
var table = new DocumentTable(mapping);
table.HasColumn(TenantIdColumn.Name).ShouldBeFalse();
}
[Fact]
public void add_the_tenant_id_column_when_it_is_conjoined_tenancy()
{
var options = new StoreOptions();
options.Connection(ConnectionSource.ConnectionString);
options.Policies.AllDocumentsAreMultiTenanted();
var mapping = new DocumentMapping(typeof(User), options);
var table = new DocumentTable(mapping);
table.Columns.Any(x => x is TenantIdColumn).ShouldBeTrue();
table.Indexes.ShouldBeEmpty();
}
[Fact]
public void try_add_the_tenant_id_index_manually_when_it_is_not_multi_tenancy()
{
var options = new StoreOptions();
var mapping = new DocumentMapping(typeof(User), options)
{
TenancyStyle = TenancyStyle.Single
};
mapping.AddTenantIdIndex(); // this should do nothing since it is not multi-tenancy
var table = new DocumentTable(mapping);
table.Indexes.ShouldBeEmpty();
}
[Fact]
public void add_the_tenant_id_index_manually_when_it_is_multi_tenancy()
{
var options = new StoreOptions();
options.Policies.AllDocumentsAreMultiTenanted();
var mapping = new DocumentMapping(typeof(User), options);
mapping.AddTenantIdIndex();
var table = new DocumentTable(mapping);
table.Columns.Any(x => x is TenantIdColumn).ShouldBeTrue();
table.Indexes.Single(x => x.Columns.Length == 1 && x.Columns[0] == TenantIdColumn.Name).ShouldNotBeNull();
}
[Fact]
public void add_the_tenant_id_index_when_it_is_conjoined_tenancy_and_PrimaryKeyTenancyOrdering_id_then_tenant()
{
var options = new StoreOptions();
var mapping = new DocumentMapping(typeof(User), options)
{
TenancyStyle = TenancyStyle.Conjoined,
PrimaryKeyTenancyOrdering = PrimaryKeyTenancyOrdering.Id_Then_TenantId
};
var table = new DocumentTable(mapping);
table.Columns.Any(x => x is TenantIdColumn).ShouldBeTrue();
table.Indexes.Single(x => x.Columns.Length == 1 && x.Columns[0] == TenantIdColumn.Name).ShouldNotBeNull();
}
[Fact]
public void default_metadata_columns()
{
var mapping = DocumentMapping.For<User>();
mapping.Metadata.LastModified.Enabled.ShouldBeTrue();
mapping.Metadata.Version.Enabled.ShouldBeTrue();
mapping.Metadata.DotNetType.Enabled.ShouldBeTrue();
}
[Fact]
public async Task cannot_use_a_doc_type_with_no_id()
{
// 9.0: configuration-error timing changed (#4303). DocumentMapping
// is now built and validated lazily on first session access for the
// affected type rather than at DocumentStore.For() time. This shaves
// cold start by skipping eager builds for every registered type.
// See migration guide.
var store = DocumentStore.For(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.Schema.For<BadDoc>();
});
await using var session = store.LightweightSession();
await Should.ThrowAsync<InvalidDocumentException>(async () =>
{
await session.Query<BadDoc>().ToListAsync();
});
}
[Fact]
public async Task cannot_use_a_doc_type_with_no_id_with_store()
{
// 9.0: as above — error surfaces on first session use, not at store
// construction (#4303).
var store = DocumentStore.For(options =>
{
options.Connection(ConnectionSource.ConnectionString);
options.Schema.For<BadDoc>();
});
await using var session = store.LightweightSession();
await Should.ThrowAsync<InvalidDocumentException>(async () =>
{
await session.Query<BadDoc>().ToListAsync();
});
}
[Fact]
public void use_the_default_pg_type_for_the_member_type_if_not_overridden()
{
DocumentMapping.For<Customer>().DatabaseSchemaName.ShouldBe("organization");
}
[Fact]
public void validate_should_fail_if_using_both_optimistic_concurrency_and_revisioning()
{
var mapping = DocumentMapping.For<Target>();
mapping.UseNumericRevisions = true;
mapping.UseOptimisticConcurrency = true;
var ex = Should.Throw<InvalidDocumentException>(() => mapping.CompileAndValidate());
ex.Message.ShouldContain("cannot be configured with UseNumericRevision and UseOptimisticConcurrency. Choose one or the other", Case.Insensitive);
}
[Fact]
public void duplicate_field_needs_to_be_idempotent_for_single_member()
{
var mapping = DocumentMapping.For<Target>();
var field1 = mapping.DuplicateField(nameof(Target.Color));
var field2 = mapping.DuplicateField(nameof(Target.Color));
field1.ShouldBeSameAs(field2);
}
[Fact]
public void duplicate_field_needs_to_be_idempotent_for_deep_members()
{
var mapping = DocumentMapping.For<Target>();
MemberInfo[] props = new[]
{
ReflectionHelper.GetProperty<Target>(x => x.Inner),
ReflectionHelper.GetProperty<Target>(x => x.Number)
};
var field1 = mapping.DuplicateField(props);
var field2 = mapping.DuplicateField(props);
field1.ShouldBeSameAs(field2);
}
public class FieldId
{
public string id;
}
public abstract class AbstractDoc
{
public int id;
}
public interface IDoc
{
string id { get; set; }
}
[UseOptimisticConcurrency]
public class VersionedDoc
{
public Guid Id { get; set; } = Guid.NewGuid();
}
public class IntId
{
public int Id;
}
public class LongId
{
public long Id;
}
public class StringId
{
public string Id;
}
public class UpperCaseProperty
{
public Guid Id { get; set; }
}
public class LowerCaseProperty
{
public Guid id { get; set; }
}
public class UpperCaseField
{
public int Id;
}
public class LowerCaseField
{
public int id;
}
public class MySpecialDocument
{
public Guid Id { get; set; }
}
[GinIndexed]
public class BaseDocumentWithAttribute
{
public int Id;
}
public class BaseDocumentSubClass: BaseDocumentWithAttribute
{
}
[PropertySearching(PropertySearching.JSON_Locator_Only)]
public class Organization
{
[DuplicateField] public string OtherName;
public string OtherProp;
public Guid Id { get; set; }
[DuplicateField] public string Name { get; set; }
public string OtherField { get; set; }
}
public class CustomIdGeneration: IIdGeneration
{
public bool IsNumeric { get; } = false;
}
#region sample_configuremarten-generic
public class ConfiguresItself
{
public Guid Id;
public static void ConfigureMarten(DocumentMapping mapping)
{
mapping.Alias = "different";
}
}
#endregion
#region sample_configuremarten-specifically
public class ConfiguresItselfSpecifically
{
public Guid Id;
public string Name;
public static void ConfigureMarten(DocumentMapping<ConfiguresItselfSpecifically> mapping)
{
mapping.Duplicate(x => x.Name);
}
}
#endregion
[Fact]
public void uses_ConfigureMarten_for_projection_view_type_registered_only_via_projection()
{
using var store = DocumentStore.For(opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.Projections.Add<ProjectionWithConfiguredView>(ProjectionLifecycle.Inline);
});
var mapping = store.Options.Storage.MappingFor(typeof(ProjectionConfiguredView));
mapping.ShouldBeOfType<DocumentMapping<ProjectionConfiguredView>>();
mapping.Indexes.OfType<ComputedIndex>().Any().ShouldBeTrue();
}
#region sample_using_databaseschemaname_attribute
[DatabaseSchemaName("organization")]
public class Customer
{
[Identity] public string Name { get; set; }
}
#endregion
}
public class BadDoc
{
public string Name { get; set; }
}
public record SomeProjectionEvent(Guid Id, string ImportantField);
public class ProjectionConfiguredView
{
public string Id { get; set; } = "";
public string ImportantField { get; set; } = "";
public static void ConfigureMarten(DocumentMapping<ProjectionConfiguredView> mapping)
{
mapping.Index(x => x.ImportantField);
}
}
public partial class ProjectionWithConfiguredView: MultiStreamProjection<ProjectionConfiguredView, string>
{
public ProjectionWithConfiguredView()
{
Identity<IEvent<SomeProjectionEvent>>(e => e.Data.Id.ToString());
}
public ProjectionConfiguredView Apply(SomeProjectionEvent @event, ProjectionConfiguredView current)
{
current.ImportantField = @event.ImportantField;
return current;
}
}