-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from flatironinstitute/use-stan-sampler-tests
Tests for useStanSampler hooks
- Loading branch information
Showing
6 changed files
with
330 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { vi } from "vitest"; | ||
|
||
import type StanModel from "tinystan"; | ||
import type { PrintCallback } from "tinystan"; | ||
import { defaultSamplingOpts } from "../../../src/app/Project/ProjectDataModel"; | ||
|
||
import fakeURL from "./empty.ts?url"; | ||
import erroringURL from "./fail.ts?url"; | ||
import failSentinel from "./fail.ts"; | ||
|
||
export const mockCompiledMainJsUrl = fakeURL; | ||
export const erroringCompiledMainJsUrl = erroringURL; | ||
|
||
const erroring_num_chains = 999; | ||
|
||
export const erroringSamplingOpts = { | ||
...defaultSamplingOpts, | ||
num_chains: erroring_num_chains, | ||
}; | ||
|
||
export const mockedParamNames = ["a", "b"]; | ||
export const mockedDraws = [ | ||
[1, 2], | ||
[3, 4], | ||
]; | ||
|
||
export const mockedProgress = { | ||
chain: 1, | ||
iteration: 123, | ||
totalIterations: 1000, | ||
percent: 45, | ||
warmup: false, | ||
}; | ||
|
||
const mockedProgressString = `Chain [${mockedProgress.chain}] \ | ||
Iteration: ${mockedProgress.iteration} \ | ||
/ ${mockedProgress.totalIterations} \ | ||
[${mockedProgress.percent.toString().padStart(3)}%] \ | ||
(${mockedProgress.warmup ? "Warmup" : "Sampling"})`; | ||
|
||
const mockedLoad = async ( | ||
_create: any, | ||
printCallback: PrintCallback | null, | ||
) => { | ||
await new Promise((resolve) => setTimeout(resolve, 50)); | ||
|
||
if (_create === failSentinel) { | ||
return Promise.reject("error for testing in load!"); | ||
} | ||
|
||
const model = { | ||
stanVersion: vi.fn(() => "1.2.3"), | ||
sample: vi.fn(({ num_chains }) => { | ||
if (num_chains === erroring_num_chains) { | ||
throw new Error("error for testing in sample!"); | ||
} | ||
|
||
printCallback && printCallback(mockedProgressString); | ||
|
||
return { | ||
paramNames: mockedParamNames, | ||
draws: mockedDraws, | ||
}; | ||
}), | ||
} as unknown as StanModel; | ||
|
||
return model; | ||
}; | ||
|
||
export default mockedLoad; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// intentionally empty, used to create a URL vitest can resolve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// used to create a URL vitest can resolve | ||
const failSentinel = "fail"; | ||
export default failSentinel; |
Oops, something went wrong.