Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#125 Write unit tests for shiny.i18n package #126

Merged
merged 7 commits into from
Jul 26, 2023
Merged

Conversation

Eduardodudu
Copy link
Contributor

#125 Write unit tests for shiny.i18n package

Added unit tests for the functions:

  1. translate_with_google_cloud
  2. create_translation_addin
  3. usei18n
  4. update_lang
  5. .onLoad

Code coverage increased from 62% to 83%

@Eduardodudu Eduardodudu self-assigned this Jul 6, 2023
Copy link
Member

@jakubnowicki jakubnowicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general this is going in the right direction, but I have some comments in the code.
I also have a more general comment. I understand, that with the test descriptions you have followed the convention of the existing tests, but I want to ask you to provide more descriptive information. For example:
Instead of test_that("Test update_lang", { I would expect something like test_that("update_lang changes the selected language in session$userData$shiny.i18n$lang", {.
This is especially important for more complex tests like the one for the RStudio addin.

Comment on lines 3 to 4
test_that("translate_with_google_cloud", {
it("Should check if API is set", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I don't see a reason to use BDD language here, since there is only a single test.
  2. If you want to stick to BDD, use describe-it - more details here: https://testthat.r-lib.org/reference/describe.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Kuba! Updated!

Comment on lines 5 to 18
withr::with_envvar(
new = c(
GL_AUTH = "None"
), {
# Arrange
txt <- "Hello, how are you?"
target_lang <- "fr"
# Act
result <- evaluate_promise(
translate_with_google_cloud(txt, target_lang)
)
# Assert
expect_equal(length(result$messages), 7)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test makes little sense. Since there is no authorization, the result is just information that there is no authorization. This would require end-to-end tests if we want to ensure the automated translation works.
I suggest checking if the error part in the tryCatch produces the desired message for unit tests. But for that, you should mock gl_translate function with a simple one that emits an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

Comment on lines 52 to 79
test_that("Test create_translation_addin", {
temp <- tempfile()
# Mock the behavior of RStudio API calls
with_mock(
`rstudioapi::showDialog` = function(...) {
message("Mock: showDialog called")
TRUE
},
`rstudioapi::getActiveDocumentContext` = function() {
message("Mock: getActiveDocumentContext called")
list(path = temp)
},
`rstudioapi::showQuestion` = function(...) {
message("Mock: showQuestion called")
TRUE
},
`create_translation_file` = function(path, format) {
# Mock the behavior of create_translation_file
expect_equal(path, temp)
expect_equal(format, "json")
},
{
# Call the function
create_translation_addin()
}
)

})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how you were able to use mocking here. Nice work!

One small comment - with_mock is marked as superseded. The alternatives are still in the experimental stage, so I'm ok with not using them, but it might be worth adding a comment about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I also saw this and tried the other approaches, but couldn't succeed and there were less examples to learn from

class(i18ntag),
c("shiny.tag.list", "list")
)
expect_equal(length(i18ntag[[1]]$children), 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are checking only the length. It would be great to also check the content. The first child should include translations, second should be the js file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried doing this with shin.tag with tags$script but couldn't make expect_equal work. Because of this, I set them as characters

expect_equal(length(.i18_config), 3)
expect_equal(
names(.i18_config),
c("cultural_date_format", "cultural_bignumer_mark", "cultural_punctuation_mark")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the function takes values from this config file, you can also check if they are passed correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

# Assert
testServer(server, {
session$setInputs(selected_language = "pl")
expect_equal(reactive_language(), "pl")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test makes no sense. You are only checking if the Shiny input and Shiny reactive are working, and you should test the functionality of shiny.i18n. Since this function 1. creates a reactiveVal in `session$userData and 2. sets its value using the chosen language, you should test that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Updated

@Eduardodudu
Copy link
Contributor Author

I had to do some updates in the code due to lintr. This is my last commit update

Copy link
Member

@jakubnowicki jakubnowicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you!

@jakubnowicki jakubnowicki merged commit 132348b into main Jul 26, 2023
@jakubnowicki jakubnowicki deleted the edu.unit-tests branch July 26, 2023 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants