Skip to content

Commit

Permalink
updating testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ifoxfoot committed Sep 25, 2023
1 parent a784b9b commit 3c685d1
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ duplicate <- data.frame(acronym = c("ABIBAL", "ABIBAL", "AMMBRE", "ABEESC"),
typo <- data.frame(acronym = c("ABEESC", "ABIBAL", "AMMBRE", "typo"),
name = c("Abelmoschus esculentus", "Abies balsamea", "Ammophila breviligulata", "typo"))

#data frame with incorrect entries in both rows
#data frame with bad capitalization
fuzzy <- data.frame(acronym = c("abeesc", "ABIBAL", "AMMBRE", "ANTELE"),
name = c("Abelmoschus esculentus", "Abies balsamea", "Ammophila breviligulata", "Anticlea elegans"))

Expand Down
46 changes: 46 additions & 0 deletions tests/testthat/test-cover_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,52 @@ test_that("transect_summary() works in perfect setting", {
)))
})

test_that("transect_summary() works in with synonyms (diff names, same ID)", {
expect_equal(transect_summary(x = same_id, key = "name", db = "wyoming_2017"),

data.frame(metrics = c("Total Species Richness",
"Native Species Richness",
"Introduced Species Richness",
"% of Species with no C Value",
"% of Species with 0 C Value",
"% of Species with 1-3 C Value",
"% of Species with 4-6 C Value",
"% of Species with 7-10 C Value",
"Mean C",
"Native Mean C",
"Cover-Weighted Mean C",
"Cover-Weighted Native Mean C",
"Total FQI",
"Native FQI",
"Cover-Weighted FQI",
"Cover-Weighted Native FQI",
"Adjusted FQI",
"Mean Wetness",
"Native Mean Wetness",
"% Hydrophytes"),
values = c(species_richness(same_id, "name", "wyoming_2017"),
species_richness(same_id, "name", "wyoming_2017", native = TRUE),
0, #introduced
0, #no c score
0, #0 score
0, #1-3 score
100, #4-6 score
0, #7-10 score
mean_c(same_id, "name", "wyoming_2017"),
mean_c(same_id, "name", "wyoming_2017", native = TRUE),
cover_mean_c(same_id, "name", "wyoming_2017", allow_duplicates = T),
cover_mean_c(same_id, "name", "wyoming_2017", native = TRUE, allow_duplicates =T),
FQI(same_id, "name", "wyoming_2017"),
FQI(same_id, "name", "wyoming_2017", native = TRUE),
cover_FQI(same_id, "name", "wyoming_2017", allow_duplicates = T),
cover_FQI(same_id, "name", "wyoming_2017", allow_duplicates = T, native = TRUE),
adjusted_FQI(same_id, "name", "wyoming_2017"),
mean_w(same_id, "name", "wyoming_2017"),
mean_w(same_id, "name", "wyoming_2017", native = TRUE),
0 #hydro
)))
})

#-------------------------------------------------------------------------------
#testing plot_summary()

Expand Down
94 changes: 93 additions & 1 deletion tests/testthat/test-fqi_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ test_that("adjusted_FQI() kind of fuzzy matches", {
#-------------------------------------------------------------------------------
#testing all_metrics()

test_that("all_metrics() calculates total species richness", {
test_that("all_metrics() calculates metrics correctly", {
expect_equal(all_metrics(x = crooked_island, key = "acronym", db = "michigan_2014"),

data.frame(metrics = c("Total Species Richness",
Expand Down Expand Up @@ -217,3 +217,95 @@ test_that("all_metrics() calculates total species richness", {
37.1428571
)))
})


#testing errors
test_that("all_metrics() calculates errors correctly", {
expect_error(all_metrics(), "x is missing")
expect_error(all_metrics(character_string, db = "michigan_2014"), "must be a data frame.")
expect_error(all_metrics(numbers, db = "michigan_2014"), "must be a data frame.")
expect_error(all_metrics(bad_names, db = "michigan_2014"), "does not have a column named")
expect_error(all_metrics(crooked_island, key = "bad_key", db = "michigan_2014"), "'key' argument must be equal to")
expect_error(all_metrics(bad_names, key = "acronym", db = "michigan_2014"), "does not have a column named")
})


#test duplicate behavior
test_that("all_metrics() does not count duplicates", {
expect_message(all_metrics(duplicate, db = "michigan_2014"), "Duplicate entries detected")
expect_equal(all_metrics(duplicate, db = "michigan_2014"),
data.frame(metrics = c("Total Species Richness",
"Native Species Richness",
"Introduced Species Richness",
"% of Species with no C Value",
"% of Species with 0 C Value",
"% of Species with 1-3 C Value",
"% of Species with 4-6 C Value",
"% of Species with 7-10 C Value",
"Mean C",
"Native Mean C",
"Total FQI",
"Native FQI",
"Adjusted FQI",
"Mean Wetness",
"Native Mean Wetness",
"% Hydrophytes"),
values = c(fqacalc::species_richness(duplicate, db = "michigan_2014"),
fqacalc::species_richness(duplicate, db = "michigan_2014", native = TRUE),
1, #introduced
0, #no c score
33.333333, #0 c score
33.333333, #1-3 c sscore
0, #4-6 c score
33.333333, #7-10 c score
fqacalc::mean_c(duplicate, db = "michigan_2014"),
fqacalc::mean_c(duplicate, db = "michigan_2014", native = TRUE),
fqacalc::FQI(duplicate, db = "michigan_2014"),
fqacalc::FQI(duplicate, db = "michigan_2014", native = TRUE),
fqacalc::adjusted_FQI(duplicate, db = "michigan_2014"),
fqacalc::mean_w(duplicate, db = "michigan_2014"),
fqacalc::mean_w(duplicate, db = "michigan_2014", native = TRUE),
0 #%hydro
)))
})


#test synonym behavior
test_that("all_metrics() works with synonyms", {
expect_message(all_metrics(same_syn_sci, db = "wyoming_2017"),
"CAREX FOENEA is an accepted scientific name and a synonym.")
expect_equal(all_metrics(same_syn_sci, db = "wyoming_2017"),
data.frame(metrics = c("Total Species Richness",
"Native Species Richness",
"Introduced Species Richness",
"% of Species with no C Value",
"% of Species with 0 C Value",
"% of Species with 1-3 C Value",
"% of Species with 4-6 C Value",
"% of Species with 7-10 C Value",
"Mean C",
"Native Mean C",
"Total FQI",
"Native FQI",
"Adjusted FQI",
"Mean Wetness",
"Native Mean Wetness",
"% Hydrophytes"),
values = c(fqacalc::species_richness(same_syn_sci, db = "wyoming_2017"),
fqacalc::species_richness(same_syn_sci, db = "wyoming_2017", native = TRUE),
0, #introduced
0, #no c score
0, #0 c score
0, #1-3 c sscore
100, #4-6 c score
0, #7-10 c score
fqacalc::mean_c(same_syn_sci, db = "wyoming_2017"),
fqacalc::mean_c(same_syn_sci, db = "wyoming_2017", native = TRUE),
fqacalc::FQI(same_syn_sci, db = "wyoming_2017"),
fqacalc::FQI(same_syn_sci, db = "wyoming_2017", native = TRUE),
fqacalc::adjusted_FQI(same_syn_sci, db = "wyoming_2017"),
fqacalc::mean_w(same_syn_sci, db = "wyoming_2017"),
fqacalc::mean_w(same_syn_sci, db = "wyoming_2017", native = TRUE),
0 #%hydro
)))
})

0 comments on commit 3c685d1

Please sign in to comment.