def test_input(self, text, language=None, term_type=None, entities=None): """ Test that the user input is valid. @param text: the text to search for @type text: basestring @param language: the language to search in, defaults to None=any @type language: basestring or None @param entities: the language to search in, defaults to None=any @type entities: list (of basestring), or None @param term_type: field to search in, defaults to None=['label', 'alias'] @type term_type: basestring or None @return: if input is valid @rtype: bool """ # test text if not text.strip(): self._print('You cannot send stringSearch an empty string') return False # test language if language and language not in self.languages: self._print('{} is not a recognised language'.format(language)) return False # test term_type if term_type and term_type not in self.term_types: self._print('{} is not a recognised term_type'.format(term_type)) return False # test list of entities if entities: if not isinstance(entities, (tuple, list)): self._print('Entities must be a non-zero list') return False # Check each is correctly formatted if not all(e.startswith('Q') and helpers.is_str(e) and helpers.is_int(e[1:]) for e in entities): self._print('Each entity must be a string like Q<integer>') return False # nothing flagged return True
def test_input(self, text, language=None, term_type=None, entities=None): """ Test that the user input is valid. @param text: the text to search for @type text: basestring @param language: the language to search in, defaults to None=any @type language: basestring or None @param entities: the language to search in, defaults to None=any @type entities: list (of basestring), or None @param term_type: field to search in, defaults to None=['label', 'alias'] @type term_type: basestring or None @return: if input is valid @rtype: bool """ # test text if not text.strip(): self._print('You cannot send stringSearch an empty string') return False # test language if language and language not in self.languages: self._print('{} is not a recognised language'.format(language)) return False # test term_type if term_type and term_type not in self.term_types: self._print('{} is not a recognised term_type'.format(term_type)) return False # test list of entities if entities: if not isinstance(entities, (tuple, list)): self._print('Entities must be a non-zero list') return False # Check each is correctly formatted if not all( e.startswith('Q') and helpers.is_str(e) and helpers.is_int(e[1:]) for e in entities): self._print('Each entity must be a string like Q<integer>') return False # nothing flagged return True
def test_is_int_valid_int_succeed(self): s = '123' result = is_int(s) self.assertEqual(result, True)
def test_is_int_float_fail(self): s = '123.456' result = is_int(s) self.assertEqual(result, False)
def test_is_int_random_string_fail(self): s = 'random_string' result = is_int(s) self.assertEqual(result, False)
def test_is_int_none_fail(self): s = None result = is_int(s) self.assertEqual(result, False)
def test_is_int_empty_string_fail(self): s = '' result = is_int(s) self.assertEqual(result, False)