|
31 | 31 | from bson.son import SON |
32 | 32 | from pymongo import (ASCENDING, DESCENDING, GEO2D, |
33 | 33 | GEOHAYSTACK, GEOSPHERE, HASHED, TEXT) |
34 | | -from pymongo import MongoClient |
| 34 | +from pymongo import MongoClient, monitoring |
35 | 35 | from pymongo.collection import Collection, ReturnDocument |
36 | 36 | from pymongo.command_cursor import CommandCursor |
37 | 37 | from pymongo.cursor import CursorType |
|
51 | 51 | from pymongo.write_concern import WriteConcern |
52 | 52 | from test.test_client import IntegrationTest |
53 | 53 | from test.utils import (is_mongos, enable_text_search, get_pool, |
54 | | - rs_or_single_client, wait_until) |
| 54 | + rs_or_single_client, single_client, |
| 55 | + wait_until, EventListener) |
55 | 56 | from test import client_context, host, port, unittest |
56 | 57 |
|
57 | 58 |
|
@@ -1668,6 +1669,77 @@ def test_find_one_and(self): |
1668 | 1669 | {'$inc': {'i': 1}}, |
1669 | 1670 | sort=sort)['j']) |
1670 | 1671 |
|
| 1672 | + def test_find_one_and_write_concern(self): |
| 1673 | + listener = EventListener() |
| 1674 | + saved_listeners = monitoring._LISTENERS |
| 1675 | + monitoring._LISTENERS = monitoring._Listeners([]) |
| 1676 | + db = single_client(event_listeners=[listener])[self.db.name] |
| 1677 | + # non-default WriteConcern. |
| 1678 | + c_w0 = db.get_collection( |
| 1679 | + 'test', write_concern=WriteConcern(w=0)) |
| 1680 | + # default WriteConcern. |
| 1681 | + c_default = db.get_collection('test', write_concern=WriteConcern()) |
| 1682 | + results = listener.results |
| 1683 | + try: |
| 1684 | + if client_context.version.at_least(3, 1, 9, -1): |
| 1685 | + c_w0.find_and_modify( |
| 1686 | + {'_id': 1}, {'$set': {'foo': 'bar'}}) |
| 1687 | + self.assertEqual( |
| 1688 | + {'w': 0}, results['started'][0].command['writeConcern']) |
| 1689 | + results.clear() |
| 1690 | + |
| 1691 | + c_w0.find_one_and_update( |
| 1692 | + {'_id': 1}, {'$set': {'foo': 'bar'}}) |
| 1693 | + self.assertEqual( |
| 1694 | + {'w': 0}, results['started'][0].command['writeConcern']) |
| 1695 | + results.clear() |
| 1696 | + |
| 1697 | + c_w0.find_one_and_replace({'_id': 1}, {'foo': 'bar'}) |
| 1698 | + self.assertEqual( |
| 1699 | + {'w': 0}, results['started'][0].command['writeConcern']) |
| 1700 | + results.clear() |
| 1701 | + |
| 1702 | + c_w0.find_one_and_delete({'_id': 1}) |
| 1703 | + self.assertEqual( |
| 1704 | + {'w': 0}, results['started'][0].command['writeConcern']) |
| 1705 | + results.clear() |
| 1706 | + else: |
| 1707 | + c_w0.find_and_modify( |
| 1708 | + {'_id': 1}, {'$set': {'foo': 'bar'}}) |
| 1709 | + self.assertNotIn('writeConcern', results['started'][0].command) |
| 1710 | + results.clear() |
| 1711 | + |
| 1712 | + c_w0.find_one_and_update( |
| 1713 | + {'_id': 1}, {'$set': {'foo': 'bar'}}) |
| 1714 | + self.assertNotIn('writeConcern', results['started'][0].command) |
| 1715 | + results.clear() |
| 1716 | + |
| 1717 | + c_w0.find_one_and_replace({'_id': 1}, {'foo': 'bar'}) |
| 1718 | + self.assertNotIn('writeConcern', results['started'][0].command) |
| 1719 | + results.clear() |
| 1720 | + |
| 1721 | + c_w0.find_one_and_delete({'_id': 1}) |
| 1722 | + self.assertNotIn('writeConcern', results['started'][0].command) |
| 1723 | + results.clear() |
| 1724 | + |
| 1725 | + c_default.find_and_modify({'_id': 1}, {'$set': {'foo': 'bar'}}) |
| 1726 | + self.assertNotIn('writeConcern', results['started'][0].command) |
| 1727 | + results.clear() |
| 1728 | + |
| 1729 | + c_default.find_one_and_update({'_id': 1}, {'$set': {'foo': 'bar'}}) |
| 1730 | + self.assertNotIn('writeConcern', results['started'][0].command) |
| 1731 | + results.clear() |
| 1732 | + |
| 1733 | + c_default.find_one_and_replace({'_id': 1}, {'foo': 'bar'}) |
| 1734 | + self.assertNotIn('writeConcern', results['started'][0].command) |
| 1735 | + results.clear() |
| 1736 | + |
| 1737 | + c_default.find_one_and_delete({'_id': 1}) |
| 1738 | + self.assertNotIn('writeConcern', results['started'][0].command) |
| 1739 | + results.clear() |
| 1740 | + finally: |
| 1741 | + monitoring._LISTENERS = saved_listeners |
| 1742 | + |
1671 | 1743 | def test_find_with_nested(self): |
1672 | 1744 | c = self.db.test |
1673 | 1745 | c.drop() |
|
0 commit comments