|
| 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; |
| 17 | +using MongoDB.Driver.Core.Compression; |
| 18 | +using MongoDB.Driver.Core.Configuration; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Driver.Core.Tests.Core.Configuration |
| 22 | +{ |
| 23 | + public class CompressorConfigurationTests |
| 24 | + { |
| 25 | + [Theory] |
| 26 | + [InlineData(CompressorType.Noop)] |
| 27 | + [InlineData(CompressorType.Snappy)] |
| 28 | + [InlineData(CompressorType.Zlib)] |
| 29 | + public void constructor_should_initialize_instance(CompressorType type) |
| 30 | + { |
| 31 | + var subject = new CompressorConfiguration(type); |
| 32 | + |
| 33 | + subject.Type.Should().Be(type); |
| 34 | + subject.Properties.Should().NotBeNull(); |
| 35 | + subject.Properties.Count.Should().Be(0); |
| 36 | + } |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [InlineData("Properties")] |
| 40 | + [InlineData("Type")] |
| 41 | + public void Equals_should_return_false_if_any_field_is_not_equal(string fieldName) |
| 42 | + { |
| 43 | + var type = CompressorType.Snappy; |
| 44 | + var key = "x"; |
| 45 | + var subject1 = new CompressorConfiguration(type); |
| 46 | + subject1.Properties.Add(key, 1); |
| 47 | + switch (fieldName) |
| 48 | + { |
| 49 | + case "Properties": key = "y"; break; |
| 50 | + case "Type": type = CompressorType.Zlib; break; |
| 51 | + } |
| 52 | + var subject2 = new CompressorConfiguration(type); |
| 53 | + subject2.Properties.Add(key, 1); |
| 54 | + |
| 55 | + var result = subject1.Equals(subject2); |
| 56 | + var hashCode1 = subject1.GetHashCode(); |
| 57 | + var hashCode2 = subject2.GetHashCode(); |
| 58 | + |
| 59 | + result.Should().BeFalse(); |
| 60 | + hashCode2.Should().NotBe(hashCode1); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void Equals_should_return_false_if_obj_is_null() |
| 65 | + { |
| 66 | + var subject = new CompressorConfiguration(CompressorType.Noop); |
| 67 | + |
| 68 | + var result = subject.Equals(null); |
| 69 | + |
| 70 | + result.Should().BeFalse(); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void Equals_should_return_false_if_obj_is_wrong_type() |
| 75 | + { |
| 76 | + var subject = new CompressorConfiguration(CompressorType.Noop); |
| 77 | + var obj = new object(); |
| 78 | + |
| 79 | + var result = subject.Equals(new object()); |
| 80 | + var hashCode1 = subject.GetHashCode(); |
| 81 | + var hashCode2 = obj.GetHashCode(); |
| 82 | + |
| 83 | + result.Should().BeFalse(); |
| 84 | + hashCode2.Should().NotBe(hashCode1); |
| 85 | + } |
| 86 | + |
| 87 | + [Theory] |
| 88 | + [InlineData(CompressorType.Noop)] |
| 89 | + [InlineData(CompressorType.Snappy)] |
| 90 | + [InlineData(CompressorType.Zlib)] |
| 91 | + public void Equals_should_return_true_if_all_fields_are_equal(CompressorType type) |
| 92 | + { |
| 93 | + var subject1 = new CompressorConfiguration(type); |
| 94 | + var subject2 = new CompressorConfiguration(type); |
| 95 | + subject1.Properties.Add("x", 1); |
| 96 | + subject2.Properties.Add("x", 1); |
| 97 | + |
| 98 | + var result = subject1.Equals(subject2); |
| 99 | + var hashCode1 = subject1.GetHashCode(); |
| 100 | + var hashCode2 = subject2.GetHashCode(); |
| 101 | + |
| 102 | + result.Should().BeTrue(); |
| 103 | + hashCode2.Should().Be(hashCode1); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments