Esempio n. 1
0
def test_simple_exports():
    """
    check we can understand a simple variable assignment
    """
    testdata = {"code": "x = 5",
                "frames": [],
                "hash": "doesnmatter"}
    result = analyze_code(testdata)
    print(result)
    assert(result["exports"][0]=="x")
Esempio n. 2
0
def test_non_assignment_imports():
    """
    variables can be used in statements that are not assignments..
    """
    code = "a.dosomething(b,c)" # a, b and c should be imports
    testdata = {"code": code,
                "frames": ["a","b","c"],
                "hash": "irrelevant"}
    result = analyze_code(testdata)
    print(result)
    assert(sorted(result["imports"]) == ["a","b","c"])
Esempio n. 3
0
def test_confusing_names():
    """
    Need to tell the difference between e.g. "x" and "xx"
    """
    testdata = {"code": "xx = xxx(x) + yy",
                "frames": ["x","y"],
                "hash": "doesntmatter"}
    result = analyze_code(testdata)
    print(result)
    assert(result["exports"]==["xx"])
    assert(result["imports"]==["x"])
Esempio n. 4
0
def test_multiple_assignment():
    """
    function might have two return values to unpack
    """
    testdata = {"code": "x,y = funcoutput(z)",
                "frames": ["z","a","b"],
                "hash": "doesntmatter"}
    result = analyze_code(testdata)
    print(result)
    assert(sorted(result["exports"]) == ["x","y"])
    assert(result["imports"] == ["z"])
Esempio n. 5
0
def test_simple_imports():
    """
    check we can understand a simple variable assignment where
    one term on the rhs is an existing frame
    """
    testdata = {"code": "x = y + 5",
                "frames": ["y"],
                "hash": "doesnmatter"}
    result = analyze_code(testdata)
    print(result)
    assert(result["imports"][0]=="y")
Esempio n. 6
0
def test_imports_used_in_functions():
    """
    imported variables can be used inside functions.
    """
    code = 'def hello():\n    x = a + b\n    return x\n\nnewx = hello()\n\n'
    testdata = {"code": code,
                "frames": ["a","b"],
                "hash": "irrelevant"}
    result = analyze_code(testdata)
    print(result)
    assert(result["exports"]==["newx"])
    assert(sorted(result["imports"])==["a","b"])
Esempio n. 7
0
def test_out_of_scope_assignments():
    """
    variables assigned e.g. inside function definitions should not
    be added to exports
    """
    code = 'def hello():\n    x = pd.DataFrame({"xx":[1,2,3]})\n    return x\n\nnewx = hello()\n\n'
    testdata = {"code": code,
                "frames": [],
                "hash": "irrelevant"}
    result = analyze_code(testdata)
    print(result)
    assert(result["exports"]==["newx"])
Esempio n. 8
0
def test_exports_same_var_twice():
    """
    We only want to upload a dataframe once, even if
    the variable appears in the code fragment multiple times.
    """
    code = "x = a + b\nx = c + d\n"
    testdata = {"code": code,
                "frames": ["a","b","c"],
                "hash": "irrelevant"}
    result = analyze_code(testdata)
    print(result)
    assert(result["exports"].count("x")==1)
Esempio n. 9
0
def exports():
    data = json.loads(request.data.decode("utf-8"))
    imports_exports = analyze_code(data)
    return jsonify(imports_exports)