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

Reviewing the tests at the tiltIndicator package #578

Closed
AnneSchoenauer opened this issue Nov 5, 2023 · 9 comments
Closed

Reviewing the tests at the tiltIndicator package #578

AnneSchoenauer opened this issue Nov 5, 2023 · 9 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@AnneSchoenauer
Copy link

@maurolepore, we discussed a while ago on how best I can review the tiltIndicator package, and we said it would be good if I go through the tests that you already implemented and I see if there are tests missing. I would like to do this in the next weeks. Could you please let me know where to best look for the test best? We also talked about the challenges that there are functions in function in functions and therefore it is hard for me to understand if the code really implements the methodology that we came up with. However, I understood that if I make sure that we have all tests implemented that we need that it is okay that I don't see exactly the functions that implement the code right?
Thanks a lot!
All the best
Anne

@AnneSchoenauer AnneSchoenauer added the question Further information is requested label Nov 5, 2023
@maurolepore
Copy link
Contributor

maurolepore commented Nov 6, 2023

@AnneSchoenauer, a great place to learn about tests is here but I'll explain them a bit in this comment.

Tests always live in the folder tests/testthat/. If you're browsing them on GitHub the URL is a bit more complicated: https://github.com/2DegreesInvesting/tiltIndicator/tree/main/tests/testthat

You'll see many tests but you should mainly focus only on the test-files which names match test-emissions_profile*, and "test-sector_profile*.R.

In the beginning they will look intimidating, but thinks of each test as a reprex. plus an encoded expectation.

A reprex is a tiny reproducible example that you construct to show a specific behaviour of the code. Typically you inspect the result of the reprex by looking at it. A test only extends a reprex by adding code that inspects the result automatically -- i.e. an expectation. If the expectation is met nothing happens but if it is not met then it trows a failure message.

For example, consider this test:

test_that("outputs expected columns at company level", {
  companies <- example_companies()
  scenarios <- example_scenarios()
  inputs <- example_inputs()

  product <- sector_profile_upstream_at_product_level(companies, scenarios, inputs)
  out <- any_at_company_level(product)

  expected <- cols_at_company_level()
  expect_equal(names(out)[seq_along(expected)], expected)
})

Here I remove the test-related code to show you that it's just a reprex:

devtools::load_all()
#> ℹ Loading tiltIndicator

companies <- example_companies()
scenarios <- example_scenarios()
inputs <- example_inputs()

product <- sector_profile_upstream_at_product_level(companies, scenarios, inputs)
out <- any_at_company_level(product)

expected <- cols_at_company_level()

# Compare
names(out)[seq_along(expected)]
#> [1] "companies_id"  "grouped_by"    "risk_category" "value"

# Same
expected
#> [1] "companies_id"  "grouped_by"    "risk_category" "value"

Created on 2023-11-06 with reprex v2.0.2

So you may try guess what each test does by reading the test-title. In the example above the title is "outputs expected columns at company level". Then you can examine the code by running it interactively in RStudio.

Learning more

I recorded ds-incubators about reprex and also about tests.

https://bit.ly/ds-incubator-videos

  • reprex
  • Tests

@AnneSchoenauer
Copy link
Author

@maurolepore Great! Thanks a lot! I blocked some time for Thursday to walk through all the material :) Looking forward to it!

@AnneSchoenauer AnneSchoenauer added documentation Improvements or additions to documentation and removed question Further information is requested labels Nov 7, 2023
@maurolepore
Copy link
Contributor

Warn you that looking at tests will burn your brain :-)

A more fun and focused way to test the package from your perspective would be to formulate a number of key conceptual questions, then try create a reprex that answers your question using the package and a tiny dataset (often 1-2 rows of data are the most useful. More than that and the output gets too large to understand).

Then you can give me your reprexes and I can turn them into formal tests.

@AnneSchoenauer
Copy link
Author

AnneSchoenauer commented Jan 2, 2024

After thinking and reviewing the output files I think it is good to have the following tests. @maurolepore this is only an overview ticket for me now. I will create little tickets and reprex so that you know on what we need to work on.

  • Reprex for tiltIndicator package for profile_ranking emission profiles

Sorry, something went wrong.

@AnneSchoenauer
Copy link
Author

Dear @Tilmon following this conversation here could you please think about potential tests that we need in the tiltIndicator package to ensure that the code mirrors the methodology that we came up with.
I will create a reprex for the profile_ranking function. But maybe you want to ensure something else?

@AnneSchoenauer AnneSchoenauer removed their assignment Jan 2, 2024
@maurolepore
Copy link
Contributor

@Tilmon and @AnneSchoenauer

Here is a 10' video to help you review the tests.

A tip to quickly overview all tests is to go to a repo and search for the string "test_that(". For example:

https://github.com/search?q=repo%3A2DegreesInvesting%2FtiltIndicator%20%22test_that(%22&type=code

That will match all the test-titles so you can quickly skim the intent of all our tests and then explore only the ones you want.

Remember to expand the links "Show xxx more tests"

image

@Tilmon
Copy link
Contributor

Tilmon commented Jan 8, 2024

Thanks so much, @maurolepore !

@AnneSchoenauer , FYI, as discussed, I checked that all tests I applied in summer as part of the data prep are now also reflected in tiltIndicator and tiltIndicatorAfter.

@kalashsinghal
Copy link
Collaborator

Hi @Tilmon, should I close this issue if completed?

@Tilmon
Copy link
Contributor

Tilmon commented Oct 8, 2024

@kalashsinghal good idea, I'll close it!

@Tilmon Tilmon closed this as completed Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

4 participants