"""Test utils file.""" import pytest from permissions.types import TenantType from permissions.utils import db_entities def test_node_to_dict_success(make_graph_node): """Test node_to_dict with valid node.""" node_id = 100 labels = [ 'label1', ] params = {'foo': 'bar'} data = make_graph_node(node_id=node_id, labels=labels, data=params) actual = db_entities.node_to_dict(data) assert actual == {'type': labels[0], 'foo': 'bar', 'id': 100} @pytest.mark.parametrize( ('labels', 'expected'), [ (('LabelProfile',), {'profile_type': 'LabelProfile', 'foo': 'bar', 'id': 100}), (('Vendor', 'Label', 'Orchard'), {'type': 'Vendor', 'foo': 'bar', 'id': 100}), (('ArtistInfo',), {'type': 'ArtistInfo', 'foo': 'bar', 'id': 100}), (('Subaccount',), {'type': 'Subaccount', 'foo': 'bar', 'id': 100}), ], ) def test_node_to_dict_profile_type(labels, expected, make_graph_node): """Test node_to_dict with resource and profile type label.""" node_id = 100 params = {'foo': 'bar'} data = make_graph_node(node_id=node_id, labels=labels, data=params) actual = db_entities.node_to_dict(data) assert actual == expected def test_node_to_dict_minimum(make_graph_node): """Test node_to_dict with just node id.""" node_id = 100 data = make_graph_node(node_id=node_id) actual = db_entities.node_to_dict(data) assert actual == {'type': '', 'id': 100} def test_node_to_dict_kwargs(make_graph_node): """Test node_to_dict with kwargs param.""" node_id = 100 data = make_graph_node(node_id=node_id) actual = db_entities.node_to_dict(data, foo='bar', something='else') assert actual == {'type': '', 'id': 100, 'foo': 'bar', 'something': 'else'} def test_node_to_dict_invalid_node(): """Test node_to_dict with invalid input object.""" with pytest.raises(AttributeError): db_entities.node_to_dict({'foo': 'bar'}) @pytest.mark.parametrize( ('value', 'expected'), [ ('100', 100), ('10.5', '10.5'), ('10.00', '10.00'), ('', ''), (None, None), ], ) def test_cast_numeric_string_to_int(value, expected): """Test cast_numeric_string_to_int.""" actual = db_entities.cast_numeric_string_to_int(value) assert actual == expected @pytest.mark.parametrize( 'labels,expected_type', [ (frozenset({'Label', 'Orchard', 'Vendor'}), TenantType.ACCOUNT), (frozenset({'SubAccount', 'Subaccount', 'Orchard'}), TenantType.SUBACCOUNT), (frozenset({'LabelParticipant', 'Orchard'}), TenantType.LABEL_PARTICIPANT), (frozenset({'Collaborator'}), TenantType.COLLABORATOR), ], ) def test_get_tenant_type_from_neo4j_labels(labels, expected_type): """Test get_tenant_type_from_neo4j_labels.""" result = db_entities.get_tenant_type_from_neo4j_labels(labels) assert result == expected_type