Skip to content

Commit

Permalink
Add tests for integrity errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanEdCosta committed Aug 31, 2022
1 parent 17c871f commit 795ded6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
28 changes: 28 additions & 0 deletions tests/test_device_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest
from unittest.mock import Mock, MagicMock, patch, call
from flask import Flask
from sqlalchemy.exc import IntegrityError

from DeviceManager.DeviceHandler import DeviceHandler, flask_delete_all_device, flask_get_device, flask_remove_device, flask_add_template_to_device, flask_remove_template_from_device, flask_gen_psk,flask_internal_get_device
from DeviceManager.utils import HTTPRequestError
Expand Down Expand Up @@ -305,6 +306,33 @@ def test_create_device(self, db_mock_session, query_property_getter_mock):
with self.assertRaises(HTTPRequestError):
result = DeviceHandler.create_device(params, token)

@patch('flask_sqlalchemy._QueryProperty.__get__')
def test_create_device_integrity_errors(self, query_get_mock):
with patch('DeviceManager.DeviceHandler.init_tenant_context') as mock_init_tenant_context:
mock_init_tenant_context.return_value = 'admin'
with patch('DeviceManager.DeviceHandler.DeviceHandler.generate_device_id') as mock_device_id:
mock_device_id.return_value = 'test_device_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()):
with patch('DeviceManager.DeviceHandler.db') as db_mock:
diag = Exception()
diag.message_primary = 'test'
orig = Exception()
orig.diag = diag

db_mock.session = AlchemyMagicMock()
db_mock.session.commit = Mock()
db_mock.session.commit.side_effect = IntegrityError(statement='test', params='test', orig=orig)

token = generate_token()
data = '{"label":"test_device","templates":[1]}'
params = {'count': '1', 'verbose': 'false',
'content_type': 'application/json', 'data': data}

with self.assertRaises(HTTPRequestError):
DeviceHandler.create_device(params, token)

@patch('DeviceManager.DeviceHandler.db')
@patch('flask_sqlalchemy._QueryProperty.__get__')
def test_update_device(self, db_mock_session, query_property_getter_mock):
Expand Down
39 changes: 38 additions & 1 deletion tests/test_template_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from DeviceManager.utils import HTTPRequestError
from DeviceManager.BackendHandler import KafkaInstanceHandler
from datetime import datetime

from sqlalchemy.exc import IntegrityError

from .token_test_generator import generate_token

Expand Down Expand Up @@ -77,6 +77,43 @@ def test_create_tempĺate(self, db_mock):
self.assertEqual(result['result'], 'ok')
self.assertIsNotNone(result['template'])

def test_create_template_integrity_errors(self):
with patch('DeviceManager.TemplateHandler.init_tenant_context') as mock_init_tenant_context:
mock_init_tenant_context.return_value = 'admin'
with patch('DeviceManager.TemplateHandler.db') as db_mock:
diag = Exception()
diag.message_primary = 'test'
orig = Exception()
orig.diag = diag

db_mock.session = AlchemyMagicMock()
db_mock.session.commit = Mock()
db_mock.session.commit.side_effect = IntegrityError(statement='test', params='test', orig=orig)

token = generate_token()

data = """{
"label": "SensorModel",
"attrs": [
{
"label": "temperature",
"type": "dynamic",
"value_type": "float"
},
{
"label": "model-id",
"type": "static",
"value_type": "string",
"static_value": "model-001"
}
]
}"""

params_query = {'content_type': 'application/json', 'data': data}

with self.assertRaises(HTTPRequestError):
TemplateHandler.create_template(params_query, token)

@patch('DeviceManager.TemplateHandler.db')
def test_get_template(self, db_mock):
db_mock.session = AlchemyMagicMock()
Expand Down

0 comments on commit 795ded6

Please sign in to comment.