def test_get_search_query_with_base_query_and_term(self): """ Test getting a search query providing a base query and a search term. :return: A query is returned that filters exactly by the search term. """ role_1 = Role(name='Administrator') role_2 = Role(name='Guest') role_3 = Role(name='User') role_4 = Role(name='Author') db.session.add(role_1) db.session.add(role_2) db.session.add(role_3) db.session.add(role_4) db.session.commit() base_query = Role.query.order_by(Role._name.desc()) # Matching term. query = Role.get_search_query(query=base_query, search_term='A*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_4, role_1], roles) # Test that a different result is returned without the given base query. query = Role.get_search_query(search_term='A*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_4], roles)
def test_get_search_query_with_term_no_wildcards(self): """ Test getting a search query providing a search term without wildcards. :return: A query is returned that filters exactly by the search term. """ role_1 = Role(name='Administrator') role_2 = Role(name='Guest') role_3 = Role(name='User') role_4 = Role(name='Author') db.session.add(role_1) db.session.add(role_2) db.session.add(role_3) db.session.add(role_4) db.session.commit() # Matching term. query = Role.get_search_query(search_term='Administrator') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1], roles) # Not-matching term. query = Role.get_search_query(search_term='Editor') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([], roles) # Partially matching term, but no wildcards, thus no result. query = Role.get_search_query(search_term='A') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([], roles)
def test_get_search_query_with_base_query_and_term(self): """ Test getting a search query providing a base query and a search term. :return: A query is returned that filters exactly by the search term. """ role_1 = Role(name='Administrator') role_2 = Role(name='Guest') role_3 = Role(name='User') role_4 = Role(name='Author') db.session.add(role_1) db.session.add(role_2) db.session.add(role_3) db.session.add(role_4) db.session.commit() base_query = Role.query.order_by(Role._name.desc()) # Matching term. query = Role.get_search_query(query=base_query, search_term='A*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_4, role_1], roles) # Test that a different result is returned without the given base query. query = Role.get_search_query(search_term='A*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_4], roles)
def test_get_search_query_with_term_no_wildcards(self): """ Test getting a search query providing a search term without wildcards. :return: A query is returned that filters exactly by the search term. """ role_1 = Role(name='Administrator') role_2 = Role(name='Guest') role_3 = Role(name='User') role_4 = Role(name='Author') db.session.add(role_1) db.session.add(role_2) db.session.add(role_3) db.session.add(role_4) db.session.commit() # Matching term. query = Role.get_search_query(search_term='Administrator') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1], roles) # Not-matching term. query = Role.get_search_query(search_term='Editor') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([], roles) # Partially matching term, but no wildcards, thus no result. query = Role.get_search_query(search_term='A') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([], roles)
def test_get_search_query_no_term(self): """ Test getting a search query without providing a search term. :return: A query is returned that does not filter. """ role_1 = Role(name='Administrator') role_2 = Role(name='Guest') role_3 = Role(name='User') role_4 = Role(name='Author') db.session.add(role_1) db.session.add(role_2) db.session.add(role_3) db.session.add(role_4) db.session.commit() result = [ role_1, role_2, role_3, role_4, ] query = Role.get_search_query() self.assertIsNotNone(query) roles = query.all() self.assertListEqual(result, roles)
def test_get_search_query_no_term(self): """ Test getting a search query without providing a search term. :return: A query is returned that does not filter. """ role_1 = Role(name='Administrator') role_2 = Role(name='Guest') role_3 = Role(name='User') role_4 = Role(name='Author') db.session.add(role_1) db.session.add(role_2) db.session.add(role_3) db.session.add(role_4) db.session.commit() result = [ role_1, role_2, role_3, role_4, ] query = Role.get_search_query() self.assertIsNotNone(query) roles = query.all() self.assertListEqual(result, roles)
def test_get_search_query_with_term_wildcards(self): """ Test getting a search query providing a search term without wildcards. :return: A query is returned that filters by the search term allowing for partial matches. """ role_1 = Role(name='Administrator') role_2 = Role(name='Guest') role_3 = Role(name='User') role_4 = Role(name='Author') role_5 = Role(name='Assistant') db.session.add(role_1) db.session.add(role_2) db.session.add(role_3) db.session.add(role_4) db.session.add(role_5) db.session.commit() # Matching term. query = Role.get_search_query(search_term='*Administrator*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1], roles) # Partially matching term with wildcard at the end. query = Role.get_search_query(search_term='A*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_4, role_5], roles) # Partially matching term with wildcard at the front. query = Role.get_search_query(search_term='*r') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_3, role_4], roles) # Partially matching term with wildcard in the middle. query = Role.get_search_query(search_term='A*r') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_4], roles) # Partially matching term with wildcard at the front and end, case-insensitive. query = Role.get_search_query(search_term='*u*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_2, role_3, role_4], roles) # Wildcard term matching everything. query = Role.get_search_query(search_term='*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_2, role_3, role_4, role_5], roles) # Wildcard term matching nothing. query = Role.get_search_query(search_term='E*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([], roles)
def test_get_search_query_with_term_wildcards(self): """ Test getting a search query providing a search term without wildcards. :return: A query is returned that filters by the search term allowing for partial matches. """ role_1 = Role(name='Administrator') role_2 = Role(name='Guest') role_3 = Role(name='User') role_4 = Role(name='Author') role_5 = Role(name='Assistant') db.session.add(role_1) db.session.add(role_2) db.session.add(role_3) db.session.add(role_4) db.session.add(role_5) db.session.commit() # Matching term. query = Role.get_search_query(search_term='*Administrator*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1], roles) # Partially matching term with wildcard at the end. query = Role.get_search_query(search_term='A*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_4, role_5], roles) # Partially matching term with wildcard at the front. query = Role.get_search_query(search_term='*r') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_3, role_4], roles) # Partially matching term with wildcard in the middle. query = Role.get_search_query(search_term='A*r') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_4], roles) # Partially matching term with wildcard at the front and end, case-insensitive. query = Role.get_search_query(search_term='*u*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_2, role_3, role_4], roles) # Wildcard term matching everything. query = Role.get_search_query(search_term='*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([role_1, role_2, role_3, role_4, role_5], roles) # Wildcard term matching nothing. query = Role.get_search_query(search_term='E*') self.assertIsNotNone(query) roles = query.all() self.assertListEqual([], roles)
def roles_list() -> str: """ Show a list of all roles. :return: The HTML response. """ # Get a search term and the resulting query. If no search term is given, all roles will by returned. search_form = SearchForm() role_query = Role.get_search_query(search_term=search_form.search_term) # noinspection PyProtectedMember pagination = RolePagination(role_query.order_by(Role._name)) title = _('Roles') return render_template('administration/roles.html', title=title, pagination=pagination, search_form=search_form)
def roles_list() -> ResponseType: """ Show a list of all roles. :return: The response for this view. """ # Get a search term and the resulting query. If no search term is given, all roles will by returned. search_form = SearchForm() role_query = Role.get_search_query(search_term=search_form.search_term) # noinspection PyProtectedMember pagination = RolePagination(role_query.order_by(Role._name)) title = _('Roles') return render_template('administration/roles.html', title=title, pagination=pagination, search_form=search_form)