def test_not_operator() -> None: field = "f" value = "1" operator = "=" filter = FilterOperator(field, operator, value) not_ = Not(filter) assert not_.to_dict() == {"not_filter": filter.to_dict()}
def test_filter_operator() -> None: filter = FilterOperator("f", "=", 1) assert filter.to_dict() == { "field": "f", "operator": "=", "value": 1, }
def test_complex_filter() -> None: collection = Collection("a") filter1 = FilterOperator(field="f", value="1", operator="=") filter2 = FilterOperator(field="f", value="3", operator="=") or_filter = Or([filter1, filter2]) found = db.filter(collection=collection, filter=or_filter) assert found is not None assert len(found) > 0
def test_filter_operator() -> None: field = "f" value = "1" operator = "=" filter = FilterOperator(field, operator, value) assert filter.to_dict() == { "field": field, "value": value, "operator": operator, }
def test_complex_operator() -> None: field1 = "f" value1 = "1" operator1 = "=" filter1 = FilterOperator(field1, operator1, value1) field2 = "f" value2 = "2" operator2 = "<" filter2 = FilterOperator(field2, operator2, value2) not_ = Not(filter2) or_ = Or(filter1, not_) assert or_.to_dict() == {"or_filter": [filter1.to_dict(), not_.to_dict()]}
def test_simple_filter(self) -> None: collection = Collection("a") field = "f" value = "1" operator = "=" filter = FilterOperator(field, operator, value) command = commands.Filter(collection=collection, filter=filter) self.engine.retrieve.return_value = ( json.dumps( { "1": {"f": 1, "meta_deleted": False, "meta_position": 1}, "5": {"f": 1, "meta_deleted": False, "meta_position": 5}, "6": {"f": 1, "meta_deleted": False, "meta_position": 6}, "7": {"f": 1, "meta_deleted": False, "meta_position": 7}, } ), 200, ) found = self.db.filter(collection=collection, filter=filter) assert found is not None assert command.get_raw_data() == { "collection": str(collection), "filter": {"field": field, "operator": operator, "value": value}, } self.engine.retrieve.called_with("filter", command.data)
def test_use_changed_models_multiple_models(self) -> None: self.set_additional_models({ "test/1": { "a": 2, "weight": 10 }, "test/2": { "a": 2, "weight": 20 }, "test/3": { "a": 2, "weight": 30 }, "test/4": { "a": 3, "weight": 1 }, }) result = self.adapter.min( Collection("test"), FilterOperator("a", "=", 2), "weight", ) assert result == 10 self.db_method_mock.assert_not_called() self.filter_mock.assert_called() self.add_filter_mock.assert_called()
def test_count(self) -> None: collection = Collection("a") field = "f" value = "1" operator = "=" filter = FilterOperator(field=field, value=value, operator=operator) command = commands.Count(collection=collection, filter=filter) self.engine.retrieve.return_value = ( json.dumps({ "count": True, "position": 1 }), 200, ) count = self.db.count(collection=collection, filter=filter) assert count is not None assert command.get_raw_data() == { "collection": str(collection), "filter": { "field": field, "operator": operator, "value": value }, } self.engine.retrieve.called_with("count", command.data)
def test_max(self) -> None: collection = Collection("a") field = "f" value = "1" operator = "=" filter = FilterOperator(field=field, value=value, operator=operator) command = commands.Max(collection=collection, filter=filter, field=field) self.engine.retrieve.return_value = json.dumps({ "max": 1, "position": 1 }), 200 agg = self.db.max(collection=collection, filter=filter, field=field) assert agg is not None assert command.get_raw_data() == { "collection": str(collection), "filter": { "field": field, "operator": operator, "value": value }, "field": field, } self.engine.retrieve.called_with("max", command.data)
def test_use_changed_models_missing_fields(self) -> None: self.set_additional_models({ "test/1": { "a": 3, "f": 2 }, "test/2": { "a": 3 }, }) self.filter_return_value = {} self.mock_datastore_content = {self.collection: {2: {"f": 17}}} result = self.adapter.filter( self.collection, FilterOperator("a", "=", 3), ["f"], ) assert result == { 1: { "f": 2 }, 2: { "f": 17 }, } self.filter_mock.assert_called() self.add_filter_mock.assert_called() self.get_many_mock.assert_called()
def test_use_changed_models(self) -> None: self.set_additional_models({ "test/2": { "a": 2, "f": 3, "weight": 50 }, "test/3": { "a": 2, "weight": 42 }, }) result = self.adapter.filter( self.collection, FilterOperator("a", "=", 2), ["f", "weight"], ) assert result == { 1: { "f": 1 }, 2: { "f": 3, "weight": 50 }, 3: { "weight": 42 }, } self.filter_mock.assert_called() self.add_filter_mock.assert_called()
def assert_model_count(self, collection: str, meeting_id: int, count: int) -> None: db_count = self.datastore.count( Collection(collection), FilterOperator("meeting_id", "=", meeting_id), lock_result=False, ) self.assertEqual(db_count, count)
def test_max() -> None: collection = Collection("a") field = "f" value = "1" operator = "=" filter = FilterOperator(field=field, value=value, operator=operator) agg = db.max(collection=collection, filter=filter, field=field) assert agg is not None
def test_exists() -> None: collection = Collection("a") field = "f" value = "1" operator = "=" filter = FilterOperator(field=field, value=value, operator=operator) found = db.exists(collection=collection, filter=filter) assert found is not None assert found["exists"]
def test_use_changed_models_empty(self) -> None: result = self.adapter.count( Collection("test"), FilterOperator("a", "=", 2), ) assert result == 0 self.db_method_mock.assert_not_called() self.filter_mock.assert_called() self.add_filter_mock.assert_called()
def test_count() -> None: collection = Collection("a") field = "f" value = "1" operator = "=" filter = FilterOperator(field=field, value=value, operator=operator) count = db.count(collection=collection, filter=filter) assert count is not None assert count["count"] > 0
def test_sql_to_filter_code_simple() -> None: ds = ExtendedDatastoreAdapter(MagicMock(), MagicMock()) with patch("openslides_backend.services.datastore.extended_adapter.eval" ) as mock: ds._filter_changed_models(MagicMock(), FilterOperator("test", "=", 1), []) assert ( mock.call_args[0][0] == "{model['id']: {field: model[field] for field in mapped_fields if field in model} for fqid, model in self.changed_models.items() if fqid.collection == collection and (model.get(\"test\") == 1)}" )
def test_complex_filter() -> None: collection = Collection("a") filter1 = FilterOperator(field="f", value="1", operator="=") filter2 = FilterOperator(field="f", value="3", operator="=") or_filter = Or([filter1, filter2]) command = commands.Filters(collection=collection, filter=or_filter) engine.filter.return_value = [ {"f": 1, "meta_deleted": False, "meta_position": 1}, {"f": 3, "meta_deleted": False, "meta_position": 4}, {"f": 1, "meta_deleted": False, "meta_position": 6}, {"f": 1, "meta_deleted": False, "meta_position": 7}, ] found = db.filter(collection=collection, filter=or_filter) assert found is not None assert command.data == { "collection": str(collection), "filter": or_filter.to_dict(), } engine.filter.called_with(command)
def test_complex_filter(self) -> None: collection = Collection("a") filter1 = FilterOperator("f", "=", "1") filter2 = FilterOperator("f", "=", "3") or_filter = Or(filter1, filter2) command = commands.Filter(collection=collection, filter=or_filter) self.engine.retrieve.return_value = ( json.dumps({ "position": 1, "data": { "1": { "f": 1, "meta_deleted": False, "meta_position": 1 }, "4": { "f": 3, "meta_deleted": False, "meta_position": 4 }, "6": { "f": 1, "meta_deleted": False, "meta_position": 6 }, "7": { "f": 1, "meta_deleted": False, "meta_position": 7 }, }, }), 200, ) found = self.db.filter(collection=collection, filter=or_filter, lock_result=False) assert found is not None assert command.get_raw_data() == { "collection": str(collection), "filter": or_filter.to_dict(), } self.engine.retrieve.called_with("filter", command.data)
def test_use_changed_models_none(self) -> None: self.set_additional_models({"test/1": {"a": 2}}) result = self.adapter.max( Collection("test"), FilterOperator("a", "=", 2), "weight", ) assert result is None self.db_method_mock.assert_not_called() self.filter_mock.assert_called() self.add_filter_mock.assert_called()
def test_only_db(self) -> None: self.set_additional_models({"test/1": {"a": 2}}) result = self.adapter.count( Collection("test"), FilterOperator("a", "=", 2), use_changed_models=False, ) assert result == 42 self.db_method_mock.assert_called() self.filter_mock.assert_not_called() self.add_filter_mock.assert_not_called()
def test_sql_to_filter_code_complex() -> None: ds = ExtendedDatastoreAdapter(MagicMock(), MagicMock()) operator = FilterOperator("test", "=", 1) _filter = Or(operator, And(operator, Not(operator))) with patch("openslides_backend.services.datastore.extended_adapter.eval" ) as mock: ds._filter_changed_models(MagicMock(), _filter, []) assert ( mock.call_args[0][0] == '{model[\'id\']: {field: model[field] for field in mapped_fields if field in model} for fqid, model in self.changed_models.items() if fqid.collection == collection and ((model.get("test") == 1) or ((model.get("test") == 1) and (not (model.get("test") == 1))))}' )
def test_complex_filter(self) -> None: collection = Collection("a") filter1 = FilterOperator(field="f", value="1", operator="=") filter2 = FilterOperator(field="f", value="3", operator="=") or_filter = Or([filter1, filter2]) command = commands.Filter(collection=collection, filter=or_filter) self.engine.retrieve.return_value = ( json.dumps([ { "f": 1, "meta_deleted": False, "meta_position": 1 }, { "f": 3, "meta_deleted": False, "meta_position": 4 }, { "f": 1, "meta_deleted": False, "meta_position": 6 }, { "f": 1, "meta_deleted": False, "meta_position": 7 }, ]), 200, ) found = self.db.filter(collection=collection, filter=or_filter) assert found is not None assert command.get_raw_data() == { "collection": str(collection), "filter": or_filter.to_dict(), } self.engine.retrieve.called_with("filter", command.data)
def test_use_changed_models_db_higher(self) -> None: self.filter_return_value = { 2: {"a": 2, "weight": 100}, } self.set_additional_models({"test/1": {"a": 2, "weight": 50}}) result = self.adapter.max( Collection("test"), FilterOperator("a", "=", 2), "weight", ) assert result == 100 self.db_method_mock.assert_not_called() self.filter_mock.assert_called() self.add_filter_mock.assert_called()
def test_count() -> None: collection = Collection("a") field = "f" value = "1" operator = "=" filter = FilterOperator(field=field, value=value, operator=operator) command = commands.Count(collection=collection, filter=filter) engine.count.return_value = {"count": True, "position": 1} count = db.count(collection=collection, filter=filter) assert count is not None assert command.data == { "collection": str(collection), "filter": {"field": field, "operator": operator, "value": value}, } engine.exists.called_with(command)
def test_use_changed_models_not_comparable(self) -> None: self.set_additional_models( { "test/1": {"a": 2, "weight": 1}, "test/2": {"a": "nop", "weight": 2}, } ) result = self.adapter.max( Collection("test"), FilterOperator("a", ">=", 2), "weight", ) assert result == 1 self.db_method_mock.assert_not_called() self.filter_mock.assert_called() self.add_filter_mock.assert_called()
def test_max() -> None: collection = Collection("a") field = "f" value = "1" operator = "=" filter = FilterOperator(field=field, value=value, operator=operator) command = commands.Max(collection=collection, filter=filter, field=field) engine.max.return_value = {"max": 1, "position": 1} agg = db.max(collection=collection, filter=filter, field=field) assert agg is not None assert command.data == { "collection": str(collection), "filter": {"field": field, "operator": operator, "value": value}, "field": field, } engine.exists.called_with(command)
def test_use_changed_models_deleted(self) -> None: self.set_additional_models({ "test/1": DeletedModel(), }) result = self.adapter.filter( self.collection, FilterOperator("a", "=", 2), ["f"], ) assert result == { 2: { "f": 1 }, } self.filter_mock.assert_called() self.add_filter_mock.assert_called()
def test_or_operator() -> None: field1 = "f" value1 = "1" operator1 = "=" filter1 = FilterOperator(field1, operator1, value1) field2 = "f" value2 = "2" operator2 = "<" filter2 = FilterOperator(field2, operator2, value2) or_ = Or(filter1, filter2) assert or_.to_dict() == {"or_filter": [filter1.to_dict(), filter2.to_dict()]}
def test_and_operator() -> None: field1 = "f" value1 = "1" operator1 = "=" filter1 = FilterOperator(field1, operator1, value1) field2 = "f" value2 = "2" operator2 = "<" filter2 = FilterOperator(field2, operator2, value2) and_ = And(filter1, filter2) assert and_.to_dict() == {"and_filter": [filter1.to_dict(), filter2.to_dict()]}