-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: adds setup for mock-service-worker #127
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8404b0
test: add msw and basic handlers for a few types
schottra 8752ab5
test: add mock data for a basic workflow execution
schottra 00f8ad7
test: fixing/removing tests after adding msw
schottra 4d6067e
test: throw on unexpected requests to msw
schottra 680de6a
fix: upgrade TS to fix error and cleanup resulting errors
schottra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,5 @@ | ||
import { Env } from 'config'; | ||
|
||
/** Safely check for server environment */ | ||
export function isServer() { | ||
try { | ||
return __isServer; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** equivalent to process.env in server and client */ | ||
// tslint:disable-next-line:no-any | ||
export const env: Env = (isServer() ? process.env : window.env) as any; | ||
export const env: Env = Object.assign({}, process.env, window.env); |
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
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,6 @@ | ||
// Arbitrary start date used as a basis for generating timestamps | ||
// to keep the mocked data consistent across runs | ||
export const mockStartDate = new Date('2020-11-15T02:32:19.610Z'); | ||
|
||
// Workflow Execution duration in milliseconds | ||
export const defaultWorkflowExecutionDuration = 1000 * 60 * 60 * 1.251; |
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,15 @@ | ||
import { Core } from 'flyteidl'; | ||
import { LaunchPlan } from 'models'; | ||
|
||
// TODO: | ||
const basic: LaunchPlan = { | ||
id: { | ||
resourceType: Core.ResourceType.LAUNCH_PLAN, | ||
project: 'flytetest', | ||
domain: 'development', | ||
name: 'Basic', | ||
version: 'abc123' | ||
} | ||
} as LaunchPlan; | ||
|
||
export const launchPlans = { basic }; |
Empty file.
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,8 @@ | ||
export function emptyProject(id: string, name?: string) { | ||
return { | ||
id, | ||
name: name ?? id, | ||
domains: [], | ||
description: '' | ||
}; | ||
} |
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,48 @@ | ||
import { dateToTimestamp, millisecondsToDuration } from 'common/utils'; | ||
import { Admin } from 'flyteidl'; | ||
import { LiteralMap } from 'models/Common/types'; | ||
import { WorkflowExecutionPhase } from 'models/Execution/enums'; | ||
import { Execution, ExecutionMetadata } from 'models/Execution/types'; | ||
import { defaultWorkflowExecutionDuration, mockStartDate } from './constants'; | ||
import { launchPlans } from './launchPlans'; | ||
import { workflows } from './workflows'; | ||
|
||
export function defaultWorkflowExecutionMetadata(): ExecutionMetadata { | ||
return { | ||
mode: Admin.ExecutionMetadata.ExecutionMode.MANUAL, | ||
principal: 'sdk', | ||
nesting: 0 | ||
}; | ||
} | ||
|
||
export function emptyLiteralMap(): LiteralMap { | ||
return { literals: {} }; | ||
} | ||
|
||
const basic: Execution = { | ||
id: { | ||
project: 'flytetest', | ||
domain: 'development', | ||
name: 'abc123' | ||
}, | ||
spec: { | ||
launchPlan: { ...launchPlans.basic.id }, | ||
inputs: emptyLiteralMap(), | ||
metadata: defaultWorkflowExecutionMetadata(), | ||
notifications: { | ||
notifications: [] | ||
} | ||
}, | ||
closure: { | ||
computedInputs: emptyLiteralMap(), | ||
createdAt: dateToTimestamp(mockStartDate), | ||
duration: millisecondsToDuration(defaultWorkflowExecutionDuration), | ||
phase: WorkflowExecutionPhase.SUCCEEDED, | ||
startedAt: dateToTimestamp(mockStartDate), | ||
workflowId: { ...workflows.basic.id } | ||
} | ||
}; | ||
|
||
export const workflowExecutions = { | ||
basic | ||
}; |
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,15 @@ | ||
import { Core } from 'flyteidl'; | ||
import { Workflow } from 'models/Workflow/types'; | ||
|
||
// TODO: | ||
const basic: Workflow = { | ||
id: { | ||
resourceType: Core.ResourceType.WORKFLOW, | ||
project: 'flytetest', | ||
domain: 'development', | ||
name: 'Basic', | ||
version: 'abc123' | ||
} | ||
}; | ||
|
||
export const workflows = { basic }; |
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,10 @@ | ||
import { RequestHandlersList } from 'msw/lib/types/setupWorker/glossary'; | ||
import { workflowExecutions } from './data/workflowExecutions'; | ||
import { workflowExecutionHandler } from './handlers'; | ||
|
||
export function getDefaultData(): RequestHandlersList { | ||
const workflowExecutionHandlers = Object.values(workflowExecutions).map( | ||
workflowExecutionHandler | ||
); | ||
return [...workflowExecutionHandlers]; | ||
} |
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,102 @@ | ||
import { Admin } from 'flyteidl'; | ||
import { | ||
EncodableType, | ||
encodeProtoPayload, | ||
Execution, | ||
NameIdentifierScope, | ||
NodeExecution, | ||
Project, | ||
Workflow | ||
} from 'models'; | ||
import { | ||
makeExecutionPath, | ||
makeNodeExecutionListPath, | ||
makeNodeExecutionPath | ||
} from 'models/Execution/utils'; | ||
import { makeWorkflowPath } from 'models/Workflow/utils'; | ||
import { ResponseResolver, rest } from 'msw'; | ||
import { setupServer } from 'msw/lib/types/node'; | ||
import { RestContext } from 'msw/lib/types/rest'; | ||
import { apiPath } from './utils'; | ||
|
||
export function adminEntityResponder( | ||
data: any, | ||
encodeType: EncodableType<any> | ||
): ResponseResolver<any, RestContext> { | ||
const buffer = encodeProtoPayload(data, encodeType); | ||
const contentLength = buffer.byteLength.toString(); | ||
return (_, res, ctx) => | ||
res( | ||
ctx.set('Content-Type', 'application/octet-stream'), | ||
ctx.set('Content-Length', contentLength), | ||
ctx.body(buffer) | ||
); | ||
} | ||
|
||
export function workflowExecutionHandler(data: Partial<Execution>) { | ||
return rest.get( | ||
apiPath(makeExecutionPath(data.id!)), | ||
adminEntityResponder(data, Admin.Execution) | ||
); | ||
} | ||
|
||
export function workflowHandler(data: Partial<Workflow>) { | ||
return rest.get( | ||
apiPath(makeWorkflowPath(data.id!)), | ||
adminEntityResponder(data, Admin.Workflow) | ||
); | ||
} | ||
|
||
export function nodeExecutionHandler(data: Partial<NodeExecution>) { | ||
return rest.get( | ||
apiPath(makeNodeExecutionPath(data.id!)), | ||
adminEntityResponder(data, Admin.NodeExecution) | ||
); | ||
} | ||
|
||
// TODO: pagination responder that respects limit/token? | ||
export function nodeExecutionListHandler( | ||
scope: NameIdentifierScope, | ||
data: Partial<NodeExecution>[] | ||
) { | ||
return rest.get( | ||
apiPath(makeNodeExecutionListPath(scope)), | ||
adminEntityResponder( | ||
{ | ||
nodeExecutions: data | ||
}, | ||
Admin.NodeExecutionList | ||
) | ||
); | ||
} | ||
|
||
export function projectListHandler(data: Project[]) { | ||
return rest.get( | ||
apiPath('/projects'), | ||
adminEntityResponder({ projects: data }, Admin.Projects) | ||
); | ||
} | ||
|
||
export interface BoundAdminServer { | ||
insertNodeExecution(data: Partial<NodeExecution>): void; | ||
insertNodeExecutionList( | ||
scope: NameIdentifierScope, | ||
data: Partial<NodeExecution>[] | ||
): void; | ||
insertProjects(data: Project[]): void; | ||
insertWorkflow(data: Partial<Workflow>): void; | ||
insertWorkflowExecution(data: Partial<Execution>): void; | ||
} | ||
|
||
export function bindHandlers({ | ||
use | ||
}: ReturnType<typeof setupServer>): BoundAdminServer { | ||
return { | ||
insertNodeExecution: data => use(nodeExecutionHandler(data)), | ||
insertNodeExecutionList: (scope, data) => | ||
use(nodeExecutionListHandler(scope, data)), | ||
insertProjects: data => use(projectListHandler(data)), | ||
insertWorkflow: data => use(workflowHandler(data)), | ||
insertWorkflowExecution: data => use(workflowExecutionHandler(data)) | ||
}; | ||
} |
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,7 @@ | ||
import { setupServer } from 'msw/node'; | ||
import { getDefaultData } from './getDefaultData'; | ||
import { bindHandlers } from './handlers'; | ||
|
||
const server = setupServer(...getDefaultData()); | ||
const handlers = bindHandlers(server); | ||
export const mockServer = { ...server, ...handlers }; |
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,5 @@ | ||
import { apiPrefix } from 'models/AdminEntity/constants'; | ||
|
||
export function apiPath(path: string) { | ||
return `${apiPrefix}${path}`; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these intentionally kept in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. This keeps the test runner from throwing errors. I will remove them in the next PR when I fix and re-enable the commented-out tests.