@malloy-publisher/sdk 0.12.1 → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -6
- package/dist/{RenderedResult-DrNhQ2_9.cjs.js → RenderedResult-C2arX5Zz.cjs.js} +1 -1
- package/dist/{RenderedResult-AyM-RQaA.es.js → RenderedResult-DqPvGZgL.es.js} +2 -2
- package/dist/ServerProvider-CqL6xzta.cjs.js +1 -0
- package/dist/{ServerProvider-a9Ki0QXO.es.js → ServerProvider-DRbZ6tLP.es.js} +1213 -1187
- package/dist/builder/index.cjs.js +1 -1
- package/dist/builder/index.es.js +3 -3
- package/dist/client/api.d.ts +43 -13
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +28 -27
- package/dist/components/Model/ExplorerPage.d.ts +9 -0
- package/dist/components/Model/Model.d.ts +27 -11
- package/dist/components/ModelViewer/AccessChips.d.ts +34 -0
- package/dist/components/ModelViewer/EmptyRow.d.ts +23 -0
- package/dist/components/ModelViewer/Facts.d.ts +15 -0
- package/dist/components/ModelViewer/FieldGroup.d.ts +31 -0
- package/dist/components/ModelViewer/FieldRow.d.ts +69 -0
- package/dist/components/ModelViewer/GivenFacts.d.ts +21 -0
- package/dist/components/ModelViewer/HeaderBar.d.ts +75 -0
- package/dist/components/ModelViewer/InlineDoc.d.ts +13 -0
- package/dist/components/ModelViewer/ModelGivensSection.d.ts +32 -0
- package/dist/components/ModelViewer/ModelSection.d.ts +21 -0
- package/dist/components/ModelViewer/ModelViewer.d.ts +91 -0
- package/dist/components/ModelViewer/QueriesGroup.d.ts +15 -0
- package/dist/components/ModelViewer/RowGlyph.d.ts +27 -0
- package/dist/components/ModelViewer/RunResult.d.ts +46 -0
- package/dist/components/ModelViewer/SampleValues.d.ts +20 -0
- package/dist/components/ModelViewer/SchemaJoin.d.ts +51 -0
- package/dist/components/ModelViewer/SchemaView.d.ts +52 -0
- package/dist/components/ModelViewer/SegmentIcons.d.ts +70 -0
- package/dist/components/ModelViewer/SourcesDropdown.d.ts +53 -0
- package/dist/components/ModelViewer/SourcesNav.d.ts +25 -0
- package/dist/components/ModelViewer/StatusPill.d.ts +56 -0
- package/dist/components/ModelViewer/ValueChip.d.ts +17 -0
- package/dist/components/ModelViewer/ViewRow.d.ts +36 -0
- package/dist/components/ModelViewer/clickableBox.d.ts +29 -0
- package/dist/components/ModelViewer/codeDisplay.d.ts +44 -0
- package/dist/components/ModelViewer/docAnnotations.d.ts +90 -0
- package/dist/components/ModelViewer/explorerModel.d.ts +12 -0
- package/dist/components/ModelViewer/extensions.d.ts +94 -0
- package/dist/components/ModelViewer/fieldCode.d.ts +74 -0
- package/dist/components/ModelViewer/fieldEntities.d.ts +45 -0
- package/dist/components/ModelViewer/fieldExpansion.d.ts +41 -0
- package/dist/components/ModelViewer/givenUsage.d.ts +71 -0
- package/dist/components/ModelViewer/headerCard.d.ts +31 -0
- package/dist/components/ModelViewer/httpStatus.d.ts +4 -0
- package/dist/components/ModelViewer/icons.d.ts +31 -0
- package/dist/components/ModelViewer/index.d.ts +9 -0
- package/dist/components/ModelViewer/joinAccess.d.ts +52 -0
- package/dist/components/ModelViewer/malloyRunText.d.ts +22 -0
- package/dist/components/ModelViewer/malloyText.d.ts +21 -0
- package/dist/components/ModelViewer/modelGivens.d.ts +61 -0
- package/dist/components/ModelViewer/modelGivensContext.d.ts +9 -0
- package/dist/components/ModelViewer/modelHeaderPresentation.d.ts +28 -0
- package/dist/components/ModelViewer/modelTextDeclarations.d.ts +32 -0
- package/dist/components/ModelViewer/modelTextParse.d.ts +27 -0
- package/dist/components/ModelViewer/namedQueryCode.d.ts +12 -0
- package/dist/components/ModelViewer/offSurface.d.ts +54 -0
- package/dist/components/ModelViewer/rowLayers.d.ts +30 -0
- package/dist/components/ModelViewer/runGate.d.ts +49 -0
- package/dist/components/ModelViewer/schemaModel.d.ts +109 -0
- package/dist/components/ModelViewer/sectionOpenCommand.d.ts +45 -0
- package/dist/components/ModelViewer/sourceAccess.d.ts +67 -0
- package/dist/components/ModelViewer/sourceLineage.d.ts +14 -0
- package/dist/components/ModelViewer/sourcePicker.d.ts +66 -0
- package/dist/components/ModelViewer/surface.d.ts +47 -0
- package/dist/components/ModelViewer/text.d.ts +6 -0
- package/dist/components/ModelViewer/theme.d.ts +63 -0
- package/dist/components/ModelViewer/tokens.d.ts +52 -0
- package/dist/components/ModelViewer/ui.d.ts +48 -0
- package/dist/components/ModelViewer/useDimensionSamples.d.ts +76 -0
- package/dist/components/ModelViewer/useModelSources.d.ts +92 -0
- package/dist/components/ModelViewer/useRunQuery.d.ts +115 -0
- package/dist/components/ReportBuilder/loadMalloy.d.ts +4 -0
- package/dist/components/ServerProvider.d.ts +15 -1
- package/dist/components/filter/utils.d.ts +1 -1
- package/dist/components/given/GivenInput.d.ts +7 -1
- package/dist/components/given/GivensPanel.d.ts +3 -1
- package/dist/components/given/filterSummary.d.ts +6 -0
- package/dist/components/given/useGivenUrlParams.d.ts +11 -0
- package/dist/components/index.d.ts +2 -1
- package/dist/components/modelPage.d.ts +36 -0
- package/dist/hooks/useDimensionalFilterRangeData.d.ts +9 -0
- package/dist/hooks/useSuggestOptions.d.ts +26 -2
- package/dist/index.cjs.js +47 -19
- package/dist/index.es.js +14281 -9879
- package/dist/useCompiledDocument--Z7vKZL5.cjs.js +143 -0
- package/dist/{useCompiledDocument-Utj2u8CG.es.js → useCompiledDocument-BnT2QpG0.es.js} +6975 -6893
- package/dist/utils/filterCondition.d.ts +14 -0
- package/dist/utils/malloyLiteral.d.ts +20 -1
- package/package.json +2 -2
- package/src/components/Model/ExplorerPage.tsx +269 -0
- package/src/components/Model/Model.tsx +66 -250
- package/src/components/Model/ModelExplorer.spec.tsx +64 -1
- package/src/components/ModelViewer/AccessChips.tsx +149 -0
- package/src/components/ModelViewer/EmptyRow.tsx +71 -0
- package/src/components/ModelViewer/Facts.tsx +46 -0
- package/src/components/ModelViewer/FieldGroup.tsx +137 -0
- package/src/components/ModelViewer/FieldRow.tsx +566 -0
- package/src/components/ModelViewer/GivenFacts.tsx +89 -0
- package/src/components/ModelViewer/HeaderBar.tsx +362 -0
- package/src/components/ModelViewer/InlineDoc.tsx +50 -0
- package/src/components/ModelViewer/ModelGivensSection.tsx +142 -0
- package/src/components/ModelViewer/ModelSection.tsx +98 -0
- package/src/components/ModelViewer/ModelViewer.spec.tsx +937 -0
- package/src/components/ModelViewer/ModelViewer.tsx +777 -0
- package/src/components/ModelViewer/QueriesGroup.tsx +107 -0
- package/src/components/ModelViewer/RowGlyph.tsx +138 -0
- package/src/components/ModelViewer/RunResult.tsx +298 -0
- package/src/components/ModelViewer/SampleValues.tsx +97 -0
- package/src/components/ModelViewer/SchemaJoin.tsx +258 -0
- package/src/components/ModelViewer/SchemaView.tsx +222 -0
- package/src/components/ModelViewer/SegmentIcons.tsx +129 -0
- package/src/components/ModelViewer/SourcesDropdown.tsx +265 -0
- package/src/components/ModelViewer/SourcesNav.tsx +75 -0
- package/src/components/ModelViewer/StatusPill.tsx +145 -0
- package/src/components/ModelViewer/ValueChip.tsx +39 -0
- package/src/components/ModelViewer/ViewRow.tsx +274 -0
- package/src/components/ModelViewer/clickableBox.ts +59 -0
- package/src/components/ModelViewer/codeDisplay.spec.ts +176 -0
- package/src/components/ModelViewer/codeDisplay.ts +118 -0
- package/src/components/ModelViewer/docAnnotations.spec.ts +181 -0
- package/src/components/ModelViewer/docAnnotations.ts +244 -0
- package/src/components/ModelViewer/docAnnotations.typeDetail.spec.ts +114 -0
- package/src/components/ModelViewer/explorerModel.spec.ts +43 -0
- package/src/components/ModelViewer/explorerModel.ts +31 -0
- package/src/components/ModelViewer/extensions.tsx +112 -0
- package/src/components/ModelViewer/fieldCode.spec.ts +168 -0
- package/src/components/ModelViewer/fieldCode.ts +118 -0
- package/src/components/ModelViewer/fieldEntities.ts +63 -0
- package/src/components/ModelViewer/fieldExpansion.spec.ts +116 -0
- package/src/components/ModelViewer/fieldExpansion.ts +95 -0
- package/src/components/ModelViewer/givenUsage.spec.ts +304 -0
- package/src/components/ModelViewer/givenUsage.ts +325 -0
- package/src/components/ModelViewer/headerCard.tsx +102 -0
- package/src/components/ModelViewer/httpStatus.ts +15 -0
- package/src/components/ModelViewer/icons.tsx +67 -0
- package/src/components/ModelViewer/index.ts +26 -0
- package/src/components/ModelViewer/joinAccess.spec.ts +95 -0
- package/src/components/ModelViewer/joinAccess.ts +54 -0
- package/src/components/ModelViewer/malloyRunText.spec.ts +48 -0
- package/src/components/ModelViewer/malloyRunText.ts +51 -0
- package/src/components/ModelViewer/malloyText.spec.ts +47 -0
- package/src/components/ModelViewer/malloyText.ts +77 -0
- package/src/components/ModelViewer/modelGivens.ts +228 -0
- package/src/components/ModelViewer/modelGivensContext.ts +13 -0
- package/src/components/ModelViewer/modelHeaderPresentation.spec.ts +31 -0
- package/src/components/ModelViewer/modelHeaderPresentation.ts +41 -0
- package/src/components/ModelViewer/modelTextDeclarations.spec.ts +336 -0
- package/src/components/ModelViewer/modelTextDeclarations.ts +228 -0
- package/src/components/ModelViewer/modelTextParse.ts +112 -0
- package/src/components/ModelViewer/namedQueryCode.spec.ts +58 -0
- package/src/components/ModelViewer/namedQueryCode.ts +26 -0
- package/src/components/ModelViewer/offSurface.tsx +124 -0
- package/src/components/ModelViewer/rowLayers.tsx +104 -0
- package/src/components/ModelViewer/runGate.spec.ts +219 -0
- package/src/components/ModelViewer/runGate.ts +145 -0
- package/src/components/ModelViewer/schemaModel.defaultSource.spec.ts +126 -0
- package/src/components/ModelViewer/schemaModel.queries.spec.ts +37 -0
- package/src/components/ModelViewer/schemaModel.ts +300 -0
- package/src/components/ModelViewer/sectionOpenCommand.tsx +125 -0
- package/src/components/ModelViewer/sourceAccess.spec.ts +130 -0
- package/src/components/ModelViewer/sourceAccess.ts +194 -0
- package/src/components/ModelViewer/sourceLineage.spec.ts +73 -0
- package/src/components/ModelViewer/sourceLineage.ts +38 -0
- package/src/components/ModelViewer/sourcePicker.spec.ts +294 -0
- package/src/components/ModelViewer/sourcePicker.ts +162 -0
- package/src/components/ModelViewer/surface.ts +51 -0
- package/src/components/ModelViewer/text.tsx +33 -0
- package/src/components/ModelViewer/theme.spec.ts +38 -0
- package/src/components/ModelViewer/theme.ts +58 -0
- package/src/components/ModelViewer/tokens.ts +87 -0
- package/src/components/ModelViewer/ui.tsx +201 -0
- package/src/components/ModelViewer/useDimensionSamples.spec.tsx +139 -0
- package/src/components/ModelViewer/useDimensionSamples.ts +220 -0
- package/src/components/ModelViewer/useModelSources.spec.ts +110 -0
- package/src/components/ModelViewer/useModelSources.ts +238 -0
- package/src/components/ModelViewer/useRunQuery.spec.ts +50 -0
- package/src/components/ModelViewer/useRunQuery.ts +223 -0
- package/src/components/ReportBuilder/emptyStart.spec.ts +8 -0
- package/src/components/ReportBuilder/loadMalloy.ts +8 -1
- package/src/components/ServerProvider.tsx +27 -3
- package/src/components/filter/utils.ts +4 -60
- package/src/components/given/FilterButton.tsx +4 -1
- package/src/components/given/GivenInput.tsx +8 -0
- package/src/components/given/GivensPanel.tsx +6 -1
- package/src/components/given/filterSummary.spec.ts +31 -1
- package/src/components/given/filterSummary.ts +41 -19
- package/src/components/given/useGivenUrlParams.spec.tsx +32 -0
- package/src/components/given/useGivenUrlParams.ts +49 -14
- package/src/components/index.ts +6 -1
- package/src/components/legacy/malloynb/Notebook.spec.tsx +38 -4
- package/src/components/legacy/malloynb/NotebookCell.tsx +8 -2
- package/src/components/modelPage.spec.ts +79 -0
- package/src/components/modelPage.ts +48 -0
- package/src/hooks/useDimensionFilters.ts +3 -58
- package/src/hooks/useDimensionFiltersQuery.ts +3 -58
- package/src/hooks/useDimensionalFilterRangeData.spec.ts +57 -0
- package/src/hooks/useDimensionalFilterRangeData.ts +8 -63
- package/src/hooks/useSuggestOptions.dom.spec.ts +54 -1
- package/src/hooks/useSuggestOptions.spec.ts +71 -0
- package/src/hooks/useSuggestOptions.ts +102 -12
- package/src/utils/filterCondition.spec.ts +87 -0
- package/src/utils/filterCondition.ts +66 -0
- package/src/utils/malloyLiteral.spec.ts +53 -1
- package/src/utils/malloyLiteral.ts +47 -12
- package/dist/ServerProvider-BvD7PYZ7.cjs.js +0 -1
- package/dist/useCompiledDocument-CRhFBKv_.cjs.js +0 -142
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
5
|
+
import { beforeEach, describe, expect, it, mock } from "bun:test";
|
|
6
|
+
import {
|
|
7
|
+
cacheKeys,
|
|
8
|
+
clearCache,
|
|
9
|
+
mockServerProvider,
|
|
10
|
+
serverWrapper,
|
|
11
|
+
} from "../../../test/serverProvider";
|
|
12
|
+
|
|
13
|
+
const string = { kind: "string_type" };
|
|
14
|
+
const number = { kind: "number_type" };
|
|
15
|
+
const items = {
|
|
16
|
+
name: "items",
|
|
17
|
+
schema: {
|
|
18
|
+
fields: [
|
|
19
|
+
{
|
|
20
|
+
kind: "dimension",
|
|
21
|
+
name: "name",
|
|
22
|
+
type: string,
|
|
23
|
+
annotations: [{ value: "#(index)\n" }],
|
|
24
|
+
},
|
|
25
|
+
{ kind: "measure", name: "item_count", type: number },
|
|
26
|
+
{ kind: "calculate", name: "running_total", type: number },
|
|
27
|
+
{ kind: "view", name: "by_name", schema: { fields: [] } },
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
const other = {
|
|
32
|
+
name: "other",
|
|
33
|
+
schema: { fields: [{ kind: "dimension", name: "code", type: string }] },
|
|
34
|
+
};
|
|
35
|
+
const sourceText = `source: items is duckdb.table('items.parquet') extend {
|
|
36
|
+
dimension: name is upper(raw_name)
|
|
37
|
+
measure: item_count is count()
|
|
38
|
+
view: by_name is { group_by: name }
|
|
39
|
+
}
|
|
40
|
+
source: other is duckdb.table('other.parquet')
|
|
41
|
+
query: all_names is items -> by_name
|
|
42
|
+
`;
|
|
43
|
+
const model = {
|
|
44
|
+
data: {
|
|
45
|
+
sourceInfos: [JSON.stringify(items), JSON.stringify(other)],
|
|
46
|
+
queries: [{ name: "all_names", sourceName: "items" }],
|
|
47
|
+
sourceText,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
const result = JSON.stringify({
|
|
51
|
+
schema: { fields: [{ kind: "dimension", name: "name", type: string }] },
|
|
52
|
+
data: {
|
|
53
|
+
kind: "array_cell",
|
|
54
|
+
array_value: [
|
|
55
|
+
{
|
|
56
|
+
kind: "record_cell",
|
|
57
|
+
record_value: [
|
|
58
|
+
{ kind: "string_cell", string_value: `27" Monitor` },
|
|
59
|
+
],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const getModel = mock(
|
|
66
|
+
(..._args: unknown[]): Promise<{ data: unknown }> => Promise.resolve(model),
|
|
67
|
+
);
|
|
68
|
+
const executeQueryModel = mock((..._args: unknown[]) =>
|
|
69
|
+
Promise.resolve({ data: { result } }),
|
|
70
|
+
);
|
|
71
|
+
mockServerProvider({ models: { getModel, executeQueryModel } });
|
|
72
|
+
|
|
73
|
+
const { ModelViewer } = await import("./ModelViewer");
|
|
74
|
+
|
|
75
|
+
const URI = "publisher://environments/env/packages/pkg/models/shop.malloy";
|
|
76
|
+
const sentQueries = () =>
|
|
77
|
+
executeQueryModel.mock.calls.map(
|
|
78
|
+
(call) => (call[3] as { query: string }).query,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
clearCache();
|
|
83
|
+
getModel.mockClear();
|
|
84
|
+
executeQueryModel.mockClear();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("ModelViewer", () => {
|
|
88
|
+
it("opens on the source with views, its sections in reading order", async () => {
|
|
89
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
90
|
+
expect(
|
|
91
|
+
await screen.findByRole("heading", { name: "items", level: 1 }),
|
|
92
|
+
).toBeTruthy();
|
|
93
|
+
const sections = screen
|
|
94
|
+
.getAllByRole("region")
|
|
95
|
+
.map((r) => r.getAttribute("aria-label"));
|
|
96
|
+
expect(sections).toEqual([
|
|
97
|
+
"Views",
|
|
98
|
+
"Queries",
|
|
99
|
+
"Measures",
|
|
100
|
+
"Calculations",
|
|
101
|
+
"Dimensions",
|
|
102
|
+
]);
|
|
103
|
+
// Browsing costs nothing: no row runs until it is opened.
|
|
104
|
+
expect(executeQueryModel).not.toHaveBeenCalled();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("runs an opened view by name, with no value in the text", async () => {
|
|
108
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
109
|
+
fireEvent.click(await screen.findByRole("button", { name: /by_name/ }));
|
|
110
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
111
|
+
expect(sentQueries()).toEqual(["run: `items` -> `by_name`"]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("runs a named query by name", async () => {
|
|
115
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
116
|
+
fireEvent.click(await screen.findByRole("button", { name: /all_names/ }));
|
|
117
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
118
|
+
expect(sentQueries()).toEqual(["run: `all_names`"]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("lists a tagged dimension's values from one quoted query", async () => {
|
|
122
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
123
|
+
fireEvent.click(await screen.findByRole("button", { name: /^name/ }));
|
|
124
|
+
expect(await screen.findByText(`27" Monitor`)).toBeTruthy();
|
|
125
|
+
const [query] = sentQueries();
|
|
126
|
+
expect(query).toContain("group_by: `name`");
|
|
127
|
+
expect(query).not.toContain("Monitor");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("shows the source a host names, and says so when the model has none by that name", async () => {
|
|
131
|
+
const { rerender } = render(
|
|
132
|
+
<ModelViewer resourceUri={URI} sourceName="other" />,
|
|
133
|
+
{ wrapper: serverWrapper },
|
|
134
|
+
);
|
|
135
|
+
expect(
|
|
136
|
+
await screen.findByRole("heading", { name: "other", level: 1 }),
|
|
137
|
+
).toBeTruthy();
|
|
138
|
+
rerender(<ModelViewer resourceUri={URI} sourceName="nope" />);
|
|
139
|
+
expect(
|
|
140
|
+
await screen.findByText("This model has no source named nope."),
|
|
141
|
+
).toBeTruthy();
|
|
142
|
+
expect(screen.getByRole("button", { name: "items" })).toBeTruthy();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("hands a picked source to its host", async () => {
|
|
146
|
+
const onSourceChange = mock((_name: string) => {});
|
|
147
|
+
render(
|
|
148
|
+
<ModelViewer
|
|
149
|
+
resourceUri={URI}
|
|
150
|
+
sourceName="items"
|
|
151
|
+
onSourceChange={onSourceChange}
|
|
152
|
+
/>,
|
|
153
|
+
{ wrapper: serverWrapper },
|
|
154
|
+
);
|
|
155
|
+
fireEvent.click(await screen.findByRole("button", { name: /2 sources/ }));
|
|
156
|
+
fireEvent.click(
|
|
157
|
+
await screen.findByRole("button", { name: /Open source other/ }),
|
|
158
|
+
);
|
|
159
|
+
expect(onSourceChange.mock.calls).toEqual([["other"]]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("says what the server said when the model isn't there", async () => {
|
|
163
|
+
getModel.mockImplementationOnce(() =>
|
|
164
|
+
Promise.reject({
|
|
165
|
+
response: {
|
|
166
|
+
status: 404,
|
|
167
|
+
data: { message: "gone.malloy does not exist" },
|
|
168
|
+
},
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
render(
|
|
172
|
+
<ModelViewer resourceUri="publisher://environments/env/packages/pkg/models/gone.malloy" />,
|
|
173
|
+
{ wrapper: serverWrapper },
|
|
174
|
+
);
|
|
175
|
+
expect(
|
|
176
|
+
await screen.findByText("gone.malloy does not exist"),
|
|
177
|
+
).toBeTruthy();
|
|
178
|
+
expect(screen.queryByRole("button", { name: "Retry" })).toBeNull();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("draws a host's additions on a field's line", async () => {
|
|
182
|
+
render(
|
|
183
|
+
<ModelViewer
|
|
184
|
+
resourceUri={URI}
|
|
185
|
+
extensions={{
|
|
186
|
+
renderFieldStatus: ({ field }) =>
|
|
187
|
+
field.name === "item_count" ? <span>Rolled up</span> : null,
|
|
188
|
+
}}
|
|
189
|
+
/>,
|
|
190
|
+
{ wrapper: serverWrapper },
|
|
191
|
+
);
|
|
192
|
+
expect(await screen.findByText("Rolled up")).toBeTruthy();
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// A model whose `items` reads a given with no default, and a named query the
|
|
197
|
+
// model gives no source, over `other`, which reads none.
|
|
198
|
+
const gatedText = `source: items is duckdb.table('items.parquet') extend {
|
|
199
|
+
where: name = $REGION
|
|
200
|
+
dimension: name is upper(raw_name)
|
|
201
|
+
view: by_name is { group_by: name }
|
|
202
|
+
}
|
|
203
|
+
source: other is duckdb.table('other.parquet')
|
|
204
|
+
query: other_codes is other -> { group_by: code }
|
|
205
|
+
`;
|
|
206
|
+
const gatedModel = (given: Record<string, unknown> = {}) => ({
|
|
207
|
+
data: {
|
|
208
|
+
sourceInfos: [JSON.stringify(items), JSON.stringify(other)],
|
|
209
|
+
queries: [{ name: "other_codes" }],
|
|
210
|
+
sourceText: gatedText,
|
|
211
|
+
givens: [{ name: "REGION", type: "string", ...given }],
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
const sentBodies = () =>
|
|
215
|
+
executeQueryModel.mock.calls.map(
|
|
216
|
+
(call) =>
|
|
217
|
+
call[3] as {
|
|
218
|
+
query: string;
|
|
219
|
+
givens?: Record<string, unknown>;
|
|
220
|
+
queryMetadata?: Record<string, string>;
|
|
221
|
+
},
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
describe("ModelViewer givens", () => {
|
|
225
|
+
it("sends a host's given values as the request's givens, never in the text", async () => {
|
|
226
|
+
getModel.mockImplementation(() => Promise.resolve(gatedModel()));
|
|
227
|
+
try {
|
|
228
|
+
render(
|
|
229
|
+
<ModelViewer
|
|
230
|
+
resourceUri={URI}
|
|
231
|
+
// `source` is the host's own parameter, not a given.
|
|
232
|
+
givens={{ REGION: `x" or "a" = "a`, source: "items" }}
|
|
233
|
+
/>,
|
|
234
|
+
{ wrapper: serverWrapper },
|
|
235
|
+
);
|
|
236
|
+
fireEvent.click(
|
|
237
|
+
await screen.findByRole("button", { name: /by_name/ }),
|
|
238
|
+
);
|
|
239
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
240
|
+
const [body] = sentBodies();
|
|
241
|
+
expect(body.query).toBe("run: `items` -> `by_name`");
|
|
242
|
+
expect(body.givens).toEqual({ REGION: `x" or "a" = "a` });
|
|
243
|
+
} finally {
|
|
244
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("reports applied values to its host, with every name it manages", async () => {
|
|
249
|
+
getModel.mockImplementation(() => Promise.resolve(gatedModel()));
|
|
250
|
+
const onGivensChange = mock(
|
|
251
|
+
(_givens: Record<string, string>, _managed: readonly string[]) => {},
|
|
252
|
+
);
|
|
253
|
+
try {
|
|
254
|
+
render(
|
|
255
|
+
<ModelViewer
|
|
256
|
+
resourceUri={URI}
|
|
257
|
+
givens={{ REGION: "West" }}
|
|
258
|
+
onGivensChange={onGivensChange}
|
|
259
|
+
/>,
|
|
260
|
+
{ wrapper: serverWrapper },
|
|
261
|
+
);
|
|
262
|
+
fireEvent.click(await screen.findByRole("button", { name: "Reset" }));
|
|
263
|
+
await waitFor(() =>
|
|
264
|
+
expect(onGivensChange.mock.calls.at(-1)).toEqual([{}, ["REGION"]]),
|
|
265
|
+
);
|
|
266
|
+
} finally {
|
|
267
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("holds a run, and a dimension's values, that need a given with no value", async () => {
|
|
272
|
+
getModel.mockImplementation(() => Promise.resolve(gatedModel()));
|
|
273
|
+
try {
|
|
274
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
275
|
+
fireEvent.click(
|
|
276
|
+
await screen.findByRole("button", { name: /by_name/ }),
|
|
277
|
+
);
|
|
278
|
+
expect(
|
|
279
|
+
await screen.findByText(/This needs a value for the given REGION/),
|
|
280
|
+
).toBeTruthy();
|
|
281
|
+
fireEvent.click(screen.getByRole("button", { name: /^name/ }));
|
|
282
|
+
expect(
|
|
283
|
+
(await screen.findByTestId("model-viewer-values-unavailable"))
|
|
284
|
+
.textContent,
|
|
285
|
+
).toContain("REGION");
|
|
286
|
+
expect(executeQueryModel).not.toHaveBeenCalled();
|
|
287
|
+
} finally {
|
|
288
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("runs a named query with no source on the givens its text reads, not the model's every one", async () => {
|
|
293
|
+
getModel.mockImplementation(() => Promise.resolve(gatedModel()));
|
|
294
|
+
try {
|
|
295
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
296
|
+
fireEvent.click(
|
|
297
|
+
await screen.findByRole("button", { name: /other_codes/ }),
|
|
298
|
+
);
|
|
299
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
300
|
+
expect(sentQueries()).toEqual(["run: `other_codes`"]);
|
|
301
|
+
} finally {
|
|
302
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("leaves a given the host fills out of the section and the gate", async () => {
|
|
307
|
+
getModel.mockImplementation(() => Promise.resolve(gatedModel()));
|
|
308
|
+
try {
|
|
309
|
+
render(<ModelViewer resourceUri={URI} hostGivenNames={["REGION"]} />, {
|
|
310
|
+
wrapper: serverWrapper,
|
|
311
|
+
});
|
|
312
|
+
fireEvent.click(
|
|
313
|
+
await screen.findByRole("button", { name: /by_name/ }),
|
|
314
|
+
);
|
|
315
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
316
|
+
expect(screen.queryByTestId("model-givens-section")).toBeNull();
|
|
317
|
+
} finally {
|
|
318
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("reads a picker's options when it is opened, never on render", async () => {
|
|
323
|
+
getModel.mockImplementation(() =>
|
|
324
|
+
Promise.resolve(
|
|
325
|
+
gatedModel({
|
|
326
|
+
default: "West",
|
|
327
|
+
control: "select",
|
|
328
|
+
suggest: { source: "items", dimension: "name" },
|
|
329
|
+
}),
|
|
330
|
+
),
|
|
331
|
+
);
|
|
332
|
+
try {
|
|
333
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
334
|
+
const picker = await screen.findByRole("button", { name: /^REGION/ });
|
|
335
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
336
|
+
expect(executeQueryModel).not.toHaveBeenCalled();
|
|
337
|
+
fireEvent.click(picker);
|
|
338
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
339
|
+
expect(sentQueries()[0]).toContain("group_by: `name`");
|
|
340
|
+
} finally {
|
|
341
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
describe("ModelViewer host props", () => {
|
|
347
|
+
it("reads a hidden file, and keeps it apart from the curated read", async () => {
|
|
348
|
+
const { unmount } = render(<ModelViewer resourceUri={URI} />, {
|
|
349
|
+
wrapper: serverWrapper,
|
|
350
|
+
});
|
|
351
|
+
await screen.findByRole("heading", { name: "items", level: 1 });
|
|
352
|
+
unmount();
|
|
353
|
+
render(<ModelViewer resourceUri={URI} includeHiddenFilesAndSources />, {
|
|
354
|
+
wrapper: serverWrapper,
|
|
355
|
+
});
|
|
356
|
+
await screen.findByRole("heading", { name: "items", level: 1 });
|
|
357
|
+
expect(getModel.mock.calls.map((call) => call[4])).toEqual([false, true]);
|
|
358
|
+
// Two entries: the curated read's answer is never served for the hidden
|
|
359
|
+
// read, nor the hidden file's text to a reader who can't edit it.
|
|
360
|
+
const keys = cacheKeys("model-viewer-sources");
|
|
361
|
+
expect(keys).toHaveLength(2);
|
|
362
|
+
expect(keys.filter((key) => key.includes("off-surface"))).toHaveLength(1);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("says a field's code couldn't be read when a host's entities failed", async () => {
|
|
366
|
+
// `running_total` has no declaration in the file's text, so its code is
|
|
367
|
+
// the host's to give; a failed read says so rather than spinning.
|
|
368
|
+
render(<ModelViewer resourceUri={URI} entities={[]} entitiesError />, {
|
|
369
|
+
wrapper: serverWrapper,
|
|
370
|
+
});
|
|
371
|
+
fireEvent.click(
|
|
372
|
+
await screen.findByRole("button", { name: /^running_total/ }),
|
|
373
|
+
);
|
|
374
|
+
expect(
|
|
375
|
+
await screen.findByText("Couldn't load this model's code right now."),
|
|
376
|
+
).toBeTruthy();
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("sends its labels with every run and reports each run that settles", async () => {
|
|
380
|
+
const onQueryRun = mock((_event: unknown) => {});
|
|
381
|
+
render(
|
|
382
|
+
<ModelViewer
|
|
383
|
+
resourceUri={URI}
|
|
384
|
+
queryMetadata={{ surface: "test" }}
|
|
385
|
+
onQueryRun={onQueryRun}
|
|
386
|
+
/>,
|
|
387
|
+
{ wrapper: serverWrapper },
|
|
388
|
+
);
|
|
389
|
+
fireEvent.click(await screen.findByRole("button", { name: /by_name/ }));
|
|
390
|
+
await waitFor(() => expect(onQueryRun).toHaveBeenCalled());
|
|
391
|
+
expect(sentBodies()[0].queryMetadata).toEqual({ surface: "test" });
|
|
392
|
+
expect(onQueryRun.mock.calls[0][0]).toMatchObject({ outcome: "success" });
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it("shows a host's entities: a field's code, the table a source reads, a join's way out", async () => {
|
|
396
|
+
const joinHref = mock(
|
|
397
|
+
(target: { modelPath: string; sourceName: string }) =>
|
|
398
|
+
`/models/${target.modelPath}?source=${target.sourceName}`,
|
|
399
|
+
);
|
|
400
|
+
const withJoin = {
|
|
401
|
+
...items,
|
|
402
|
+
schema: {
|
|
403
|
+
fields: [
|
|
404
|
+
...items.schema.fields,
|
|
405
|
+
{
|
|
406
|
+
kind: "join",
|
|
407
|
+
name: "maker",
|
|
408
|
+
relationship: "one",
|
|
409
|
+
schema: {
|
|
410
|
+
fields: [
|
|
411
|
+
{ kind: "dimension", name: "country", type: string },
|
|
412
|
+
],
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
],
|
|
416
|
+
},
|
|
417
|
+
};
|
|
418
|
+
getModel.mockImplementation(() =>
|
|
419
|
+
Promise.resolve({
|
|
420
|
+
data: { ...model.data, sourceInfos: [JSON.stringify(withJoin)] },
|
|
421
|
+
}),
|
|
422
|
+
);
|
|
423
|
+
try {
|
|
424
|
+
const { container } = render(
|
|
425
|
+
<ModelViewer
|
|
426
|
+
resourceUri={URI}
|
|
427
|
+
joinHref={joinHref}
|
|
428
|
+
entities={[
|
|
429
|
+
{
|
|
430
|
+
name: "item_count",
|
|
431
|
+
source: "items",
|
|
432
|
+
fieldType: "measure",
|
|
433
|
+
code: "measure: item_count is count(distinct id)",
|
|
434
|
+
table: {
|
|
435
|
+
connectionName: "duckdb",
|
|
436
|
+
tableName: "items",
|
|
437
|
+
},
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
name: "maker",
|
|
441
|
+
source: "items",
|
|
442
|
+
fieldType: "join",
|
|
443
|
+
code: "join_one: maker is makers on maker_id = maker.id",
|
|
444
|
+
joinTarget: {
|
|
445
|
+
modelPath: "makers.malloy",
|
|
446
|
+
sourceName: "makers",
|
|
447
|
+
},
|
|
448
|
+
},
|
|
449
|
+
]}
|
|
450
|
+
/>,
|
|
451
|
+
{ wrapper: serverWrapper },
|
|
452
|
+
);
|
|
453
|
+
expect(
|
|
454
|
+
(await screen.findByTestId("model-viewer-source-table"))
|
|
455
|
+
.textContent,
|
|
456
|
+
).toBe("duckdb.items");
|
|
457
|
+
fireEvent.click(screen.getByRole("button", { name: /^item_count/ }));
|
|
458
|
+
expect(
|
|
459
|
+
(await screen.findByTestId("model-viewer-definition")).textContent,
|
|
460
|
+
).toContain("count(distinct id)");
|
|
461
|
+
expect(joinHref.mock.calls[0][0]).toEqual({
|
|
462
|
+
modelPath: "makers.malloy",
|
|
463
|
+
sourceName: "makers",
|
|
464
|
+
});
|
|
465
|
+
expect(
|
|
466
|
+
container.querySelector(
|
|
467
|
+
'a[href="/models/makers.malloy?source=makers"]',
|
|
468
|
+
),
|
|
469
|
+
).toBeTruthy();
|
|
470
|
+
} finally {
|
|
471
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it("marks a host's column description as generated", async () => {
|
|
476
|
+
render(
|
|
477
|
+
<ModelViewer
|
|
478
|
+
resourceUri={URI}
|
|
479
|
+
sourceName="other"
|
|
480
|
+
entities={[
|
|
481
|
+
{
|
|
482
|
+
name: "code",
|
|
483
|
+
source: "other",
|
|
484
|
+
fieldType: "column",
|
|
485
|
+
code: "duckdb.table('other.parquet')",
|
|
486
|
+
},
|
|
487
|
+
]}
|
|
488
|
+
columnDescriptions={
|
|
489
|
+
new Map([["other", new Map([["code", "The product code"]])]])
|
|
490
|
+
}
|
|
491
|
+
/>,
|
|
492
|
+
{ wrapper: serverWrapper },
|
|
493
|
+
);
|
|
494
|
+
expect(await screen.findByText("The product code")).toBeTruthy();
|
|
495
|
+
expect(
|
|
496
|
+
screen.getByTestId("model-viewer-field-doc-generated"),
|
|
497
|
+
).toBeTruthy();
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
describe("ModelViewer extensions", () => {
|
|
502
|
+
it("draws a host's chips and card on the source, and its summary and chips in the picker", async () => {
|
|
503
|
+
render(
|
|
504
|
+
<ModelViewer
|
|
505
|
+
resourceUri={URI}
|
|
506
|
+
renderSourceStatus={(name) => <span>Status of {name}</span>}
|
|
507
|
+
extensions={{
|
|
508
|
+
renderSourceDetail: ({ sourceName }) => (
|
|
509
|
+
<span>Built from {sourceName}</span>
|
|
510
|
+
),
|
|
511
|
+
describePickerSource: ({ sourceName }) =>
|
|
512
|
+
sourceName === "other"
|
|
513
|
+
? {
|
|
514
|
+
summary: "Other things.",
|
|
515
|
+
status: <span>Fresh</span>,
|
|
516
|
+
}
|
|
517
|
+
: undefined,
|
|
518
|
+
}}
|
|
519
|
+
/>,
|
|
520
|
+
{ wrapper: serverWrapper },
|
|
521
|
+
);
|
|
522
|
+
expect(await screen.findByText("Status of items")).toBeTruthy();
|
|
523
|
+
expect(screen.getByText("Built from items")).toBeTruthy();
|
|
524
|
+
fireEvent.click(screen.getByRole("button", { name: /2 sources/ }));
|
|
525
|
+
expect(await screen.findByText("Other things.")).toBeTruthy();
|
|
526
|
+
expect(screen.getByText("Fresh")).toBeTruthy();
|
|
527
|
+
expect(
|
|
528
|
+
screen.getByTestId("model-sources-summary-generated"),
|
|
529
|
+
).toBeTruthy();
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it("lists only the rows a host shows, and drops a section it empties", async () => {
|
|
533
|
+
render(
|
|
534
|
+
<ModelViewer
|
|
535
|
+
resourceUri={URI}
|
|
536
|
+
extensions={{
|
|
537
|
+
showField: ({ field }) => field.name !== "item_count",
|
|
538
|
+
}}
|
|
539
|
+
/>,
|
|
540
|
+
{ wrapper: serverWrapper },
|
|
541
|
+
);
|
|
542
|
+
await screen.findByRole("heading", { name: "items", level: 1 });
|
|
543
|
+
expect(screen.queryByRole("button", { name: /^item_count/ })).toBeNull();
|
|
544
|
+
// Its only measure not listed, the section goes with it.
|
|
545
|
+
expect(screen.queryByRole("region", { name: "Measures" })).toBeNull();
|
|
546
|
+
expect(screen.getByRole("region", { name: "Dimensions" })).toBeTruthy();
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
it("puts a host's action on a field's line, and its card in place of the row's", async () => {
|
|
550
|
+
render(
|
|
551
|
+
<ModelViewer
|
|
552
|
+
resourceUri={URI}
|
|
553
|
+
extensions={{
|
|
554
|
+
renderFieldAction: ({ field }) =>
|
|
555
|
+
field.name === "item_count" ? (
|
|
556
|
+
<button type="button">Rebuild</button>
|
|
557
|
+
) : null,
|
|
558
|
+
renderFieldDetail: ({ field }, builtIn) =>
|
|
559
|
+
field.name === "item_count" ? (
|
|
560
|
+
<span>Host card</span>
|
|
561
|
+
) : field.name === "running_total" ? null : (
|
|
562
|
+
builtIn
|
|
563
|
+
),
|
|
564
|
+
}}
|
|
565
|
+
/>,
|
|
566
|
+
{ wrapper: serverWrapper },
|
|
567
|
+
);
|
|
568
|
+
expect(
|
|
569
|
+
await screen.findByRole("button", { name: "Rebuild" }),
|
|
570
|
+
).toBeTruthy();
|
|
571
|
+
fireEvent.click(screen.getByRole("button", { name: /^item_count/ }));
|
|
572
|
+
expect(await screen.findByText("Host card")).toBeTruthy();
|
|
573
|
+
expect(screen.queryByTestId("model-viewer-field-card")).toBeNull();
|
|
574
|
+
// Nothing to show: the row doesn't open.
|
|
575
|
+
expect(
|
|
576
|
+
screen
|
|
577
|
+
.getByRole("region", { name: "Calculations" })
|
|
578
|
+
.querySelector("button[aria-expanded]"),
|
|
579
|
+
).toBeNull();
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
it("hands a run's actions its givens and cache key", async () => {
|
|
583
|
+
const renderRunActions = mock((_run: unknown) => (
|
|
584
|
+
<button type="button">Download</button>
|
|
585
|
+
));
|
|
586
|
+
render(
|
|
587
|
+
<ModelViewer resourceUri={URI} extensions={{ renderRunActions }} />,
|
|
588
|
+
{
|
|
589
|
+
wrapper: serverWrapper,
|
|
590
|
+
},
|
|
591
|
+
);
|
|
592
|
+
fireEvent.click(await screen.findByRole("button", { name: /by_name/ }));
|
|
593
|
+
expect(
|
|
594
|
+
await screen.findByRole("button", { name: "Download" }),
|
|
595
|
+
).toBeTruthy();
|
|
596
|
+
const run = renderRunActions.mock.calls.at(-1)?.[0] as {
|
|
597
|
+
query: string;
|
|
598
|
+
queryKey: readonly unknown[];
|
|
599
|
+
};
|
|
600
|
+
expect(run.query).toBe("run: `items` -> `by_name`");
|
|
601
|
+
expect(run.queryKey).toContain("run: `items` -> `by_name`");
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
it("shows a host's count of values, and its reason when there are none", async () => {
|
|
605
|
+
const fetchSamples = mock((request: { fieldName: string }) =>
|
|
606
|
+
Promise.resolve(
|
|
607
|
+
request.fieldName === "name"
|
|
608
|
+
? { values: ["a", "b"], total: 40 }
|
|
609
|
+
: { values: [] },
|
|
610
|
+
),
|
|
611
|
+
);
|
|
612
|
+
render(<ModelViewer resourceUri={URI} extensions={{ fetchSamples }} />, {
|
|
613
|
+
wrapper: serverWrapper,
|
|
614
|
+
});
|
|
615
|
+
fireEvent.click(await screen.findByRole("button", { name: /^name/ }));
|
|
616
|
+
expect(
|
|
617
|
+
await screen.findByText("The most common of 40 values"),
|
|
618
|
+
).toBeTruthy();
|
|
619
|
+
expect(executeQueryModel).not.toHaveBeenCalled();
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it("reads a host's values afresh for each generation it names", async () => {
|
|
623
|
+
const fetchSamples = mock(() => Promise.resolve(["a", "b"]));
|
|
624
|
+
const viewer = (samplesGeneration: string) => (
|
|
625
|
+
<ModelViewer
|
|
626
|
+
resourceUri={URI}
|
|
627
|
+
extensions={{ fetchSamples, samplesGeneration }}
|
|
628
|
+
/>
|
|
629
|
+
);
|
|
630
|
+
const { rerender } = render(viewer("r1"), { wrapper: serverWrapper });
|
|
631
|
+
fireEvent.click(await screen.findByRole("button", { name: /^name/ }));
|
|
632
|
+
await waitFor(() => expect(fetchSamples).toHaveBeenCalledTimes(1));
|
|
633
|
+
rerender(viewer("r1"));
|
|
634
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
635
|
+
expect(fetchSamples).toHaveBeenCalledTimes(1);
|
|
636
|
+
rerender(viewer("r2"));
|
|
637
|
+
await waitFor(() => expect(fetchSamples).toHaveBeenCalledTimes(2));
|
|
638
|
+
});
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
// `items` reads REGION; `other` reads CODE.
|
|
642
|
+
const twoGivenText = `source: items is duckdb.table('items.parquet') extend {
|
|
643
|
+
where: name = $REGION
|
|
644
|
+
dimension: name is upper(raw_name)
|
|
645
|
+
view: by_name is { group_by: name }
|
|
646
|
+
}
|
|
647
|
+
source: other is duckdb.table('other.parquet') extend {
|
|
648
|
+
where: code = $CODE
|
|
649
|
+
}
|
|
650
|
+
`;
|
|
651
|
+
const twoGivenModel = {
|
|
652
|
+
data: {
|
|
653
|
+
sourceInfos: [JSON.stringify(items), JSON.stringify(other)],
|
|
654
|
+
queries: [],
|
|
655
|
+
sourceText: twoGivenText,
|
|
656
|
+
givens: [
|
|
657
|
+
{ name: "REGION", type: "string", default: "All" },
|
|
658
|
+
{ name: "CODE", type: "string", default: "Any" },
|
|
659
|
+
],
|
|
660
|
+
},
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
describe("ModelViewer runs and the values they read", () => {
|
|
664
|
+
it("reports a dimension's sample read as a run, like a view's", async () => {
|
|
665
|
+
const onQueryRun = mock((_event: unknown) => {});
|
|
666
|
+
render(<ModelViewer resourceUri={URI} onQueryRun={onQueryRun} />, {
|
|
667
|
+
wrapper: serverWrapper,
|
|
668
|
+
});
|
|
669
|
+
fireEvent.click(await screen.findByRole("button", { name: /^name/ }));
|
|
670
|
+
expect(await screen.findByText(`27" Monitor`)).toBeTruthy();
|
|
671
|
+
expect(onQueryRun.mock.calls[0][0]).toMatchObject({ outcome: "success" });
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
it("sends and keys a run on the givens it reads, so another source's given leaves it current", async () => {
|
|
675
|
+
getModel.mockImplementation(() => Promise.resolve(twoGivenModel));
|
|
676
|
+
try {
|
|
677
|
+
const { rerender } = render(
|
|
678
|
+
<ModelViewer resourceUri={URI} givens={{ REGION: "West" }} />,
|
|
679
|
+
{ wrapper: serverWrapper },
|
|
680
|
+
);
|
|
681
|
+
fireEvent.click(
|
|
682
|
+
await screen.findByRole("button", { name: /by_name/ }),
|
|
683
|
+
);
|
|
684
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
685
|
+
rerender(
|
|
686
|
+
<ModelViewer
|
|
687
|
+
resourceUri={URI}
|
|
688
|
+
givens={{ REGION: "West", CODE: "X1" }}
|
|
689
|
+
/>,
|
|
690
|
+
);
|
|
691
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
692
|
+
expect(screen.queryByTestId("model-viewer-run-stale")).toBeNull();
|
|
693
|
+
expect(executeQueryModel).toHaveBeenCalledTimes(1);
|
|
694
|
+
expect(sentBodies()[0].givens).toEqual({ REGION: "West" });
|
|
695
|
+
} finally {
|
|
696
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
it("shows and sends a given its source may read through an import", async () => {
|
|
701
|
+
// `items` builds on `regional`, from the import, which may read REGION;
|
|
702
|
+
// `other` reads it here, so a scan that stopped at the file would settle
|
|
703
|
+
// that `items` doesn't, and its run would go out without the value.
|
|
704
|
+
const sourceText = `import "regions.malloy"
|
|
705
|
+
source: items is regional extend {
|
|
706
|
+
dimension: name is upper(raw_name)
|
|
707
|
+
view: by_name is { group_by: name }
|
|
708
|
+
}
|
|
709
|
+
source: other is duckdb.table('other.parquet') extend {
|
|
710
|
+
where: code = $REGION
|
|
711
|
+
}
|
|
712
|
+
`;
|
|
713
|
+
getModel.mockImplementation(() =>
|
|
714
|
+
Promise.resolve({
|
|
715
|
+
data: {
|
|
716
|
+
...twoGivenModel.data,
|
|
717
|
+
sourceText,
|
|
718
|
+
givens: [{ name: "REGION", type: "string" }],
|
|
719
|
+
},
|
|
720
|
+
}),
|
|
721
|
+
);
|
|
722
|
+
try {
|
|
723
|
+
render(<ModelViewer resourceUri={URI} givens={{ REGION: "East" }} />, {
|
|
724
|
+
wrapper: serverWrapper,
|
|
725
|
+
});
|
|
726
|
+
expect(await screen.findByTestId("model-givens-section")).toBeTruthy();
|
|
727
|
+
fireEvent.click(
|
|
728
|
+
await screen.findByRole("button", { name: /by_name/ }),
|
|
729
|
+
);
|
|
730
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
731
|
+
expect(sentBodies()[0].givens).toEqual({ REGION: "East" });
|
|
732
|
+
// The page can't see what `regional` reads, and says so in the section
|
|
733
|
+
// and on the run, rather than naming REGION as if it applied.
|
|
734
|
+
expect(
|
|
735
|
+
screen.getByTestId("model-givens-unknown").textContent,
|
|
736
|
+
).toContain("may not apply to it");
|
|
737
|
+
expect(
|
|
738
|
+
(await screen.findByTestId("model-viewer-run-defaults"))
|
|
739
|
+
.textContent,
|
|
740
|
+
).toBe(
|
|
741
|
+
"This page can't tell which givens this source reads. Where it reads one, it ran with REGION: East.",
|
|
742
|
+
);
|
|
743
|
+
} finally {
|
|
744
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
745
|
+
}
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it("says nothing of the kind where the scan can tell", async () => {
|
|
749
|
+
getModel.mockImplementation(() => Promise.resolve(twoGivenModel));
|
|
750
|
+
try {
|
|
751
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
752
|
+
expect(await screen.findByTestId("model-givens-section")).toBeTruthy();
|
|
753
|
+
expect(screen.queryByTestId("model-givens-unknown")).toBeNull();
|
|
754
|
+
fireEvent.click(
|
|
755
|
+
await screen.findByRole("button", { name: /by_name/ }),
|
|
756
|
+
);
|
|
757
|
+
expect(
|
|
758
|
+
(await screen.findByTestId("model-viewer-run-defaults"))
|
|
759
|
+
.textContent,
|
|
760
|
+
).toBe("Ran with the default REGION: All.");
|
|
761
|
+
} finally {
|
|
762
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it("lists the givens of a model that declares nothing else", async () => {
|
|
767
|
+
getModel.mockImplementation(() =>
|
|
768
|
+
Promise.resolve({
|
|
769
|
+
data: {
|
|
770
|
+
sourceInfos: [],
|
|
771
|
+
queries: [],
|
|
772
|
+
sourceText: "given: REGION :: string is 'All'\n",
|
|
773
|
+
givens: [
|
|
774
|
+
{
|
|
775
|
+
name: "REGION",
|
|
776
|
+
type: "string",
|
|
777
|
+
default: "'All'",
|
|
778
|
+
label: "Region",
|
|
779
|
+
},
|
|
780
|
+
],
|
|
781
|
+
},
|
|
782
|
+
}),
|
|
783
|
+
);
|
|
784
|
+
try {
|
|
785
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
786
|
+
expect(
|
|
787
|
+
await screen.findByText(
|
|
788
|
+
"This model defines no sources or queries.",
|
|
789
|
+
),
|
|
790
|
+
).toBeTruthy();
|
|
791
|
+
const section = screen.getByTestId("model-viewer-declared-givens");
|
|
792
|
+
expect(section.textContent).toContain("REGION");
|
|
793
|
+
expect(section.textContent).toContain("Region");
|
|
794
|
+
expect(screen.getByText("1 given")).toBeTruthy();
|
|
795
|
+
expect(
|
|
796
|
+
screen.getByTestId("model-viewer-declared-givens-where")
|
|
797
|
+
.textContent,
|
|
798
|
+
).toBe(
|
|
799
|
+
"Declared here for the models that import this file. Nothing on this page reads them.",
|
|
800
|
+
);
|
|
801
|
+
} finally {
|
|
802
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
it("says which of a givens-only model's givens it imports", async () => {
|
|
807
|
+
// The compiled model lists CODE too, which the file only imports.
|
|
808
|
+
getModel.mockImplementation(() =>
|
|
809
|
+
Promise.resolve({
|
|
810
|
+
data: {
|
|
811
|
+
sourceInfos: [],
|
|
812
|
+
queries: [],
|
|
813
|
+
sourceText:
|
|
814
|
+
"import { CODE } from \"codes.malloy\"\ngiven: REGION :: string is 'All'\n",
|
|
815
|
+
givens: [
|
|
816
|
+
{ name: "REGION", type: "string", default: "'All'" },
|
|
817
|
+
{ name: "CODE", type: "string", default: "'x'" },
|
|
818
|
+
],
|
|
819
|
+
},
|
|
820
|
+
}),
|
|
821
|
+
);
|
|
822
|
+
try {
|
|
823
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
824
|
+
expect(
|
|
825
|
+
(await screen.findByTestId("model-viewer-declared-givens-where"))
|
|
826
|
+
.textContent,
|
|
827
|
+
).toBe(
|
|
828
|
+
"For the models that import this file. CODE comes from a file this one imports; the rest are declared here. Nothing on this page reads them.",
|
|
829
|
+
);
|
|
830
|
+
} finally {
|
|
831
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
832
|
+
}
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
// `items` builds on `regional`, from the import, and REGION has no default:
|
|
836
|
+
// the page can't tell whether `items` reads it.
|
|
837
|
+
const importedRequired = (queries: unknown[] = [], text = "") => ({
|
|
838
|
+
data: {
|
|
839
|
+
...twoGivenModel.data,
|
|
840
|
+
queries,
|
|
841
|
+
sourceText: `import "regions.malloy"
|
|
842
|
+
source: items is regional extend {
|
|
843
|
+
dimension: name is upper(raw_name)
|
|
844
|
+
view: by_name is { group_by: name }
|
|
845
|
+
}
|
|
846
|
+
source: other is duckdb.table('other.parquet') extend {
|
|
847
|
+
where: code = $REGION
|
|
848
|
+
}
|
|
849
|
+
${text}`,
|
|
850
|
+
givens: [{ name: "REGION", type: "string" }],
|
|
851
|
+
},
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
it("runs anyway, at the reader's word, without a given the page can't tie to the source", async () => {
|
|
855
|
+
getModel.mockImplementation(() => Promise.resolve(importedRequired()));
|
|
856
|
+
try {
|
|
857
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
858
|
+
fireEvent.click(
|
|
859
|
+
await screen.findByRole("button", { name: /by_name/ }),
|
|
860
|
+
);
|
|
861
|
+
expect(
|
|
862
|
+
(await screen.findByTestId("model-viewer-run-blocked")).textContent,
|
|
863
|
+
).toContain("This page can't tell whether this source reads it.");
|
|
864
|
+
expect(executeQueryModel).not.toHaveBeenCalled();
|
|
865
|
+
fireEvent.click(screen.getByRole("button", { name: "Run anyway" }));
|
|
866
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
867
|
+
expect(sentBodies()[0].givens).toBeUndefined();
|
|
868
|
+
expect(
|
|
869
|
+
(await screen.findByTestId("model-viewer-run-defaults"))
|
|
870
|
+
.textContent,
|
|
871
|
+
).toBe(
|
|
872
|
+
"Ran without a value for the given REGION, which this page can't tell this source reads.",
|
|
873
|
+
);
|
|
874
|
+
} finally {
|
|
875
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
876
|
+
}
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
it("says the same of a named query that runs over an imported source", async () => {
|
|
880
|
+
getModel.mockImplementation(() =>
|
|
881
|
+
Promise.resolve(
|
|
882
|
+
importedRequired(
|
|
883
|
+
[{ name: "by_region" }],
|
|
884
|
+
"query: by_region is regional -> { group_by: name }\n",
|
|
885
|
+
),
|
|
886
|
+
),
|
|
887
|
+
);
|
|
888
|
+
try {
|
|
889
|
+
render(<ModelViewer resourceUri={URI} />, { wrapper: serverWrapper });
|
|
890
|
+
fireEvent.click(
|
|
891
|
+
await screen.findByRole("button", { name: /by_region/ }),
|
|
892
|
+
);
|
|
893
|
+
expect(
|
|
894
|
+
(await screen.findByTestId("model-viewer-run-blocked")).textContent,
|
|
895
|
+
).toBe(
|
|
896
|
+
"This needs a value for the given REGION. Set it in the Givens section above. This page can't tell whether this query reads it.",
|
|
897
|
+
);
|
|
898
|
+
expect(
|
|
899
|
+
screen.getByRole("button", { name: "Run anyway" }),
|
|
900
|
+
).toBeTruthy();
|
|
901
|
+
} finally {
|
|
902
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
|
|
906
|
+
it("is current again when the values return to ones it already ran", async () => {
|
|
907
|
+
getModel.mockImplementation(() => Promise.resolve(twoGivenModel));
|
|
908
|
+
try {
|
|
909
|
+
const view = (region: string) => (
|
|
910
|
+
<ModelViewer resourceUri={URI} givens={{ REGION: region }} />
|
|
911
|
+
);
|
|
912
|
+
const { rerender } = render(view("A"), { wrapper: serverWrapper });
|
|
913
|
+
fireEvent.click(
|
|
914
|
+
await screen.findByRole("button", { name: /by_name/ }),
|
|
915
|
+
);
|
|
916
|
+
await waitFor(() =>
|
|
917
|
+
expect(executeQueryModel).toHaveBeenCalledTimes(1),
|
|
918
|
+
);
|
|
919
|
+
rerender(view("B"));
|
|
920
|
+
fireEvent.click(
|
|
921
|
+
await screen.findByRole("button", {
|
|
922
|
+
name: "Run with these values",
|
|
923
|
+
}),
|
|
924
|
+
);
|
|
925
|
+
await waitFor(() =>
|
|
926
|
+
expect(executeQueryModel).toHaveBeenCalledTimes(2),
|
|
927
|
+
);
|
|
928
|
+
rerender(view("A"));
|
|
929
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
930
|
+
expect(screen.queryByTestId("model-viewer-run-stale")).toBeNull();
|
|
931
|
+
expect(screen.getByTestId("model-viewer-result-card")).toBeTruthy();
|
|
932
|
+
expect(executeQueryModel).toHaveBeenCalledTimes(2);
|
|
933
|
+
} finally {
|
|
934
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
935
|
+
}
|
|
936
|
+
});
|
|
937
|
+
});
|