-
Notifications
You must be signed in to change notification settings - Fork 10
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 #722 from OpenFn/promises-all-the-way-down
Support promises
- Loading branch information
Showing
33 changed files
with
1,282 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# @openfn/integration-tests-execute | ||
|
||
## 1.0.1 | ||
|
||
### Patch Changes | ||
|
||
- Updated dependencies [40fd45b] | ||
- Updated dependencies [40fd45b] | ||
- @openfn/compiler@0.2.0 | ||
- @openfn/runtime@1.4.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,32 @@ | ||
{ | ||
"name": "@openfn/integration-tests-execute", | ||
"private": true, | ||
"version": "1.0.1", | ||
"description": "Job execution tests", | ||
"author": "Open Function Group <[email protected]>", | ||
"license": "ISC", | ||
"type": "module", | ||
"scripts": { | ||
"test": "pnpm ava" | ||
}, | ||
"dependencies": { | ||
"@openfn/compiler": "workspace:^", | ||
"@openfn/language-common": "1.7.7", | ||
"@openfn/language-http": "6.4.0", | ||
"@openfn/runtime": "workspace:^", | ||
"@types/node": "^18.15.13", | ||
"ava": "5.3.1", | ||
"date-fns": "^2.30.0", | ||
"rimraf": "^3.0.2", | ||
"ts-node": "10.8.1", | ||
"tslib": "^2.4.0", | ||
"typescript": "^5.1.6" | ||
}, | ||
"files": [ | ||
"dist", | ||
"README.md" | ||
], | ||
"devDependencies": { | ||
"@types/rimraf": "^3.0.2" | ||
} | ||
} |
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 a suite of examples of jobs. | ||
|
||
We don't really have a place where we can just write and test arbtirary job code with compilation. | ||
|
||
You can do it through the CLI or worker but they have significant overheads. |
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,35 @@ | ||
import path from 'node:path'; | ||
import run from '@openfn/runtime'; | ||
import compiler from '@openfn/compiler'; | ||
|
||
const execute = async (job: string, state: any, adaptor = 'common') => { | ||
// compile with common and dumb imports | ||
const options = { | ||
'add-imports': { | ||
adaptor: { | ||
name: `@openfn/language-${adaptor}`, | ||
exportAll: true, | ||
}, | ||
}, | ||
}; | ||
const compiled = compiler(job, options); | ||
// console.log(compiled); | ||
|
||
const result = await run(compiled, state, { | ||
// preload the linker with some locally installed modules | ||
linker: { | ||
modules: { | ||
'@openfn/language-common': { | ||
path: path.resolve('node_modules/@openfn/language-common'), | ||
}, | ||
'@openfn/language-http': { | ||
path: path.resolve('node_modules/@openfn/language-http'), | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return result; | ||
}; | ||
|
||
export default execute; |
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 @@ | ||
import execute from './execute'; | ||
|
||
export default execute; |
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,148 @@ | ||
import test from 'ava'; | ||
|
||
import execute from '../src/execute'; | ||
|
||
const wait = `function wait() { | ||
return (state) => | ||
new Promise((resolve) => { | ||
setTimeout(() => resolve(state), 2); | ||
}); | ||
};`; | ||
|
||
test.serial('should return state', async (t) => { | ||
const state = { data: { x: 1 } }; | ||
|
||
const job = ` | ||
fn(s => s) | ||
`; | ||
const result = await execute(job, state); | ||
|
||
t.deepEqual(state, result); | ||
}); | ||
|
||
test.serial('should use .then()', async (t) => { | ||
const state = { data: { x: 1 } }; | ||
|
||
const job = ` | ||
fn(s => s) | ||
.then((s) => | ||
({ | ||
data: { x: 33 } | ||
}) | ||
) | ||
`; | ||
const result = await execute(job, state); | ||
|
||
t.deepEqual(result, { data: { x: 33 } }); | ||
}); | ||
|
||
test.serial('should chain .then() with state', async (t) => { | ||
const state = { data: { x: 1 } }; | ||
|
||
const job = ` | ||
fn(s => ({ x: 1 })) | ||
.then((s) => | ||
({ | ||
x: s.x + 1 | ||
}) | ||
) | ||
`; | ||
const result = await execute(job, state); | ||
|
||
t.deepEqual(result, { x: 2 }); | ||
}); | ||
|
||
test.serial('should use .then() as an argument', async (t) => { | ||
const state = {}; | ||
|
||
const job = `fn( | ||
fn(() => ({ x: 5 })).then((s) => ({ x: s.x + 1})) | ||
)`; | ||
const result = await execute(job, state); | ||
|
||
t.deepEqual(result, { x: 6 }); | ||
}); | ||
|
||
test.serial('use then() with wait()', async (t) => { | ||
const state = { | ||
data: { | ||
x: 22, | ||
}, | ||
}; | ||
|
||
const job = `${wait} | ||
wait().then(fn(s => s))`; | ||
|
||
const result = await execute(job, state); | ||
|
||
t.deepEqual(result.data, { x: 22 }); | ||
}); | ||
|
||
test.serial('catch an error and return it', async (t) => { | ||
const state = { | ||
data: { | ||
x: 22, | ||
}, | ||
}; | ||
|
||
const job = `fn(() => { | ||
throw { err: true } | ||
}).catch(e => e)`; | ||
|
||
const result = await execute(job, state); | ||
t.deepEqual(result, { err: true }); | ||
}); | ||
|
||
test.serial('catch an error and re-throw it', async (t) => { | ||
const state = { | ||
data: { | ||
x: 22, | ||
}, | ||
}; | ||
|
||
const job = `fn(() => { | ||
throw new Error('err') | ||
}).catch(e => { throw e })`; | ||
|
||
const result = await execute(job, state); | ||
t.is(result.errors['job-1'].name, 'JobError'); | ||
t.is(result.errors['job-1'].message, 'err'); | ||
}); | ||
|
||
test.serial('catch an error and return state', async (t) => { | ||
const state = { | ||
data: { | ||
x: 22, | ||
}, | ||
}; | ||
|
||
const job = `fn(() => { | ||
throw { err: true } | ||
}).catch((e, s) => s)`; | ||
|
||
const result = await execute(job, state); | ||
t.deepEqual(result, state); | ||
}); | ||
|
||
test.serial('each with then ', async (t) => { | ||
const state = { | ||
ids: [1, 2, 3], | ||
results: [], | ||
}; | ||
|
||
const job = `each($.ids, | ||
get(\`https://jsonplaceholder.typicode.com/todos/\${$.data}\`).then( | ||
(s) => { | ||
s.results.push(s.data); | ||
return s; | ||
} | ||
) | ||
)`; | ||
|
||
const result = await execute(job, state, 'http'); | ||
|
||
t.is(result.results.length, 3); | ||
t.is(result.results[0].id, 1); | ||
t.is(result.results[1].id, 2); | ||
t.is(result.results[2].id, 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"ts-node": { | ||
"experimentalSpecifierResolution": "node" | ||
}, | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"module": "es2020", | ||
"moduleResolution": "node", | ||
"allowJs": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
"skipLibCheck": true | ||
} | ||
} |
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,7 +1,7 @@ | ||
{ | ||
"name": "@openfn/integration-tests-worker", | ||
"private": true, | ||
"version": "1.0.50", | ||
"version": "1.0.51", | ||
"description": "Lightning WOrker integration tests", | ||
"author": "Open Function Group <[email protected]>", | ||
"license": "ISC", | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@openfn/compiler", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "Compiler and language tooling for openfn jobs.", | ||
"author": "Open Function Group <[email protected]>", | ||
"license": "ISC", | ||
|
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
Oops, something went wrong.