import unittest
from unittest import TestCase, mock

from web import init_app
from config import appcfgs
from mods.service import sample_service


# TODO 샘플 테스트 코드. 참고하여 작성합니다.
class TestSampleService(TestCase):
    app = None

    @classmethod
    def setUpClass(cls):
        """
        테스트 클래스가 시작되기 이전에 한 번 호출됨
        :return:
        """
        TestSampleService.app = init_app(appcfgs.TestingConfig)
        sample_service.log = mock.MagicMock()

    def setUp(self):
        """
        각 테스트 케이스가 시작되기 전에 호출됨
        :return:
        """
        self.app = TestSampleService.app.test_client()

    @mock.patch('mods.service.sample_service.insert_sample_record')
    @mock.patch('mods.service.sample_service.db_conn')
    def test_add_sample_success(self, mocked_db_conn, mocked_insert_sample_record):
        # Given
        test_sample_data = 'TEST'
        mocked_db_conn.return_value = mock.MagicMock()
        mocked_insert_sample_record.return_value = mock.MagicMock()

        # When
        actual = sample_service.add_sample_data(test_sample_data)

        # Then
        expected = True
        self.assertEqual(expected, actual)

    @mock.patch('mods.service.sample_service.insert_sample_record')
    @mock.patch('mods.service.sample_service.db_conn')
    def test_add_sample_fail(self, mocked_db_conn, mocked_insert_sample_record):
        # Given
        test_sample_data = 'TEST'
        mocked_db_conn.return_value = mock.MagicMock()
        mocked_insert_sample_record.side_effect = Exception()

        # When
        actual = sample_service.add_sample_data(test_sample_data)

        # Then
        expected = False
        self.assertEqual(expected, actual)


if __name__ == '__main__':
    unittest.main()
