def test_unknown_adapter_raises_exception(self): registry_manager = adapters.AdapterRegistryManager() registry_manager.register_adapter( adapters.ModelAdapter(rest_model_class=RESTModelA, persistent_model_class=PersistentModelA)) # fetch via the REST model found_adapter = registry_manager.get_adapter_for_rest_model( RESTModelA()) self.assertEqual(found_adapter.rest_model_class, RESTModelA) self.assertEqual(found_adapter.persistent_model_class, PersistentModelA) # fetch via the persistent model found_adapter = registry_manager.get_adapter_for_persistent_model( PersistentModelA()) self.assertEqual(found_adapter.rest_model_class, RESTModelA) self.assertEqual(found_adapter.persistent_model_class, PersistentModelA) self.assertRaises(TypeError, registry_manager.get_adapter_for_rest_model, RESTModelB) self.assertRaises(TypeError, registry_manager.get_adapter_for_persistent_model, PersistentModelB)
def test_clear_registered_adapters(self): registry_manager = adapters.AdapterRegistryManager() registry_manager.register_adapter( adapters.ModelAdapter(rest_model_class=RESTModelA, persistent_model_class=PersistentModelA)) # fetch via the REST model found_adapter = registry_manager.get_adapter_for_rest_model( RESTModelA()) self.assertEqual(found_adapter.rest_model_class, RESTModelA) self.assertEqual(found_adapter.persistent_model_class, PersistentModelA) # fetch via the persistent model found_adapter = registry_manager.get_adapter_for_persistent_model( PersistentModelA()) self.assertEqual(found_adapter.rest_model_class, RESTModelA) self.assertEqual(found_adapter.persistent_model_class, PersistentModelA) # clear the registry registry_manager.clear_registered_adapters() # check they have been cleared self.assertRaises(TypeError, registry_manager.get_adapter_for_rest_model, RESTModelA()) self.assertRaises(TypeError, registry_manager.get_adapter_for_persistent_model, PersistentModelA())
def test_adapt_persistent_to_rest_with_model_array_as_child_filtered(self): model_adapter = adapters.ModelAdapter( rest_model_class=PersonREST, persistent_model_class=PersonWithAddresses) address = Address() address.street = "123 Street Address" person = PersonWithAddresses() person.first_name = "John" person.last_name = "Doe" person.addresses.append(address) attribute_filter = parser.AttributeFilter.from_model( PersonREST(), default_value=False) attribute_filter.last_name = True attribute_filter.addresses.street = True person_rest = model_adapter.adapt_persistent_to_rest( person, attribute_filter) self.assertEqual(person.first_name, "John") self.assertEqual(person.last_name, "Doe") self.assertEqual(person.addresses, [address]) self.assertIsNone(person_rest.first_name) self.assertEqual(person.last_name, person_rest.last_name) self.assertEqual([{ "short_string": None, "street": "123 Street Address" }], person_rest.addresses.as_serializable())
def test_adapt_persistent_to_rest_with_basic_arrays_as_children(self): model_adapter = adapters.ModelAdapter( rest_model_class=PersonREST, persistent_model_class=PersonWithBasicArrays) person = PersonWithBasicArrays() person.first_name = "John" person.last_name = "Doe" person.booleans.append(True) person.floats.append(1.1) person.integers.append(2) person.strings.append("string") person_rest = model_adapter.adapt_persistent_to_rest(person) self.assertEqual(person.first_name, "John") self.assertEqual(person.last_name, "Doe") self.assertEqual(person.booleans, [True]) self.assertEqual(person.floats, [1.1]) self.assertEqual(person.integers, [2]) self.assertEqual(person.strings, ["string"]) self.assertEqual(person.first_name, person_rest.first_name) self.assertEqual(person.last_name, person_rest.last_name) self.assertEqual(person.booleans, person_rest.booleans.as_serializable()) self.assertEqual(person.floats, person_rest.floats.as_serializable()) self.assertEqual(person.integers, person_rest.integers.as_serializable()) self.assertEqual(person.strings, person_rest.strings.as_serializable())
def test_adapt_persistent_to_rest_with_model_as_child(self): model_adapter = adapters.ModelAdapter(rest_model_class=PersonREST, persistent_model_class=Person) person = PersonWithAddress() person.first_name = "John" person.last_name = "Doe" person.address.street = "123 Street Address" attribute_filter = parser.AttributeFilter.from_model( PersonREST(), default_value=False) attribute_filter.last_name = True attribute_filter.address.street = True person_rest = model_adapter.adapt_persistent_to_rest( person, attribute_filter) self.assertEqual(person.first_name, "John") self.assertEqual(person.last_name, "Doe") self.assertEqual(person.address.street, "123 Street Address") import logging logging.error(person_rest.first_name) self.assertIsNone(person_rest.first_name) self.assertEqual(person.last_name, person_rest.last_name) self.assertEqual(person.address.street, person_rest.address.street)
def test_adapt_persistent_to_rest_inconsistent_data_exception_raised_array( self): model_adapter = adapters.ModelAdapter(rest_model_class=PersonREST, persistent_model_class=Person) person = Person() person.short_string = "a longer string" self.assertRaises(exception.InconsistentPersistentDataError, model_adapter.adapt_persistent_to_rest, person)
def test_adapt_persistent_to_rest_no_filter(self): model_adapter = adapters.ModelAdapter(rest_model_class=PersonREST, persistent_model_class=Person) person = Person() person.first_name = "John" person.last_name = "Doe" person_rest = model_adapter.adapt_persistent_to_rest(person) self.assertEqual(person.first_name, "John") self.assertEqual(person.last_name, "Doe") self.assertEqual(person.first_name, person_rest.first_name) self.assertEqual(person.last_name, person_rest.last_name)
def test_adapt_persistent_to_rest_with_model_missing(self): model_adapter = adapters.ModelAdapter(rest_model_class=PersonREST, persistent_model_class=Person) person = PersonWithAddress() person.first_name = "John" person.last_name = "Doe" person.address = None person_rest = model_adapter.adapt_persistent_to_rest(person) self.assertEqual(person.first_name, "John") self.assertEqual(person.last_name, "Doe") self.assertEqual(person.first_name, person_rest.first_name) self.assertEqual(person.last_name, person_rest.last_name)
def test_adapt_persistent_to_rest_with_filter(self): model_adapter = adapters.ModelAdapter(rest_model_class=PersonREST, persistent_model_class=Person) person = Person() person.first_name = "John" person.last_name = "Doe" attribute_filter = parser.AttributeFilter.from_model( PersonREST(), default_value=False) attribute_filter.last_name = True person_rest = model_adapter.adapt_persistent_to_rest( person, attribute_filter) self.assertEqual(person.first_name, "John") self.assertEqual(person.last_name, "Doe") self.assertIsNone(person_rest.first_name) self.assertEqual(person.last_name, person_rest.last_name)
def test_register_adapter_of_correct_type(self): registry_manager = adapters.AdapterRegistryManager() registry_manager.register_adapter( adapters.ModelAdapter(rest_model_class=RESTModelA, persistent_model_class=PersistentModelA)) # fetch via the REST model found_adapter = registry_manager.get_adapter_for_rest_model( RESTModelA()) self.assertEqual(found_adapter.rest_model_class, RESTModelA) self.assertEqual(found_adapter.persistent_model_class, PersistentModelA) # fetch via the persistent model found_adapter = registry_manager.get_adapter_for_persistent_model( PersistentModelA()) self.assertEqual(found_adapter.rest_model_class, RESTModelA) self.assertEqual(found_adapter.persistent_model_class, PersistentModelA)
def test_adapt_persistent_to_rest_with_model_array_as_child(self): model_adapter = adapters.ModelAdapter( rest_model_class=PersonREST, persistent_model_class=PersonWithAddresses) address = Address() address.street = "123 Street Address" person = PersonWithAddresses() person.first_name = "John" person.last_name = "Doe" person.addresses.append(address) person_rest = model_adapter.adapt_persistent_to_rest(person) self.assertEqual(person.first_name, "John") self.assertEqual(person.last_name, "Doe") self.assertEqual(person.addresses, [address]) self.assertEqual(person.first_name, person_rest.first_name) self.assertEqual(person.last_name, person_rest.last_name) self.assertEqual([{ "short_string": None, "street": "123 Street Address" }], person_rest.addresses.as_serializable())
def test_init_and_getters(self): model_adapter = adapters.ModelAdapter(rest_model_class=PersonREST, persistent_model_class=Person) self.assertEqual(model_adapter.rest_model_class, PersonREST) self.assertEqual(model_adapter.persistent_model_class, Person)