Skip to content

Commit

Permalink
[Device_manager] Fix: correção do filtro (#198)
Browse files Browse the repository at this point in the history
* [Device_Manager] Fix: correção filtro e add teste unit.

Co-authored-by: Marcelo Rebello Pimentel <marcelorebellopimentel@gmail.com>
Co-authored-by: Rodrigo Maia <rodrigoasmaia@gmail.com>
  • Loading branch information
MarceloPimentel050975 and rodrigoaasm committed Aug 3, 2022
1 parent 29ce388 commit 7fb82f3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions DeviceManager/DatabaseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = CONFIG.get_db_url()
app.config['SQLALCHEMY_BINDS'] = {}
# SQLALCHEMY_ECHO - habilitar o log das SQL do framework SQLALCHEMY - True
app.config['SQLALCHEMY_ECHO'] = False

LOGGER = Log().color_log()

Expand Down
7 changes: 4 additions & 3 deletions DeviceManager/DeviceHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,16 @@ def get_devices(token, params, sensitive_data=False):


elif label_filter or template_filter: # only filter by label or/and template

page = db.session.query(Device)

if label_filter:
LOGGER.debug(f"Filtering devices by label: {target_label}")

if template_filter:
page = page.join(DeviceTemplateMap, isouter=True)
LOGGER.debug(f"Filtering devices with template: {target_template}")

page = db.session.query(Device) \
.join(DeviceTemplateMap, isouter=True)

if sensitive_data: #aditional joins for sensitive data
page = page.join(DeviceTemplate) \
.join(DeviceAttr, isouter=True) \
Expand Down
54 changes: 53 additions & 1 deletion tests/test_device_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from DeviceManager.BackendHandler import KafkaInstanceHandler
import DeviceManager.DatabaseModels
from DeviceManager.SerializationModels import ValidationError

from DeviceManager.ImportHandler import ImportHandler
from .token_test_generator import generate_token

from alchemy_mock.mocking import AlchemyMagicMock, UnifiedAlchemyMagicMock
Expand Down Expand Up @@ -448,3 +448,55 @@ def test_endpoint_internal_get_device(self, db_mock, query_property_getter_mock)
mock_getDevice.return_value = {'id': 140110840862312, 'created': '2019-08-29T18:18:07.801602+00:00', 'attrs': {}}
result = flask_internal_get_device('test_device_id')
self.assertIsNotNone(result.response)

@patch('DeviceManager.DeviceHandler.db')
@patch('flask_sqlalchemy._QueryProperty.__get__')
def test_filter_device_label(self, db_mock, query_property_getter_mock):
db_mock.session = AlchemyMagicMock()
token = generate_token()

data = '{"label":"device001","templates":[1]}'
with patch('DeviceManager.DeviceHandler.DeviceHandler.generate_device_id') as mock_device_id:
mock_device_id.return_value = 'device001_id'

with patch('DeviceManager.DeviceHandler.DeviceHandler.validate_device_id') as mock_validate_device_id:
mock_validate_device_id.return_value = True

with patch.object(KafkaInstanceHandler, "getInstance", return_value=MagicMock()):

params = {'count': '1', 'verbose': 'false',
'content_type': 'application/json', 'data': data}
result = DeviceHandler.create_device(params, token)

params_query = {'page_number': 6, 'per_page': 0,
'attr': [], 'attr_type': [],'sortBy': 'label',
'idsOnly': 'false', 'label': 'de'}
result = DeviceHandler.get_devices(token, params_query)

self.assertIsNotNone(result)

@patch('DeviceManager.DeviceHandler.db')
@patch('flask_sqlalchemy._QueryProperty.__get__')
def test_filter_device_label_template(self, db_mock, query_property_getter_mock):
db_mock.session = AlchemyMagicMock()
token = generate_token()
data = '{"label":"device002","templates":[1]}'

with patch('DeviceManager.DeviceHandler.DeviceHandler.generate_device_id') as mock_device_id:
mock_device_id.return_value = 'device002_id'

with patch('DeviceManager.DeviceHandler.DeviceHandler.validate_device_id') as mock_validate_device_id:
mock_validate_device_id.return_value = True

with patch.object(KafkaInstanceHandler, "getInstance", return_value=MagicMock()):

params = {'count': '1', 'verbose': 'false',
'content_type': 'application/json', 'data': data}
result = DeviceHandler.create_device(params, token)

params_query = {'page_number': 6, 'per_page': 0,
'attr': [], 'attr_type': [],'sortBy': 'label',
'idsOnly': 'false', 'label': 'de','template': '1'}
result = DeviceHandler.get_devices(token, params_query)

self.assertIsNotNone(result)

0 comments on commit 7fb82f3

Please sign in to comment.