def test__get_primary_keys_raise_value_error_field_not_id(self): """Test a ValueError is raised when generator is None and field type not id.""" # Setup sampler = Mock(spec=Sampler) sampler.metadata = Mock(spec=Metadata) sampler.metadata.get_primary_key.return_value = 'pk_field' sampler.metadata.get_fields.return_value = { 'pk_field': { 'type': 'not id' } } sampler.primary_key = {'test': None} # Run with pytest.raises(ValueError): Sampler._get_primary_keys(sampler, 'test', 5)
def test__get_primary_keys_raises_not_implemented_error_datetime(self): """Test a NotImplementedError is raised when pk field is datetime.""" # Setup sampler = Mock(spec=Sampler) sampler.metadata = Mock(spec=Metadata) sampler.metadata.get_primary_key.return_value = 'pk_field' sampler.metadata.get_fields.return_value = { 'pk_field': { 'type': 'id', 'subtype': 'datetime' } } sampler.primary_key = {'test': None} # Run with pytest.raises(NotImplementedError): Sampler._get_primary_keys(sampler, 'test', 5)
def test__get_primary_keys_raise_value_error_field_not_supported(self): """Test a ValueError is raised when a field subtype is not supported.""" # Setup sampler = Mock(spec=Sampler) sampler.metadata = Mock(spec=Metadata) sampler.metadata.get_primary_key.return_value = 'pk_field' sampler.metadata.get_fields.return_value = { 'pk_field': { 'type': 'id', 'subtype': 'X' } } sampler.primary_key = {'test': None} # Run with pytest.raises(ValueError): Sampler._get_primary_keys(sampler, 'test', 5)
def test__get_primary_keys_raises_value_error_remaining(self): """Test a ValueError is raised when there are not enough uniques values""" # Setup sampler = Mock(spec=Sampler) sampler.metadata = Mock(spec=Metadata) sampler.metadata.get_primary_key.return_value = 'pk_field' sampler.metadata.get_fields.return_value = { 'pk_field': { 'type': 'id', 'subtype': 'datetime' } } sampler.primary_key = {'test': 'generator'} sampler.remaining_primary_key = {'test': 4} # Run with pytest.raises(ValueError): Sampler._get_primary_keys(sampler, 'test', 5)
def test__get_primary_keys_none(self): """Test returns a tuple of none when a table doesn't have a primary key""" # Setup sampler = Mock(spec=Sampler) sampler.metadata = Mock(spec=Metadata) sampler.metadata.get_primary_key.return_value = None # Run result = Sampler._get_primary_keys(sampler, 'test', 5) # Asserts assert result == (None, None)
def test__get_primary_keys_raises_error(self): """_get_primary_keys raises an exception if there aren't enough values.""" # Setup data_navigator = MagicMock(spec=DataNavigator) data_navigator.get_meta_data.return_value = { 'primary_key': 'table_pk', 'fields': { 'table_pk': { 'regex': 'regex for table_pk', 'type': 'number', 'subtype': 'integer' }, } } modeler = MagicMock(spec=Modeler) sampler = Sampler(data_navigator=data_navigator, modeler=modeler) sampler.primary_key['table'] = 'a generator' sampler.remaining_primary_key['table'] = 0 # Run / Check with self.assertRaises(ValueError): sampler._get_primary_keys('table', 5)
def test__get_primary_keys_no_pk(self): """If no primary key, _get_primary_keys return a duple of None """ # Setup data_navigator = MagicMock(spec=DataNavigator) data_navigator.get_meta_data.return_value = {} modeler = MagicMock(spec=Modeler) sampler = Sampler(data_navigator=data_navigator, modeler=modeler) # Run result = sampler._get_primary_keys('table', 5) # Check primary_key, primary_key_values = result assert primary_key is None assert primary_key_values is None
def test__get_primary_keys_create_generator(self, exrex_gen_mock, exrex_count_mock): """If there's a primary key, but no generator, a new one is created and used.""" # Setup data_navigator = MagicMock(spec=DataNavigator) data_navigator.get_meta_data.return_value = { 'primary_key': 'table_pk', 'fields': { 'table_pk': { 'regex': 'regex for table_pk', 'type': 'number', 'subtype': 'integer' }, } } modeler = MagicMock(spec=Modeler) sampler = Sampler(data_navigator=data_navigator, modeler=modeler) exrex_gen_mock.return_value = (str(x) for x in range(10)) exrex_count_mock.return_value = 10 expected_primary_key = 'table_pk' expected_primary_key_values = pd.Series(range(5)) # Run result = sampler._get_primary_keys('table', 5) # Check primary_key, primary_key_values = result assert primary_key == expected_primary_key primary_key_values.equals(expected_primary_key_values) assert sampler.primary_key['table'] == exrex_gen_mock.return_value assert sampler.remaining_primary_key['table'] == 5 data_navigator.get_meta_data.assert_called_once_with('table') exrex_count_mock.assert_called_once_with('regex for table_pk') exrex_gen_mock.assert_called_once_with('regex for table_pk')