def test_dict_conversion(self): """Test that a record can be converted into a dict structure.""" record = Record([ 'name', 'drink', 'city', 'recordId', 'modId', 'portal_notes', 'portal_addresses' ], ['David', 'Coffee', 'Hamburg', 1, 2, 'dummy', 'dummy2']) fake_dict = { 'name': 'David', 'drink': 'Coffee', 'city': 'Hamburg', 'recordId': 1, 'modId': 2, 'portal_notes': 'dummy', 'portal_addresses': 'dummy2' } self.assertEqual(record.to_dict(), fake_dict) # test without portals fake_dict.pop('portal_notes') fake_dict.pop('portal_addresses') self.assertEqual(record.to_dict(ignore_portals=True), fake_dict) # test without internal ids fake_dict.pop('recordId') fake_dict.pop('modId') self.assertEqual( record.to_dict(ignore_portals=True, ignore_internal_ids=True), fake_dict)
def test_key_value_mismatch_handling(self): """Test that Record cannot be initialized with a key-value length mismatch.""" with self.assertRaises(ValueError): Record(['key1', 'key2'], [1]) with self.assertRaises(ValueError): Record(['key1'], [1, 2])
def test_key_error_on_invalid_keys(self): """Test that trying to set a non-existing key will raise an error.""" record = Record(['name'], ['David']) with self.assertRaises(AttributeError): record.drink = 'Dr. Pepper' with self.assertRaises(KeyError): record['city'] = 'Hamburg'
def test_index_access(self) -> None: """Test that values in generator are accesible via their index. Values are cached, so we are actually testing that we can access the cached list.""" sample_gen = (record for record in [ Record(['name', 'recordId'], ['john doe', 1], True), Record(['name', 'recordId'], ['john smith', 2], True), Record(['name', 'recordId'], ['john wayne', 3], True) ]) foundset = Foundset(sample_gen) self.assertEqual(foundset[1].name, 'john smith') # Accessing an out of range index of cached values should raise IndexError with self.assertRaises(IndexError): foundset[3]
def test_get_record(self) -> None: """Test that get_record returns the Record value we are expecting.""" with self._fms as server: server.login() fake_record = Record(['name', 'drink'], ['Do not delete record 1', 'Coffee']) record = server.get_record(1) self.assertEqual(fake_record.name, record.name) self.assertEqual(fake_record.drink, record.drink)
def test_setting_portal_data_error(self): """Test that attempting to set portal data raises an error. Once supported, this test can be replaced by a test, that verifies portal data can be set. """ record = Record(['name', 'portal_notes'], ['David', 'dummy']) with self.assertRaises(KeyError): record['portal_notes'] = 1234
def test_generator_access(self): """Test that passed in generator is accessible""" sample_gen = (i for i in [Record(['name', 'recordId'], ['john doe', 1])]) foundset = Foundset(sample_gen) self.assertIsInstance(next(foundset), Record) with self.assertRaises(StopIteration): next(foundset)
def test_create_record_from_record_instance(self) -> None: """Create a record from a new record instance.""" record = Record(['name', 'drink'], ['David', 'Coffee']) with self._fms as server: server.login() record_id = server.create(record) self.assertIsInstance(record_id, int)
def test_key_access(self): """Test that Record keys and values can be accessed.""" assert_name = 'David' assert_drink = 'Coffee' record = Record(['name', 'drink'], [assert_name, assert_drink]) self.assertEqual(record.keys(), ['name', 'drink']) self.assertEqual(record.values(), [assert_name, assert_drink]) self.assertEqual(record.name, assert_name) self.assertEqual(record['name'], assert_name) self.assertEqual(record.drink, assert_drink) self.assertEqual(record['drink'], assert_drink) with self.assertRaises(KeyError): record['city'] with self.assertRaises(AttributeError): record.city
def test_dirty_record_flagging(self): """Test that a record gets flagged as dirty when you change its value.""" assert_name = 'David' assert_drink = 'Coffee' # setting the same value should not flag as dirty record = Record(['name', 'drink'], [assert_name, assert_drink]) record.name = 'David' self.assertFalse(record.is_dirty) # ... again for dict access record = Record(['name', 'drink'], [assert_name, assert_drink]) record['name'] = 'David' self.assertFalse(record.is_dirty) # now do change the value record = Record(['name', 'drink'], [assert_name, assert_drink]) record.name = 'Caspar' self.assertTrue(record.is_dirty) record = Record(['name', 'drink'], [assert_name, assert_drink]) record['name'] = 'Caspar' self.assertTrue(record.is_dirty)
def test_modification_tracking(self): """Test that record modifications are tracked.""" fake_modifications = {'drink': 'Dr. Pepper', 'city': 'New York'} record = Record(['name', 'drink', 'city'], ['David', 'Coffee', 'Hamburg']) record.name = 'David' # should not be flagged as it is the same value record.drink = 'Dr. Pepper' record.city = 'New York' self.assertEqual(fake_modifications, record.modifications())
def test_pop_values(self): """Test that we can pop values from the record.""" record = Record(['name', 'drink', 'city'], ['David', 'Coffee', 'Hamburg']) self.assertEqual(record.pop('drink'), 'Coffee') self.assertEqual(record.keys(), ['name', 'city']) self.assertEqual(record.values(), ['David', 'Hamburg']) self.assertEqual(record.pop('not existing'), None)
def test_delete_record_instance(self) -> None: with self._fms as server: server.login() # create dummy record record = Record(['name'], ['David']) new_record_id = server.create(record) # "hand-made" record not fetched from server should fail for deletion with self.assertRaises(RecordError): server.delete(record) # fetch record from server so that we have a valid record instance record = server.get_record(new_record_id) # test deletion deletion_result = server.delete(record) self.assertTrue(deletion_result)
def test_setting_class_slots(self): """Test that slots can be set w/o being intercepted and written to the modification dict.""" record = Record(['name'], ['David']) record._keys = ['drink'] self.assertIn('drink', record.keys())