def test_i_can_read_a_locally_stored_sheet_to_an_order(): r = OrderSheetReader() filename = "Garrood 2.xlsx" path = os.path.join(settings.LOCAL_FETCH_SHEETS_DIR, filename) r.read(path) r.order_details pass
def test_a_new_customer_will_get_stored_keyed_by_email_address(): expected_actual = ( ("customer_first_name", "Nigel"), ("customer_last_name", "Samplestock"), ("customer_address", "107, Fairfield Drive"), ("customer_postcode", "rh4 1jj"), ("customer_email", "*****@*****.**"), ("customer_phone", "0789 449 542"), ("fulfillment_method", "Collect from Denbies Shop"), ("fulfillment_event__target_date", datetime(2020, 5, 12, 0, 0)), ) expected_actual = dict((x, y) for x, y in expected_actual) r = OrderSheetReader() xlsx_file = os.path.join(settings.SAMPLE_ORDER_SHEET_DIR, FILENAME) r.read_to_model(xlsx_file) customer = r.customer.__dict__ indexes = [ "first_name", "last_name", "address", "postcode", "postcode", "email", "phone", ] for i in indexes: assert expected_actual.get(f"customer_{i}") == customer.get(i)
def test_create_a_new_fulfillment_event_from_order_sheet(): mocked_date_in_order_details = { # openpxyl returns a datetime.datetime vs. target_date field is datetime.date "fulfillment_event__target_date": datetime(2020, 12, 1, 0, 0) } r = OrderSheetReader() # scenario 1: There are no fevents in DB, so create one for the mocked event r.read() f_event = r.get_or_create_fulfillment_event(mocked_date_in_order_details) f_event_target_date_to_dt = datetime.combine(f_event.target_date, datetime.min.time()) assert f_event_target_date_to_dt == mocked_date_in_order_details.get( "fulfillment_event__target_date")
def test_order_details_are_captured_to_instance_variable(self): expected_actual = ( ("customer_first_name", "Nigel"), ("customer_last_name", "Samplestock"), ("customer_address", "107, Fairfield Drive"), ("customer_postcode", "rh4 1jj"), ("customer_email", "*****@*****.**"), ("customer_phone", "0789 449 542"), ("fulfillment_method", "Collect from Denbies Shop"), ("fulfillment_event__target_date", datetime(2020, 5, 12, 0, 0)), ) r = OrderSheetReader() r.read(os.path.join(settings.SAMPLE_ORDER_SHEET_DIR, "current.xlsx")) actual = r.order_details for expected_idx, expected in expected_actual: assert actual.get(expected_idx, "") == expected
def test_order_sheet_products_count(): expected = { "Butternut Squash FV0013": 1, } r = OrderSheetReader() r.read() actual = r.product_counts for exp_p_name, exp_count in expected.items(): if exp_p_name in list(actual.keys()): assert exp_count == actual.get(exp_p_name) # loop through actual, where product counts are None, assert that an entry does not exist in expected for actual_p_name, count in actual.items(): if count is None: found = [ p_name for p_name, count in expected.items() if actual_p_name == p_name ] if found is None: assert False
def local_fetch(request): """Used in debugging""" forms_status = {"processed": 0, "failure": 0} for count, f in enumerate( collect_files_for_reading(settings.LOCAL_FETCH_SHEETS_DIR)): r = OrderSheetReader() try: r.read_to_model(f) # remove_remote_form_after_fetch_success(r.obj.filename) # upload_form_to_processed_folder(f,order) forms_status["processed"] += 1 messages.add_message(request, messages.SUCCESS, f"{r.obj.filename} processed.") except OrderFormReaderException as e: OrderFormFailure.objects.create(reason=e, form=r.obj) forms_status["failure"] += 1 messages.add_message(request, messages.WARNING, f"{r.obj.filename} failure.") continue url = reverse("admin:order_orderformfailure_changelist") return redirect(url)
def fetch(request): forms_status = {"processed": 0, "failure": 0} containing_dir, zip_path, files_meta = cloud_fetcher() for count, f in enumerate(collect_files_for_reading(containing_dir)): r = OrderSheetReader() try: order = r.read_to_model(f) remove_remote_form_after_fetch_success(r.obj.filename) upload_form_to_processed_folder(f, order) forms_status["processed"] += 1 except OrderFormReaderException as e: OrderFormFailure.objects.create(reason=e, form=r.obj) forms_status["failure"] += 1 continue # SCENARIOS # 'No new-orders found in Dropbox # - orders_processed == 0 and order_failures == 0 # 'Orders fetched and processed' # - orders_processed > 0 and order_failures == 0 # 'Some orders processed but also some failures. Please check "Order Form Failures" or persisting files in 'new-orders' folder # - orders_processed > 0 and order_failures > 0 # 'There were Order form failures. Please see below' # - orders_processed == 0 and orders_failures > 0 if forms_status["processed"] == 0 and forms_status["failure"] == 0: os.remove(zip_path) shutil.rmtree(containing_dir) messages.add_message(request, messages.WARNING, "No new-orders found in Dropbox") return redirect("/admin/order/fulfillmentevent/") if forms_status["processed"] > 0 and forms_status["failure"] == 0: os.remove(zip_path) extracted_dir = os.path.join(settings.MEDIA_ROOT, settings.NEW_ORDERS_FOLDER) shutil.rmtree(extracted_dir) messages.add_message(request, messages.SUCCESS, "Orders fetched and processed") return redirect("/admin/order/fulfillmentevent/") if forms_status["processed"] > 0 and forms_status["failure"] > 0: os.remove(zip_path) extracted_dir = os.path.join(settings.MEDIA_ROOT, settings.NEW_ORDERS_FOLDER) shutil.rmtree(extracted_dir) messages.add_message( request, messages.WARNING, 'Some orders processed but also some failures. Please check below. The failed files will persist in the "new-orders" folder', ) return redirect("/admin/order/orderformfailure/") os.remove(zip_path) extracted_dir = os.path.join(settings.MEDIA_ROOT, settings.NEW_ORDERS_FOLDER) shutil.rmtree(extracted_dir) messages.add_message( request, messages.WARNING, 'There were Order form failures. Please see below. The failed files will persist in the "new-orders" Dropbox folder', ) return redirect("/admin/order/orderformfailure/")
def test_first_cell_in_sample_order_form_is_read_by_openpyxl(): r = OrderSheetReader() r.read() assert (str(type(r.excel_data[0][0])) == "<class 'openpyxl.cell.read_only.ReadOnlyCell'>")