Skip to content

Commit

Permalink
some fixes to inv, ADAT permissions, and testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
graeme-f committed May 23, 2012
1 parent 890d983 commit 0b5c147
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 88 deletions.
11 changes: 9 additions & 2 deletions controllers/inv.py
Expand Up @@ -238,7 +238,7 @@ def inv_item():
)
)
def prep(r):
if r.method != "search":
if r.method != "search" and r.method != "report":
response.s3.dataTable_group = 1
return True

Expand Down Expand Up @@ -399,6 +399,8 @@ def send():
# Set Validator for checking against the number of items in the warehouse
vars = request.vars
if (vars.send_inv_item_id):
if not vars.item_pack_id:
vars.item_pack_id = s3db.inv_inv_item[vars.send_inv_item_id].item_pack_id
s3db.inv_track_item.quantity.requires = QUANTITY_INV_ITEM(db,
vars.send_inv_item_id,
vars.item_pack_id)
Expand Down Expand Up @@ -574,7 +576,12 @@ def prep(r):
return True

if len(request.args) > 1 and request.args[1] == "track_item":
status = sendtable[request.args[0]].status
# Shouldn't fail but...
# if user enters the send id then it could so wrap in a try...
try:
status = sendtable[request.args[0]].status
except:
status = None
if status:
editable = False
if status == SHIP_STATUS_RETURNING:
Expand Down
20 changes: 18 additions & 2 deletions modules/eden/inv.py
Expand Up @@ -296,6 +296,22 @@ def model(self):
comment=T("If none are selected, then all are searched."),
cols = 2
),
S3SearchOptionsWidget(
name="owner_org_seach",
label=T("Owning Organisation"),
field="owner_org_id",
represent ="%(name)s",
comment=T("If none are selected, then all are searched."),
cols = 2
),
S3SearchOptionsWidget(
name="supply_org_seach",
label=T("Donating Organisation"),
field="supply_org_id",
represent ="%(name)s",
comment=T("If none are selected, then all are searched."),
cols = 2
),
# NotImplemented yet
# S3SearchOptionsWidget(
# name="inv_item_search_category",
Expand All @@ -315,7 +331,7 @@ def model(self):
#rows=["item_id", "currency"],
rows=["item_id", (T("Category"), "item_category"),],
#cols=["site_id", "currency"],
cols=["site_id"],
cols=["site_id", "owner_org_id", "supply_org_id"],
facts=["quantity", (T("Total Value"), "total_value"),],
methods=["sum"],
groupby=self.inv_inv_item.site_id,
Expand Down Expand Up @@ -1163,7 +1179,7 @@ def model(self):
title_list = LIST_TRACK_ITEMS,
title_update = T("Edit Shipment Item"),
title_search = T("Search Shipment Items"),
subtitle_create = T("Add New Shpment Item"),
subtitle_create = T("Add New Shipment Item"),
subtitle_list = T("Shipment Items"),
label_list_button = LIST_TRACK_ITEMS,
label_create_button = ADD_TRACK_ITEM,
Expand Down
5 changes: 3 additions & 2 deletions modules/s3/s3validators.py
Expand Up @@ -1720,8 +1720,9 @@ def __call__(self, value):
limitby = (0, 1)).first() # @todo: this should be a virtual field
if inv_item_record and value:
query = (db.supply_item_pack.id == self.item_pack_id)
send_quantity = (float(value) - track_quantity) * db(query).select(db.supply_item_pack.quantity,
limitby=(0, 1)).first().quantity
send_record = db(query).select(db.supply_item_pack.quantity,
limitby=(0, 1)).first()
send_quantity = (float(value) - track_quantity) * send_record.quantity
inv_quantity = inv_item_record.inv_inv_item.quantity * \
inv_item_record.supply_item_pack.quantity
if send_quantity > inv_quantity:
Expand Down
24 changes: 18 additions & 6 deletions modules/tests/core/auth.py
Expand Up @@ -15,12 +15,6 @@ def login(account="normal", nexturl=None):
config = current.test_config
browser = config.browser

if nexturl:
url = "%s/default/user/login?_next=%s" % (config.url, nexturl)
else:
url = "%s/default/user/login" % config.url
browser.get(url)

if isinstance(account,(list,tuple)):
email = account[0]
password = account[1]
Expand All @@ -30,9 +24,27 @@ def login(account="normal", nexturl=None):
elif account == "admin":
email = "admin@example.com"
password = "testing"
elif isinstance(account, (tuple,list)) and len(account) == 2:
email = account[0]
password = account[1]
else:
raise NotImplementedError

# If the user is already logged in no need to do anything so return
if browser.page_source.find("<a id=\"auth_menu_email\">%s</a>" % email) > 0:
# if the url is different then move to the new url
if not browser.current_url.endswith(nexturl):
url = "%s/%s" % (config.url, nexturl)
browser.get(url)
return

if nexturl:
url = "%s/default/user/login?_next=%s" % (config.url, nexturl)
else:
url = "%s/default/user/login" % config.url
browser.get(url)


# Login
elem = browser.find_element_by_id("auth_user_email")
elem.send_keys(email)
Expand Down
50 changes: 40 additions & 10 deletions modules/tests/core/widgets.py
@@ -1,4 +1,5 @@
__all__ = ["w_autocomplete",
"w_inv_item_select",
]

# @todo Their are performance issues need to profile and find out in which functions are the bottlenecks
Expand All @@ -16,10 +17,10 @@

# -----------------------------------------------------------------------------
def w_autocomplete(search,
autocomplete,
needle = None,
quiet = True,
):
autocomplete,
needle = None,
quiet = True,
):
config = current.test_config
browser = config.browser

Expand All @@ -34,11 +35,12 @@ def w_autocomplete(search,
# give time for the throbber to appear
time.sleep(1)
# now wait for throbber to close
giveup = 0
giveup = 0.0
sleeptime = 0.2
while browser.find_element_by_id(throbber_id).is_displayed():
time.sleep(1)
giveup += 1
if giveup == 20:
time.sleep(sleeptime)
giveup += sleeptime
if giveup > 60:
return False
# throbber has closed and data was found, return
for i in range(10):
Expand All @@ -65,8 +67,36 @@ def w_autocomplete(search,
try:
menu = browser.find_element_by_id("ui-menu-%s" % automenu)
except:
print "Unable to find another ui_menu"
menu = None
# end of looping through each autocomplete menu
print "Sleeping"
time.sleep(1)
time.sleep(sleeptime)

def w_inv_item_select (item_repr,
tablename,
field,
quiet = True,
):
config = current.test_config
browser = config.browser

el_id = "%s_%s" % (tablename, field)
el = browser.find_element_by_id(el_id)
for option in el.find_elements_by_tag_name("option"):
if option.text == item_repr:
option.click()
raw_value = int(option.get_attribute("value"))
break
# Now wait for the pack_item to be populated
el_id = "%s_%s" % (tablename, "item_pack_id")
el = browser.find_element_by_id(el_id)
giveup = 0.0
sleeptime = 0.2
while el.find_elements_by_tag_name("option")[0].text == "":
# The pack drop down hasn't been populated yet so sleep
time.sleep(sleeptime)
giveup += sleeptime
if giveup > 60:
break
return raw_value

126 changes: 104 additions & 22 deletions modules/tests/inv/logistics.py
Expand Up @@ -6,9 +6,98 @@ class Logistics(SeleniumUnitTest):
# These tests assume that regression/inv-mngt has been added to prepop
# -----------------------------------------------------------------------------

def helper_inv_send(self, user, data):
"""
Helper method to add a inv_send record by the given user
"""
self.login(account=user, nexturl="inv/send/create")
table = "inv_send"
result = self.create(table, data)
return result

def helper_inv_send_rec(self, result):
"""
Simple helper function to get the waybill reference of the newly
created inv_send row so it can be used to filter dataTables
"""
# The newly created inv_send will be the first record in the "after" list
if len(result["after"]) > 0:
new_inv_send = result["after"].records[0]
return new_inv_send.inv_send
return None

def helper_inv_send_get_id(self, result):
"""
Simple helper function to get the waybill reference of the newly
created inv_send row so it can be used to filter dataTables
"""
# The newly created inv_send will be the first record in the "after" list
if len(result["after"]) > 0:
new_inv_send = result["after"].records[0]
return new_inv_send.inv_send.id
return None

def helper_inv_send_get_ref(self, result):
"""
Simple helper function to get the waybill reference of the newly
created inv_send row so it can be used to filter dataTables
"""
# The newly created inv_send will be the first record in the "after" list
if len(result["after"]) > 0:
new_inv_send = result["after"].records[0]
return new_inv_send.inv_send.send_ref
return None

def helper_inv_track_item(self, user, send_id, data, removed=True):
"""
Helper method to add a track item to the inv_send with the
given send_id by the given user
"""
try:
add_btn = self.browser.find_element_by_id("show-add-btn")
if add_btn.is_displayed():
add_btn.click()
except:
pass
self.login(account=user, nexturl="inv/send/%s/track_item" % send_id)
table = "inv_track_item"
result = self.create(table, data, dbcallback = self.dbcallback_getStockLevels)
# Get the last record in the before & after
# this will give the stock record which has been added to the end by
# the getStockLevels callback
if removed:
qnty = 0
for line in data:
if line[0] == "quantity":
qnty = float(line[1])
break
stock_before = result["before"].records[len(result["before"])-1].quantity
stock_after = result["after"].records[len(result["after"])-1].quantity
stock_shipped = qnty
self.assertTrue( stock_before - stock_after == stock_shipped, "Warehouse stock not properly adjusted, was %s should be %s but is recorded as %s" % (stock_before, stock_after, stock_before - stock_shipped))
return result

# dbcallback for the inv_track item create function
def dbcallback_getStockLevels(self, table, data, rows):
"""
Callback to add the total in stock for the selected item.
This can then be used to look at the value before and after
to ensure that the totals have been removed from the warehouse.
The stock row will be added to the *end* of the list of rows
"""
table = self.current.s3db["inv_inv_item"]
for details in data:
if details[0] == "send_inv_item_id":
inv_item_id = details[1]
break
stock_row = table[inv_item_id]
rows.records.append(stock_row)
return rows

def test_001_send(self):
""" Tests for Send Workflow """
self.login(account="normal", nexturl="inv/send/create")
data = [("site_id",
"Cruz Vermelha de Timor-Leste (CVTL) National Warehouse (Warehouse)",
"option",
Expand All @@ -26,34 +115,27 @@ def test_001_send(self):
"autocomplete",
)
]
table = "inv_send"
result = self.create(table, data)
result = self.helper_inv_send("normal", data)
send_id = self.helper_inv_send_get_id(result)

data = [("send_inv_item_id",
"Blankets - 123457 - Australian Red Cross",
"option",
"inv_widget",
),
("quantity",
"3",
),
]
table = "inv_track_item"
result = self.create(table, data, dbcallback = self.getStockLevels)
# Get the last record in the before & after (it should give the stock
stock_before = result["before"].records[len(result["before"])-1].quantity
stock_after = result["after"].records[len(result["after"])-1].quantity
stock_shipped = 3
self.assertTrue( stock_before - stock_after == stock_shipped, "Warehouse stock not properly adjusted, was %s should be %s but is recorded as %s" % (stock_before, stock_after, stock_before - stock_shipped))
return result

def getStockLevels(self, table, data, rows):
table = self.current.s3db["inv_inv_item"]
for details in data:
if details[0] == "send_inv_item_id":
inv_item_id = details[1]
break
stock_row = table[inv_item_id]
rows.records.append(stock_row)
return rows
result = self.helper_inv_track_item("normal", send_id, data)
data = [("send_inv_item_id",
"Jerry Cans - 123461 - Australian Red Cross",
"inv_widget",
),
("quantity",
"7",
),
]
result = self.helper_inv_track_item("normal", send_id, data)

# def test_inventory(self):
# """ Tests for Inventory """
Expand Down
19 changes: 16 additions & 3 deletions modules/tests/web2unittest.py
Expand Up @@ -67,7 +67,7 @@ def create(self,
for details in data:
el_id = "%s_%s" % (tablename, details[0])
el_value = details[1]
try:
if len(details) == 3:
el_type = details[2]
if el_type == "option":
el = browser.find_element_by_id(el_id)
Expand All @@ -80,7 +80,12 @@ def create(self,
raw_value = self.w_autocomplete(el_value,
el_id,
)
except:
elif el_type == "inv_widget":
raw_value = self.w_inv_item_select(el_value,
tablename,
details[0],
)
else:
el = browser.find_element_by_id(el_id)
el.send_keys(el_value)
raw_value = el_value
Expand Down Expand Up @@ -145,4 +150,12 @@ def w_autocomplete(self,
needle = None,
quiet = True,
):
return w_autocomplete(search, autocomplete, needle, quiet)
return w_autocomplete(search, autocomplete, needle, quiet)

def w_inv_item_select(self,
item_repr,
tablename,
field,
quiet = True,
):
return w_inv_item_select(item_repr, tablename, field, quiet)

0 comments on commit 0b5c147

Please sign in to comment.