dodil/erp-brick-pos

DODIL ERP brick: point of sale

tests/test_setup.py

"""Slice B, item 6: POS-01 - a shop is set up once, and what it names must exist where it lives: its company in gl, its warehouse in
inventory (that company's, and open) and its consumption location (one of that warehouse's internal locations). Written from Given / When /
Then through the doors; a neighbour that is down refuses the change and nothing is saved; one that is not installed has nothing to check against."""
from conftest import CASHIER, OPS, api, make_shop, must, refused, tool
from erp_platform import seam


def shop_body(**more):
    return {"shop_id": "LON-SOHO", "name": "Soho", "company_code": "UK01", "warehouse_id": "LON-SOHO", "consumption_location_id": "LON-SOHO/BAR",
            "price_list_id": "LIST-UK-SHOP", "country": "GB", **more}


def test_pos_01_a_shop_is_created_when_its_company_warehouse_and_location_exist(nb):
    shop = must(api("POST", "/shops", OPS, shop_body()))
    assert (shop["shop_id"], shop["company_code"], shop["warehouse_id"], shop["consumption_location_id"], shop["unchecked"]) == \
        ("LON-SOHO", "UK01", "LON-SOHO", "LON-SOHO/BAR", [])
    assert nb.company_calls == ["UK01"] and nb.warehouse_calls == ["LON-SOHO"]
    assert nb.location_calls == [{"usage": "internal", "warehouse_id": "LON-SOHO"}]
    de = must(api("POST", "/shops", OPS, {"shop_id": "BER-MITTE", "company_code": "DE01", "warehouse_id": "BER-MITTE",
                                          "consumption_location_id": "BER-MITTE/BAR", "price_list_id": "LIST-DE-SHOP", "country": "DE",
                                          "fiscal_profile": "de_kassensichv"}))
    assert (de["fiscal_profile"], de["company_code"]) == ("de_kassensichv", "DE01")


def test_pos_01_a_company_that_gl_does_not_have_is_refused(nb):
    refused(api("POST", "/shops", OPS, shop_body(company_code="UK99")), 409, "company UK99 does not exist in gl", gate="company_known")
    assert api("GET", "/shops/LON-SOHO", OPS)[0] == 404                                      # nothing was saved


def test_pos_01_a_warehouse_inventory_does_not_have_or_that_belongs_to_another_company_or_is_closed_is_refused(nb):
    refused(api("POST", "/shops", OPS, shop_body(warehouse_id="NOWHERE")), 409, "warehouse NOWHERE does not exist in inventory", gate="warehouse_known")
    refused(api("POST", "/shops", OPS, shop_body(warehouse_id="BER-MITTE", consumption_location_id="BER-MITTE/BAR")), 409,
            "belongs to company DE01, the shop to UK01", gate="warehouse_company")
    nb.warehouse("OLD-STORE", "UK01", ("OLD-STORE/BAR",), status="closed")
    refused(api("POST", "/shops", OPS, shop_body(warehouse_id="OLD-STORE", consumption_location_id="OLD-STORE/BAR")), 409, "is closed", gate="warehouse_active")
    assert api("GET", "/shops/LON-SOHO", OPS)[0] == 404


def test_pos_01_the_consumption_location_is_an_internal_location_of_the_shops_warehouse(nb):
    refused(api("POST", "/shops", OPS, shop_body(consumption_location_id="LON-SOHO/NOPE")), 409,
            "LON-SOHO/NOPE is not an internal location of warehouse LON-SOHO", gate="location_known")
    nb.warehouses["LON-SOHO"]["locations"].append(("LON-SOHO/DOCK", "receiving"))              # a receiving dock is not where drinks are made
    refused(api("POST", "/shops", OPS, shop_body(consumption_location_id="LON-SOHO/DOCK")), 409, "not an internal location", gate="location_known")
    other = api("POST", "/shops", OPS, shop_body(consumption_location_id="BER-MITTE/BAR"))     # another warehouse's bar
    assert other[0] == 409 and other[1]["gate"] == "location_known"
    refused(api("POST", "/shops", OPS, {"shop_id": "X", "consumption_location_id": "X/BAR"}), 409, "name the warehouse too", gate="warehouse_known")


def test_pos_01_only_what_changed_is_checked_and_saving_the_same_shop_reads_nothing(nb):
    first = must(api("POST", "/shops", OPS, shop_body()))
    nb.company_calls.clear(), nb.warehouse_calls.clear(), nb.location_calls.clear()
    again = must(api("POST", "/shops", OPS, shop_body(version=first["version"])))
    renamed = must(api("POST", "/shops", OPS, {"shop_id": "LON-SOHO", "name": "Soho Square", "version": again["version"]}))
    assert renamed["name"] == "Soho Square" and (nb.company_calls, nb.warehouse_calls, nb.location_calls) == ([], [], [])
    moved = must(api("POST", "/shops", OPS, {"shop_id": "LON-SOHO", "cost_centre": "CC-410", "version": renamed["version"]}))
    assert moved["version"] == renamed["version"] + 1 and (nb.company_calls, nb.warehouse_calls, nb.location_calls) == ([], [], [])


def test_pos_01_changing_the_company_checks_the_warehouse_still_belongs_to_it(nb):
    first = must(api("POST", "/shops", OPS, shop_body()))
    refused(api("POST", "/shops", OPS, {"shop_id": "LON-SOHO", "company_code": "DE01", "version": first["version"]}), 409,
            "belongs to company UK01, the shop to DE01", gate="warehouse_company")
    assert must(api("GET", "/shops/LON-SOHO", OPS))["company_code"] == "UK01"


def test_pos_01_a_neighbour_that_is_down_refuses_the_change_and_nothing_is_saved(nb):
    nb.setup_status = 500
    refused(api("POST", "/shops", OPS, shop_body()), 502, "gl failed (500")
    assert api("GET", "/shops/LON-SOHO", OPS)[0] == 404
    nb.setup_status = None
    assert must(api("POST", "/shops", OPS, shop_body()))["shop_id"] == "LON-SOHO"            # and it works when they are back


def test_pos_01_a_neighbour_that_is_not_installed_has_nothing_to_check_against(nb):
    seam.unregister_local("gl")
    seam.unregister_local("inventory")
    shop = must(api("POST", "/shops", OPS, shop_body(company_code="ANY", warehouse_id="ANYWHERE", consumption_location_id="ANYWHERE/BAR")))
    assert shop["unchecked"] == ["company_code", "warehouse_id", "consumption_location_id"]
    assert must(api("GET", "/shops/LON-SOHO", OPS))["company_code"] == "ANY"


def test_pos_01_who_may_set_a_shop_up_and_the_tool_says_the_same(nb):
    refused(api("POST", "/shops", CASHIER, shop_body()), 403, "pos:shop:write")
    kind, text = tool(OPS, "keep_shop", shop_body(company_code="UK99"))
    assert kind == "refused" and "company UK99 does not exist in gl" in text
    kind, shop = tool(OPS, "keep_shop", shop_body())
    assert kind == "ok" and shop["unchecked"] == []


def test_pos_01_the_world_make_shop_seeds_is_the_one_the_story_gives(nb):
    make_shop("NEW-SHOP", registers=("R1",))
    assert nb.warehouses["NEW-SHOP"]["locations"] == [("NEW-SHOP/BAR", "internal")] and "UK01" in nb.companies