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

Use testthat 3e #455

Merged
merged 22 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Suggests:
fansi,
keyring,
knitr,
mockr,
nycflights13,
odbc,
RMariaDB (>= 1.0.10),
Expand All @@ -81,4 +82,5 @@ Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE, roclets = c("collate",
"namespace", "rd"))
RoxygenNote: 7.1.1.9000
RoxygenNote: 7.1.1.9001
Config/testthat/edition: 3
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ qtest: qtest-sqlite qtest-postgres qtest-mssql
test: test-sqlite test-postgres test-mssql

qtest-%:
DM_TEST_SRC=$@ time R -q -e 'options("testthat.summary.omit_skips" = TRUE, "crayon.enabled" = TRUE); devtools::test(filter = "${DM_TEST_FILTER}", reporter = c("summary", "fail"))'
TESTTHAT_PARALLEL=FALSE DM_TEST_SRC=$@ time R -q -e 'options("crayon.enabled" = TRUE); testthat::test_local(filter = "${DM_TEST_FILTER}")'

test-%:
DM_TEST_SRC=$@ time R -q -e 'devtools::test(filter = "${DM_TEST_FILTER}", reporter = c("progress", "summary", "fail"))'
DM_TEST_SRC=$@ time R -q -e 'testthat::test_local(filter = "${DM_TEST_FILTER}")'
2 changes: 1 addition & 1 deletion R/db-interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#' as the original table names.
#'
#' Use a variant of
#' `table_names = ~ DBI::SQL(dbplyr::in_schema("schema_name", .x))`
#' `table_names = ~ DBI::SQL(paste0("schema_name", ".", .x))`
#' to specify the same schema for all tables.
#' Use `table_names = identity` with `temporary = TRUE`
#' to avoid giving temporary tables unique names.
Expand Down
14 changes: 9 additions & 5 deletions R/dm.R
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,8 @@ format.dm <- function(x, ...) { # for both dm and zoomed_dm

#' @export
format.zoomed_dm <- function(x, ..., n = NULL, width = NULL, n_extra = NULL) {
df <- get_zoomed_tbl(x)
# so far only 1 table can be zoomed on
zoomed_df <- new_zoomed_df(
df,
name_df = orig_name_zoomed(x)
)
zoomed_df <- as_zoomed_df(x)
cat_line(format(zoomed_df, ..., n = n, width = width, n_extra = n_extra))
invisible(x)
}
Expand All @@ -528,6 +524,14 @@ def_get_n_fks <- function(def) {
sum(map_int(def$fks, vctrs::vec_size))
}

as_zoomed_df <- function(x) {
# for tests
new_zoomed_df(
get_zoomed_tbl(x),
name_df = orig_name_zoomed(x)
)
}

new_zoomed_df <- function(x, ...) {
if (!is.data.frame(x)) {
return(structure(x, class = c("zoomed_df", class(x)), ...))
Expand Down
2 changes: 1 addition & 1 deletion R/flatten.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ prepare_dm_for_flatten <- function(dm, tables, gotta_rename) {
# renaming will be minimized if we reduce the `dm` to the necessary tables here
red_dm <-
dm_reset_all_filters(dm) %>%
dm_select_tbl(tables)
dm_select_tbl(!!!tables)
# Only need to compute `tbl(dm, start)`, `dm_apply_filters()` not necessary
# Need to use `dm` and not `clean_dm` here, because of possible filter conditions.
start_tbl <- dm_get_filtered_table(dm, start)
Expand Down
2 changes: 1 addition & 1 deletion R/foreign-keys.R
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ check_fk <- function(t1, t1_name, colname, t2, t2_name, pk) {
ungroup() %>% # dbplyr problem?
mutate(n_mismatch = sum(if_else(is.na(mismatch_or_null), 0L, n), na.rm = TRUE)) %>%
mutate(n_total = sum(n, na.rm = TRUE)) %>%
arrange(desc(n)) %>%
filter(!is.na(mismatch_or_null)) %>%
arrange(desc(n)) %>%
head(MAX_COMMAS + 1L) %>%
collect(),
error = identity
Expand Down
2 changes: 1 addition & 1 deletion R/learn.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,6 @@ nest_compat <- function(.data, ...) {
mutate(!!new_col := !!nest)
} else {
nest(.data, ...) %>%
mutate_at(vars(new_col), vctrs::as_list_of)
mutate_at(vars(!!!new_col), vctrs::as_list_of)
}
}
3 changes: 1 addition & 2 deletions R/table-surgery.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ decompose_table <- function(.data, new_id_column, ...) {
parent_table <-
select(.data, !!!sel_vars$indices) %>%
distinct() %>%
arrange(!!!syms(names(sel_vars$indices))) %>%
# Without as.integer(), RPostgres creates integer64 column (#15)
mutate(!!id_col_q := as.integer(row_number())) %>%
mutate(!!id_col_q := as.integer(row_number(!!sym(names(sel_vars$indices)[[1]])))) %>%
select(!!id_col_q, everything())

non_key_indices <-
Expand Down
2 changes: 1 addition & 1 deletion man/copy_dm_to.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions man/utils_table_manipulation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions tests/testthat/_snaps/draw-dm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# output

Code
dm_nycflights13() %>% dm_draw() %>% DiagrammeRsvg::export_svg() %>% cli::
cat_line()
Error <simpleError>
<text>:2:0: unexpected end of line
1: dm_nycflights13() %>% dm_draw() %>% DiagrammeRsvg::export_svg() %>% cli::
^

---

Code
dm_nycflights13() %>% dm_zoom_to(planes) %>% dm_insert_zoomed("planes_copy") %>%
dm_draw() %>% DiagrammeRsvg::export_svg() %>% cli::cat_line()
Output
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.40.1 (20161225.0304)
-->
<!-- Title: %0 Pages: 1 -->
<svg width="190pt" height="236pt"
viewBox="0.00 0.00 190.00 236.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 232)">
<title>%0</title>
<g id="a_graph0"><a xlink:title="Data Model">
<polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-232 186,-232 186,4 -4,4"/>
</a>
</g>
<!-- airlines -->
<g id="node1" class="node">
<title>airlines</title>
<polygon fill="#ed7d31" stroke="transparent" points="122.5,-201 122.5,-221 167.5,-221 167.5,-201 122.5,-201"/>
<text text-anchor="start" x="124.3969" y="-206.4" font-family="Times,serif" font-size="14.00" fill="#ffffff">airlines</text>
<polygon fill="#fbe5d5" stroke="transparent" points="122.5,-181 122.5,-201 167.5,-201 167.5,-181 122.5,-181"/>
<text text-anchor="start" x="124.5" y="-187.4" font-family="Times,serif" text-decoration="underline" font-size="14.00" fill="#444444">carrier</text>
<polygon fill="none" stroke="#9e5320" stroke-opacity="0.666667" points="121,-180 121,-222 168,-222 168,-180 121,-180"/>
</g>
<!-- airports -->
<g id="node2" class="node">
<title>airports</title>
<polygon fill="#ed7d31" stroke="transparent" points="121.5,-21 121.5,-41 167.5,-41 167.5,-21 121.5,-21"/>
<text text-anchor="start" x="123.1192" y="-26.4" font-family="Times,serif" font-size="14.00" fill="#ffffff">airports</text>
<polygon fill="#fbe5d5" stroke="transparent" points="121.5,-1 121.5,-21 167.5,-21 167.5,-1 121.5,-1"/>
<text text-anchor="start" x="123.5" y="-7.4" font-family="Times,serif" text-decoration="underline" font-size="14.00" fill="#444444">faa</text>
<polygon fill="none" stroke="#9e5320" stroke-opacity="0.666667" points="120.5,0 120.5,-42 168.5,-42 168.5,0 120.5,0"/>
</g>
<!-- flights -->
<g id="node3" class="node">
<title>flights</title>
<polygon fill="#5b9bd5" stroke="transparent" points="21,-131 21,-151 67,-151 67,-131 21,-131"/>
<text text-anchor="start" x="26.1115" y="-136.4" font-family="Times,serif" font-size="14.00" fill="#ffffff">flights</text>
<polygon fill="#deebf6" stroke="transparent" points="21,-111 21,-131 67,-131 67,-111 21,-111"/>
<text text-anchor="start" x="23" y="-116.4" font-family="Times,serif" font-size="14.00" fill="#444444">carrier</text>
<polygon fill="#deebf6" stroke="transparent" points="21,-91 21,-111 67,-111 67,-91 21,-91"/>
<text text-anchor="start" x="22.6115" y="-96.4" font-family="Times,serif" font-size="14.00" fill="#444444">tailnum</text>
<polygon fill="#deebf6" stroke="transparent" points="21,-71 21,-91 67,-91 67,-71 21,-71"/>
<text text-anchor="start" x="23" y="-76.4" font-family="Times,serif" font-size="14.00" fill="#444444">origin</text>
<polygon fill="none" stroke="#3c678e" stroke-opacity="0.666667" points="20,-70 20,-152 68,-152 68,-70 20,-70"/>
</g>
<!-- flights&#45;&gt;airlines -->
<g id="edge1" class="edge">
<title>flights:carrier&#45;&gt;airlines:carrier</title>
<path fill="none" stroke="#555555" d="M67,-121C102.826,-121 86.8594,-177.9957 112.5696,-189.1277"/>
<polygon fill="#555555" stroke="#555555" points="112.0246,-192.5865 122.5,-191 113.3217,-185.7077 112.0246,-192.5865"/>
</g>
<!-- flights&#45;&gt;airports -->
<g id="edge2" class="edge">
<title>flights:origin&#45;&gt;airports:faa</title>
<path fill="none" stroke="#555555" d="M67,-81C102.5782,-81 86.2203,-24.0043 111.6548,-12.8723"/>
<polygon fill="#555555" stroke="#555555" points="112.33,-16.3067 121.5,-11 111.0221,-9.43 112.33,-16.3067"/>
</g>
<!-- planes -->
<g id="node4" class="node">
<title>planes</title>
<polygon fill="#ed7d31" stroke="transparent" points="121.5,-141 121.5,-161 167.5,-161 167.5,-141 121.5,-141"/>
<text text-anchor="start" x="126.6178" y="-146.4" font-family="Times,serif" font-size="14.00" fill="#ffffff">planes</text>
<polygon fill="#fbe5d5" stroke="transparent" points="121.5,-121 121.5,-141 167.5,-141 167.5,-121 121.5,-121"/>
<text text-anchor="start" x="123.1115" y="-127.4" font-family="Times,serif" text-decoration="underline" font-size="14.00" fill="#444444">tailnum</text>
<polygon fill="none" stroke="#9e5320" stroke-opacity="0.666667" points="120.5,-120 120.5,-162 168.5,-162 168.5,-120 120.5,-120"/>
</g>
<!-- flights&#45;&gt;planes -->
<g id="edge3" class="edge">
<title>flights:tailnum&#45;&gt;planes:tailnum</title>
<path fill="none" stroke="#555555" d="M67,-101C90.7613,-101 93.5126,-123.1558 111.3649,-129.3871"/>
<polygon fill="#555555" stroke="#555555" points="111.0742,-132.8848 121.5,-131 112.1744,-125.9718 111.0742,-132.8848"/>
</g>
<!-- planes_copy -->
<g id="node5" class="node">
<title>planes_copy</title>
<polygon fill="#efebdd" stroke="transparent" points="108.5,-81 108.5,-101 181.5,-101 181.5,-81 108.5,-81"/>
<text text-anchor="start" x="110.0105" y="-86.4" font-family="Times,serif" font-size="14.00" fill="#000000">planes_copy</text>
<polygon fill="#ffffff" stroke="transparent" points="108.5,-61 108.5,-81 181.5,-81 181.5,-61 108.5,-61"/>
<text text-anchor="start" x="110.5" y="-67.4" font-family="Times,serif" text-decoration="underline" font-size="14.00" fill="#444444">tailnum</text>
<polygon fill="none" stroke="#555555" points="107,-60 107,-102 182,-102 182,-60 107,-60"/>
</g>
<!-- flights&#45;&gt;planes_copy -->
<g id="edge4" class="edge">
<title>flights:tailnum&#45;&gt;planes_copy:tailnum</title>
<path fill="none" stroke="#555555" d="M67,-101C85.6696,-101 86.3204,-80.8126 98.5144,-73.5578"/>
<polygon fill="#555555" stroke="#555555" points="99.6813,-76.872 108.5,-71 97.9442,-70.0909 99.6813,-76.872"/>
</g>
<!-- weather -->
<g id="node6" class="node">
<title>weather</title>
<polygon fill="#70ad47" stroke="transparent" points="3,-200 3,-220 51,-220 51,-200 3,-200"/>
<text text-anchor="start" x="4.8492" y="-205.4" font-family="Times,serif" font-size="14.00" fill="#ffffff">weather</text>
<polygon fill="none" stroke="#4a732f" stroke-opacity="0.666667" points="2,-199 2,-221 52,-221 52,-199 2,-199"/>
</g>
</g>
</svg>


47 changes: 47 additions & 0 deletions tests/testthat/_snaps/examine-constraints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# output

Code
dm() %>% dm_examine_constraints()
Message <cliMessage>
i No constraints defined.
Code
dm_nycflights13() %>% dm_examine_constraints()
Message <cliMessage>
! Unsatisfied constraints:
Output
* Table `flights`: foreign key tailnum into table `planes`: 1640 entries (14.6%) of `flights$tailnum` not in `planes$tailnum`: N722MQ (27), N725MQ (20), N520MQ (19), N723MQ (19), N508MQ (16), ...
Code
dm_nycflights13(cycle = TRUE) %>% dm_examine_constraints()
Message <cliMessage>
! Unsatisfied constraints:
Output
* Table `flights`: foreign key dest into table `airports`: 242 entries (2.2%) of `flights$dest` not in `airports$faa`: SJU (187), BQN (28), STT (15), PSE (12)
* Table `flights`: foreign key tailnum into table `planes`: 1640 entries (14.6%) of `flights$tailnum` not in `planes$tailnum`: N722MQ (27), N725MQ (20), N520MQ (19), N723MQ (19), N508MQ (16), ...
Code
dm_nycflights13(cycle = TRUE) %>% dm_select_tbl(-flights) %>%
dm_examine_constraints()
Message <cliMessage>
i All constraints satisfied.
Code
# n column
Code
dm_for_filter_w_cycle() %>% dm_examine_constraints()
Message <cliMessage>
i All constraints satisfied.

# output as tibble

Code
dm_nycflights13(cycle = TRUE) %>% dm_examine_constraints() %>% as_tibble()
Output
# A tibble: 7 x 6
table kind columns ref_table is_key problem
<chr> <chr> <keys> <chr> <lgl> <chr>
1 flights FK dest airports FALSE "242 entries (2.2%) of `flights$dest` ~
2 flights FK tailnum planes FALSE "1640 entries (14.6%) of `flights$tail~
3 airlin~ PK carrier <NA> TRUE ""
4 airpor~ PK faa <NA> TRUE ""
5 planes PK tailnum <NA> TRUE ""
6 flights FK carrier airlines TRUE ""
7 flights FK origin airports TRUE ""

Loading