|
1 |
| -/* Copyright 2010-2016 MongoDB Inc. |
| 1 | +/* Copyright 2010-2017 MongoDB Inc. |
2 | 2 | *
|
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | * you may not use this file except in compliance with the License.
|
|
23 | 23 | using FluentAssertions;
|
24 | 24 | using MongoDB.Bson;
|
25 | 25 | using MongoDB.Bson.Serialization;
|
| 26 | +using MongoDB.Bson.Serialization.Attributes; |
26 | 27 | using MongoDB.Bson.Serialization.Serializers;
|
27 | 28 | using MongoDB.Bson.TestHelpers.XunitExtensions;
|
28 | 29 | using MongoDB.Driver.Core.Clusters;
|
@@ -565,6 +566,96 @@ public void Distinct_should_execute_the_DistinctOperation(
|
565 | 566 | operation.ReadConcern.Should().Be(_readConcern);
|
566 | 567 | }
|
567 | 568 |
|
| 569 | + private enum EnumForDistinctWithArrayField { A, B } |
| 570 | + |
| 571 | + private class ClassForDistinctWithArrayField |
| 572 | + { |
| 573 | + public int Id { get; set; } |
| 574 | + [BsonRepresentation(BsonType.String)] |
| 575 | + public EnumForDistinctWithArrayField[] A { get; set; } |
| 576 | + } |
| 577 | + |
| 578 | + [Theory] |
| 579 | + [ParameterAttributeData] |
| 580 | + public void Distinct_should_execute_the_DistinctOperation_when_type_parameter_is_array_field_item_type( |
| 581 | + [Values(false, true)] bool async) |
| 582 | + { |
| 583 | + var fieldName = "A"; |
| 584 | + var filter = new BsonDocument("x", 1); |
| 585 | + var options = new DistinctOptions |
| 586 | + { |
| 587 | + Collation = new Collation("en_US"), |
| 588 | + MaxTime = TimeSpan.FromSeconds(20) |
| 589 | + }; |
| 590 | + |
| 591 | + var subject = CreateSubject<ClassForDistinctWithArrayField>(); |
| 592 | + |
| 593 | + if (async) |
| 594 | + { |
| 595 | + subject.DistinctAsync<EnumForDistinctWithArrayField>(fieldName, filter, options, CancellationToken.None).GetAwaiter().GetResult(); |
| 596 | + } |
| 597 | + else |
| 598 | + { |
| 599 | + subject.Distinct<EnumForDistinctWithArrayField>(fieldName, filter, options, CancellationToken.None); |
| 600 | + } |
| 601 | + |
| 602 | + var call = _operationExecutor.GetReadCall<IAsyncCursor<EnumForDistinctWithArrayField>>(); |
| 603 | + |
| 604 | + call.Operation.Should().BeOfType<DistinctOperation<EnumForDistinctWithArrayField>>(); |
| 605 | + var operation = (DistinctOperation<EnumForDistinctWithArrayField>)call.Operation; |
| 606 | + operation.Collation.Should().BeSameAs(options.Collation); |
| 607 | + operation.CollectionNamespace.FullName.Should().Be("foo.bar"); |
| 608 | + operation.FieldName.Should().Be(fieldName); |
| 609 | + operation.Filter.Should().Be(filter); |
| 610 | + operation.MaxTime.Should().Be(options.MaxTime); |
| 611 | + operation.ReadConcern.Should().Be(_readConcern); |
| 612 | + |
| 613 | + var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<ClassForDistinctWithArrayField>(); |
| 614 | + BsonSerializationInfo fieldSerializationInfo; |
| 615 | + ((IBsonDocumentSerializer)documentSerializer).TryGetMemberSerializationInfo(fieldName, out fieldSerializationInfo).Should().BeTrue(); |
| 616 | + var fieldSerializer = (ArraySerializer<EnumForDistinctWithArrayField>)fieldSerializationInfo.Serializer; |
| 617 | + operation.ValueSerializer.Should().BeSameAs(fieldSerializer.ItemSerializer); |
| 618 | + } |
| 619 | + |
| 620 | + [Theory] |
| 621 | + [ParameterAttributeData] |
| 622 | + public void Distinct_should_execute_the_DistinctOperation_when_type_parameter_is_string_instead_of_ennum( |
| 623 | + [Values(false, true)] bool async) |
| 624 | + { |
| 625 | + var fieldName = "A"; |
| 626 | + var filter = new BsonDocument("x", 1); |
| 627 | + var options = new DistinctOptions |
| 628 | + { |
| 629 | + Collation = new Collation("en_US"), |
| 630 | + MaxTime = TimeSpan.FromSeconds(20) |
| 631 | + }; |
| 632 | + |
| 633 | + var subject = CreateSubject<ClassForDistinctWithArrayField>(); |
| 634 | + |
| 635 | + if (async) |
| 636 | + { |
| 637 | + subject.DistinctAsync<string>(fieldName, filter, options, CancellationToken.None).GetAwaiter().GetResult(); |
| 638 | + } |
| 639 | + else |
| 640 | + { |
| 641 | + subject.Distinct<string>(fieldName, filter, options, CancellationToken.None); |
| 642 | + } |
| 643 | + |
| 644 | + var call = _operationExecutor.GetReadCall<IAsyncCursor<string>>(); |
| 645 | + |
| 646 | + call.Operation.Should().BeOfType<DistinctOperation<string>>(); |
| 647 | + var operation = (DistinctOperation<string>)call.Operation; |
| 648 | + operation.Collation.Should().BeSameAs(options.Collation); |
| 649 | + operation.CollectionNamespace.FullName.Should().Be("foo.bar"); |
| 650 | + operation.FieldName.Should().Be(fieldName); |
| 651 | + operation.Filter.Should().Be(filter); |
| 652 | + operation.MaxTime.Should().Be(options.MaxTime); |
| 653 | + operation.ReadConcern.Should().Be(_readConcern); |
| 654 | + |
| 655 | + var stringSerializer = BsonSerializer.SerializerRegistry.GetSerializer<string>(); |
| 656 | + operation.ValueSerializer.Should().BeSameAs(stringSerializer); |
| 657 | + } |
| 658 | + |
568 | 659 | [Theory]
|
569 | 660 | [ParameterAttributeData]
|
570 | 661 | public void Find_should_execute_the_FindOperation(
|
|
0 commit comments