From 72b827e5b0cbbe4c93783ccfb10de15fc724740f Mon Sep 17 00:00:00 2001 From: Maciej Banas Date: Wed, 27 Nov 2024 15:10:32 +0000 Subject: [PATCH] Add metadata to results. Together with test mocker. --- DESCRIPTION | 2 +- R/process_content.R | 9 +++++++++ R/process_repos.R | 14 +++++++++++--- R/test-helpers.R | 21 +++++++++++++++++++++ tests/testthat/setup.R | 1 + tests/testthat/test-process_content.R | 23 +++++++++++++++++++++++ tests/testthat/test-process_repos.R | 6 ++++-- tests/testthat/test-set_repos.R | 2 ++ 8 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 R/test-helpers.R create mode 100644 tests/testthat/setup.R diff --git a/DESCRIPTION b/DESCRIPTION index b16d7a3..f59b9dc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: GitAI Title: Extracts Knowledge From Git Repositories -Version: 0.0.0.9007 +Version: 0.0.0.9008 Authors@R: c( person("Kamil", "Wais", , "kamil.wais@gmail.com", role = c("aut", "cre")), person("Krystian", "Igras", , "krystian8207@gmail.com", role = "aut"), diff --git a/R/process_content.R b/R/process_content.R index a0139fa..936b2df 100644 --- a/R/process_content.R +++ b/R/process_content.R @@ -15,3 +15,12 @@ process_content <- function(gitai, content) { text = turn@text ) } + +add_metadata <- function(result, content) { + result[["metadata"]] <- list( + repo_url = content$repo_url[1], + files = paste0(content$file_path, collapse = ", "), + timestamp = "" + ) + result +} diff --git a/R/process_repos.R b/R/process_repos.R index 91a3b53..e6e622b 100644 --- a/R/process_repos.R +++ b/R/process_repos.R @@ -29,13 +29,21 @@ process_repos <- function(gitai, verbose = is_verbose()) { cli::cli_alert_info("Processing repository: {.pkg {repo_name}}") } - content_to_process <- + filtered_content <- files_content |> - dplyr::filter(repo_name == !!repo_name) |> + dplyr::filter(repo_name == !!repo_name) + content_to_process <- + filtered_content |> dplyr::pull(file_content) |> paste(collapse = "\n\n") - process_content(gitai = gitai, content = content_to_process) + result <- process_content( + gitai = gitai, + content = content_to_process + ) |> + add_metadata( + content = filtered_content + ) }) |> purrr::set_names(repositories) diff --git a/R/test-helpers.R b/R/test-helpers.R new file mode 100644 index 0000000..b1d3010 --- /dev/null +++ b/R/test-helpers.R @@ -0,0 +1,21 @@ +#' @noRd +#' @description A helper class to cache and mock results. +Mocker <- R6::R6Class( + "Mocker", + public = list( + + #' @field storage A list to store objects. + storage = list(), + + #' @description Method to cache objects. + cache = function(object = NULL) { + object_name <- deparse(substitute(object)) + self$storage[[paste0(object_name)]] <- object + }, + + #' @description Method to retrieve objects. + use = function(object_name) { + self$storage[[paste0(object_name)]] + } + ) +) diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R new file mode 100644 index 0000000..ca930d0 --- /dev/null +++ b/tests/testthat/setup.R @@ -0,0 +1 @@ +test_mocker <- Mocker$new() diff --git a/tests/testthat/test-process_content.R b/tests/testthat/test-process_content.R index 970a03b..4e738e6 100644 --- a/tests/testthat/test-process_content.R +++ b/tests/testthat/test-process_content.R @@ -43,4 +43,27 @@ test_that("processing a single file content with deterministic output", { expect_equal(result$text, process_content(gitai = my_project, content = test_content)$text) + + test_mocker$cache(result) +}) + +test_that("metadata is added to content", { + mocked_files_content <- dplyr::tibble( + repo_name = c("TestRepo", "TestRepo"), + repo_id = c("repo_id", "repo_id"), + organization = c("org", "org"), + file_path = c("file1.md", "file2.md"), + file_content = c("test1", "test2"), + file_size = c(1, 1), + repo_url = c("test_URL", "test_URL") + ) + result_with_metadata <- + test_mocker$use("result") |> + add_metadata( + content = mocked_files_content + ) + expect_true("metadata" %in% names(result_with_metadata)) + expect_type(result_with_metadata[["metadata"]], "list") + expect_equal(names(result_with_metadata[["metadata"]]), c("repo_url", "files", "timestamp")) + }) diff --git a/tests/testthat/test-process_repos.R b/tests/testthat/test-process_repos.R index dd42a6f..7da49f8 100644 --- a/tests/testthat/test-process_repos.R +++ b/tests/testthat/test-process_repos.R @@ -1,5 +1,7 @@ test_that("process_repos() returns results with repo metadata", { + verbose_off() + my_project <- initialize_project("gitai_test_project") |> set_github_repos( @@ -9,12 +11,12 @@ test_that("process_repos() returns results with repo metadata", { set_llm() |> set_prompt(system_prompt = "Summarize the user content if one sentence.") - my_project$gitstats$verbose_off() - results <- my_project |> process_repos() results |> is.list() |> expect_true() results |> names() |> expect_equal(c("GitStats", "DataFakeR")) results |> purrr::map(~ nchar(.x$text) > 10) |> unlist() |> all() |> expect_true() + + results |> purrr::walk(~ expect_true("metadata" %in% names(.))) }) diff --git a/tests/testthat/test-set_repos.R b/tests/testthat/test-set_repos.R index 207b8e9..4c5d0cb 100644 --- a/tests/testthat/test-set_repos.R +++ b/tests/testthat/test-set_repos.R @@ -1,4 +1,6 @@ test_that("set_*_repos creates GitStats object inside GitAI with repos set", { + verbose_off() + my_project <- initialize_project("gitai_test_project") my_project <-