56 lines
No EOL
2.1 KiB
Python
56 lines
No EOL
2.1 KiB
Python
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
from app.terraform.proxy import ProxyAutomation, update_smart_proxy_instance
|
|
from app import app
|
|
|
|
|
|
class TestUpdateSmartProxyInstance(unittest.TestCase):
|
|
def setUp(self):
|
|
self.group_id = 1
|
|
self.provider = 'test_provider'
|
|
self.region = 'test_region'
|
|
self.instance_id = 'test_instance_id'
|
|
app.config['TESTING'] = True
|
|
self.app = app.test_client()
|
|
|
|
@patch('app.terraform.proxy.SmartProxy')
|
|
@patch('app.terraform.proxy.db')
|
|
def test_update_smart_proxy_instance_new(self, mock_db, mock_smart_proxy):
|
|
# Configure the mocked SmartProxy query to return None
|
|
mock_smart_proxy.query.filter.return_value.first.return_value = None
|
|
|
|
# Call the function
|
|
update_smart_proxy_instance(self.group_id, self.provider, self.region, self.instance_id)
|
|
|
|
# Assert that a new SmartProxy instance was created and added to the session
|
|
mock_smart_proxy.assert_called_once()
|
|
mock_db.session.add.assert_called_once()
|
|
|
|
@patch('app.terraform.proxy.SmartProxy')
|
|
@patch('app.terraform.proxy.db')
|
|
def test_update_smart_proxy_instance_existing(self, mock_db, mock_smart_proxy):
|
|
# Configure the mocked SmartProxy query to return an existing instance
|
|
mock_instance = MagicMock()
|
|
mock_smart_proxy.query.filter.return_value.first.return_value = mock_instance
|
|
|
|
# Call the function
|
|
update_smart_proxy_instance(self.group_id, self.provider, self.region, self.instance_id)
|
|
|
|
# Assert that the existing SmartProxy instance was updated
|
|
self.assertEqual(mock_instance.instance_id, self.instance_id)
|
|
|
|
|
|
class TestProxyAutomation(unittest.TestCase):
|
|
def setUp(self):
|
|
app.config['TESTING'] = True
|
|
self.app = app.test_client()
|
|
|
|
def test_proxy_automation_abstract_methods(self):
|
|
# Test NotImplementedError for import_state
|
|
with self.assertRaises(NotImplementedError):
|
|
proxy_automation = ProxyAutomation()
|
|
proxy_automation.import_state(None)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main() |