<reponame>stevekineeve88/doubloon<filename>tests/unit/pool_manager_test.py
import unittest
from unittest.mock import patch, MagicMock
from modules.pool.data.pool_data import PoolData
from modules.pool.exceptions.pool_const_error import PoolConstError
from modules.pool.exceptions.pool_create_error import PoolCreateError
from modules.pool.exceptions.pool_search_error import PoolSearchError
from modules.pool.exceptions.pool_update_error import PoolUpdateError
from modules.pool.managers.pool_manager import PoolManager
from modules.pool.managers.pool_status_manager import PoolStatusManager
from modules.pool.objects.pool import Pool
from modules.pool.objects.pool_status import PoolStatus
from modules.pool.objects.pool_ticket import PoolTicket
from modules.util.managers.postgres_conn_manager import PostgresConnManager
from modules.util.objects.data_list import DataList
from modules.util.objects.result import Result


class PoolManagerTest(unittest.TestCase):
    @classmethod
    @patch("modules.util.managers.postgres_conn_manager.PostgresConnManager")
    @patch("modules.pool.managers.pool_status_manager.PoolStatusManager")
    def setUpClass(cls, postgres_conn_manager, pool_status_manager) -> None:
        cls.postgres_conn_manager: PostgresConnManager = postgres_conn_manager
        cls.pool_status_manager: PoolStatusManager = pool_status_manager

        cls.active_status: PoolStatus = PoolStatus(1, "ACTIVE", "Active status")
        cls.disabled_status: PoolStatus = PoolStatus(2, "DISABLED", "Disabled status")
        cls.pool_status_manager.get_all = MagicMock(return_value=DataList(
            "POOL_STATUSES",
            [cls.active_status, cls.disabled_status],
            "id",
            "const"
        ))

        cls.pool_manager: PoolManager = PoolManager(
            pool_data=PoolData(
                postgres_conn_manager=cls.postgres_conn_manager
            ),
            pool_status_manager=cls.pool_status_manager
        )

    def test_create_creates_pool(self):
        pool_ticket = PoolTicket("POOL_NAME")
        pool_data = {
            "id": 1,
            "uuid": "ERT-123",
            "const": pool_ticket.get_const(),
            "status_id": self.active_status.get_id()
        }
        self.postgres_conn_manager.insert = MagicMock(return_value=Result(True))
        self.postgres_conn_manager.select = MagicMock(return_value=Result(
            True,
            "",
            [pool_data]
        ))
        pool = self.pool_manager.create(pool_ticket)
        self.postgres_conn_manager.insert.assert_called_once()
        self.postgres_conn_manager.select.assert_called_once()
        self.assertEqual(pool_data["id"], pool.get_id())
        self.assertEqual(pool_data["uuid"], pool.get_uuid())
        self.assertEqual(pool_data["const"], pool.get_const())
        self.assertEqual(pool_data["status_id"], pool.get_status().get_id())

    def test_create_pool_ticket_fails_on_const_error(self):
        with self.assertRaises(PoolConstError):
            PoolTicket("not_upper_case")
            self.fail("Did not fail on lower case")
        with self.assertRaises(PoolConstError):
            PoolTicket("HAS_NUMBERS_1234")
            self.fail("Did not fail on numbers")
        with self.assertRaises(PoolConstError):
            PoolTicket("HAS_SPECIAL_CHARACTER_%")
            self.fail("Did not fail on special character")

    def test_create_fails_on_create_error(self):
        pool_ticket = PoolTicket("SOME_NAME")
        self.postgres_conn_manager.insert = MagicMock(return_value=Result(False))
        with self.assertRaises(PoolCreateError):
            self.pool_manager.create(pool_ticket)
            self.fail("Did not fail on create")
        self.postgres_conn_manager.insert.assert_called_once()

    def test_get_fails_on_search_error(self):
        self.postgres_conn_manager.select = MagicMock(return_value=Result(False))
        with self.assertRaises(PoolSearchError):
            self.pool_manager.get(1)
            self.fail("Did not fail on search")
        self.postgres_conn_manager.select.assert_called_once()

    def test_update_status_updates(self):
        pool_id = 1
        status_id = self.disabled_status.get_id()
        pool_data = {
            "id": pool_id,
            "uuid": "ERT-123",
            "status_id": status_id,
            "const": "CONST_HERE"
        }
        self.postgres_conn_manager.query = MagicMock(return_value=Result(True))
        self.postgres_conn_manager.select = MagicMock(return_value=Result(
            True,
            "",
            [pool_data]
        ))
        try:
            updated_pool = self.pool_manager.update_status(pool_id, status_id)
            self.assertEqual(pool_id, updated_pool.get_id())
            self.assertEqual(status_id, updated_pool.get_status().get_id())
        except Exception as e:
            self.fail(str(e))

    def test_update_status_fails_on_update_error(self):
        self.postgres_conn_manager.query = MagicMock(return_value=Result(False))
        self.postgres_conn_manager.select = MagicMock(return_value=Result(True))
        with self.assertRaises(PoolUpdateError):
            self.pool_manager.update_status(1, 1)
            self.fail("Did not fail on update")
        self.postgres_conn_manager.query.assert_called_once()
        self.postgres_conn_manager.select.assert_not_called()

    def test_search_searches_pools(self):
        data = [
            {
                "id": 1,
                "uuid": "ERT-123",
                "status_id": self.active_status.get_id(),
                "const": "CONST_ONE"
            },
            {
                "id": 2,
                "uuid": "ERT-456",
                "status_id": self.active_status.get_id(),
                "const": "CONST_TWO"
            }
        ]
        result = Result(True, "", data)
        result.set_full_count(len(data))
        self.postgres_conn_manager.paginate = MagicMock(return_value=result)
        fetch_result = self.pool_manager.search(
            const="CONST",
            statuses=[self.active_status.get_id()]
        )
        self.assertEqual(len(data), fetch_result.get_full_count())
        fetched_pools = fetch_result.get_data()
        self.assertEqual(len(data), len(fetched_pools))
        pool: Pool
        for i in range(0, len(fetched_pools)):
            pool = fetched_pools[i]
            self.assertEqual(data[i]["id"], pool.get_id())
            self.assertEqual(data[i]["uuid"], pool.get_uuid())
            self.assertEqual(data[i]["status_id"], pool.get_status().get_id())
            self.assertEqual(data[i]["const"], pool.get_const())

    def test_search_fails_on_search_error(self):
        self.postgres_conn_manager.paginate = MagicMock(return_value=Result(False))
        with self.assertRaises(PoolSearchError):
            self.pool_manager.search()
            self.fail("Did not fail on search")
        self.postgres_conn_manager.paginate.assert_called_once()

import unittest
from unittest.mock import patch, MagicMock
from modules.pool.data.pool_data import PoolData
from modules.pool.exceptions.pool_const_error import PoolConstError
from modules.pool.exceptions.pool_create_error import PoolCreateError
from modules.pool.exceptions.pool_search_error import PoolSearchError
from modules.pool.exceptions.pool_update_error import PoolUpdateError
from modules.pool.managers.pool_manager import PoolManager
from modules.pool.managers.pool_status_manager import PoolStatusManager
from modules.pool.objects.pool import Pool
from modules.pool.objects.pool_status import PoolStatus
from modules.pool.objects.pool_ticket import PoolTicket
from modules.util.managers.postgres_conn_manager import PostgresConnManager
from modules.util.objects.data_list import DataList
from modules.util.objects.result import Result


class PoolManagerTest(unittest.TestCase):
    @classmethod
    @patch("modules.util.managers.postgres_conn_manager.PostgresConnManager")
    @patch("modules.pool.managers.pool_status_manager.PoolStatusManager")
    def setUpClass(cls, postgres_conn_manager, pool_status_manager) -> None:
        cls.postgres_conn_manager: PostgresConnManager = postgres_conn_manager
        cls.pool_status_manager: PoolStatusManager = pool_status_manager

        cls.active_status: PoolStatus = PoolStatus(1, "ACTIVE", "Active status")
        cls.disabled_status: PoolStatus = PoolStatus(2, "DISABLED", "Disabled status")
        cls.pool_status_manager.get_all = MagicMock(return_value=DataList(
            "POOL_STATUSES",
            [cls.active_status, cls.disabled_status],
            "id",
            "const"
        ))

        cls.pool_manager: PoolManager = PoolManager(
            pool_data=PoolData(
                postgres_conn_manager=cls.postgres_conn_manager
            ),
            pool_status_manager=cls.pool_status_manager
        )

    def test_create_creates_pool(self):
        pool_ticket = PoolTicket("POOL_NAME")
        pool_data = {
            "id": 1,
            "uuid": "ERT-123",
            "const": pool_ticket.get_const(),
            "status_id": self.active_status.get_id()
        }
        self.postgres_conn_manager.insert = MagicMock(return_value=Result(True))
        self.postgres_conn_manager.select = MagicMock(return_value=Result(
            True,
            "",
            [pool_data]
        ))
        pool = self.pool_manager.create(pool_ticket)
        self.postgres_conn_manager.insert.assert_called_once()
        self.postgres_conn_manager.select.assert_called_once()
        self.assertEqual(pool_data["id"], pool.get_id())
        self.assertEqual(pool_data["uuid"], pool.get_uuid())
        self.assertEqual(pool_data["const"], pool.get_const())
        self.assertEqual(pool_data["status_id"], pool.get_status().get_id())

    def test_create_pool_ticket_fails_on_const_error(self):
        with self.assertRaises(PoolConstError):
            PoolTicket("not_upper_case")
            self.fail("Did not fail on lower case")
        with self.assertRaises(PoolConstError):
            PoolTicket("HAS_NUMBERS_1234")
            self.fail("Did not fail on numbers")
        with self.assertRaises(PoolConstError):
            PoolTicket("HAS_SPECIAL_CHARACTER_%")
            self.fail("Did not fail on special character")

    def test_create_fails_on_create_error(self):
        pool_ticket = PoolTicket("SOME_NAME")
        self.postgres_conn_manager.insert = MagicMock(return_value=Result(False))
        with self.assertRaises(PoolCreateError):
            self.pool_manager.create(pool_ticket)
            self.fail("Did not fail on create")
        self.postgres_conn_manager.insert.assert_called_once()

    def test_get_fails_on_search_error(self):
        self.postgres_conn_manager.select = MagicMock(return_value=Result(False))
        with self.assertRaises(PoolSearchError):
            self.pool_manager.get(1)
            self.fail("Did not fail on search")
        self.postgres_conn_manager.select.assert_called_once()

    def test_update_status_updates(self):
        pool_id = 1
        status_id = self.disabled_status.get_id()
        pool_data = {
            "id": pool_id,
            "uuid": "ERT-123",
            "status_id": status_id,
            "const": "CONST_HERE"
        }
        self.postgres_conn_manager.query = MagicMock(return_value=Result(True))
        self.postgres_conn_manager.select = MagicMock(return_value=Result(
            True,
            "",
            [pool_data]
        ))
        try:
            updated_pool = self.pool_manager.update_status(pool_id, status_id)
            self.assertEqual(pool_id, updated_pool.get_id())
            self.assertEqual(status_id, updated_pool.get_status().get_id())
        except Exception as e:
            self.fail(str(e))

    def test_update_status_fails_on_update_error(self):
        self.postgres_conn_manager.query = MagicMock(return_value=Result(False))
        self.postgres_conn_manager.select = MagicMock(return_value=Result(True))
        with self.assertRaises(PoolUpdateError):
            self.pool_manager.update_status(1, 1)
            self.fail("Did not fail on update")
        self.postgres_conn_manager.query.assert_called_once()
        self.postgres_conn_manager.select.assert_not_called()

    def test_search_searches_pools(self):
        data = [
            {
                "id": 1,
                "uuid": "ERT-123",
                "status_id": self.active_status.get_id(),
                "const": "CONST_ONE"
            },
            {
                "id": 2,
                "uuid": "ERT-456",
                "status_id": self.active_status.get_id(),
                "const": "CONST_TWO"
            }
        ]
        result = Result(True, "", data)
        result.set_full_count(len(data))
        self.postgres_conn_manager.paginate = MagicMock(return_value=result)
        fetch_result = self.pool_manager.search(
            const="CONST",
            statuses=[self.active_status.get_id()]
        )
        self.assertEqual(len(data), fetch_result.get_full_count())
        fetched_pools = fetch_result.get_data()
        self.assertEqual(len(data), len(fetched_pools))
        pool: Pool
        for i in range(0, len(fetched_pools)):
            pool = fetched_pools[i]
            self.assertEqual(data[i]["id"], pool.get_id())
            self.assertEqual(data[i]["uuid"], pool.get_uuid())
            self.assertEqual(data[i]["status_id"], pool.get_status().get_id())
            self.assertEqual(data[i]["const"], pool.get_const())

    def test_search_fails_on_search_error(self):
        self.postgres_conn_manager.paginate = MagicMock(return_value=Result(False))
        with self.assertRaises(PoolSearchError):
            self.pool_manager.search()
            self.fail("Did not fail on search")
        self.postgres_conn_manager.paginate.assert_called_once()