|
| 1 | +# Copyright 2018-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 | +"""Run the unit tests for WriteConcern.""" |
| 16 | + |
| 17 | +import collections |
| 18 | +import unittest |
| 19 | + |
| 20 | +from pymongo.write_concern import WriteConcern |
| 21 | + |
| 22 | + |
| 23 | +class TestWriteConcern(unittest.TestCase): |
| 24 | + |
| 25 | + def test_equality(self): |
| 26 | + concern = WriteConcern(j=True, wtimeout=3000) |
| 27 | + self.assertEqual(concern, WriteConcern(j=True, wtimeout=3000)) |
| 28 | + self.assertNotEqual(concern, WriteConcern()) |
| 29 | + |
| 30 | + def test_equality_to_none(self): |
| 31 | + concern = WriteConcern() |
| 32 | + self.assertNotEqual(concern, None) |
| 33 | + # Explicitly use the != operator. |
| 34 | + self.assertTrue(concern != None) # noqa |
| 35 | + |
| 36 | + def test_equality_compatible_type(self): |
| 37 | + |
| 38 | + class _FakeWriteConcern(object): |
| 39 | + |
| 40 | + def __init__(self, **document): |
| 41 | + self.document = document |
| 42 | + |
| 43 | + def __eq__(self, other): |
| 44 | + try: |
| 45 | + return self.document == other.document |
| 46 | + except AttributeError: |
| 47 | + return NotImplemented |
| 48 | + |
| 49 | + def __ne__(self, other): |
| 50 | + try: |
| 51 | + return self.document != other.document |
| 52 | + except AttributeError: |
| 53 | + return NotImplemented |
| 54 | + |
| 55 | + self.assertEqual(WriteConcern(j=True), _FakeWriteConcern(j=True)) |
| 56 | + self.assertEqual(_FakeWriteConcern(j=True), WriteConcern(j=True)) |
| 57 | + self.assertEqual(WriteConcern(j=True), _FakeWriteConcern(j=True)) |
| 58 | + self.assertEqual(WriteConcern(wtimeout=42), _FakeWriteConcern(wtimeout=42)) |
| 59 | + self.assertNotEqual(WriteConcern(wtimeout=42), _FakeWriteConcern(wtimeout=2000)) |
| 60 | + |
| 61 | + def test_equality_incompatible_type(self): |
| 62 | + _fake_type = collections.namedtuple('NotAWriteConcern', ['document']) |
| 63 | + self.assertNotEqual(WriteConcern(j=True), _fake_type({'j': True})) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + unittest.main() |
0 commit comments