本文整理汇总了Python中mongo_connector.oplog_manager.OplogThread.filter_oplog_entry方法的典型用法代码示例。如果您正苦于以下问题:Python OplogThread.filter_oplog_entry方法的具体用法?Python OplogThread.filter_oplog_entry怎么用?Python OplogThread.filter_oplog_entry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongo_connector.oplog_manager.OplogThread
的用法示例。
在下文中一共展示了OplogThread.filter_oplog_entry方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestOplogManager
# 需要导入模块: from mongo_connector.oplog_manager import OplogThread [as 别名]
# 或者: from mongo_connector.oplog_manager.OplogThread import filter_oplog_entry [as 别名]
#.........这里部分代码省略.........
self.assertEqual(len(bad), 0)
# test delete
self.primary_conn[db][coll].remove({"_id": 1})
assert_soon(lambda: len(docman._search()) == 0)
bad = [d for d in docman._search() if d["ns"] == dest_mapping[ns]]
self.assertEqual(len(bad), 0)
# cleanup
self.primary_conn[db][coll].remove()
self.opman.doc_managers[0]._delete()
# doc not in namespace set
for ns in phony_ns:
db, coll = ns.split(".", 1)
# test insert
self.primary_conn[db][coll].insert(base_doc)
time.sleep(1)
self.assertEqual(len(docman._search()), 0)
# test update
self.primary_conn[db][coll].update({"_id": 1}, {"$set": {"weakness": "kryptonite"}})
time.sleep(1)
self.assertEqual(len(docman._search()), 0)
def test_many_targets(self):
"""Test that one OplogThread is capable of replicating to more than
one target.
"""
doc_managers = [DocManager(), DocManager(), DocManager()]
self.opman.doc_managers = doc_managers
# start replicating
self.opman.start()
self.primary_conn["test"]["test"].insert({"name": "kermit", "color": "green"})
self.primary_conn["test"]["test"].insert({"name": "elmo", "color": "firetruck red"})
assert_soon(
lambda: sum(len(d._search()) for d in doc_managers) == 6,
"OplogThread should be able to replicate to multiple targets",
)
self.primary_conn["test"]["test"].remove({"name": "elmo"})
assert_soon(
lambda: sum(len(d._search()) for d in doc_managers) == 3,
"OplogThread should be able to replicate to multiple targets",
)
for d in doc_managers:
self.assertEqual(d._search()[0]["name"], "kermit")
def test_filter_oplog_entry(self):
# Test oplog entries: these are callables, since
# filter_oplog_entry modifies the oplog entry in-place
insert_op = lambda: {"op": "i", "o": {"_id": 0, "a": 1, "b": 2, "c": 3}}
update_op = lambda: {"op": "u", "o": {"$set": {"a": 4, "b": 5}, "$unset": {"c": True}}, "o2": {"_id": 1}}
# Case 0: insert op, no fields provided
self.opman.fields = None
filtered = self.opman.filter_oplog_entry(insert_op())
self.assertEqual(filtered, insert_op())
# Case 1: insert op, fields provided
self.opman.fields = ["a", "b"]
filtered = self.opman.filter_oplog_entry(insert_op())
self.assertEqual(filtered["o"], {"_id": 0, "a": 1, "b": 2})
# Case 2: insert op, fields provided, doc becomes empty except for _id
self.opman.fields = ["d", "e", "f"]
filtered = self.opman.filter_oplog_entry(insert_op())
self.assertEqual(filtered["o"], {"_id": 0})
# Case 3: update op, no fields provided
self.opman.fields = None
filtered = self.opman.filter_oplog_entry(update_op())
self.assertEqual(filtered, update_op())
# Case 4: update op, fields provided
self.opman.fields = ["a", "c"]
filtered = self.opman.filter_oplog_entry(update_op())
self.assertNotIn("b", filtered["o"]["$set"])
self.assertIn("a", filtered["o"]["$set"])
self.assertEqual(filtered["o"]["$unset"], update_op()["o"]["$unset"])
# Case 5: update op, fields provided, empty $set
self.opman.fields = ["c"]
filtered = self.opman.filter_oplog_entry(update_op())
self.assertNotIn("$set", filtered["o"])
self.assertEqual(filtered["o"]["$unset"], update_op()["o"]["$unset"])
# Case 6: update op, fields provided, empty $unset
self.opman.fields = ["a", "b"]
filtered = self.opman.filter_oplog_entry(update_op())
self.assertNotIn("$unset", filtered["o"])
self.assertEqual(filtered["o"]["$set"], update_op()["o"]["$set"])
# Case 7: update op, fields provided, entry is nullified
self.opman.fields = ["d", "e", "f"]
filtered = self.opman.filter_oplog_entry(update_op())
self.assertEqual(filtered, None)
示例2: test_fields_constructor
# 需要导入模块: from mongo_connector.oplog_manager import OplogThread [as 别名]
# 或者: from mongo_connector.oplog_manager.OplogThread import filter_oplog_entry [as 别名]
def test_fields_constructor(self):
# Test with "_id" field in constructor
fields = ["_id", "title", "content", "author"]
opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
dest_mapping_stru=self.dest_mapping_stru,
fields=fields
)
self._check_fields(opman, fields, [],
dict((f, 1) for f in fields))
extra_fields = fields + ['extra1', 'extra2']
filtered = opman.filter_oplog_entry(
{'op': 'i',
'o': dict((f, 1) for f in extra_fields)})['o']
self.assertEqual(dict((f, 1) for f in fields), filtered)
# Test without "_id" field in constructor
fields = ["title", "content", "author"]
opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
dest_mapping_stru=self.dest_mapping_stru,
fields=fields
)
fields.append('_id')
self._check_fields(opman, fields, [],
dict((f, 1) for f in fields))
extra_fields = fields + ['extra1', 'extra2']
filtered = opman.filter_oplog_entry(
{'op': 'i',
'o': dict((f, 1) for f in extra_fields)})['o']
self.assertEqual(dict((f, 1) for f in fields), filtered)
# Test with only "_id" field
fields = ["_id"]
opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
dest_mapping_stru=self.dest_mapping_stru,
fields=fields
)
self._check_fields(opman, fields, [],
dict((f, 1) for f in fields))
extra_fields = fields + ['extra1', 'extra2']
filtered = opman.filter_oplog_entry(
{'op': 'i',
'o': dict((f, 1) for f in extra_fields)})['o']
self.assertEqual({'_id': 1}, filtered)
# Test with no fields set
opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
dest_mapping_stru=self.dest_mapping_stru
)
self._check_fields(opman, [], [], None)
extra_fields = ['_id', 'extra1', 'extra2']
filtered = opman.filter_oplog_entry(
{'op': 'i',
'o': dict((f, 1) for f in extra_fields)})['o']
self.assertEqual(dict((f, 1) for f in extra_fields), filtered)
示例3: TestFilterFields
# 需要导入模块: from mongo_connector.oplog_manager import OplogThread [as 别名]
# 或者: from mongo_connector.oplog_manager.OplogThread import filter_oplog_entry [as 别名]
#.........这里部分代码省略.........
self.assertIn(inc, keys)
self.assertNotIn(exc, keys)
def test_filter_exclude_oplog_entry(self):
# Test oplog entries: these are callables, since
# filter_oplog_entry modifies the oplog entry in-place
insert_op = lambda: {
"op": "i",
"o": {
"_id": 0,
"a": 1,
"b": 2,
"c": 3
}
}
update_op = lambda: {
"op": "u",
"o": {
"$set": {
"a": 4,
"b": 5
},
"$unset": {
"c": True
}
},
"o2": {
"_id": 1
}
}
# Case 0: insert op, no fields provided
self.opman.exclude_fields = None
filtered = self.opman.filter_oplog_entry(insert_op())
self.assertEqual(filtered, insert_op())
self.assertEqual(None, self.opman._projection)
# Case 1: insert op, fields provided
self.opman.exclude_fields = ['c']
filtered = self.opman.filter_oplog_entry(insert_op())
self.assertEqual(filtered['o'], {'_id': 0, 'a': 1, 'b': 2})
self.assertEqual({'c': 0}, self.opman._projection)
# Case 2: insert op, fields provided, doc becomes empty except for _id
self.opman.exclude_fields = ['a', 'b', 'c']
filtered = self.opman.filter_oplog_entry(insert_op())
self.assertEqual(filtered['o'], {'_id': 0})
self.assertEqual({'a': 0, 'b': 0, 'c': 0}, self.opman._projection)
# Case 3: update op, no fields provided
self.opman.exclude_fields = None
filtered = self.opman.filter_oplog_entry(update_op())
self.assertEqual(filtered, update_op())
self.assertEqual(None, self.opman._projection)
# Case 4: update op, fields provided
self.opman.exclude_fields = ['b']
filtered = self.opman.filter_oplog_entry(update_op())
self.assertNotIn('b', filtered['o']['$set'])
self.assertIn('a', filtered['o']['$set'])
self.assertEqual(filtered['o']['$unset'], update_op()['o']['$unset'])
self.assertEqual({'b': 0}, self.opman._projection)
# Case 5: update op, fields provided, empty $set
self.opman.exclude_fields = ['a', 'b']
filtered = self.opman.filter_oplog_entry(update_op())
示例4: test_exclude_fields_constructor
# 需要导入模块: from mongo_connector.oplog_manager import OplogThread [as 别名]
# 或者: from mongo_connector.oplog_manager.OplogThread import filter_oplog_entry [as 别名]
def test_exclude_fields_constructor(self):
# Test with the "_id" field in exclude_fields
exclude_fields = ["_id", "title", "content", "author"]
opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
dest_mapping_stru=self.dest_mapping_stru,
exclude_fields=exclude_fields
)
exclude_fields.remove('_id')
self._check_fields(opman, [], exclude_fields,
dict((f, 0) for f in exclude_fields))
extra_fields = exclude_fields + ['extra1', 'extra2']
filtered = opman.filter_oplog_entry(
{'op': 'i',
'o': dict((f, 1) for f in extra_fields)})['o']
self.assertEqual(dict((f, 1) for f in ['extra1', 'extra2']), filtered)
# Test without "_id" field included in exclude_fields
exclude_fields = ["title", "content", "author"]
opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
dest_mapping_stru=self.dest_mapping_stru,
exclude_fields=exclude_fields
)
self._check_fields(opman, [], exclude_fields,
dict((f, 0) for f in exclude_fields))
extra_fields = extra_fields + ['extra1', 'extra2']
filtered = opman.filter_oplog_entry(
{'op': 'i',
'o': dict((f, 1) for f in extra_fields)})['o']
self.assertEqual({'extra1': 1, 'extra2': 1}, filtered)
# Test with only "_id" field in exclude_fields
exclude_fields = ["_id"]
opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
dest_mapping_stru=self.dest_mapping_stru,
exclude_fields=exclude_fields
)
self._check_fields(opman, [], [], None)
extra_fields = exclude_fields + ['extra1', 'extra2']
filtered = opman.filter_oplog_entry(
{'op': 'i',
'o': dict((f, 1) for f in extra_fields)})['o']
self.assertEqual(dict((f, 1) for f in extra_fields), filtered)
# Test with nothing set for exclude_fields
opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
dest_mapping_stru=self.dest_mapping_stru,
exclude_fields=None
)
self._check_fields(opman, [], [], None)
extra_fields = ['_id', 'extra1', 'extra2']
filtered = opman.filter_oplog_entry(
{'op': 'i',
'o': dict((f, 1) for f in extra_fields)})['o']
self.assertEqual(dict((f, 1) for f in extra_fields), filtered)
示例5: TestFilterFields
# 需要导入模块: from mongo_connector.oplog_manager import OplogThread [as 别名]
# 或者: from mongo_connector.oplog_manager.OplogThread import filter_oplog_entry [as 别名]
class TestFilterFields(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.repl_set = ReplicaSetSingle().start()
cls.primary_conn = cls.repl_set.client()
cls.oplog_coll = cls.primary_conn.local["oplog.rs"]
@classmethod
def tearDownClass(cls):
cls.primary_conn.drop_database("test")
close_client(cls.primary_conn)
cls.repl_set.stop()
def setUp(self):
self.namespace_config = NamespaceConfig()
self.opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
namespace_config=self.namespace_config,
)
def tearDown(self):
try:
self.opman.join()
except RuntimeError:
# OplogThread may not have been started
pass
def reset_include_fields(self, fields):
self.opman.namespace_config = NamespaceConfig(include_fields=fields)
def reset_exclude_fields(self, fields):
self.opman.namespace_config = NamespaceConfig(exclude_fields=fields)
def test_filter_fields(self):
docman = self.opman.doc_managers[0]
conn = self.opman.primary_client
include_fields = ["a", "b", "c"]
exclude_fields = ["d", "e", "f"]
# Set fields to care about
self.reset_include_fields(include_fields)
# Documents have more than just these fields
doc = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "_id": 1}
db = conn["test"]["test"]
db.insert_one(doc)
assert_soon(lambda: db.count() == 1)
self.opman.dump_collection()
result = docman._search()[0]
keys = result.keys()
for inc, exc in zip(include_fields, exclude_fields):
self.assertIn(inc, keys)
self.assertNotIn(exc, keys)
def test_filter_exclude_oplog_entry(self):
# Test oplog entries: these are callables, since
# filter_oplog_entry modifies the oplog entry in-place
def insert_op():
return {"op": "i", "o": {"_id": 0, "a": 1, "b": 2, "c": 3}}
def update_op():
return {
"op": "u",
"o": {"$set": {"a": 4, "b": 5}, "$unset": {"c": True}},
"o2": {"_id": 1},
}
def filter_doc(document, fields):
if fields and "_id" in fields:
fields.remove("_id")
return self.opman.filter_oplog_entry(document, exclude_fields=fields)
# Case 0: insert op, no fields provided
filtered = filter_doc(insert_op(), None)
self.assertEqual(filtered, insert_op())
# Case 1: insert op, fields provided
filtered = filter_doc(insert_op(), ["c"])
self.assertEqual(filtered["o"], {"_id": 0, "a": 1, "b": 2})
# Case 2: insert op, fields provided, doc becomes empty except for _id
filtered = filter_doc(insert_op(), ["a", "b", "c"])
self.assertEqual(filtered["o"], {"_id": 0})
# Case 3: update op, no fields provided
filtered = filter_doc(update_op(), None)
self.assertEqual(filtered, update_op())
# Case 4: update op, fields provided
filtered = filter_doc(update_op(), ["b"])
self.assertNotIn("b", filtered["o"]["$set"])
self.assertIn("a", filtered["o"]["$set"])
self.assertEqual(filtered["o"]["$unset"], update_op()["o"]["$unset"])
# Case 5: update op, fields provided, empty $set
filtered = filter_doc(update_op(), ["a", "b"])
self.assertNotIn("$set", filtered["o"])
#.........这里部分代码省略.........
示例6: TestFilterFields
# 需要导入模块: from mongo_connector.oplog_manager import OplogThread [as 别名]
# 或者: from mongo_connector.oplog_manager.OplogThread import filter_oplog_entry [as 别名]
class TestFilterFields(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.repl_set = ReplicaSetSingle().start()
cls.primary_conn = cls.repl_set.client()
cls.oplog_coll = cls.primary_conn.local['oplog.rs']
@classmethod
def tearDownClass(cls):
cls.primary_conn.drop_database("test")
close_client(cls.primary_conn)
cls.repl_set.stop()
def setUp(self):
self.namespace_config = NamespaceConfig()
self.opman = OplogThread(
primary_client=self.primary_conn,
doc_managers=(DocManager(),),
oplog_progress_dict=LockingDict(),
namespace_config=self.namespace_config
)
def tearDown(self):
try:
self.opman.join()
except RuntimeError:
# OplogThread may not have been started
pass
def reset_include_fields(self, fields):
self.opman.namespace_config = NamespaceConfig(include_fields=fields)
def reset_exclude_fields(self, fields):
self.opman.namespace_config = NamespaceConfig(exclude_fields=fields)
def test_filter_fields(self):
docman = self.opman.doc_managers[0]
conn = self.opman.primary_client
include_fields = ["a", "b", "c"]
exclude_fields = ["d", "e", "f"]
# Set fields to care about
self.reset_include_fields(include_fields)
# Documents have more than just these fields
doc = {
"a": 1, "b": 2, "c": 3,
"d": 4, "e": 5, "f": 6,
"_id": 1
}
db = conn['test']['test']
db.insert_one(doc)
assert_soon(lambda: db.count() == 1)
self.opman.dump_collection()
result = docman._search()[0]
keys = result.keys()
for inc, exc in zip(include_fields, exclude_fields):
self.assertIn(inc, keys)
self.assertNotIn(exc, keys)
def test_filter_exclude_oplog_entry(self):
# Test oplog entries: these are callables, since
# filter_oplog_entry modifies the oplog entry in-place
insert_op = lambda: {
"op": "i",
"o": {
"_id": 0,
"a": 1,
"b": 2,
"c": 3
}
}
update_op = lambda: {
"op": "u",
"o": {
"$set": {
"a": 4,
"b": 5
},
"$unset": {
"c": True
}
},
"o2": {
"_id": 1
}
}
def filter_doc(document, fields):
if fields and '_id' in fields:
fields.remove('_id')
return self.opman.filter_oplog_entry(
document, exclude_fields=fields)
# Case 0: insert op, no fields provided
filtered = filter_doc(insert_op(), None)
self.assertEqual(filtered, insert_op())
#.........这里部分代码省略.........