def test_output_contains_extra_columns(self):
     """Output should contain extra columns when topn > 1."""
     stock = [{"text": "circuit", "id": "1", "Brand": ""}]
     output = add_commodities_to_stocks(stock, topn=3)
     assert all([
         key in output[0][0]
         for key in ("Jaccard", "Jaccard 2", "Jaccard 3",
                     "Commodity Code 2", "Commodity 3")
     ])
 def test_regression(self):
     """Rerun code on regression_test_stock_master.csv and make sure the results match."""
     #print(sys.path)
     stock_master = read_csv("regression_test_stock_master.csv")
     rows, fieldnames = commodity_matcher.add_commodities_to_stocks(stock_master[:100])
     all_matched = True
     for i, row in enumerate(rows):
         if not row['Commodity'] == stock_master[i]['Commodity']:
             print("Did not match, row "+str(i)+", "+row['text'])
             print("New: '"+row['Commodity']+"' vs. Original: '"+stock_master[i]['Commodity']+"'")
             all_matched = False
     assert(len(rows) == 100)
     assert(sorted(rows[0].keys()) == sorted(fieldnames))
     assert(sorted(fieldnames) == sorted(stock_master[0].keys()))
     if not all_matched:
         print("WARNING: all of the rows did not match. If the above results look fine, you may want to update regression_test_stock_master.csv.")
def api():
    if request.method == "GET":
        brand = request.args.get("brand", default="")
        text = request.args.get("description")
        rows = [{"Brand": brand, "id": "1", "text": text}]
        results = commodity_matcher.add_commodities_to_stocks(rows)
        msg = "Success!"
        return {
            "Results": results[0],
            "Errors": [],
            "Status": "finished",
            "Message": msg
        }
    else:
        data = request.json
        rows = []
        for i, row in enumerate(data["rows"]):
            if "brand" in row:
                brand = row["brand"]
            else:
                brand = ""
            rows.append({
                "Brand": brand,
                "id": str(i),
                "text": row["description"]
            })
        row_hash = str(hash(json.dumps(rows)))
        try:
            j = Job.fetch(row_hash, conn)
            status = j.get_status()
            if status == "finished":
                if isinstance(j.result, Exception):
                    print(j.result)
                    msg = "Something went wrong and now everything is on fire."
                    return {
                        "Results": [],
                        "Errors": [str(j.result)],
                        "Status": status,
                        "Message": msg
                    }
                else:
                    msg = "Success!"
                    return {
                        "Results": j.result[0],
                        "Errors": [],
                        "Status": status,
                        "Message": msg
                    }
            else:
                msg = "Processing. Ask again in a few minutes to see your results."
                if status == "failed":
                    msg = "Time limit exceeded. Try a smaller job."
                return {
                    "Results": [],
                    "Errors": [],
                    "Status": status,
                    "Message": msg
                }

        except NoSuchJobError:
            j = q.enqueue(commodity_matcher.add_commodities_to_stocks,
                          rows,
                          job_id=row_hash)
            msg = "Processing. Ask again in a few minutes to see your results."
            return {
                "Results": [],
                "Errors": [],
                "Status": j.get_status(),
                "Message": msg
            }
 def test_should_not_affect_input(self):
     """Should not modify input list in place."""
     stock = [{"text": "circuit", "id": "1", "Brand": ""}]
     stock_copy = copy.deepcopy(stock)
     output = add_commodities_to_stocks(stock)
     assert stock == stock_copy
 def test_output_contains_all_input_keys(self):
     """All keys in input dictionaries should also exist in output dictionaries."""
     stock = [{"text": "circuit", "id": "1", "Brand": ""}]
     output = add_commodities_to_stocks(stock)
     assert all([key in output[0][0] for key in stock[0]])