Skip to content

Commit

Permalink
Merge pull request #167 from canattofilipe/refresh-template-update-co…
Browse files Browse the repository at this point in the history
…lumn-dev2

[Dev] - Refresh "updated" column of template every time template structure changes
  • Loading branch information
canattofilipe committed Sep 25, 2020
2 parents 5b1f80a + ebb3353 commit 36292ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions DeviceManager/TemplateHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def paginate(query, page, per_page=20, error_out=False):

return Pagination(query, page, per_page, total, items)

def refresh_template_update_column(db, template):
if db.session.new or db.session.deleted:
LOGGER.debug('The template structure has changed, refreshing "updated" column.')
template.updated = datetime.now()

class TemplateHandler():

kafka = KafkaInstanceHandler()
Expand Down Expand Up @@ -365,6 +370,7 @@ def analyze_attrs(attrs_from_db, attrs_from_request, parentAttr=None):
db.session.add(orm_child)
try:
LOGGER.debug(f" Commiting new data...")
refresh_template_update_column(db, old)
db.session.commit()
LOGGER.debug("... data committed.")
except IntegrityError as error:
Expand Down
18 changes: 17 additions & 1 deletion tests/test_template_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from flask import Flask

from DeviceManager.DatabaseModels import DeviceTemplate
from DeviceManager.TemplateHandler import TemplateHandler, flask_get_templates, flask_delete_all_templates, flask_get_template, flask_remove_template, paginate, attr_format
from DeviceManager.TemplateHandler import TemplateHandler, flask_get_templates, flask_delete_all_templates, \
flask_get_template, flask_remove_template, paginate, attr_format, refresh_template_update_column
from DeviceManager.utils import HTTPRequestError
from DeviceManager.BackendHandler import KafkaInstanceHandler
from datetime import datetime


from .token_test_generator import generate_token
Expand Down Expand Up @@ -227,3 +229,17 @@ def test_endpoint_delete_template(self, db_mock, query_property_getter_mock):
result = flask_remove_template('test_template_id')
self.assertEqual(result.status, '200 OK')
self.assertEqual(json.loads(result.response[0])['result'], 'ok')

@patch('DeviceManager.TemplateHandler.db')
def test_refresh_template_update_column(self, db_mock):
template = DeviceTemplate(id=1, label='template1')
refresh_template_update_column(db_mock, template)
self.assertTrue(isinstance(template.updated, datetime))

@patch('DeviceManager.TemplateHandler.db')
def test_not_refresh_template_update_column(self, db_mock):
template = DeviceTemplate(id=1, label='template1')
db_mock.session.new = set()
db_mock.session.deleted = set()
refresh_template_update_column(db_mock, template)
self.assertIsNone(template.updated)

0 comments on commit 36292ea

Please sign in to comment.