def test_submit_unique_sample_case_name( orders_api: OrdersAPI, mip_order_to_submit: dict, ticket_number: int, user_name: str, user_mail: str, monkeypatch, ): # GIVEN we have an order with a case that is not existing in the database order_data = OrderIn.parse_obj(obj=mip_order_to_submit, project=OrderType.MIP_DNA) store = orders_api.status sample: MipDnaSample for sample in order_data.samples: case_id = sample.family_name customer_obj = store.customer(order_data.customer) assert not store.find_family(customer=customer_obj, name=case_id) monkeypatch_process_lims(monkeypatch, order_data) # WHEN calling submit orders_api.submit(project=OrderType.MIP_DNA, order_in=order_data, user_name=user_name, user_mail=user_mail)
def test_submit_fluffy_duplicate_sample_case_name( all_orders_to_submit: dict, monkeypatch, order_type: OrderType, orders_api: OrdersAPI, ticket_number: int, user_mail: str, user_name: str, ): # GIVEN we have an order with a case that is already in the database order_data = OrderIn.parse_obj(obj=all_orders_to_submit[order_type], project=order_type) monkeypatch_process_lims(monkeypatch, order_data) orders_api.submit(project=order_type, order_in=order_data, user_name=user_name, user_mail=user_mail) # WHEN calling submit # THEN an OrderError should be raised on duplicate case name with pytest.raises(OrderError): orders_api.submit(project=order_type, order_in=order_data, user_name=user_name, user_mail=user_mail)
def test_submit_scout_legal_sample_customer( all_orders_to_submit: dict, monkeypatch, order_type: OrderType, orders_api: OrdersAPI, sample_store: Store, ticket_number: int, user_mail: str, user_name: str, ): order_data = OrderIn.parse_obj(obj=all_orders_to_submit[order_type], project=order_type) monkeypatch_process_lims(monkeypatch, order_data) # GIVEN we have an order with a customer that is in the same customer group as customer # that the samples originate from customer_group = sample_store.add_customer_group( "customer999only", "customer 999 only group") sample_store.add_commit(customer_group) sample_customer = sample_store.add_customer( "customer1", "customer 1", scout_access=True, invoice_address="dummy street 1", customer_group=customer_group, invoice_reference="dummy nr", ) order_customer = sample_store.add_customer( "customer2", "customer 2", scout_access=True, invoice_address="dummy street 2", customer_group=customer_group, invoice_reference="dummy nr", ) sample_store.add_commit(sample_customer) sample_store.add_commit(order_customer) existing_sample = sample_store.samples().first() existing_sample.customer = sample_customer sample_store.commit() order_data.customer = order_customer.internal_id for sample in order_data.samples: sample.internal_id = existing_sample.internal_id break # WHEN calling submit # THEN an OrderError should not be raised on illegal customer orders_api.submit(project=order_type, order_in=order_data, user_name=user_name, user_mail=user_mail)
def submit_order(order_type): """Submit an order for samples.""" api = OrdersAPI(lims=lims, status=db, osticket=osticket) error_message: str try: request_json = request.get_json() LOG.info("processing order: %s", request_json) project: OrderType = OrderType(order_type) order_in: OrderIn = OrderIn.parse_obj(request_json, project=project) result = api.submit( project=project, order_in=order_in, user_name=g.current_user.name, user_mail=g.current_user.email, ) except ( # user misbehaviour OrderError, OrderFormError, ValidationError, ValueError, ) as error: error_message = error.message if hasattr(error, "message") else str(error) http_error_response = http.HTTPStatus.BAD_REQUEST LOG.error(error_message) except ( # system misbehaviour AttributeError, ConnectionError, HTTPError, IntegrityError, KeyError, NewConnectionError, MaxRetryError, TimeoutError, TypeError, ) as error: LOG.exception(error) error_message = error.message if hasattr(error, "message") else str(error) http_error_response = http.HTTPStatus.INTERNAL_SERVER_ERROR else: return jsonify( project=result["project"], records=[record.to_dict() for record in result["records"]]) if error_message: return abort( make_response(jsonify(message=error_message), http_error_response))
def order(order_type): """Submit an order for samples.""" api = OrdersAPI(lims=lims, status=db, osticket=osticket) post_data = request.get_json() LOG.info("processing '%s' order: %s", order_type, post_data) try: ticket = {'name': g.current_user.name, 'email': g.current_user.email} result = api.submit(OrderType[order_type.upper()], post_data, ticket=ticket) except (DuplicateRecordError, OrderError) as error: return abort(make_response(jsonify(message=error.message), 401)) except HTTPError as error: return abort(make_response(jsonify(message=error.args[0]), 401)) #LOG.info(f"{result['ticket'] or 'NA'}: successfully submitted samples") return jsonify(project=result['project'], records=[record.to_dict() for record in result['records']])
def test_submit(base_store, orders_api: OrdersAPI, all_orders_to_submit, monkeypatch, order_type): ticket_number = 123456 monkeypatch.setattr(orders_api.osticket, "open_ticket", lambda *args, **kwargs: ticket_number) order_data = all_orders_to_submit[order_type] lims_project_data = {"id": "ADM1234", "date": dt.datetime.now()} lims_map = { sample["name"]: f"ELH123A{index}" for index, sample in enumerate(order_data["samples"]) } monkeypatch.setattr(orders_api, "process_lims", lambda *args: (lims_project_data, lims_map)) # GIVEN an order and an empty store assert base_store.samples().first() is None # WHEN submitting the order order_ticket = {"name": "Paul Anderson", "email": "*****@*****.**"} result = orders_api.submit(order_type, data=order_data, ticket=order_ticket) # THEN it should work... for record in result["records"]: if isinstance(record, models.Pool) or isinstance(record, models.Sample): assert record.ticket_number == ticket_number elif isinstance(record, models.Family): for link_obj in record.links: assert link_obj.sample.ticket_number == ticket_number
def test_submit( all_orders_to_submit: dict, base_store: Store, monkeypatch, order_type: OrderType, orders_api: OrdersAPI, ticket_number: int, user_mail: str, user_name: str, ): order_data = OrderIn.parse_obj(obj=all_orders_to_submit[order_type], project=order_type) monkeypatch_process_lims(monkeypatch, order_data) # GIVEN an order and an empty store assert base_store.samples().first() is None # WHEN submitting the order result = orders_api.submit(project=order_type, order_in=order_data, user_name=user_name, user_mail=user_mail) # THEN the result should contain the ticket number for the order for record in result["records"]: if isinstance(record, (models.Pool, models.Sample)): assert record.ticket_number == ticket_number elif isinstance(record, models.Family): for link_obj in record.links: assert link_obj.sample.ticket_number == ticket_number
def orders_api(base_store): osticket_api = OsTicket() lims = MockLims() _orders_api = OrdersAPI(lims=lims, status=base_store, osticket=osticket_api) return _orders_api
def submit_order(order_type): """Submit an order for samples.""" api = OrdersAPI(lims=lims, status=db, osticket=osticket) post_data = request.get_json() LOG.info("processing '%s' order: %s", order_type, post_data) try: ticket = {"name": g.current_user.name, "email": g.current_user.email} result = api.submit(OrderType[order_type.upper()], post_data, ticket=ticket) except (DuplicateRecordError, OrderError) as error: return abort(make_response(jsonify(message=error.message), 401)) except HTTPError as error: return abort(make_response(jsonify(message=error.args[0]), 401)) return jsonify( project=result["project"], records=[record.to_dict() for record in result["records"]], )
def test_not_sarscov2_submit_duplicate_sample_name( all_orders_to_submit: dict, helpers: StoreHelpers, monkeypatch, order_type: OrderType, orders_api: OrdersAPI, sample_store: Store, ticket_number: int, user_mail: str, user_name: str, ): # GIVEN we have an order with samples that is already in the database order_data = OrderIn.parse_obj(obj=all_orders_to_submit[order_type], project=order_type) monkeypatch_process_lims(monkeypatch, order_data) store_samples_with_names_from_order(orders_api.status, helpers, order_data) # WHEN calling submit orders_api.submit(project=order_type, order_in=order_data, user_name=user_name, user_mail=user_mail)
def test_submit_unique_sample_name( all_orders_to_submit: dict, monkeypatch, order_type: OrderType, orders_api: OrdersAPI, ticket_number: int, user_mail: str, user_name: str, ): # GIVEN we have an order with a sample that is not existing in the database order_data = OrderIn.parse_obj(obj=all_orders_to_submit[order_type], project=order_type) store = orders_api.status assert store.samples().first() is None monkeypatch_process_lims(monkeypatch, order_data) # WHEN calling submit orders_api.submit(project=order_type, order_in=order_data, user_name=user_name, user_mail=user_mail)
def test_submit_duplicate_sample_case_name( all_orders_to_submit: dict, monkeypatch, order_type: OrderType, orders_api: OrdersAPI, ticket_number: int, user_mail: str, user_name: str, ): # GIVEN we have an order with a case that is already in the database order_data = OrderIn.parse_obj(obj=all_orders_to_submit[order_type], project=order_type) store = orders_api.status customer_obj = store.customer(order_data.customer) for sample in order_data.samples: case_id = sample.family_name if not store.find_family(customer=customer_obj, name=case_id): case_obj = store.add_case( data_analysis=Pipeline.MIP_DNA, data_delivery=DataDelivery.SCOUT, name=case_id, ) case_obj.customer = customer_obj store.add_commit(case_obj) assert store.find_family(customer=customer_obj, name=case_id) monkeypatch_process_lims(monkeypatch, order_data) # WHEN calling submit # THEN an OrderError should be raised on duplicate case name with pytest.raises(OrderError): orders_api.submit(project=order_type, order_in=order_data, user_name=user_name, user_mail=user_mail)
def orders_api(base_store, osticket: MockOsTicket, lims_api: MockLimsAPI): return OrdersAPI(lims=lims_api, status=base_store, osticket=osticket)