mongo-csharp-driver /tests/MongoDB.Driver.Tests/ProjectionDefinitionBuilderTests.cs

Language C# Lines 202
MD5 Hash d43f60734db5a30a2ec1512c1ea4426c Estimated Cost $3,557 (why?)
Repository git://github.com/mongodb/mongo-csharp-driver.git View Raw File View Project SPDX
  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
/* Copyright 2010-2015 MongoDB 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.Linq;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using Xunit;

namespace MongoDB.Driver.Tests
{
    public class ProjectionDefinitionBuilderTests
    {
        [Fact]
        public void Combine()
        {
            var subject = CreateSubject<Person>();

            var projection = subject.Combine(
                subject.Include(x => x.FirstName),
                subject.Exclude("LastName"));

            Assert(projection, "{fn: 1, LastName: 0}");
        }

        [Fact]
        public void Combine_with_redundant_fields()
        {
            var subject = CreateSubject<Person>();

            var projection = subject.Combine(
                subject.Include(x => x.FirstName),
                subject.Exclude("LastName"),
                subject.Include("fn"));

            Assert(projection, "{LastName: 0, fn: 1}");
        }

        [Fact]
        public void Combine_with_redundant_fields_using_extension_method()
        {
            var subject = CreateSubject<Person>();

            var projection = subject.Include(x => x.FirstName).Exclude("LastName").Include("fn");

            Assert(projection, "{LastName: 0, fn: 1}");
        }

        [Fact]
        public void ElemMatch()
        {
            var subject = CreateSubject<BsonDocument>();

            Assert(subject.ElemMatch<BsonDocument>("a", "{b: 1}"), "{a: {$elemMatch: {b: 1}}}");
        }

        [Fact]
        public void ElemMatch_Typed()
        {
            var subject = CreateSubject<Person>();

            Assert(subject.ElemMatch<Pet>("Pets", "{Name: 'Fluffy'}"), "{pets: {$elemMatch: {Name: 'Fluffy'}}}");
            Assert(subject.ElemMatch(x => x.Pets, "{Name: 'Fluffy'}"), "{pets: {$elemMatch: {Name: 'Fluffy'}}}");
            Assert(subject.ElemMatch(x => x.Pets, x => x.Name == "Fluffy"), "{pets: {$elemMatch: {name: 'Fluffy'}}}");
        }

        [Fact]
        public void ElemMatch_from_filter()
        {
            var subject = CreateSubject<BsonDocument>();

            Assert(subject.Include("a.$"), "{'a.$': 1}");
        }

        [Fact]
        public void ElemMatch_from_filter_Typed()
        {
            var subject = CreateSubject<Person>();

            Assert(subject.Include(x => x.Pets.ElementAt(-1)), "{'pets.$': 1}");
            Assert(subject.Include("Pets.$"), "{'pets.$': 1}");
        }

        [Fact]
        public void Exclude()
        {
            var subject = CreateSubject<BsonDocument>();

            Assert(subject.Exclude("a"), "{a: 0}");
        }

        [Fact]
        public void Exclude_Typed()
        {
            var subject = CreateSubject<Person>();

            Assert(subject.Exclude(x => x.FirstName), "{fn: 0}");
            Assert(subject.Exclude("FirstName"), "{fn: 0}");
        }

        [Fact]
        public void Include()
        {
            var subject = CreateSubject<BsonDocument>();

            Assert(subject.Include("a"), "{a: 1}");
        }

        [Fact]
        public void Include_Typed()
        {
            var subject = CreateSubject<Person>();

            Assert(subject.Include(x => x.FirstName), "{fn: 1}");
            Assert(subject.Include("FirstName"), "{fn: 1}");
        }

        [Fact]
        public void MetaTextScore()
        {
            var subject = CreateSubject<BsonDocument>();

            Assert(subject.MetaTextScore("a"), "{a: {$meta: 'textScore'}}");
        }

        [Fact]
        public void Slice()
        {
            var subject = CreateSubject<BsonDocument>();

            Assert(subject.Slice("a", 10), "{a: {$slice: 10}}");
        }

        [Fact]
        public void Slice_Typed()
        {
            var subject = CreateSubject<Person>();

            Assert(subject.Slice(x => x.Pets, 10), "{pets: {$slice: 10}}");
            Assert(subject.Slice("Pets", 10), "{pets: {$slice: 10}}");
        }

        [Fact]
        public void Slice_with_limit()
        {
            var subject = CreateSubject<BsonDocument>();

            Assert(subject.Slice("a", 10, 20), "{a: {$slice: [10, 20]}}");
        }

        [Fact]
        public void Slice_Typed_with_limit()
        {
            var subject = CreateSubject<Person>();

            Assert(subject.Slice(x => x.Pets, 10, 20), "{pets: {$slice: [10, 20]}}");
            Assert(subject.Slice("Pets", 10, 20), "{pets: {$slice: [10, 20]}}");
        }

        private void Assert<TDocument>(ProjectionDefinition<TDocument> projection, string expectedJson)
        {
            var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<TDocument>();
            var renderedProjection = projection.Render(documentSerializer, BsonSerializer.SerializerRegistry);

            renderedProjection.Should().Be(expectedJson);
        }

        private ProjectionDefinitionBuilder<TDocument> CreateSubject<TDocument>()
        {
            return new ProjectionDefinitionBuilder<TDocument>();
        }

        private class Person
        {
            [BsonElement("fn")]
            public string FirstName { get; set; }
        
            [BsonElement("pets")]
            public Pet[] Pets { get; set; }
        }

        private class Pet
        {
            [BsonElement("name")]
            public string Name { get; set; }
        }    
    }
}
Back to Top