datacamp / testwhat

Write Submission Correctness Tests for R exercises
https://datacamp.github.io/testwhat
GNU Affero General Public License v3.0
33 stars 25 forks source link

test_ggplot() fails to test facet layer #69

Closed tommyjee closed 6 years ago

tommyjee commented 7 years ago

@machow (cc @ncarchedi) I noticed that the test_ggplot() SCT does not correctly test the facet layer (despite the check_facet and facet_fail_msg arguments to test_ggplot()) in the following exercises in the EDA-UN course:

All of these exercises require testing the contents inside the facet_wrap() function (e.g. ~ country in the facet_wrap() call)

machow commented 7 years ago

Can you paste the code pieces (pre-exercise-code, sct, solution) for whichever case above is simplest?

tommyjee commented 7 years ago

@machow Will do in the future. Here it is:

*\ =pre_exercise_code

library(ggplot2)
source("http://s3.amazonaws.com/assets.datacamp.com/production/course_1414/datasets/shared.R")
by_year_country <- read_dataset("by_year_country")

*\ =solution

# Vector of six countries to examine
countries <- c("United States", "United Kingdom",
               "France", "Japan", "Brazil", "India")

# Filtered by_year_country: filtered_6_countries
filtered_6_countries <- by_year_country %>%
  filter(country %in% countries)

# Line plot of % yes over time faceted by country
ggplot(filtered_6_countries, aes(year, percent_yes)) +
  geom_line() +
  facet_wrap(~ country)

*\ =sct

test_predefined_objects("countries")

test_correct({
    test_data_frame("filtered_6_countries", incorrect_msg = "Did you `filter()` such that your new dataset contains all the countries listed in the vector `countries`? The `%in%` operator may prove useful here.")
    }, {
    test_function("filter", ".data")
})

#TODO: how to test facet_wrap() content? facet_fail_msg never gets used...
test_ggplot(data_fail_msg = "Did you use the `filtered_6_countries` as the `data` argument to your `ggplot()` call?",
            aes_fail_msg = "Something's wrong in the `aes()` layer of your `ggplot()` call. Did you plot `year` on the x-axis and `percent_yes` on the y-axis?",
            geom_fail_msg = "Did you add a `geom_line` layer with `geom_line()` to your call to `ggplot()`?",
            facet_fail_msg = "Did you add a facet layer with `facet_wrap()`? Make sure to use the correct syntax: `~ country`.")
test_function("facet_wrap")       
test_error()
success_msg("Great work!")
machow commented 7 years ago

It seems like this might be a bug, but it will be a bit before I can dismantle the ggplot testing code.

In the meantime, you can work around it by using something like...

test_function("facet_wrap", args = "facets")

It should work with the default eval=TRUE argument, since If you run ~ country in the R terminal if produces a formula. Note that a downside here is that this would pass this submission...

# Vector of six countries to examine
countries <- c("United States", "United Kingdom",
               "France", "Japan", "Brazil", "India")

# Filtered by_year_country: filtered_6_countries
filtered_6_countries <- by_year_country %>%
  filter(country %in% countries)

# Line plot of % yes over time faceted by country
ggplot(filtered_6_countries, aes(year, percent_yes)) +
  geom_line()

# Not even being used in plot -------------------------
facet_wrap(~ country)
tommyjee commented 7 years ago

Hm, okay. In the meantime, I'll also add test_student_typed("+", times = 2) along with the test_function("facet_wrap", args = "facets"). Thanks!

cc @ncarchedi