def test_create_missing(self): """Call method ``create_missing``.""" class FKEntityWithCreate(entity_mixins.Entity, entity_mixins.EntityCreateMixin): """An entity that can be created and has foreign key fields.""" def __init__(self, server_config=None, **kwargs): self._fields = { 'int': IntegerField(required=True), 'int_choices': IntegerField(choices=(1, 2), required=True), 'int_default': IntegerField(default=5, required=True), 'many': OneToManyField(SampleEntity, required=True), 'one': OneToOneField(SampleEntity, required=True), } super(FKEntityWithCreate, self).__init__(server_config, **kwargs) cfg = config.ServerConfig('example.com') entity = FKEntityWithCreate(cfg) with mock.patch.object(entity._fields['many'], 'gen_value') as gen1: with mock.patch.object(entity._fields['one'], 'gen_value') as gen2: self.assertEqual(entity.create_missing(), None) for gen_value in gen1, gen2: self.assertEqual( gen_value.mock_calls, [ mock.call(), # gen_value() returns a class. The returned mock.call()(cfg), # class is instantiated, and mock.call()().create(True), # create(True) is called. ]) self.assertEqual( set(entity.get_fields().keys()) - {'id'}, set(entity.get_values().keys()), ) self.assertIn(entity.int_choices, (1, 2)) self.assertEqual(entity.int_default, 5)
def test_init_v2(self): """Provide a server config object via ``DEFAULT_SERVER_CONFIG``.""" backup = entity_mixins.DEFAULT_SERVER_CONFIG try: entity_mixins.DEFAULT_SERVER_CONFIG = config.ServerConfig('url') self.assertEqual( SampleEntity()._server_config, entity_mixins.DEFAULT_SERVER_CONFIG, ) finally: entity_mixins.DEFAULT_SERVER_CONFIG = backup
def test_update_payload_v1(self): """Call :meth:`nailgun.entity_mixins.EntityUpdateMixin.update_payload`. Assert that the method behaves correctly given various values for the ``field`` argument. """ class TestEntity(EntityWithUpdate): """Just like its parent class, but with fields.""" def __init__(self, server_config=None, **kwargs): self._fields = {'one': IntegerField(), 'two': IntegerField()} super(TestEntity, self).__init__(server_config, **kwargs) cfg = config.ServerConfig('url') args_list = ( {}, { 'one': gen_integer() }, { 'two': gen_integer() }, { 'one': gen_integer(), 'two': gen_integer() }, ) # Make `update_payload` return all or no values. for args in args_list: entity = TestEntity(cfg, **args) self.assertEqual(entity.update_payload(), args) self.assertEqual(entity.update_payload(list(args.keys())), args) self.assertEqual(entity.update_payload([]), {}) # Make `update_payload` return only some values. entity = TestEntity(cfg, **args_list[-1]) self.assertEqual( entity.update_payload(['one']), {'one': args_list[-1]['one']}, ) self.assertEqual( entity.update_payload(['two']), {'two': args_list[-1]['two']}, ) # Ask `update_payload` to return unavailable values. entity = TestEntity(cfg) for field_names in (['one'], ['two'], ['one', 'two']): with self.assertRaises(KeyError): entity.update_payload(field_names)
def test_update_payload_v2(self): """Call :meth:`nailgun.entity_mixins.EntityUpdateMixin.update_payload`. Assign ``None`` to a ``OneToOneField`` and call ``update_payload``. """ class TestEntity(EntityWithUpdate): """Just like its parent class, but with fields.""" def __init__(self, server_config=None, **kwargs): self._fields = {'other': OneToOneField(SampleEntity)} super(TestEntity, self).__init__(server_config, **kwargs) cfg = config.ServerConfig('url') entities = [TestEntity(cfg, other=None), TestEntity(cfg)] entities[1].other = None for entity in entities: with self.subTest(entity): self.assertEqual(entity.update_payload(), {'other_id': None})
def test_update(self): """Test :meth:`nailgun.entity_mixins.EntityUpdateMixin.update`.""" class EntityWithUpdateRead(EntityWithUpdate, entity_mixins.EntityReadMixin): """An entity that can be updated and read.""" readable = EntityWithUpdateRead( config.ServerConfig('example.com'), id=gen_integer(), ) with mock.patch.object(readable, 'update_json') as update_json: update_json.return_value = gen_integer() with mock.patch.object(readable, 'read') as read: readable.update() self.assertEqual(update_json.call_count, 1) self.assertEqual(read.call_count, 1) self.assertEqual( read.call_args[1]['attrs'], update_json.return_value, )
def test_missing_value_error(self): """Raise a :class:`nailgun.entity_mixins.MissingValueError`.""" entity = self.test_entity(config.ServerConfig('example.com')) for attrs in ( { 'id': gen_integer(min_value=1), 'none': None, 'one_id': gen_integer(min_value=1), }, { 'id': gen_integer(min_value=1), 'many_ids': [gen_integer(min_value=1)], 'none': None, }, ): with self.subTest(attrs): with mock.patch.object(entity, 'read_json') as read_json: read_json.return_value = attrs with self.assertRaises(entity_mixins.MissingValueError): entity.read(ignore={'ignore_me'})
def test_create(self): """Test :meth:`nailgun.entity_mixins.EntityCreateMixin.create`.""" class EntityWithCreateRead(EntityWithCreate, entity_mixins.EntityReadMixin): """An entity that can be created and read.""" readable = EntityWithCreateRead( config.ServerConfig('example.com'), id=gen_integer(), ) for create_missing in (None, True, False): with mock.patch.object(readable, 'create_json') as create_json: create_json.return_value = gen_integer() with mock.patch.object(readable, 'read') as read: readable.create(create_missing) self.assertEqual(create_json.call_count, 1) self.assertEqual(create_json.call_args[0][0], create_missing) self.assertEqual(read.call_count, 1) self.assertEqual( read.call_args[1]['attrs'], create_json.return_value, )
def setUp(self): """Set ``self.entity = EntityWithDelete(…)``.""" self.entity = EntityWithDelete( config.ServerConfig('example.com'), id=gen_integer(min_value=1), )
def setUp(self): """Set ``self.entity = EntityWithRead(…)``.""" self.cfg = config.ServerConfig('example.com') self.entity = EntityWithRead(self.cfg, id=gen_integer(min_value=1))
def setUp(self): """Set ``self.cfg``.""" self.cfg = config.ServerConfig('http://example.com')
def setUp(self): """Create a bogus server configuration object.""" self.cfg = config.ServerConfig('bogus url')
def setUp(self): """Set ``self.cfg`` and ``self.entity``.""" self.cfg = config.ServerConfig('example.com') self.entity = EntityWithSearch(self.cfg)
def _get_entity_ids(self): return {} def search_json(self, fields=None, query=None): return {"results": [ORG_DATA]} def search_normalize(self, results): return [ORG_DATA] @staticmethod def search_filter(entities, filters): return entities cfg = config.ServerConfig(ORG_DATA['domain']) org = Organization(cfg, **ORG_DATA) @patch('nailgun.entities.Organization.read', return_value=org) @patch('nailgun.client.get', return_value=FakeResponse()) class SignalsTestCase(TestCase): """This test case tests only if all signals are emitted and if the arguments were passed and catch by connected listeners """ def test_signals_available(self, *args, **kwargs): self.assertTrue(signals.SIGNALS_AVAILABLE) def test_patching(self, *args, **kwargs): '''Make sure patching works''' self.assertIsInstance(client.get('http://example.com'), FakeResponse)