Example #1
0
def test_update_request_names(client, jwt, app):
    """
    Setup:
    Test:
    Validate:
    :param client:
    :param jwt:
    :param app:
    :return:
    """
    do_test_cleanup()

    # Initialize the service
    nr_svc = NameRequestService()
    """
    Test updating three names 
    """
    # We will need a base NR
    nr = build_nr(State.DRAFT, {}, [test_names_no_id[0], test_names_no_id[1]])
    # We can't save the NR without an NR Num
    nr.nrNum = 'NR L000001'
    # Save to DB so PK sequences are updated
    nr.save_to_db()
    db.session.flush()

    # NR
    added_names = list(map(lambda n: n.as_dict(), nr.names.all()))
    added_name_0 = pick_name_from_list(added_names,
                                       test_names_no_id[0].get('name'))
    added_name_1 = pick_name_from_list(added_names,
                                       test_names_no_id[1].get('name'))

    # Set data to the service, all we need to test is names so just provide what's necessary
    nr_svc.request_data = {
        'names': [
            # Same as test name 1
            added_name_0,  # Map this over
            added_name_1,  # Map this over
            test_names_no_id[2]
        ]
    }

    # Build the names
    nr = nr_svc.map_request_names(nr)

    nr.save_to_db()

    nr = Request.find_by_nr(nr.nrNum)

    # Convert to dict
    nr = nr.json()

    assert nr is not None
    # Test the names
    assert_names_are_mapped_correctly(nr_svc.request_data.get('names'),
                                      nr.get('names'))

    # Clean up
    do_test_cleanup()
 def put_nr(nr: Request, svc: NameRequestService) -> Request:
     """
     Logic for updating the name request DATA goes inside this handler, which is invoked on successful state change.
     All request data is mapped.
     :param nr: The name request model
     :param svc A NameRequestService instance
     :return:
     """
     map_draft_attrs = nr.stateCd == State.DRAFT
     nr = svc.map_request_data(nr, map_draft_attrs)
     # Map applicants from request_data to the name request
     nr = svc.map_request_applicants(nr)
     # Map any submitted names from request_data to the name request
     nr = svc.map_request_names(nr)
     # Save
     nr = svc.save_request(nr)
     # Return the updated name request
     return nr
 def post_nr(nr: Request, svc: NameRequestService) -> Request:
     """
     All logic for creating the name request goes inside this handler, which is invoked on successful state change.
     By default just call the inherited post_nr method.
     :param nr: The name request model
     :param svc A NameRequestService instance
     """
     # Map the request data and save so we have a name request ID to use for collection ops
     nr = svc.map_request_data(nr, True)  # Set map_draft_attrs to True
     nr = svc.save_request(nr)
     # Map applicants from the request data to the name request
     nr = svc.map_request_applicants(nr)
     # Map any submitted names from the request data to the name request
     nr = svc.map_request_names(nr)
     # Update the submit count to 1
     nr = svc.update_request_submit_count(nr)
     # Save
     nr = svc.save_request(nr)
     # Return the updated name request
     return nr
    def patch_nr(nr: Request, svc: NameRequestService, nr_action, request_data: dict) -> Request:
        """
        Logic for updating the name request DATA goes inside this handler, which is invoked on successful state change.
        Re-map the names and the applicants (just the applicant / contact if applicable).
        :param nr: The name request model
        :param svc A NameRequestService instance
        :param nr_action: The Name Request action
        :param request_data: A request data object
        :return:
        """
        lock_actions = [NameRequestPatchActions.CHECKIN.value, NameRequestPatchActions.CHECKOUT.value]

        if nr_action in lock_actions and nr.stateCd in [State.DRAFT, State.INPROGRESS]:
            # Map the checkout data
            nr.checkedOutBy = request_data.get('checkedOutBy', None)
            nr.checkedOutDt = request_data.get('checkedOutDt', None)

            nr = svc.save_request(nr)
            # Return the updated name request
            return nr

        if nr.stateCd in request_editable_states:
            # Map data from request_data to the name request
            map_draft_attrs = nr.stateCd == State.DRAFT
            nr = svc.map_request_data(nr, map_draft_attrs)

            # Map any submitted names from request_data to the name request
            nr = svc.map_request_names(nr)

        if nr.stateCd in contact_editable_states:
            # Map applicants from request_data to the name request
            nr = svc.map_request_applicants(nr)

        # Save
        nr = svc.save_request(nr)
        # Return the updated name request
        return nr