"""Unit tests for filters.py.""" import collections import pytest from features import filters from features.constants import context as context_constants def test_get_filter(): """Test getting an existing filter.""" for filter_name in filters._yaml_to_filter_map.keys(): assert isinstance(filters.get(filter_name), collections.abc.Callable) def test_get_from_context(): """Test getting filter using context map.""" for context_key in filters._context_to_yaml_map.keys(): assert isinstance( filters.get_from_context(context_key), collections.abc.Callable ) def test_get_unexisting_filter(): """Test getting a filter that does not exist.""" with pytest.raises(filters.FilterNotFoundException): filters.get('unexisting filter') def test_filter_user_id(): """Test the filter user id.""" values = ['id1', 'id2'] assert not filters.filter_user_id( values, { context_constants.USER_ID: 'foo', context_constants.VENDOR_ID: 'doesnt matter', }, ) for value in values: assert filters.filter_user_id( values, { context_constants.USER_ID: value, context_constants.VENDOR_ID: 'doesnt matter', }, ) def test_filter_vendor_id_no_match(): """Filter vendor ID returns False for non-matching context.""" values = ['1', '2'] assert not filters.filter_vendor_id( values, { context_constants.USER_ID: 'doesnt matter', context_constants.VENDOR_ID: 'not a value', }, ) def test_filter_vendor_id_match(): """Filter vendor ID returns True for matching context.""" values = ['1', '2'] for value in values: assert filters.filter_vendor_id( values, { context_constants.USER_ID: 'doesnt matter', context_constants.VENDOR_ID: value, }, ) def test_filter_profile_id(): """Test the filter profile id.""" values = ['AristProfile:111', 'ArtistProfile:112'] assert not filters.filter_profile_id( values, { context_constants.PROFILE_TYPE_AND_ID: 'foo', context_constants.IDENTITY_ID: 'whatever', }, ) for value in values: assert filters.filter_profile_id( values, { context_constants.PROFILE_TYPE_AND_ID: value, context_constants.IDENTITY_ID: 'whatever', }, ) def test_filter_identity_id(): """Test the filter identity id.""" values = ['5cd2dd3390c6cb0f6927b8c8', '5bfd388b43c05c4f27222cdd'] assert not filters.filter_identity_id( values, { context_constants.PROFILE_TYPE_AND_ID: 'whatever', context_constants.IDENTITY_ID: 'foo', }, ) for value in values: assert filters.filter_identity_id( values, { context_constants.PROFILE_TYPE_AND_ID: 'whatever', context_constants.IDENTITY_ID: value, }, )