forked from darklang/dark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestIntrospect.res
116 lines (108 loc) · 3.01 KB
/
TestIntrospect.res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
open Prelude
open Tester
open Introspect
module TL = Toplevel
module B = BlankOr
let run = () => {
describe("Introspect", () => {
let h1tlid = gtlid()
let h1data: PT.Handler.t = {
ast: FluidAST.ofExpr(EBlank(gid())),
spec: PT.Handler.Spec.newWorker("processOrder"),
tlid: h1tlid,
pos: {x: 0, y: 0},
}
let h2tlid = gtlid()
let dbRefID = gid()
let h2data: PT.Handler.t = {
ast: FluidAST.ofExpr(
EFnCall(
gid(),
Stdlib({module_: "DB", function: "deleteAll", version: 1}),
list{EVariable(dbRefID, "Books")},
NoRail,
),
),
spec: PT.Handler.Spec.newHTTP("/hello", "GET"),
tlid: h2tlid,
pos: {x: 0, y: 0},
}
let dbtlid = gtlid()
let dbdata: PT.DB.t = {
tlid: dbtlid,
name: "Books",
nameID: gid(),
cols: list{},
version: 0,
pos: {x: 0, y: 0},
}
let dbs = TD.fromList(list{(dbdata.tlid, dbdata)})
let handlers = TD.fromList(list{(h1data.tlid, h1data), (h2data.tlid, h2data)})
test("dbsByName", () =>
expect(dbsByName(dbs)) |> toEqual(Map.add(~key="Books", ~value=dbtlid, Map.String.empty))
)
test("handlersByName", () => {
let v = handlers |> handlersByName |> Map.get(~key="WORKER:processOrder")
expect(v) |> toEqual(Some(h1tlid))
})
test("findUsagesInAST", () => {
let handlers = handlersByName(handlers)
let datastores = dbsByName(dbs)
let functions = Map.String.empty
let packageFunctions = Map.String.empty
let usages = switch findUsagesInAST(
h2tlid,
~datastores,
~handlers,
~functions,
~packageFunctions,
h2data.ast,
) {
| list{{refersTo, usedIn, id}} => refersTo == h2tlid && (usedIn == dbtlid && id === dbRefID)
| _ => false
}
expect(usages) |> toEqual(true)
})
test("tlidsToUpdateUsage", () => {
let fntlid = gtlid()
let ops = list{
PT.Op.SetHandler(h1tlid, {x: 0, y: 0}, h1data),
SetExpr(h1tlid, gid(), EBlank(gid())),
SetFunction({
tlid: fntlid,
name: "trollClean",
nameID: gid(),
parameters: list{},
description: "can users put docs here?",
returnType: TVariable("b"),
returnTypeID: gid(),
infix: false,
body: FluidAST.ofExpr(FluidExpression.newB()),
}),
}
expect(tlidsToUpdateUsage(ops)) |> toEqual(list{h1tlid, fntlid})
})
test("updateAssocList from empty", () =>
expect(
updateAssocList(~key="a", list{}, ~f=u =>
switch u {
| Some(v) => Some(v)
| None => Some(1)
}
),
) |> toEqual(list{("a", 1)})
)
test("updateAssocList add non existing", () =>
expect(
updateAssocList(~key="b", list{("a", 1)}, ~f=u =>
switch u {
| Some(v) => Some(v)
| None => Some(1)
}
),
) |> toEqual(list{("a", 1), ("b", 1)})
)
()
})
()
}