Skip to content

Commit

Permalink
Add TablePreferences entity (#928)
Browse files Browse the repository at this point in the history
* Add TablePreferences entity

* Tests for TablePreferences entity

(cherry picked from commit 33db04b)
  • Loading branch information
lhellebr authored and web-flow committed Jun 23, 2023
1 parent 4f69f27 commit 279590c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
52 changes: 52 additions & 0 deletions nailgun/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8754,3 +8754,55 @@ def sync(self, synchronous=True, timeout=None, **kwargs):
kwargs.update(self._server_config.get_client_kwargs())
response = client.put(self.path('sync'), **kwargs)
return _handle_response(response, self._server_config, synchronous, timeout)


class TablePreferences(
Entity,
EntityCreateMixin,
EntityDeleteMixin,
EntityReadMixin,
EntitySearchMixin,
EntityUpdateMixin,
):
"""A representation of a Table Preference entity."""

def __init__(self, server_config=None, **kwargs):
_check_for_value('user', kwargs)
self._fields = {
'name': entity_fields.StringField(),
'columns': entity_fields.ListField(),
'created_at': entity_fields.DateTimeField(),
'updated_at': entity_fields.DateTimeField(),
}
self._path_fields = {
'user': entity_fields.OneToOneField(User),
}
self._fields.update(self._path_fields)
self.user = kwargs.get('user')
if isinstance(self.user, int):
self._meta = {
'api_path': f'/api/v2/users/{self.user}/table_preferences',
}
else:
self._meta = {
'api_path': f'/api/v2/users/{self.user.id}/table_preferences',
}
super().__init__(server_config, **kwargs)

def read(self, entity=None, attrs=None, ignore=None, params=None):
"""Ignore path related fields as they're never returned by the server
and are only added to entity to be able to use proper path.
"""
entity = entity or self.entity_with_parent(user=self.user)
if ignore is None:
ignore = set()
ignore.add('user')
return super().read(entity, attrs, ignore, params)

def search(self, fields=None, query=None, filters=None):
"""List/search for TablePreferences.
Field 'user' is only used for path and is not returned.
"""
return super().search(
fields=fields, query=query, filters=filters, path_fields={'user': self.user}
)
6 changes: 3 additions & 3 deletions nailgun/entity_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ def search_normalize(self, results):
normalized.append(attrs)
return normalized

def search(self, fields=None, query=None, filters=None):
def search(self, fields=None, query=None, filters=None, path_fields={}):
"""Search for entities.
At its simplest, this method searches for all entities of a given kind.
Expand Down Expand Up @@ -1392,11 +1392,11 @@ def search(self, fields=None, query=None, filters=None):
entities = []
for result in results:
try:
entity = type(self)(self._server_config, **result)
entity = type(self)(self._server_config, **path_fields, **result)
except TypeError:
# in the event that an entity's init is overwritten
# with a positional server_config
entity = type(self)(**result)
entity = type(self)(**path_fields, **result)
entities.append(entity)
if filters is not None:
entities = self.search_filter(entities, filters)
Expand Down
47 changes: 47 additions & 0 deletions tests/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def test_init_succeeds(self):
(entities.SSHKey, {'user': 1}),
(entities.SyncPlan, {'organization': 1}),
(entities.TemplateInput, {'template': 1}),
(entities.TablePreferences, {'user': 1}),
]
)
for entity, params in entities_:
Expand Down Expand Up @@ -2880,6 +2881,52 @@ def test_read(self):
self.assertEqual(template_input.template.id, self.template_input_data['template'])


class TablePreferencesTestCase(TestCase):
"""Tests for :class:`nailgun.entities.TablePreferences`."""

def setUp(self):
self.sc = config.ServerConfig('some url')

def test_read(self):
user_id = gen_integer(min_value=1)
user = entities.User(self.sc, id=user_id)
entity = entities.TablePreferences(self.sc, user=user)
self.assertEqual(entity.user.id, user_id)
self.assertIn(f'/{user_id}/', entity._meta['api_path'])
read_json_patcher = mock.patch.object(entity, 'read_json')
read_json = read_json_patcher.start()
table_id = gen_integer(min_value=1)
read_json.return_value = {
'id': table_id,
'name': 'testname',
'columns': ['testcol'],
'created_at': '2023-06-01 12:38:05 UTC',
'updated_at': '2023-06-01 12:38:05 UTC',
}
res = entity.read()
read_json_patcher.stop()
self.assertEqual(read_json.call_count, 1)
self.assertEqual(res.name, 'testname')
self.assertEqual(res.id, table_id)
self.assertEqual(res.columns, ['testcol'])

def test_search(self):
user_id = gen_integer(min_value=1)
user = entities.User(self.sc, id=user_id)
ret = {
'total': 1,
'page': 1,
'results': [{'id': 1, 'name': 'testname', 'columns': ['testcol']}],
}
entity = entities.TablePreferences(self.sc, user=user)
with mock.patch.object(entity, 'search_json', return_value=ret):
res = entity.search()
self.assertEqual(len(res), 1)
self.assertEqual(res[0].name, 'testname')
self.assertEqual(res[0].id, 1)
self.assertEqual(res[0].columns, ['testcol'])


class HostGroupTestCase(TestCase):
"""Tests for :class:`nailgun.entities.HostGroup`."""

Expand Down

0 comments on commit 279590c

Please sign in to comment.