Skip to content

Commit

Permalink
Update serverless-example dependencies and test in CI
Browse files Browse the repository at this point in the history
As an additional job (rather than as another .yml file)
  • Loading branch information
hayd committed Mar 25, 2020
1 parent a210549 commit a53735d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 19 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -62,3 +62,21 @@ jobs:
cat template.yml
sam publish --region us-east-1
echo ---
test_example:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denolib/setup-deno@v1.2.0
with:
deno-version: 0.37.0
- name: start a local dynamodb
run: |
mkdir dyno
curl -q https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz | tar --directory=dyno -zxf -
java -D"java.library.path=$DIR/DynamoDBLocal_lib" -jar "dyno/DynamoDBLocal.jar" -sharedDb &
sleep 4.2
- name: run serverless tests
working-directory: example-serverless
run: |
AWS_ACCESS_KEY_ID=fakeMyKeyId AWS_SECRET_ACCESS_KEY=fakeSecretAccessKey AWS_DEFAULT_REGION=local deno test --allow-env --allow-net test.ts
20 changes: 20 additions & 0 deletions .github/workflows/example.yml
@@ -0,0 +1,20 @@
name: Test
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denolib/setup-deno@v1.2.0
with:
deno-version: 0.37.0
- name: start a local dynamodb
run: |
mkdir dyno
curl -q https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz | tar --directory=dyno -zxf -
java -D"java.library.path=$DIR/DynamoDBLocal_lib" -jar "dyno/DynamoDBLocal.jar" -sharedDb &
sleep 4.2
- name: run serverless tests
working-directory: example-serverless
run: |
AWS_ACCESS_KEY_ID=fakeMyKeyId AWS_SECRET_ACCESS_KEY=fakeSecretAccessKey AWS_DEFAULT_REGION=local deno test --allow-env --allow-net test.ts
13 changes: 11 additions & 2 deletions example-serverless/api/candidate.ts
Expand Up @@ -19,7 +19,11 @@ function error(message: string, statusCode: number = 500) {
}

export async function get(event: APIGatewayProxyEvent, context: Context) {
const id = event.pathParameters.id;
const id = event.pathParameters && event.pathParameters.id;
if (id === null) {
return error(`id not found in pathParameters`);
}

const params = {
TableName,
Key: {
Expand Down Expand Up @@ -67,7 +71,12 @@ export async function list(event: APIGatewayProxyEvent, context: Context) {
}

export async function submit(event: APIGatewayProxyEvent, context: Context) {
const requestBody = JSON.parse(event.body);
let requestBody;
try {
requestBody = JSON.parse(event.body!);
} catch {
return error("invalid input", 422);
}
const { fullname, email, experience } = requestBody;

if (
Expand Down
10 changes: 1 addition & 9 deletions example-serverless/client.ts
@@ -1,11 +1,3 @@
import { DynamoDBClient, createClient } from "./deps.ts";

const conf = {
accessKeyId: Deno.env("AWS_ACCESS_KEY_ID"),
secretAccessKey: Deno.env("AWS_SECRET_ACCESS_KEY"),
sessionToken: () => Deno.env("AWS_SESSION_TOKEN"),
region: Deno.env("AWS_REGION") || "local",
port: Deno.env("AWS_REGION") ? 443 : 8000
};

export const client: DynamoDBClient = createClient(conf);
export const client: DynamoDBClient = createClient();
2 changes: 1 addition & 1 deletion example-serverless/deps.ts
Expand Up @@ -2,7 +2,7 @@ export {
createClient,
Doc,
DynamoDBClient
} from "https://deno.land/x/dynamodb@v0.2.0/mod.ts";
} from "https://deno.land/x/dynamodb@v0.3.1/mod.ts";

import { v4 } from "https://deno.land/std@v0.37.0/uuid/mod.ts";
export const uuid = v4.generate;
4 changes: 1 addition & 3 deletions example-serverless/test.ts
Expand Up @@ -2,7 +2,7 @@
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html

// You must also pass a access key/secret environment variables, but these don't have to be real e.g.
// AWS_ACCESS_KEY_ID=fakeMyKeyId AWS_SECRET_ACCESS_KEY=fakeSecretAccessKey deno --allow-env --allow-net test.ts
// AWS_ACCESS_KEY_ID=fakeMyKeyId AWS_SECRET_ACCESS_KEY=fakeSecretAccessKey AWS_DEFAULT_REGION=local deno run --allow-env --allow-net

import {
assert,
Expand Down Expand Up @@ -107,5 +107,3 @@ test({
assertEquals(body.message, "invalid input");
}
});

await Deno.runTests();
6 changes: 4 additions & 2 deletions tests/deps.ts
@@ -1,5 +1,7 @@
export { assert,
assertEquals } from "https://deno.land/std@v0.37.0/testing/asserts.ts";
export {
assert,
assertEquals
} from "https://deno.land/std@v0.37.0/testing/asserts.ts";
export {
serve
} from "https://deno.land/std@v0.37.0/http/server.ts";
4 changes: 2 additions & 2 deletions tests/test.ts
Expand Up @@ -25,9 +25,9 @@ async function addFiles(
if (typeof zipOrFiles == "string") {
const zipFile = zipOrFiles;
// TODO check it raises on errors?
const p = Deno.run({
const p = Deno.run({
cmd: ["unzip", "-qq", `/src/runtime/${zipFile}`, "-d", toDir]
})
});
await p.status();
p.close();
} else {
Expand Down

0 comments on commit a53735d

Please sign in to comment.