Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion ci/all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,22 @@ if [ "$NO_BUILD" != "1" ]; then
fi
fi

echo "roc check"
echo "roc check + matching .exp file"
for roc_file in $TESTS_DIR*.roc; do
$ROC check $roc_file
done
for roc_file in $EXAMPLES_DIR*.roc; do
$ROC check $roc_file

## Check if every example has matching expect script

# Extract the base filename without extension
base_name=$(basename "$roc_file" .roc)

if [ ! -f "ci/expect_scripts/${base_name}.exp" ]; then
echo "ERROR: No matching expect script found for $base_name" >&2
exit 1
fi
done

# roc build
Expand Down
13 changes: 10 additions & 3 deletions ci/expect_scripts/issue_104.exp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ set timeout 7

source ./ci/expect_scripts/shared-code.exp

set env(DB_PATH) "$env(TESTS_DIR)todos.db"

spawn $env(TESTS_DIR)issue_104
set env(DB_PATH) "$env(TESTS_DIR)/todos.db"

expect "Program exited with error:\r\n" {
exit 0
expect -re {.*RowsReturned.*} {
exit 0
}

puts stderr "\nError: output was different from expected value."
exit 1

}

puts stderr "\nError: output was different from expected value."
exit 1
exit 1
25 changes: 25 additions & 0 deletions ci/expect_scripts/sqlite.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/expect

# uncomment line below for debugging
# exp_internal 1

set timeout 7

set env(DB_PATH) "$env(EXAMPLES_DIR)todos.db"

spawn $env(EXAMPLES_DIR)sqlite

expect "Listening on <http://127.0.0.1:8000>\r\n" {

set curlOutputIndex [exec curl -sS localhost:8000]

if {[string match "*Completed,*" $curlOutputIndex]} {
exit 0
} else {
puts "Error: curl output was different than expected: $curlOutputIndex"
exit 1
}
}

puts stderr "\nError: output was different than expected."
exit 1
10 changes: 3 additions & 7 deletions ci/expect_scripts/todos.exp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ set env(DB_PATH) "$env(EXAMPLES_DIR)todos.db"

spawn $env(EXAMPLES_DIR)todos

expect "INFO: Checking if sqlite3 is installed...\r\n"

expect "3*" # sqlite3 version

Comment on lines -12 to -15
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer in the output.

expect "Listening on <http://127.0.0.1:8000>\r\n" {

set curlOutputIndex [exec curl -sS localhost:8000]
Expand All @@ -27,10 +23,10 @@ expect "Listening on <http://127.0.0.1:8000>\r\n" {
if {[string match "*Prepare for AoC*" $curlOutputTodos]} {
expect "Z GET /todos\r\n" {

set curlOutputPost [exec curl -sSX POST "localhost:8000/todos?task=Task%206&status=planned"]
set curlOutputPost [exec curl -sSX POST "localhost:8000/todos?task=Task%206&status=completed"]

if {[string match "*planned*" $curlOutputPost]} {
expect "Z POST /todos?task=Task%206&status=planned\r\n" {
if {[string match "*completed*" $curlOutputPost]} {
expect "Z POST /todos?task=Task%206&status=completed\r\n" {

set curlOutputTodos2 [exec curl -sS localhost:8000/todos]

Expand Down
17 changes: 14 additions & 3 deletions examples/sqlite.roc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ init! = |{}|
Sqlite.prepare!(
{
path: db_path,
query: "SELECT id, task FROM todos WHERE status = :status;",
query: "SELECT * FROM todos WHERE status = :status;",
},
)
? |err| ServerErr("Failed to prepare Sqlite statement: ${Inspect.to_str(err)}")
Expand All @@ -36,10 +36,11 @@ respond! = |_, { stmt }|
rows: { Sqlite.decode_record <-
id: Sqlite.i64("id"),
task: Sqlite.str("task"),
status: Sqlite.str("status") |> Sqlite.map_value_result(decode_db_status),
},
},
)?
|> List.map(|{ id, task }| "row ${Num.to_str(id)}, task: ${task}")
|> List.map(|todo| Inspect.to_str(todo))
|> Str.join_with("\n")

# Print out the results
Expand All @@ -51,4 +52,14 @@ respond! = |_, { stmt }|
headers: [{ name: "Content-Type", value: "text/html; charset=utf-8" }],
body: Str.to_utf8(strings),
},
)
)

TodoStatus : [Todo, Completed, InProgress]

decode_db_status : Str -> Result TodoStatus _
decode_db_status = |status_str|
when status_str is
"todo" -> Ok(Todo)
"completed" -> Ok(Completed)
"in-progress" -> Ok(InProgress)
_ -> Err(ParseError("Unknown status str: ${status_str}"))
Comment on lines +57 to +65
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To demonstrate the new map_value_result function.

Binary file modified examples/todos.db-wal
Binary file not shown.
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions platform/Sqlite.roc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module [
errcode_to_str,
decode_record,
map_value,
map_value_result,
tagged_value,
str,
bytes,
Expand Down Expand Up @@ -379,6 +380,31 @@ map_value = |@SqlDecode(gen_decode), mapper|
Ok(mapper(val)),
)

## Transform the output of a decoder by applying a function (that returns a Result) to the decoded value.
## The Result is converted to SqlDecode.
##
## Example:
## ```
## decode_status : Str -> Result OnlineStatus UnknownStatusErr
## decode_status = |status_str|
## when status_str is
## "online" -> Ok(Online)
## "offline" -> Ok(Offline)
## _ -> Err(UnknownStatus("${status_str}"))
##
## Sqlite.str("status") |> Sqlite.map_value_result(decode_status)
## ```
map_value_result : SqlDecode a err, (a -> Result c (SqlDecodeErr err)) -> SqlDecode c err
map_value_result = |@SqlDecode(gen_decode), mapper|
@SqlDecode(
|cols|
decode! = gen_decode(cols)

|stmt|
val = try(decode!, stmt)
mapper(val),
)

RowCountErr err : [NoRowsReturned, TooManyRowsReturned]err

# internal use only
Expand Down