Skip to content

Commit

Permalink
Add metadata to results. Together with test mocker.
Browse files Browse the repository at this point in the history
  • Loading branch information
maciekbanas committed Nov 27, 2024
1 parent d041c7e commit 72b827e
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
9 changes: 9 additions & 0 deletions R/process_content.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 11 additions & 3 deletions R/process_repos.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions R/test-helpers.R
Original file line number Diff line number Diff line change
@@ -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)]]
}
)
)
1 change: 1 addition & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_mocker <- Mocker$new()
23 changes: 23 additions & 0 deletions tests/testthat/test-process_content.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

})
6 changes: 4 additions & 2 deletions tests/testthat/test-process_repos.R
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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(.)))
})
2 changes: 2 additions & 0 deletions tests/testthat/test-set_repos.R
Original file line number Diff line number Diff line change
@@ -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 <-
Expand Down

0 comments on commit 72b827e

Please sign in to comment.