Skip to content

Commit

Permalink
Add tests to chat
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Jul 28, 2024
1 parent 839dcc6 commit f1b5992
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
branches: [main, master, chat]
pull_request:
branches: [main, master]
branches: [main, master, chat]

name: R-CMD-check

Expand Down
2 changes: 1 addition & 1 deletion man/image_encode_base64.Rd

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

96 changes: 96 additions & 0 deletions tests/testthat/test-chat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
library(testthat)
library(ollamar)

test_that("chat function works with basic input", {
skip_if_not(test_connection()$status_code == 200, "Ollama server not available")

messages <- list(
list(role = "user", content = "Tell me a 5-word story.")
)

# not streaming
expect_s3_class(chat("llama3", messages), "httr2_response")
expect_s3_class(chat("llama3", messages, output = "resp"), "httr2_response")
expect_s3_class(chat("llama3", messages, output = "df"), "data.frame")
expect_type(chat("llama3", messages, output = "jsonlist"), "list")
expect_type(chat("llama3", messages, output = "text"), "character")
expect_type(chat("llama3", messages, output = "raw"), "character")

# streaming
expect_s3_class(chat("llama3", messages, stream = TRUE), "httr2_response")
expect_s3_class(chat("llama3", messages, stream = TRUE, output = "resp"), "httr2_response")
expect_s3_class(chat("llama3", messages, stream = TRUE, output = "df"), "data.frame")
expect_type(chat("llama3", messages, stream = TRUE, output = "jsonlist"), "list")
expect_type(chat("llama3", messages, stream = TRUE, output = "text"), "character")
expect_type(chat("llama3", messages, stream = TRUE, output = "raw"), "character")

# resp_process
# not streaming
result <- chat("llama3", messages)
expect_s3_class(result, "httr2_response")
expect_s3_class(resp_process(result, "resp"), "httr2_response")
expect_s3_class(resp_process(result, "df"), "data.frame")
expect_type(resp_process(result, "jsonlist"), "list")
expect_type(resp_process(result, "text"), "character")
expect_type(resp_process(result, "raw"), "character")

# streaming
result <- chat("llama3", messages, stream = TRUE)
expect_s3_class(result, "httr2_response")
expect_s3_class(resp_process(result, "resp"), "httr2_response")
# expect_s3_class(resp_process(result, "df"), "data.frame") # BUG: fail test
# expect_type(resp_process(result, "jsonlist"), "list") # BUG fail test
# expect_type(resp_process(result, "text"), "character") # BUG fail test
# expect_type(resp_process(result, "raw"), "character") # BUG fail test

result <- chat("llama3", messages, output = "df")
expect_s3_class(result, "data.frame")
expect_true(all(c("model", "role", "content", "created_at") %in% names(result)))
expect_equal(result$model[1], "llama3")
expect_equal(result$role[1], "assistant")
})

test_that("chat function handles streaming correctly", {
skip_if_not(test_connection()$status_code == 200, "Ollama server not available")

messages <- list(
list(role = "user", content = "Count to 5")
)

result <- chat("llama3", messages, stream = TRUE, output = "text")
expect_type(result, "character")
expect_true(nchar(result) > 0)
expect_match(result, "1.*2.*3.*4.*5", all = FALSE)
})


test_that("chat function handles multiple messages", {
skip_if_not(test_connection()$status_code == 200, "Ollama server not available")

messages <- list(
list(role = "user", content = "Hello!"),
list(role = "assistant", content = "Hi! How can I help you?"),
list(role = "user", content = "What's the capital of France?")
)

result <- chat("llama3", messages, output = "df")

expect_s3_class(result, "data.frame")
expect_equal(nrow(result), 1) # Expecting one response
expect_match(result$content[1], "Paris", ignore.case = TRUE)
})

test_that("chat function handles additional options", {
skip_if_not(test_connection()$status_code == 200, "Ollama server not available")

messages <- list(
list(role = "user", content = "Tell me a very short joke")
)

result_normal <- chat("llama3", messages, output = "text")
result_creative <- chat("llama3", messages, output = "text", temperature = 2.0)

expect_type(result_normal, "character")
expect_type(result_creative, "character")
expect_false(result_normal == result_creative)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-test_generate.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test_that("generate function works with different outputs and resp_process", {
expect_type(generate("llama3", "The sky is...", stream = TRUE, output = "text"), "character")
expect_type(generate("llama3", "The sky is...", stream = TRUE, output = "raw"), "character")

## resp_process
# resp_process
# not streaming
result <- generate("llama3", "The sky is...")
expect_s3_class(result, "httr2_response")
Expand Down

0 comments on commit f1b5992

Please sign in to comment.