|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +from django.db import connection |
| 4 | +from django.test import TestCase |
| 5 | + |
| 6 | + |
| 7 | +class SupportsTransactionsTests(TestCase): |
| 8 | + def setUp(self): |
| 9 | + # Clear the cached property. |
| 10 | + del connection.features.supports_transactions |
| 11 | + |
| 12 | + def tearDown(self): |
| 13 | + del connection.features.supports_transactions |
| 14 | + |
| 15 | + def test_replica_set(self): |
| 16 | + """A replica set supports transactions.""" |
| 17 | + |
| 18 | + def mocked_command(command): |
| 19 | + if command == "hello": |
| 20 | + return {"setName": "foo"} |
| 21 | + if command == "serverStatus": |
| 22 | + return {"storageEngine": {"name": "wiredTiger"}} |
| 23 | + raise Exception("Unexpected command") |
| 24 | + |
| 25 | + with patch("pymongo.synchronous.database.Database.command", wraps=mocked_command): |
| 26 | + self.assertIs(connection.features.supports_transactions, True) |
| 27 | + |
| 28 | + def test_replica_set_other_storage_engine(self): |
| 29 | + """No support on a non-wiredTiger replica set.""" |
| 30 | + |
| 31 | + def mocked_command(command): |
| 32 | + if command == "hello": |
| 33 | + return {"setName": "foo"} |
| 34 | + if command == "serverStatus": |
| 35 | + return {"storageEngine": {"name": "other"}} |
| 36 | + raise Exception("Unexpected command") |
| 37 | + |
| 38 | + with patch("pymongo.synchronous.database.Database.command", wraps=mocked_command): |
| 39 | + self.assertIs(connection.features.supports_transactions, False) |
| 40 | + |
| 41 | + def test_sharded_cluster(self): |
| 42 | + """A sharded cluster with wiredTiger storage engine supports them.""" |
| 43 | + |
| 44 | + def mocked_command(command): |
| 45 | + if command == "hello": |
| 46 | + return {"msg": "isdbgrid"} |
| 47 | + if command == "serverStatus": |
| 48 | + return {"storageEngine": {"name": "wiredTiger"}} |
| 49 | + raise Exception("Unexpected command") |
| 50 | + |
| 51 | + with patch("pymongo.synchronous.database.Database.command", wraps=mocked_command): |
| 52 | + self.assertIs(connection.features.supports_transactions, True) |
| 53 | + |
| 54 | + def test_sharded_cluster_other_storage_engine(self): |
| 55 | + """No support on a non-wiredTiger shared cluster.""" |
| 56 | + |
| 57 | + def mocked_command(command): |
| 58 | + if command == "hello": |
| 59 | + return {"msg": "isdbgrid"} |
| 60 | + if command == "serverStatus": |
| 61 | + return {"storageEngine": {"name": "other"}} |
| 62 | + raise Exception("Unexpected command") |
| 63 | + |
| 64 | + with patch("pymongo.synchronous.database.Database.command", wraps=mocked_command): |
| 65 | + self.assertIs(connection.features.supports_transactions, False) |
| 66 | + |
| 67 | + def test_no_support(self): |
| 68 | + """No support on a non-replica set, non-sharded cluster.""" |
| 69 | + |
| 70 | + def mocked_command(command): |
| 71 | + if command == "hello": |
| 72 | + return {} |
| 73 | + raise Exception("Unexpected command") |
| 74 | + |
| 75 | + with patch("pymongo.synchronous.database.Database.command", wraps=mocked_command): |
| 76 | + self.assertIs(connection.features.supports_transactions, False) |
0 commit comments