Exemplo n.º 1
0
def test_set_loan_refused_by_age(loan_fixture, snapshot):
    test_db = mongomock.MongoClient()["Test"]
    db = Database(test_db)
    _id = loan_fixture["_id"]

    db.loan_collection.insert_one(loan_fixture)
    db.find_and_set_loan_refused_by_age(_id)

    snapshot.assert_match(db.find_loan(_id))
Exemplo n.º 2
0
def test_set_loan_refused_by_pmt(loan_fixture, snapshot):
    test_db = mongomock.MongoClient()["Test"]
    db = Database(test_db)
    _id = loan_fixture["_id"]
    info = {"_id": _id, "score": 954, "commitment": 0.3}

    db.loan_collection.insert_one(loan_fixture)
    db.find_and_set_loan_refused_by_commitment(**info)

    snapshot.assert_match(db.find_loan(_id))
Exemplo n.º 3
0
def test_insert_loan(loan_fixture, snapshot):
    test_db = mongomock.MongoClient()["Test"]
    db = Database(test_db)
    loan = parse_model_and_validate(loan_fixture, Loan)
    snapshot.assert_match(loan.status)
    snapshot.assert_match(loan.data)

    db._insert_loan(loan.data)
    loan_inserted = db.find_loan(loan_fixture["_id"])

    snapshot.assert_match(loan_inserted)
Exemplo n.º 4
0
def test_start_process_error():
    test_db = mongomock.MongoClient()["Test"]
    db = Database(test_db)
    _id = "uuid_1234"
    data = {
        "_id": _id,
        "birthdate": parse_str_to_date("15/08/1992"),
        "amount": 3000,
        "terms": 6,
        "income": 12000,
    }

    insert_status = db.start_process(data)
    assert not insert_status.status
Exemplo n.º 5
0
def find(_id):
    cpf, income, amount, birth_date = get_client_info(_id)
    if not cpf:
        return False

    try:
        age_policy_status, age_metadata = AgePolicy(_id, birth_date).check()
        if not age_policy_status.status:
            return age_policy_status.loan

        score_policy_status, score_metadata = ScorePolicy(_id, cpf).check()
        if not score_policy_status.status:
            return score_policy_status.loan

        commitment_policy_status, commitment_metadata = CommitmentPolicy(
            _id, amount, income, cpf,
            score_metadata.get("metadata", {}).get("score")).check()

        return commitment_policy_status.loan

    except Exception as err:
        logger.error(f"An error occurred when processing id {id}\n"
                     f"type err: {type(err)}\n"
                     f"Description: {err}")

        # Set error metadata in Database
        with Database() as db:
            db.set_loan_task_error(_id, err)

        # Rerun it in 5 minutes
        find.apply_async(args=[_id], countdown=300)
Exemplo n.º 6
0
def test_with_database():
    try:
        with Database():
            opened = True
        assert opened
    except Exception:
        print("The MongoDB wasn't found in your machine!")
Exemplo n.º 7
0
def get_client_info(_id):
    with Database() as db:
        client = db.find_client(_id)
        return (
            client["cpf"],
            float(client["income"]),
            float(client["amount"]),
            client["birthdate"],
        )
Exemplo n.º 8
0
def test_set_loan_task_error(loan_fixture, snapshot):
    test_db = mongomock.MongoClient()["Test"]
    db = Database(test_db)
    _id = loan_fixture["_id"]

    try:
        raise ValueError("Raising an error")
    except Exception as err:
        info = {"_id": _id, "err": err}

        db.loan_collection.insert_one(loan_fixture)
        db.set_loan_task_error(**info)

        loan = db.find_loan(_id)
        assert "last_err" in loan["metadata"]["err"]

        del loan["metadata"]["err"]["last_err"]

        snapshot.assert_match(loan)
Exemplo n.º 9
0
def test_insert_client(snapshot):
    test_db = mongomock.MongoClient()["Test"]
    db = Database(test_db)
    _id = "uuid-1234"
    data = {
        "_id": _id,
        "name": "Everton Tomalok",
        "cpf": "12345678900",
        "birthdate": parse_str_to_date("15/08/1992"),
        "amount": 3000,
        "terms": 6,
        "income": 12000,
    }

    client = parse_model_and_validate(data, Client)
    db._insert_client(client.data)
    client_inserted = db.find_client(_id)

    snapshot.assert_match(client_inserted)
Exemplo n.º 10
0
def test_start_process(snapshot):
    test_db = mongomock.MongoClient()["Test"]
    db = Database(test_db)
    _id = "uuid_1234"
    data = {
        "_id": _id,
        "name": "Everton Tomalok",
        "cpf": "12345678900",
        "birthdate": parse_str_to_date("15/08/1992"),
        "amount": 3000,
        "terms": 6,
        "income": 12000,
    }

    insert_status = db.start_process(data)
    assert insert_status.status

    client_inserted = db.find_client(_id)
    loan_inserted = db.find_loan(_id)

    snapshot.assert_match(client_inserted)
    snapshot.assert_match(loan_inserted)
Exemplo n.º 11
0
def looping_id_verification(_id, function_success):
    with Database() as db:
        success = False
        i = 0
        tries = 25
        while i <= tries:
            i += 1
            client = db.loan_collection.find_one({"_id": _id})
            if client and function_success(client):
                success = True
                break
            sleep(0.3)

        db.delete_loan_and_client(_id)
    return success
Exemplo n.º 12
0
def loan():
    """
    Because of the sensitive from the data and it's being included
    in the database, we'll check the data twice.

    The first time we want to detect Rogue Fields, missing Fields
    and fields with value type not allowed.

    The second verification is done by the model from the database.
    See more about this verification in the model modules.
    """

    api_key = request.headers.get("api-key")

    if not api_key or not is_api_key_valid(api_key):
        abort(401, "Forbidden access")

    data = parse_loan_form(request.form)
    # First Validation
    field_errors = check_loan_fields(data)
    if field_errors:
        error_response = {"errors": field_errors}
        return jsonify(error_response), 400

    testing = request.args.get("testingCase")
    with Database(testing) as db:
        # Second Validation
        process_status = db.start_process(prepare_data(data))

    if not process_status.status:
        error_response = {"errors": process_status.data["fields_with_error"]}

        return jsonify(error_response), 400

    # Sending id to background task
    if not testing:
        celery.send_task("loan.process", args=[process_status.id])

    return jsonify({"id": process_status.id})
Exemplo n.º 13
0
 def set_policy_denied(self):
     with Database(self.testing) as db:
         self.loan = db.find_and_set_loan_refused_by_commitment(
             self._id, self.score, self.commitment)
Exemplo n.º 14
0
 def set_policy_accepted(self):
     with Database(self.testing) as db:
         self.loan = db.find_and_set_loan_accept(self._id, self.amount,
                                                 self.pmt.terms, self.score,
                                                 self.commitment)
Exemplo n.º 15
0
 def set_policy_denied(self):
     with Database(self.testing) as db:
         self.loan = db.find_and_set_loan_refused_by_age(self._id)
Exemplo n.º 16
0
def get_loan_status(_id):
    testing = request.args.get("testingCase")
    with Database(testing) as db:
        loan_status = db.find_loan(_id)

    return jsonify(loan_status)
Exemplo n.º 17
0
def test_delete_loan_and_cliente(loan_fixture, client_fixture, snapshot):
    test_db = mongomock.MongoClient()["Test"]
    db = Database(test_db)
    loan_id = loan_fixture["_id"]
    client_id = client_fixture["_id"]

    assert loan_id == client_id

    db._insert_client(client_fixture)
    db._insert_loan(loan_fixture)

    loan = db.find_loan(loan_id)
    client = db.find_client(client_id)

    snapshot.assert_match(loan)
    snapshot.assert_match(client)

    db.delete_loan_and_client(client_id)

    loan = db.find_loan(loan_id)
    client = db.find_client(client_id)

    snapshot.assert_match(loan)
    snapshot.assert_match(client)