def get_full_filter_predicate( ids: Iterable[int], components: Iterable[str], search_string: str, to_str: Callable[[Any], str] = str, ) -> predicates.predicate: """ Returns a Test API predicate to only get recent data from the specified IDs/components, and containing the given search string. If any of these are left blank, no restrictions are assumed for that field. :param ids: A list of possible ID values to accept (empty if we should accept SysData with any ID) :param components: A list of possible components to accept (empty if we should accept SysData from any component) :param search_string: The substring to look for in the given object :param to_str: An optional function for converting a given object to a string :return: A predicate that only accepts SysData objects with the given IDs, components, and search term """ return_all = predicates.always_true() id_pred = get_id_predicate(ids) comp_pred = get_component_predicate(components) search_pred = get_search_predicate(search_string, to_str) return predicates.satisfies_all( [return_all, id_pred, comp_pred, search_pred])
def test_get_item_list_with_empty_item_dictionary(): item_list = test_api_utils.get_item_list( item_dictionary={}, search_filter=predicates.always_true(), template_to_data=t2d) assert item_list == []
def get_component_predicate(components: Iterable[str]) -> predicates.predicate: """ Returns a Test API predicate that only accepts items from one of the given components (if no components are given, accept all items). :param components: A list of possible components to accept (empty if we should accept SysData from any component) :return: A predicate that checks if a SysData object come from one of the given components """ if components: component_preds = [component_predicate(comp) for comp in components] return predicates.satisfies_any(component_preds) return predicates.always_true()
def get_id_predicate(ids: Iterable[int]) -> predicates.predicate: """ Returns a Test API predicate that only accepts items with one of the given type IDs (if no IDs are given, accept all items). :param ids: A list of possible ID values to accept (empty if we should accept SysData with any ID) :return: A predicate that checks if a SysData object has one of the given IDs """ if ids: id_preds = [id_predicate(id_num) for id_num in ids] return predicates.satisfies_any(id_preds) return predicates.always_true()
def get_search_predicate( search_string: str, to_str: Callable[[Any], str] = str) -> predicates.predicate: """ Returns a Test API predicate that only accepts items whose string representation contains the exact given term. :param search_string: The substring to look for in the given object :param to_str: An optional function for converting a given object to a string :return: A predicate that checks if an object contains "search_string" when it's converted to a string """ if search_string: return contains_search_string(search_string, to_str) return predicates.always_true()
def __init__(self, filter_pred=None): """ Constructor used to set-up history. If the history is given a filter, it will ignore (drop) objects that don't satisfy the filter predicate. Args: filter_pred: an optional predicate to filter incoming data_objects """ self.objects = [] self.filter = predicates.always_true() if filter_pred is not None: if predicates.is_predicate(filter_pred): self.filter = filter_pred else: raise TypeError( "The given filter was an instance of predicate.") self.retrieved_cursor = 0
def test_valid_get_item_list(item_template_dictionary): original_dict = deepcopy(item_template_dictionary) item_list = test_api_utils.get_item_list( item_dictionary=item_template_dictionary, search_filter=predicates.always_true(), template_to_data=t2d, ) # Test all items present AND in ID-sorted order assert_equal_data_lists( item_list, [ t2d(item_template_dictionary["SURPRISE!"]), t2d(item_template_dictionary["12"]), t2d(item_template_dictionary["34"]), t2d(item_template_dictionary["56"]), t2d(item_template_dictionary["78"]), ], ) # Check nothing changed in the original dictionary assert_equal_template_dictionaries(item_template_dictionary, original_dict)
def test_invert(self): pred = predicates.invert(predicates.always_true()) assert not pred(1), "numbers are not true" assert not pred("string"), "strings are not true" assert not pred(object()), "an object is not true" self.check_str(pred)
def test_always_true(self): pred = predicates.always_true() assert pred(1), "numbers are true" assert pred("string"), "strings are true" assert pred(object()), "an object is true" self.check_str(pred)