Skip to content

Commit

Permalink
Convert expect_known_output() to expect_snapshot()
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Apr 12, 2021
1 parent b10e763 commit 6159618
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 195 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Expand Up @@ -18,8 +18,9 @@ Depends:
Imports:
tibble
Suggests:
testthat (>= 2.0.0)
testthat (>= 3.0.0)
Encoding: UTF-8
LazyData: true
LazyDataCompression: xz
RoxygenNote: 7.1.1
Config/testthat/edition: 3
174 changes: 174 additions & 0 deletions tests/testthat/_snaps/data.md
@@ -0,0 +1,174 @@
# applicants head and tail

Code
first_last(applicants)
Output
# A tibble: 20 x 3
year sex n_all
<int> <chr> <int>
1 1880 F 97605
2 1880 M 118400
3 1881 F 98855
4 1881 M 108282
5 1882 F 115695
6 1882 M 122031
7 1883 F 120059
8 1883 M 112477
9 1884 F 137586
10 1884 M 122738
11 2013 F 1923856
12 2013 M 2016731
13 2014 F 1951652
14 2014 M 2044378
15 2015 F 1945317
16 2015 M 2038331
17 2016 F 1928438
18 2016 M 2017790
19 2017 F 1874899
20 2017 M 1963290

---

Code
first_last(births)
Output
# A tibble: 20 x 2
year births
<int> <int>
1 1909 2718000
2 1910 2777000
3 1911 2809000
4 1912 2840000
5 1913 2869000
6 1914 2966000
7 1915 2965000
8 1916 2964000
9 1917 2944000
10 1918 2948000
11 2008 4247694
12 2009 4130665
13 2010 3999386
14 2011 3953590
15 2012 3952841
16 2013 3932181
17 2014 3988076
18 2015 3978497
19 2016 3945875
20 2017 3855500

---

Code
first_last(lifetables)
Output
# A tibble: 20 x 9
x qx lx dx Lx Tx ex sex year
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <dbl>
1 0 0.146 100000 14596 90026 5151511 51.5 M 1900
2 1 0.0328 85404 2803 84003 5061484 59.3 M 1900
3 2 0.0163 82601 1350 81926 4977482 60.3 M 1900
4 3 0.0105 81251 855 80824 4895556 60.2 M 1900
5 4 0.00875 80397 703 80045 4814732 59.9 M 1900
6 5 0.00628 79693 501 79443 4734687 59.4 M 1900
7 6 0.00462 79193 366 79010 4655244 58.8 M 1900
8 7 0.00326 78827 257 78698 4576234 58.0 M 1900
9 8 0.00256 78569 201 78469 4497536 57.2 M 1900
10 9 0.00203 78368 159 78288 4419068 56.4 M 1900
11 110 0.342 487 167 404 1065 2.18 F 2010
12 111 0.361 321 116 263 661 2.06 F 2010
13 112 0.381 205 78 166 398 1.94 F 2010
14 113 0.402 127 51 101 232 1.83 F 2010
15 114 0.424 76 32 60 130 1.72 F 2010
16 115 0.448 44 20 34 71 1.62 F 2010
17 116 0.473 24 11 18 37 1.52 F 2010
18 117 0.495 13 6 10 18 1.44 F 2010
19 118 0.518 6 3 5 9 1.36 F 2010
20 119 0.542 3 2 2 4 1.28 F 2010

---

Code
first_last(lifetables[lifetables$year == 1990, ])
Output
# A tibble: 20 x 9
x qx lx dx Lx Tx ex sex year
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <dbl>
1 0 0.0103 100000 1028 99108 7890323 78.9 M 1990
2 1 0.00075 98972 75 98934 7791215 78.7 M 1990
3 2 0.00049 98897 49 98873 7692281 77.8 M 1990
4 3 0.00039 98848 38 98829 7593408 76.8 M 1990
5 4 0.00031 98810 30 98795 7494579 75.8 M 1990
6 5 0.000260 98780 25 98767 7395784 74.9 M 1990
7 6 0.00024 98755 23 98743 7297016 73.9 M 1990
8 7 0.0002 98731 20 98721 7198273 72.9 M 1990
9 8 0.00018 98711 18 98702 7099552 71.9 M 1990
10 9 0.000150 98693 15 98686 7000849 70.9 M 1990
11 110 0.374 265 99 215 525 1.98 F 1990
12 111 0.394 166 65 133 310 1.87 F 1990
13 112 0.416 101 42 80 177 1.76 F 1990
14 113 0.439 59 26 46 97 1.65 F 1990
15 114 0.463 33 15 25 51 1.55 F 1990
16 115 0.489 18 9 13 26 1.46 F 1990
17 116 0.516 9 5 7 12 1.37 F 1990
18 117 0.540 4 2 3 6 1.29 F 1990
19 118 0.564 2 1 1 2 1.22 F 1990
20 119 0.590 1 1 1 1 1.15 F 1990

---

Code
first_last(babynames)
Output
# A tibble: 20 x 5
year sex name n prop
<dbl> <chr> <chr> <int> <dbl>
1 1880 F Mary 7065 0.0724
2 1880 F Anna 2604 0.0267
3 1880 F Emma 2003 0.0205
4 1880 F Elizabeth 1939 0.0199
5 1880 F Minnie 1746 0.0179
6 1880 F Margaret 1578 0.0162
7 1880 F Ida 1472 0.0151
8 1880 F Alice 1414 0.0145
9 1880 F Bertha 1320 0.0135
10 1880 F Sarah 1288 0.0132
11 2017 M Zubeyr 5 0.00000255
12 2017 M Zy 5 0.00000255
13 2017 M Zyel 5 0.00000255
14 2017 M Zyheem 5 0.00000255
15 2017 M Zyhier 5 0.00000255
16 2017 M Zykai 5 0.00000255
17 2017 M Zykeem 5 0.00000255
18 2017 M Zylin 5 0.00000255
19 2017 M Zylis 5 0.00000255
20 2017 M Zyrie 5 0.00000255

---

Code
first_last(babynames[babynames$year == 1990, ])
Output
# A tibble: 20 x 5
year sex name n prop
<dbl> <chr> <chr> <int> <dbl>
1 1990 F Jessica 46475 0.0226
2 1990 F Ashley 45558 0.0222
3 1990 F Brittany 36538 0.0178
4 1990 F Amanda 34408 0.0168
5 1990 F Samantha 25866 0.0126
6 1990 F Sarah 25816 0.0126
7 1990 F Stephanie 24861 0.0121
8 1990 F Jennifer 22226 0.0108
9 1990 F Elizabeth 20744 0.0101
10 1990 F Lauren 20500 0.00998
11 1990 M Zan 5 0.00000232
12 1990 M Zander 5 0.00000232
13 1990 M Zedekiah 5 0.00000232
14 1990 M Zephan 5 0.00000232
15 1990 M Zerick 5 0.00000232
16 1990 M Zeus 5 0.00000232
17 1990 M Ziyad 5 0.00000232
18 1990 M Zoilo 5 0.00000232
19 1990 M Zoran 5 0.00000232
20 1990 M Zvi 5 0.00000232

73 changes: 17 additions & 56 deletions tests/testthat/test-data.R
@@ -1,66 +1,27 @@
context("data regression tests")
options(tibble.print_min = 20)
# It is expected that all data has been stored sorted in the same way as the source data.
# For babynames: year, sex, and desc(n)
# For applicants: year, sex
# For lifetables: year, sex, x; note that sex is a factor, M is 1, F is 2
# For births: year

# convenience function for getting first and last 10 rows
first_last <- function(x) {
n <- nrow(x)
if(n >= 20) {
x[c(1:10, (n - 9):n),]
} else {
return(x)
}
}


test_that("applicants head and tail", {
expect_known_output(
first_last(applicants),
"test-data_applicants_first_last.txt",
print = TRUE
)
})

test_that("births head and tail", {
expect_known_output(
first_last(births),
"test-data_births_first_last.txt",
print = TRUE
)
})

test_that("lifetables head and tail", {
expect_known_output(
first_last(lifetables),
"test-data_lifetables_first_last.txt",
print = TRUE
)
})

test_that("lifetables head and tail of 1990s; should not typically change", {
expect_known_output(
first_last(lifetables[lifetables$year == 1990,]),
"test-data_lifetables_first_last_1990s.txt",
print = TRUE
)
})
old <- options(tibble.print_min = 20)
on.exit(options(old))

first_last <- function(x) {
n <- nrow(x)
if(n >= 20) {
x[c(1:10, (n - 9):n),]
} else {
return(x)
}
}

test_that("babynames head and tail", {
expect_known_output(
first_last(babynames),
"test-data_babynames_first_last.txt",
print = TRUE
)
})
expect_snapshot(first_last(applicants))
expect_snapshot(first_last(births))
expect_snapshot(first_last(lifetables))
expect_snapshot(first_last(lifetables[lifetables$year == 1990,]))

test_that("babynames head and tail of 1990; should not typically change", {
expect_known_output(
first_last(babynames[babynames$year == 1990,]),
"test-data_babynames_first_last_1990.txt",
print = TRUE
)
expect_snapshot(first_last(babynames))
expect_snapshot(first_last(babynames[babynames$year == 1990,]))
})
23 changes: 0 additions & 23 deletions tests/testthat/test-data_applicants_first_last.txt

This file was deleted.

23 changes: 0 additions & 23 deletions tests/testthat/test-data_babynames_first_last.txt

This file was deleted.

23 changes: 0 additions & 23 deletions tests/testthat/test-data_babynames_first_last_1990.txt

This file was deleted.

23 changes: 0 additions & 23 deletions tests/testthat/test-data_births_first_last.txt

This file was deleted.

23 changes: 0 additions & 23 deletions tests/testthat/test-data_lifetables_first_last.txt

This file was deleted.

0 comments on commit 6159618

Please sign in to comment.