-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Caro <[email protected]>
- Loading branch information
1 parent
337e03e
commit a4c1fe1
Showing
7 changed files
with
222 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env bats | ||
BASE_URL=http://127.0.0.1:8080 | ||
|
||
do_post() { | ||
local path="${1?}" | ||
local data="${2?}" | ||
curl \ | ||
-X POST \ | ||
--silent \ | ||
"${BASE_URL}/${path}" \ | ||
-H 'Content-Type: application/json' \ | ||
-d "$data" | ||
} | ||
|
||
do_put() { | ||
local path="${1?}" | ||
local data="${2?}" | ||
curl \ | ||
-X PUT \ | ||
--silent \ | ||
"${BASE_URL}/${path}" \ | ||
-H 'Content-Type: application/json' \ | ||
-d "$data" | ||
} | ||
|
||
do_get() { | ||
local path="${1?}" | ||
curl \ | ||
-X GET \ | ||
--silent \ | ||
"${BASE_URL}/${path}" \ | ||
-H 'Content-Type: application/json' | ||
} | ||
|
||
do_delete() { | ||
local path="${1?}" | ||
curl \ | ||
-X DELETE \ | ||
--silent \ | ||
"${BASE_URL}/${path}" \ | ||
-H 'Content-Type: application/json' | ||
} | ||
|
||
is_equal() { | ||
local left="${1?}" | ||
local right="${2?}" | ||
diff <( printf '%s' "$left" ) <( printf "%s" "$right" ) \ | ||
&& return 0 | ||
echo -e "is_equal failed\nleft: $left\nright: $right" >&2 | ||
return 1 | ||
} | ||
|
||
match_regex() { | ||
local regex="${1?}" | ||
local what="${2?}" | ||
[[ "$what" =~ $regex ]] && return 0 | ||
echo -e "match_regex failed\nregex: '$regex'\nwhat: $what" >&2 | ||
return 1 | ||
} | ||
|
||
json_has_equal() { | ||
local key="${1?}" | ||
local value="${2?}" | ||
local data="${3?}" | ||
local cur_value=$(echo "$data" | jq -r ".$key") \ | ||
&& is_equal "$cur_value" "$value" \ | ||
&& return 0 | ||
echo -e "json_has_equal: key '$key' with value '$value' not found in \n$data" >&2 | ||
return 1 | ||
} | ||
|
||
json_has_match() { | ||
local key="${1?}" | ||
local match="${2?}" | ||
local data="${3?}" | ||
local cur_value=$(echo "$data" | jq -r ".$key") | ||
match_regex "$match" "$cur_value" && return 0 | ||
echo -e "json_has_match: key '$key' value '$cur_value' does not match '$match'" >&2 | ||
return 1 | ||
} | ||
|
||
json_get() { | ||
local key="${1?}" | ||
jq -r ".$key" | ||
} |
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,114 @@ | ||
#!/usr/bin/env bats | ||
|
||
# per whole file | ||
setup_file() { | ||
curdir="${BATS_TEST_FILENAME%/*}" | ||
db_host="127.0.0.1" | ||
test_db="expenses" | ||
test_db_pass="dummypass" | ||
test_db_user="expenses" | ||
mysql="mysql \ | ||
--host=$db_host \ | ||
--port=3306 \ | ||
--protocol=tcp \ | ||
--user=$test_db_user \ | ||
--password=$test_db_pass" | ||
|
||
$mysql -e "drop database if exists $test_db" | ||
$mysql -e "create database $test_db" | ||
$mysql "$test_db" < $curdir/../db/schema.sql | ||
$mysql -e "insert into accounts values (0, 'Test account 0'),(1, 'Test account 1')" "$test_db" | ||
$mysql -e "select * from expenses;" "$test_db" | ||
} | ||
|
||
|
||
# per test | ||
setup() { | ||
load helpers | ||
} | ||
|
||
@test "I can get the expenses (empty database)" { | ||
run do_get "api/expenses" | ||
|
||
[[ "$status" == "0" ]] | ||
is_equal \ | ||
'[]' \ | ||
"$output" | ||
} | ||
|
||
@test "I can create a new expense" { | ||
new_expense='{ | ||
"currency": "EUR", | ||
"amount": "42.0", | ||
"category": "eating-out", | ||
"description": "Some fake expense number 1", | ||
"accountId": 1, | ||
"timestamp": "'"$(date +%Y-%m-%dT%H:%M:%SZ)"'" | ||
}' | ||
run do_post "api/expenses" "$new_expense" | ||
|
||
[[ "$status" == "0" ]] | ||
json_has_match 'amount' '42.0' "$output" | ||
json_has_match 'category' 'eating-out' "$output" | ||
json_has_match 'description' 'Some fake expense number 1' "$output" | ||
json_has_match 'account.id' '1' "$output" | ||
} | ||
|
||
@test "I can update an expense" { | ||
new_expense='{ | ||
"currency": "EUR", | ||
"amount": "42.0", | ||
"category": "eating-out", | ||
"description": "Some fake expense number 1", | ||
"accountId": 1, | ||
"timestamp": "'"$(date +%Y-%m-%dT%H:%M:%SZ)"'" | ||
}' | ||
run do_post "api/expenses" "$new_expense" | ||
|
||
[[ "$status" == "0" ]] | ||
json_has_match 'amount' '42.0' "$output" | ||
|
||
new_expense_id="$(echo "$output" | json_get "id")" | ||
modified_expense='{ | ||
"currency": "EUR", | ||
"amount": "84.0", | ||
"category": "eating-out", | ||
"description": "Some fake expense number 1", | ||
"accountId": 1, | ||
"timestamp": "'"$(date +%Y-%m-%dT%H:%M:%SZ)"'" | ||
}' | ||
run do_put "api/expenses/$new_expense_id" "$modified_expense" | ||
|
||
[[ "$status" == "0" ]] | ||
json_has_match "amount" '84.0' "$output" | ||
|
||
run do_get "api/expenses" | ||
|
||
json_has_match "[] | select( .id == $new_expense_id) | .amount" '84.0' "$output" | ||
} | ||
|
||
@test "I can delete an expense" { | ||
new_expense='{ | ||
"currency": "EUR", | ||
"amount": "42.0", | ||
"category": "eating-out", | ||
"description": "Some fake expense number 1", | ||
"accountId": 1, | ||
"timestamp": "'"$(date +%Y-%m-%dT%H:%M:%SZ)"'" | ||
}' | ||
run do_post "api/expenses" "$new_expense" | ||
|
||
[[ "$status" == "0" ]] | ||
json_has_match 'amount' '42.0' "$output" | ||
|
||
new_expense_id="$(echo "$output" | json_get "id")" | ||
|
||
run do_delete "api/expenses/$new_expense_id" | ||
|
||
[[ "$status" == "0" ]] | ||
[[ "$output" == "$new_expense_id" ]] | ||
|
||
run do_get "api/expenses" | ||
|
||
json_has_match "[] | select( .id == $new_expense_id)" '' "$output" | ||
} |
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