-
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.
- Loading branch information
Showing
19 changed files
with
206 additions
and
16 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
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,38 @@ | ||
from enum import Enum | ||
|
||
class PaymentSystemType(Enum): | ||
PAYPAL = "paypal" | ||
WEBMONEY = "webmoney" | ||
CAT_BANK = "cat-bank" | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
def case1( | ||
type: PaymentSystemType, | ||
base: int, | ||
amount: int | ||
) -> int: | ||
raise NotImplementedError | ||
|
||
class PaymentSystemActionType(Enum): | ||
PAYMENT = "payment" | ||
PAYOUT = "payout" | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
def case2( | ||
type: PaymentSystemType, | ||
base: int, | ||
action: PaymentSystemActionType, | ||
amount: int | ||
) -> int: | ||
raise NotImplementedError |
40 changes: 40 additions & 0 deletions
40
src/content/design-patterns/factory/python/test-runners.ts
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,40 @@ | ||
import type { TestRunnerFactory } from "@/lib/testing"; | ||
import { PyTestRunner, pyRuntimeFactory } from "@/lib/testing/python"; | ||
|
||
import { | ||
type SimpleTestInput, | ||
type ComplexTestInput, | ||
CaseType, | ||
type Inputs, | ||
type Outputs, | ||
} from "../test-cases"; | ||
|
||
class SimpleTestRunner extends PyTestRunner<SimpleTestInput, number> { | ||
protected caseExecutionCode({ | ||
paymentSystem, | ||
base, | ||
amount, | ||
}: SimpleTestInput): string { | ||
return `case1("${paymentSystem}", ${base}, ${amount})`; | ||
} | ||
} | ||
|
||
class ComplexTestRunner extends PyTestRunner<ComplexTestInput, number> { | ||
protected caseExecutionCode({ | ||
paymentSystem, | ||
base, | ||
action, | ||
amount, | ||
}: ComplexTestInput): string { | ||
return `case2("${paymentSystem}", ${base}, "${action}", ${amount})`; | ||
} | ||
} | ||
|
||
export const testRunnerFactories: { | ||
[k in CaseType]: TestRunnerFactory<Inputs[k], Outputs[k]>; | ||
} = { | ||
[CaseType.Simple]: async (code: string) => | ||
new SimpleTestRunner(await pyRuntimeFactory(), code), | ||
[CaseType.Complex]: async (code: string) => | ||
new ComplexTestRunner(await pyRuntimeFactory(), code), | ||
}; |
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,2 +1,3 @@ | ||
export * from "./model"; | ||
export * from "./js-test-runner"; | ||
export * from "./ts-test-runner"; |
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 @@ | ||
export { version as tsVersion } from "typescript"; |
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,13 +1,17 @@ | ||
import { version } from "typescript"; | ||
import { tsVersion } from './js' | ||
import { version as phpVersion } from './php' | ||
import { version as pyVersion } from './python' | ||
|
||
export enum Language { | ||
PHP = "php", | ||
Python = "python", | ||
TypeScript = "typescript", | ||
JavaScript = "javascript", | ||
} | ||
|
||
export const LANGUAGE_TITLE: Record<Language, string> = { | ||
[Language.PHP]: "PHP 8.3", | ||
[Language.TypeScript]: `TypeScript ${version}`, | ||
[Language.PHP]: `PHP ${phpVersion}`, | ||
[Language.TypeScript]: `TypeScript ${tsVersion}`, | ||
[Language.Python]: `Python ${pyVersion}`, | ||
[Language.JavaScript]: "JavaScript", | ||
}; |
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,3 +1,4 @@ | ||
export * from "./model"; | ||
export * from "./php-test-runner"; | ||
export * from "./fail-safe-php"; | ||
export * from "./php-runtime-factory"; |
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 @@ | ||
export const version = "8.3"; |
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,3 +1,5 @@ | ||
import { WebPHP } from '@php-wasm/web'; | ||
import { WebPHP } from "@php-wasm/web"; | ||
|
||
export const phpRuntimeFactory = () => WebPHP.loadRuntime('8.3') | ||
import { version } from "./model"; | ||
|
||
export const phpRuntimeFactory = () => WebPHP.loadRuntime(version); |
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 @@ | ||
export * from './model' | ||
export * from './py-test-runner' | ||
export * from './py-runtime-factory' |
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 @@ | ||
// This is only package version, maybe we need it in the future | ||
// export { version } from "pyodide"; | ||
|
||
// According to https://github.com/pyodide/pyodide/pull/4435 | ||
export const version = "3.12.1"; |
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 @@ | ||
import { loadPyodide } from "pyodide"; | ||
|
||
export const pyRuntimeFactory = () => | ||
loadPyodide({ | ||
indexURL: import.meta.env.DEV | ||
? undefined | ||
: `${import.meta.env.BASE_URL}/assets/pyodide`, | ||
}); |
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,46 @@ | ||
import { loadPyodide } from "pyodide"; | ||
import type { PyProxy } from "pyodide/ffi"; | ||
|
||
import type { TestRunner } from "../testing"; | ||
|
||
function isPyProxy(obj: any): obj is PyProxy { | ||
return typeof obj === "object" && obj; | ||
} | ||
|
||
export abstract class PyTestRunner<I, O> implements TestRunner<I, O> { | ||
private proxies: PyProxy[] = []; | ||
|
||
constructor( | ||
protected readonly python: Awaited<ReturnType<typeof loadPyodide>>, | ||
protected readonly code: string | ||
) {} | ||
|
||
protected caseExecutionCode(input: I): string { | ||
throw new Error("Not implemented"); | ||
} | ||
|
||
protected transformCode(input: I): string { | ||
return `${this.code}\n${this.caseExecutionCode(input)}`; | ||
} | ||
|
||
protected transformResult(result: any): O { | ||
if (isPyProxy(result)) { | ||
this.proxies.push(result); | ||
return result.toJs({ pyproxies: this.proxies }); | ||
} | ||
return result; | ||
} | ||
|
||
async run(inputIn: I): Promise<O> { | ||
return this.transformResult( | ||
await this.python.runPythonAsync(this.transformCode(inputIn)) | ||
); | ||
} | ||
|
||
[Symbol.dispose](): void { | ||
for (const p of this.proxies) { | ||
p.destroy(); | ||
} | ||
this.proxies.length = 0; | ||
} | ||
} |