From 81899459ae70c3c10823f5fc77927b6b75040310 Mon Sep 17 00:00:00 2001 From: Tawab Safi Date: Sat, 27 Jul 2024 17:39:40 -0400 Subject: [PATCH] Added Tests for chat function #7 --- .github/workflows/R-CMD-check.yaml | 4 +- tests/testthat/test-chat.R | 81 +++++++++++++++++++++++++++ tests/testthat/test-test_connection.R | 3 - 3 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/test-chat.R diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index fc32834..a0d5e49 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -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, dev, test, tawab, stream] + branches: [main, master, dev, test, chat, stream] pull_request: - branches: [main, master, dev, test, tawab, stream] + branches: [main, master, dev, test, chat, stream] name: R-CMD-check diff --git a/tests/testthat/test-chat.R b/tests/testthat/test-chat.R new file mode 100644 index 0000000..dc5563b --- /dev/null +++ b/tests/testthat/test-chat.R @@ -0,0 +1,81 @@ +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 = "Hello! How are you?") + ) + + 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") + expect_true(nchar(result$content[1]) > 0) +}) + +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 respects output parameter", { + skip_if_not(test_connection()$status_code == 200, "Ollama server not available") + + messages <- list( + list(role = "user", content = "Hello! How are you?") + ) + + result_jsonlist <- chat("llama3", messages, output = "jsonlist") + expect_type(result_jsonlist, "list") + expect_true(length(result_jsonlist) > 0) + + result_df <- chat("llama3", messages, output = "df") + expect_s3_class(result_df, "data.frame") + + result_text <- chat("llama3", messages, output = "text") + expect_type(result_text, "character") +}) + +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) +}) diff --git a/tests/testthat/test-test_connection.R b/tests/testthat/test-test_connection.R index 8849056..e69de29 100644 --- a/tests/testthat/test-test_connection.R +++ b/tests/testthat/test-test_connection.R @@ -1,3 +0,0 @@ -test_that("multiplication works", { - expect_equal(2 * 2, 4) -})