From cb63cf20a9b40c2db71f209574982b1874c862c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Tue, 5 Dec 2023 15:52:37 +0100 Subject: [PATCH] add test, note smaller fixtures for git2 --- .../fixtures/git1/api/languages-298d87.json | 157 ++++++++++++++++++ .../fixtures/git1/api/languages-d70faa.json | 118 +++++++++++++ .../git1/api/translate-dfd682-POST.json | 8 + .../git2/api/translate-b3fa90-POST.json | 8 + .../git2/api/translate-e96072-POST.json | 8 + tests/testthat/git1/api/languages-d70faa.R | 10 ++ tests/testthat/test-update.R | 55 ++++++ 7 files changed, 364 insertions(+) create mode 100644 tests/testthat/fixtures/git1/api/languages-298d87.json create mode 100644 tests/testthat/fixtures/git1/api/languages-d70faa.json create mode 100644 tests/testthat/fixtures/git1/api/translate-dfd682-POST.json create mode 100644 tests/testthat/fixtures/git2/api/translate-b3fa90-POST.json create mode 100644 tests/testthat/fixtures/git2/api/translate-e96072-POST.json create mode 100644 tests/testthat/git1/api/languages-d70faa.R create mode 100644 tests/testthat/test-update.R diff --git a/tests/testthat/fixtures/git1/api/languages-298d87.json b/tests/testthat/fixtures/git1/api/languages-298d87.json new file mode 100644 index 0000000..98a8bfb --- /dev/null +++ b/tests/testthat/fixtures/git1/api/languages-298d87.json @@ -0,0 +1,157 @@ +[ + { + "language": "BG", + "name": "Bulgarian", + "supports_formality": false + }, + { + "language": "CS", + "name": "Czech", + "supports_formality": false + }, + { + "language": "DA", + "name": "Danish", + "supports_formality": false + }, + { + "language": "DE", + "name": "German", + "supports_formality": true + }, + { + "language": "EL", + "name": "Greek", + "supports_formality": false + }, + { + "language": "EN-GB", + "name": "English (British)", + "supports_formality": false + }, + { + "language": "EN-US", + "name": "English (American)", + "supports_formality": false + }, + { + "language": "ES", + "name": "Spanish", + "supports_formality": true + }, + { + "language": "ET", + "name": "Estonian", + "supports_formality": false + }, + { + "language": "FI", + "name": "Finnish", + "supports_formality": false + }, + { + "language": "FR", + "name": "French", + "supports_formality": true + }, + { + "language": "HU", + "name": "Hungarian", + "supports_formality": false + }, + { + "language": "ID", + "name": "Indonesian", + "supports_formality": false + }, + { + "language": "IT", + "name": "Italian", + "supports_formality": true + }, + { + "language": "JA", + "name": "Japanese", + "supports_formality": true + }, + { + "language": "KO", + "name": "Korean", + "supports_formality": false + }, + { + "language": "LT", + "name": "Lithuanian", + "supports_formality": false + }, + { + "language": "LV", + "name": "Latvian", + "supports_formality": false + }, + { + "language": "NB", + "name": "Norwegian", + "supports_formality": false + }, + { + "language": "NL", + "name": "Dutch", + "supports_formality": true + }, + { + "language": "PL", + "name": "Polish", + "supports_formality": true + }, + { + "language": "PT-BR", + "name": "Portuguese (Brazilian)", + "supports_formality": true + }, + { + "language": "PT-PT", + "name": "Portuguese (European)", + "supports_formality": true + }, + { + "language": "RO", + "name": "Romanian", + "supports_formality": false + }, + { + "language": "RU", + "name": "Russian", + "supports_formality": true + }, + { + "language": "SK", + "name": "Slovak", + "supports_formality": false + }, + { + "language": "SL", + "name": "Slovenian", + "supports_formality": false + }, + { + "language": "SV", + "name": "Swedish", + "supports_formality": false + }, + { + "language": "TR", + "name": "Turkish", + "supports_formality": false + }, + { + "language": "UK", + "name": "Ukrainian", + "supports_formality": false + }, + { + "language": "ZH", + "name": "Chinese (simplified)", + "supports_formality": false + } +] diff --git a/tests/testthat/fixtures/git1/api/languages-d70faa.json b/tests/testthat/fixtures/git1/api/languages-d70faa.json new file mode 100644 index 0000000..8f73560 --- /dev/null +++ b/tests/testthat/fixtures/git1/api/languages-d70faa.json @@ -0,0 +1,118 @@ +[ + { + "language": "BG", + "name": "Bulgarian" + }, + { + "language": "CS", + "name": "Czech" + }, + { + "language": "DA", + "name": "Danish" + }, + { + "language": "DE", + "name": "German" + }, + { + "language": "EL", + "name": "Greek" + }, + { + "language": "EN", + "name": "English" + }, + { + "language": "ES", + "name": "Spanish" + }, + { + "language": "ET", + "name": "Estonian" + }, + { + "language": "FI", + "name": "Finnish" + }, + { + "language": "FR", + "name": "French" + }, + { + "language": "HU", + "name": "Hungarian" + }, + { + "language": "ID", + "name": "Indonesian" + }, + { + "language": "IT", + "name": "Italian" + }, + { + "language": "JA", + "name": "Japanese" + }, + { + "language": "KO", + "name": "Korean" + }, + { + "language": "LT", + "name": "Lithuanian" + }, + { + "language": "LV", + "name": "Latvian" + }, + { + "language": "NB", + "name": "Norwegian" + }, + { + "language": "NL", + "name": "Dutch" + }, + { + "language": "PL", + "name": "Polish" + }, + { + "language": "PT", + "name": "Portuguese" + }, + { + "language": "RO", + "name": "Romanian" + }, + { + "language": "RU", + "name": "Russian" + }, + { + "language": "SK", + "name": "Slovak" + }, + { + "language": "SL", + "name": "Slovenian" + }, + { + "language": "SV", + "name": "Swedish" + }, + { + "language": "TR", + "name": "Turkish" + }, + { + "language": "UK", + "name": "Ukrainian" + }, + { + "language": "ZH", + "name": "Chinese" + } +] diff --git a/tests/testthat/fixtures/git1/api/translate-dfd682-POST.json b/tests/testthat/fixtures/git1/api/translate-dfd682-POST.json new file mode 100644 index 0000000..72796b0 --- /dev/null +++ b/tests/testthat/fixtures/git1/api/translate-dfd682-POST.json @@ -0,0 +1,8 @@ +{ + "translations": [ + { + "detected_source_language": "EN", + "text": "\n\n\n \n cabecera<\/text>\n <\/heading>\n \n esto es un texto<\/text>\n <\/paragraph>\n \n subtítulo<\/text>\n <\/heading>\n \n ¡bonito!<\/text>\n <\/paragraph>\n<\/document>\n" + } + ] +} diff --git a/tests/testthat/fixtures/git2/api/translate-b3fa90-POST.json b/tests/testthat/fixtures/git2/api/translate-b3fa90-POST.json new file mode 100644 index 0000000..4e7c92f --- /dev/null +++ b/tests/testthat/fixtures/git2/api/translate-b3fa90-POST.json @@ -0,0 +1,8 @@ +{ + "translations": [ + { + "detected_source_language": "EN", + "text": "\n\n\n \n un título<\/text>\n <\/heading>\n<\/document>\n" + } + ] +} diff --git a/tests/testthat/fixtures/git2/api/translate-e96072-POST.json b/tests/testthat/fixtures/git2/api/translate-e96072-POST.json new file mode 100644 index 0000000..3232811 --- /dev/null +++ b/tests/testthat/fixtures/git2/api/translate-e96072-POST.json @@ -0,0 +1,8 @@ +{ + "translations": [ + { + "detected_source_language": "EN", + "text": "\n\n\n \n impresionante<\/text>\n <\/paragraph>\n<\/document>\n" + } + ] +} diff --git a/tests/testthat/git1/api/languages-d70faa.R b/tests/testthat/git1/api/languages-d70faa.R new file mode 100644 index 0000000..bd29935 --- /dev/null +++ b/tests/testthat/git1/api/languages-d70faa.R @@ -0,0 +1,10 @@ +structure(list(method = "GET", url = "api/languages?type=source", + status_code = 403L, headers = structure(list(date = "Tue, 05 Dec 2023 12:44:02 GMT", + `access-control-allow-origin` = "*", `strict-transport-security` = "max-age=63072000; includeSubDomains; preload", + `server-timing` = "l7_lb_tls;dur=117, l7_lb_idle;dur=0, l7_lb_receive;dur=0, l7_lb_total;dur=130", + `access-control-expose-headers` = "Server-Timing"), class = "httr2_headers"), + body = raw(0), request = structure(list(url = "https://api.deepl.com/v2/languages?type=source", + method = "GET", headers = structure(list(Authorization = "DeepL-Auth-Key lalala"), redact = character(0)), + body = NULL, fields = list(), options = list(), policies = list( + retry_max_tries = 3)), class = "httr2_request"), + cache = new.env(parent = emptyenv())), class = "httr2_response") diff --git a/tests/testthat/test-update.R b/tests/testthat/test-update.R new file mode 100644 index 0000000..9ced000 --- /dev/null +++ b/tests/testthat/test-update.R @@ -0,0 +1,55 @@ +test_that("deepl_update() works", { + dir <- withr::local_tempdir() + file <- file.path(dir, "bla.md") + fs::file_create(file) + out_file <- file.path(dir, "bla.es.md") + + brio::write_lines( + c("# header", "", "this is some text", "## subtitle", "", "nice!"), + file + ) + + with_mock_dir("git1", { + deepl_translate( + path = file, + out_path = out_file, + source_lang = "EN", + target_lang = "ES", + formality = "less", + yaml_fields = NULL + ) + }) + + gert::git_init(dir) + gert::git_add(c(fs::path_file(file), fs::path_file(out_file)), repo = dir) + gert::git_commit_all("First commit", repo = dir) + + brio::write_lines( + c("# a title", "", "this is some text", "", "awesome", "", "## subtitle", ""), + file + ) + gert::git_add(fs::path_file(file), repo = dir) + gert::git_commit("Second commit", repo = dir) + + original_translation <- brio::read_lines(out_file) + + with_mock_dir("git2", { + deepl_update( + path = file, + out_path = out_file, + source_lang = "EN", + target_lang = "ES", + formality = "less", + yaml_fields = NULL + ) + }) + + new_translation <- brio::read_lines(out_file) + expect_true( + !all(new_translation %in% original_translation) + ) + expect_true( + any(new_translation %in% original_translation) + ) + +})