|
1 |
| -using Xunit; |
2 |
| -using MongoDB.Bson.Serialization.Serializers; |
| 1 | +/* Copyright 2019-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using FluentAssertions; |
3 | 17 | using MongoDB.Bson.Serialization;
|
4 |
| -using MongoDB.Bson.Serialization.IdGenerators; |
5 | 18 | using MongoDB.Bson.Serialization.Conventions;
|
| 19 | +using MongoDB.Bson.Serialization.IdGenerators; |
| 20 | +using MongoDB.Bson.Serialization.Serializers; |
| 21 | +using Moq; |
| 22 | +using Xunit; |
6 | 23 |
|
7 | 24 | namespace MongoDB.Bson.Tests.Serialization.Conventions
|
8 | 25 | {
|
9 | 26 | public class StringIdStoredAsObjectIdConventionTests
|
10 | 27 | {
|
11 |
| - BsonMemberMap SampleMap<T>() => new BsonClassMap<T>(cm => cm.AutoMap()).GetMemberMap("Id"); |
| 28 | + private readonly IBsonSerializer _defaultInt32Serializer = BsonSerializer.LookupSerializer(typeof(int)); |
| 29 | + private readonly IBsonSerializer _defaultStringSerializer = BsonSerializer.LookupSerializer(typeof(string)); |
12 | 30 |
|
13 | 31 | [Fact]
|
14 |
| - public void Apply_StringId_SetsSerializer() |
| 32 | + public void Apply_should_ignore_any_member_that_is_not_the_id() |
15 | 33 | {
|
16 |
| - var target = new StringIdStoredAsObjectIdConvention(); |
17 |
| - var subject = SampleMap<TestClassWithStringId>(); |
| 34 | + var subject = CreateSubject(); |
| 35 | + var memberMap = GetMemberMap<TestClassWithStringId>("X"); |
18 | 36 |
|
19 |
| - target.Apply(subject); |
| 37 | + subject.Apply(memberMap); |
20 | 38 |
|
21 |
| - Assert.IsType<StringSerializer>(subject.GetSerializer()); |
| 39 | + memberMap.GetSerializer().Should().BeSameAs(_defaultStringSerializer); |
| 40 | + memberMap.IdGenerator.Should().BeNull(); |
22 | 41 | }
|
23 | 42 |
|
24 | 43 | [Fact]
|
25 |
| - public void Apply_StringId_SetsIdGenerator() |
| 44 | + public void Apply_should_ignore_any_id_that_is_not_of_type_string() |
26 | 45 | {
|
27 |
| - var target = new StringIdStoredAsObjectIdConvention(); |
28 |
| - var subject = SampleMap<TestClassWithStringId>(); |
| 46 | + var subject = CreateSubject(); |
| 47 | + var memberMap = GetIdMemberMap<TestClassWithIntId>(); |
29 | 48 |
|
30 |
| - target.Apply(subject); |
| 49 | + subject.Apply(memberMap); |
31 | 50 |
|
32 |
| - Assert.IsType<StringObjectIdGenerator>(subject.IdGenerator); |
| 51 | + memberMap.GetSerializer().Should().BeSameAs(_defaultInt32Serializer); |
| 52 | + memberMap.IdGenerator.Should().BeNull(); |
33 | 53 | }
|
34 | 54 |
|
35 | 55 | [Fact]
|
36 |
| - public void Apply_ExistingIdGenerator_DoesNotApply() |
| 56 | + public void Apply_should_ignore_any_id_that_already_has_a_serializer_configured() |
37 | 57 | {
|
38 |
| - var target = new StringIdStoredAsObjectIdConvention(); |
39 |
| - var subject = SampleMap<TestClassWithStringId>(); |
40 |
| - subject.SetIdGenerator(CombGuidGenerator.Instance); |
| 58 | + var subject = CreateSubject(); |
| 59 | + var memberMap = GetIdMemberMap<TestClassWithStringId>(); |
| 60 | + var serializer = new StringSerializer(); |
| 61 | + memberMap.SetSerializer(serializer); |
41 | 62 |
|
42 |
| - target.Apply(subject); |
| 63 | + subject.Apply(memberMap); |
43 | 64 |
|
44 |
| - Assert.IsType<CombGuidGenerator>(subject.IdGenerator); |
| 65 | + memberMap.GetSerializer().Should().BeSameAs(serializer); |
| 66 | + memberMap.IdGenerator.Should().BeNull(); |
45 | 67 | }
|
46 | 68 |
|
47 | 69 | [Fact]
|
48 |
| - public void Apply_NotStringSerializer_DoesNotApply() |
| 70 | + public void Apply_should_ignore_any_id_that_already_has_an_idGenerator_configured() |
49 | 71 | {
|
50 |
| - var target = new StringIdStoredAsObjectIdConvention(); |
51 |
| - var subject = SampleMap<TestClassWithStringId>(); |
52 |
| - subject.SetSerializer(new FakeStringSerializer()); |
| 72 | + var subject = CreateSubject(); |
| 73 | + var memberMap = GetIdMemberMap<TestClassWithStringId>(); |
| 74 | + var idGenerator = Mock.Of<IIdGenerator>(); |
| 75 | + memberMap.SetIdGenerator(idGenerator); |
53 | 76 |
|
54 |
| - target.Apply(subject); |
| 77 | + subject.Apply(memberMap); |
55 | 78 |
|
56 |
| - Assert.IsType<FakeStringSerializer>(subject.GetSerializer()); |
| 79 | + memberMap.GetSerializer().Should().BeSameAs(_defaultStringSerializer); |
| 80 | + memberMap.IdGenerator.Should().BeSameAs(idGenerator); |
57 | 81 | }
|
58 | 82 |
|
59 |
| - |
60 | 83 | [Fact]
|
61 |
| - public void Apply_IntId_LeavesSerializer() |
| 84 | + public void Apply_should_configure_id_serializer_and_idGenerator() |
62 | 85 | {
|
63 |
| - var target = new StringIdStoredAsObjectIdConvention(); |
64 |
| - var subject = SampleMap<TestClassWithIntId>(); |
| 86 | + var subject = CreateSubject(); |
| 87 | + var memberMap = GetIdMemberMap<TestClassWithStringId>(); |
65 | 88 |
|
66 |
| - target.Apply(subject); |
| 89 | + subject.Apply(memberMap); |
67 | 90 |
|
68 |
| - Assert.IsNotType<StringSerializer>(subject.GetSerializer()); |
| 91 | + var stringSerializer = memberMap.GetSerializer().Should().BeOfType<StringSerializer>().Subject; |
| 92 | + stringSerializer.Representation.Should().Be(BsonType.ObjectId); |
| 93 | + memberMap.IdGenerator.Should().BeOfType<StringObjectIdGenerator>(); |
69 | 94 | }
|
70 | 95 |
|
71 |
| - [Fact] |
72 |
| - public void Apply_IntId_NoIdGenerator() |
73 |
| - { |
74 |
| - var target = new StringIdStoredAsObjectIdConvention(); |
75 |
| - var subject = SampleMap<TestClassWithIntId>(); |
| 96 | + // private methods |
| 97 | + private StringIdStoredAsObjectIdConvention CreateSubject() |
| 98 | + => new StringIdStoredAsObjectIdConvention(); |
76 | 99 |
|
77 |
| - target.Apply(subject); |
| 100 | + private BsonMemberMap GetIdMemberMap<T>() |
| 101 | + => GetMemberMap<T>("Id"); |
78 | 102 |
|
79 |
| - Assert.Null(subject.IdGenerator); |
80 |
| - } |
| 103 | + private BsonMemberMap GetMemberMap<T>(string memberName) |
| 104 | + => new BsonClassMap<T>(cm => cm.AutoMap()).GetMemberMap(memberName); |
81 | 105 |
|
| 106 | + // nested types |
| 107 | + private class TestClassWithIntId |
| 108 | + { |
| 109 | + public int Id { get; set; } |
| 110 | + } |
82 | 111 |
|
83 |
| - public class TestClassWithStringId { public string Id; } |
84 |
| - |
85 |
| - public class TestClassWithIntId { public int Id; } |
86 |
| - |
87 |
| - class FakeStringSerializer : SealedClassSerializerBase<string> |
| 112 | + private class TestClassWithStringId |
88 | 113 | {
|
89 |
| - public BsonType Representation => BsonType.String; |
| 114 | + public string Id { get; set; } |
| 115 | + public string X { get; set; } |
90 | 116 | }
|
91 | 117 | }
|
92 | 118 | }
|
93 |
| - |
94 |
| - |
95 |
| - |
|
0 commit comments