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)
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)
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
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)
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)
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
def append_calculation(self, calculation: Calculation): calculation_id = self._next_id() self.calculations[calculation_id] = calculation.dumps() return calculation_id