예제 #1
0
    def test_dumps(self):
        expected = dict(
            input_data=dict(X=10, Y=20),
            calc_result=dict(Z=30)
        )
        calc_result = SumCalculationResult(Z=30)
        input_data = SumCalculationInput(X=10, Y=20)
        c = Calculation(input_data, calc_result)

        result = c.dumps()

        self.assertEqual(expected, result)
예제 #2
0
    def test_loads(self):
        expected = Calculation(
            input_data=SumCalculationInput(X=10, Y=20),
            calc_result=SumCalculationResult(Z=30)
        )
        dumped = dict(
            input_data=dict(X=10, Y=20),
            calc_result=dict(Z=30)
        )
        result = Calculation.load(dumped)

        self.assertEqual(expected, result)
예제 #3
0
    def execute(self, request: SumCalculateRequest):
        X = request.X
        Y = request.Y

        input_data = SumCalculationInput(X, Y)
        calc_result = SumCalculation().calculate(input_data)

        calculation = Calculation(input_data, calc_result)
        calculation_id = self.repository.append_calculation(calculation)
        result = SumCalculateResponse(calculation_id)

        return result
예제 #4
0
    def test__repository__save_calculation(self):
        """
            тестируется вычисление суммы двух чисел
            выполняется вариант использования
            в репозиторий должен сохраниться верный результат
        """
        expected = Calculation(input_data=SumCalculationInput(X=10, Y=20),
                               calc_result=SumCalculationResult(Z=30))
        repository = CalculationRepository()
        use_case = SumCalculateUseCase(repository)
        request = SumCalculateRequest(X=10, Y=20)
        response = use_case.execute(request)
        calculation_id = response.calculation_id

        result = repository.find_calculation_by_id(calculation_id)

        self.assertEqual(expected, result)
예제 #5
0
import unittest

from calculator.step_3_use_case.calculation_repository import (
    CalculationRepository, NotFoundCalculation)
from calculator.step_3_use_case.sum_calculation import (Calculation,
                                                        SumCalculationInput,
                                                        SumCalculationResult)

calculation = Calculation(input_data=SumCalculationInput(X=10, Y=20),
                          calc_result=SumCalculationResult(Z=30))


class TestCalculationRepository(unittest.TestCase):
    def test__append_calculation(self):
        expected = 1
        repository = CalculationRepository()

        result = repository.append_calculation(calculation)

        self.assertEqual(expected, result)

    def test__find_calculation_by_id__with_not_found_calculation(self):
        repository = CalculationRepository()

        with self.assertRaises(NotFoundCalculation):
            repository.find_calculation_by_id(1)

    def test__find_calculation_by_id__with_existing_calculation(self):
        repository = CalculationRepository()
        id_ = repository.append_calculation(calculation)
예제 #6
0
 def find_calculation_by_id(self, calculation_id) -> Calculation:
     calculation = self.calculations.get(calculation_id)
     if not calculation:
         raise NotFoundCalculation
     result = Calculation.load(calculation)
     return result
예제 #7
0
 def append_calculation(self, calculation: Calculation):
     calculation_id = self._next_id()
     self.calculations[calculation_id] = calculation.dumps()
     return calculation_id