def test_should_pass_policy_with_other_terms(): fake_adapter = FakeExternalApiAdapter(900, Decimal(0.66)) loan = Loan('name', 'cpf', datetime.date(2000, 7, 12), Decimal(2500), 6, Decimal(1000)) loan.person_score = 900 CommitmentPolicy(fake_adapter).apply(loan) assert loan.terms == 9
def test_should_throw_policy_exception(): with pytest.raises(PolicyException, match='Política negada: commitment'): fake_adapter = FakeExternalApiAdapter(900, Decimal(0.9)) loan = Loan('name', 'cpf', datetime.date(2000, 7, 12), Decimal(2500), 6, Decimal(1000)) loan.person_score = 900 CommitmentPolicy(fake_adapter).apply(loan)
def test_should_approve_loan(): loan = Loan('name', 'cpf', datetime.date(2000, 7, 12), 1000, 6, 1000) policies_to_apply = [FakePolicy(True)] port = LoanPort(FakeLoanAdapter()) ComputeLoanUseCase(port, policies_to_apply).execute(loan) assert loan.status == 'completed' assert loan.result == 'approved'
def test_shouldnt_approve_loan(): loan = Loan('name', 'cpf', datetime.date(2000, 7, 12), 1000, 6, 1000) policies_to_apply = [FakePolicy(False)] port = LoanPort(FakeLoanAdapter()) ComputeLoanUseCase(port, policies_to_apply).execute(loan) assert loan.refused_policy == 'fake_policy' assert loan.result == 'refused' assert loan.status == 'completed' assert loan.terms is None assert loan.amount is None
def test_should_not_construct_loan(): with pytest.raises(ValidationException, match='Erros: Nome inválido, CPF inválido, ' + 'Data de nascimento inválida, Valor ' + 'inválido, Quantidade de parcelas inválidas'): name = '' cpf = '' birthdate = '' amount = 999 terms = 5 income = 1000 Loan(name, cpf, birthdate, amount, terms, income)
def __find_monthly_payment(self, loan: Loan, monthly_saving): tax = self.__get_tax_percentages(loan.person_score) desired_terms_payment = self.__calculate_pmt(loan.amount, loan.terms, tax[str(loan.terms)]) if desired_terms_payment > monthly_saving: del tax[str(loan.terms)] for key in tax: terms = int(key) other_term_payment = self.__calculate_pmt( loan.amount, terms, tax[key]) if other_term_payment < monthly_saving: loan.terms = terms return raise PolicyException('commitment')
def compute(event, context): for record in event.get('Records'): if record.get('eventName') == 'INSERT': print(record) id = uuid.UUID(record['dynamodb']['Keys']['id']['S']) name = record['dynamodb']['NewImage']['name']['S'] cpf = record['dynamodb']['NewImage']['cpf']['S'] birthdate = dateutil.parser.isoparse( record['dynamodb']['NewImage']['birthdate']['S']) amount = Decimal(record['dynamodb']['NewImage']['amount']['N']) terms = int(record['dynamodb']['NewImage']['terms']['N']) income = Decimal(record['dynamodb']['NewImage']['income']['N']) loan = Loan(name, cpf, birthdate, amount, terms, income, id=id) compute_loan_use_case_instace.execute(loan)
def create_loan(event, context): try: data = json.loads(event['body']) birthdate = datetime.datetime.strptime( data.get('birthdate'), '%Y-%m-%d') if 'birthdate' in data else None loan = Loan(data.get('name', ''), data.get('cpf', ''), birthdate, data.get('amount', 0), data.get('terms', 0), data.get('income', '')) create_loan_use_case_instace.execute(loan) body = {'id': loan.id} return {'statusCode': 200, 'body': json.dumps(body)} except ValidationException as ex: body = {'errors': ex.errors} return {'statusCode': 400, 'body': json.dumps(body)} except Exception as e: body = {'errorMessage': getattr(e, 'message', str(e))} return {'statusCode': 500, 'body': json.dumps(body)}
def test_should_construct_loan(): name = 'teste' cpf = '123123' birthdate = date.today() amount = 1000 terms = 6 income = 1000 loan = Loan(name, cpf, birthdate, amount, terms, income) assert name == loan.name assert cpf == loan.cpf assert birthdate == loan.birthdate assert amount == loan.amount assert terms == loan.terms assert income == loan.income assert 'processing' == loan.status assert None is loan.refused_policy assert None is loan.result UUID(hex=loan.id, version=4)
def test_should_throw_policy_exception(): with pytest.raises(PolicyException, match='Política negada: score'): fake_adapter = FakeExternalApiAdapter(599, 0) loan = Loan('name', 'cpf', datetime.date(2000, 7, 12), 1000, 6, 1000) ScorePolicy(fake_adapter).apply(loan)
def get_by_id(self, id) -> Loan: item = self.dynamo_table.get_item(Key={'id': id})['Item'] print(item) loan = Loan(item['name'], item['cpf'], dateutil.parser.isoparse(item['birthdate']), item['amount'], item['terms'], item['income'], status=item['status'], result=item['result'], refused_policy=item['refused_policy'], id=item['id']) print(loan) return loan
def test_shouldnt_throw_policy_exception(): fake_adapter = FakeExternalApiAdapter(600, 0) loan = Loan('name', 'cpf', datetime.date(2000, 7, 12), 1000, 6, 1000) ScorePolicy(fake_adapter).apply(loan)
def save(self, item: Loan): item.birthdate = item.birthdate.isoformat() self.dynamo_table.put_item(Item=item.__dict__)
def test_shouldnt_throw_policy_exception(): loan = Loan('name', 'cpf', datetime.date(2000, 7, 12), 1000, 6, 1000) AgePolicy().apply(loan)
def __deny_loan(self, loan: Loan, policy): loan.refused_policy = policy loan.terms = None loan.result = 'refused' loan.status = 'completed' loan.amount = None
def apply(self, loan: Loan): person_score = self.external_api_port.get_person_score(loan.cpf) loan.person_score = person_score if person_score < 600: raise PolicyException('score')
def get_by_id(self, id) -> Loan: return Loan('test', 'cpf', datetime.date.today(), 1000, 6, 1000)
def __approve_loan(self, loan: Loan): loan.status = 'completed' loan.result = 'approved'
def test_should_throw_policy_exception(): with pytest.raises(PolicyException, match='Política negada: age'): loan = Loan('name', 'cpf', datetime.date.today(), 1000, 6, 1000) AgePolicy().apply(loan)