58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
CALC = ROOT / "tools" / "est-test" / "calc.js"
|
|
|
|
|
|
def calculate(data):
|
|
script = 'const vm=require("vm"), fs=require("fs"); const ctx={}; vm.runInNewContext(fs.readFileSync(process.argv[1],"utf8")+";this.calc=EstCalc",ctx); console.log(JSON.stringify(ctx.calc.calculate(JSON.parse(process.argv[2]))));'
|
|
return json.loads(
|
|
subprocess.check_output(
|
|
["node", "-e", script, str(CALC), json.dumps(data)], text=True, encoding="utf-8"
|
|
)
|
|
)
|
|
|
|
|
|
def test_est_test_calculation_and_linked_price_change():
|
|
data = {
|
|
"sources": [{"id": "s", "kind": "재료비", "price": 10.129}],
|
|
"units": [{"id": "u", "lines": [{"sourceId": "s", "quantity": 2}]}],
|
|
"estimate": [{"unitId": "u", "quantity": 3}],
|
|
"costs": [{"base": "직접공사비", "rate": 10}, {"base": "누계", "rate": 5}],
|
|
}
|
|
result = calculate(data)
|
|
assert result["units"]["u"]["lines"][0]["amount"] == 20.25
|
|
assert result["directTotal"] == 60
|
|
assert [row["amount"] for row in result["costs"]] == [6, 3]
|
|
assert result["total"] == 69
|
|
data["sources"][0]["price"] = 20
|
|
assert calculate(data)["total"] == 138
|
|
|
|
|
|
def test_est_test_unlinked_rows_contribute_zero():
|
|
data = {
|
|
"sources": [],
|
|
"units": [{"id": "u", "lines": [{"sourceId": "", "quantity": 2}]}],
|
|
"estimate": [{"unitId": "missing", "quantity": 4}],
|
|
"costs": [],
|
|
}
|
|
result = calculate(data)
|
|
assert result["units"]["u"]["total"] == result["total"] == 0
|
|
|
|
|
|
def test_progress_uses_approved_quantity_only_when_selected():
|
|
script = (
|
|
'const vm=require("vm"),fs=require("fs");const c={};'
|
|
'vm.runInNewContext(fs.readFileSync(process.argv[1],"utf8")+";this.calc=EstCalc",c);'
|
|
"console.log(JSON.stringify([c.calc.progress(100,2,3,1,false),"
|
|
"c.calc.progress(100,2,3,1,true)]));"
|
|
)
|
|
ordinary, approved = json.loads(
|
|
subprocess.check_output(["node", "-e", script, str(CALC)], text=True, encoding="utf-8")
|
|
)
|
|
assert ordinary["currentAmount"] == 300
|
|
assert ordinary["cumulativeAmount"] == 500
|
|
assert approved["cumulativeAmount"] == 300
|