Skip to content

Commit 94770dd

Browse files
committed
🐛 Fix writeParam. Add e2e tests
1 parent c72cc30 commit 94770dd

File tree

8 files changed

+105
-12
lines changed

8 files changed

+105
-12
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ node_modules
44
*.go
55
*.json
66
tmp
7-
.node-version
7+
.node-version
8+
tests/docs
9+
tests/jsonplaceholder.typicode.com

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"scripts": {
1212
"sync-version": "npm version from-git --no-git-tag-version && git add package.json && git commit -m \":bookmark: [skip ci] v$(cat package.json | jq -r .version)\"",
1313
"fetch": "cd vendor && curl -OL https://raw.githubusercontent.com/mholt/json-to-go/master/json-to-go.js",
14-
"setup": "yarn install && yarn fetch && npm -f link && exec $SHELL -l"
14+
"setup": "yarn install && yarn fetch && npm -f link && exec $SHELL -l",
15+
"test": "jest"
1516
},
1617
"description": "Convert REST API's JSON payload to Golang struct.",
1718
"bin": {

src/buildPath.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test('build path', () => {
1818
}
1919
const received = buildPath(
2020
new URL("https://api.github.com/users/github/repos"),
21-
"./.api-to-go.test.yml"
21+
"./src/.api-to-go.test.yml"
2222
)
2323
expect(received).toEqual(expected);
2424
});
@@ -32,7 +32,7 @@ test('build path without format setting', () => {
3232
}
3333
const received = buildPath(
3434
new URL("https://api.github.com/organizations"),
35-
"./.api-to-go.test.yml"
35+
"./src/.api-to-go.test.yml"
3636
)
3737
expect(received.path).toEqual(expected);
3838
});

src/run.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ function run(urlStr, body, options) {
3232
if (cfg?.["docs"] !== undefined) console.log(`Docs: ${cfg?.["docs"].join(", ")}`)
3333
comment = buildComment(url, path, opts.method, res)
3434

35-
if (opts?.body) {
36-
const paramStruct = jsonToGo(opts?.body, path.struct + "Param");
37-
const paramContent = buildContent(
38-
paramStruct.go, path, buildComment(url, path, opts.method), true
39-
)
40-
writeParam(JSON.stringify(JSON.parse(opts?.body), null, "\t"), path, paramContent)
41-
}
42-
4335
return res.json()
4436
})
4537
.then(json => {
4638
const struct = jsonToGo(JSON.stringify(json), path.struct);
4739
const content = buildContent(struct.go, path, comment)
4840
write(json, path, content)
41+
42+
if (opts?.body) {
43+
const paramStruct = jsonToGo(opts?.body, path.struct + "Param");
44+
const paramContent = buildContent(
45+
paramStruct.go, path, buildComment(url, path, opts.method), true
46+
)
47+
writeParam(JSON.stringify(JSON.parse(opts?.body), null, "\t"), path, paramContent)
48+
}
4949
}, () => {
5050
console.log()
5151
console.log("Response Body is empty.")

tests/.api-to-go.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"jsonplaceholder.typicode.com":
2+
docs:
3+
- https://jsonplaceholder.typicode.com/

tests/api-to-go.bats

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bats
2+
3+
load helper/helper
4+
5+
setup() {
6+
if [ "$BATS_TEST_NUMBER" -eq 1 ]; then
7+
init_doc
8+
rm -rf ./jsonplaceholder.typicode.com
9+
fi
10+
11+
BASE_URL="https://jsonplaceholder.typicode.com"
12+
API_TO_GO="node ../bin/api-to-go.js"
13+
doc "## $BATS_TEST_DESCRIPTION"
14+
}
15+
16+
teardown(){
17+
write_doc_details "api-to-go"
18+
}
19+
20+
@test "Command with valid URL should succeed" {
21+
COMMAND="$API_TO_GO $BASE_URL/todos/1"
22+
run eval "$COMMAND"
23+
[ "$status" -eq 0 ]
24+
}
25+
26+
@test "Command with URL and JSON body" {
27+
DATA='{"title": "foo", "body": "bar", "userId": 1}'
28+
COMMAND="$API_TO_GO $BASE_URL/posts '$DATA'"
29+
run eval "$COMMAND"
30+
[ "$status" -eq 0 ]
31+
}

tests/helper/doc_helper.bash

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
TEST_FILE=$(basename ${BATS_TEST_FILENAME})
4+
DOC_PATH="docs/$TEST_FILE.md"
5+
doc(){
6+
echo "$1" >> "$DOC_PATH"
7+
}
8+
9+
init_doc(){
10+
rm -f "$DOC_PATH"
11+
doc "Generated by $TEST_FILE . last modified $(date '+%Y/%m/%d %T')"
12+
}
13+
14+
write_doc_details(){
15+
doc
16+
doc "Command:"
17+
doc
18+
doc "\`\`\`sh"
19+
doc "$COMMAND"
20+
doc "\`\`\`"
21+
doc
22+
doc "Output:"
23+
doc
24+
if jq -e . >/dev/null 2>&1 <<< "$output"; then
25+
doc "\`\`\`sh"
26+
doc "$(echo "$output" | jq)"
27+
doc "\`\`\`"
28+
else
29+
doc "\`\`\`sh"
30+
doc "$output"
31+
doc "\`\`\`"
32+
fi
33+
}

tests/helper/helper.bash

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
load helper/doc_helper
4+
5+
setup() {
6+
echo "--start--"
7+
}
8+
9+
teardown() {
10+
echo "command:"
11+
echo "$COMMAND"
12+
echo ""
13+
echo "status:"
14+
echo "$status"
15+
echo ""
16+
echo "output:"
17+
if jq -e . >/dev/null 2>&1 <<< "$output"; then
18+
echo "$output" | jq
19+
else
20+
echo "$output"
21+
fi
22+
echo "--end--"
23+
}

0 commit comments

Comments
 (0)