Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Feb 22, 2024
1 parent 1b4b511 commit fe51592
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/testthat/test-add_data_type.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## Data for tests ----

df_w <- data.frame("data_type" = rep("Net", 3),
"column" = 1:3)

df_wo <- data.frame("column" = 1:3)


## add_data_type() ----

test_that("Test add_data_type() for success", {

df <- add_data_type(df_w, "Net")

expect_true(is.data.frame(df))
expect_true(class(df[ , 1]) == "character")

expect_equal(ncol(df), 2L)
expect_equal(nrow(df), 3L)

expect_equal(unique(df[ , 1]), "Net")

df <- add_data_type(df_w, "Pump")

expect_true(is.data.frame(df))
expect_true(class(df[ , 1]) == "character")

expect_equal(ncol(df), 2L)
expect_equal(nrow(df), 3L)

expect_equal(unique(df[ , 1]), "Pump")

df <- add_data_type(df_wo, "Pump")

expect_true(is.data.frame(df))
expect_true(class(df[ , 1]) == "character")

expect_equal(ncol(df), 2L)
expect_equal(nrow(df), 3L)

expect_equal(unique(df[ , 1]), "Pump")
})
26 changes: 26 additions & 0 deletions tests/testthat/test-get_data_type.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Data for tests ----

df_g <- data.frame("data_type" = rep("Net", 3),
"column" = 1:3)

df_w <- data.frame("data_type" = c("Net", "Net", "Pump"),
"column" = 1:3)


## get_data_type() ----

test_that("Test get_data_type() for error", {

expect_error(get_data_type(df_w),
"The column 'data_type' cannot contain different values",
fixed = TRUE)
})

test_that("Test get_data_type() for success", {

dt <- get_data_type(df_g)

expect_true(is.character(dt))
expect_equal(length(dt), 1L)
expect_equal(dt, "Net")
})
18 changes: 18 additions & 0 deletions tests/testthat/test-get_ocean_names.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## get_ocean_names() ----

test_that("Test get_ocean_names() for success", {

oceans <- get_ocean_names()

expect_true(class(oceans) == "character")
expect_true(length(oceans) == 8L)

expect_equal(oceans[1], "Arctic Ocean")
expect_equal(oceans[2], "Indian Ocean")
expect_equal(oceans[3], "Mediterranean Sea")
expect_equal(oceans[4], "North Atlantic Ocean")
expect_equal(oceans[5], "North Pacific Ocean")
expect_equal(oceans[6], "South Atlantic Ocean")
expect_equal(oceans[7], "South Pacific Ocean")
expect_equal(oceans[8], "Southern Ocean")
})
35 changes: 35 additions & 0 deletions tests/testthat/test-get_species_names.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Data for tests ----

df_cpr <- data.frame("data_type" = rep("CPR North", 5),
"species" = c("n_pachyderma", "n_pachyderma",
"conglobatus", "g_rubescens",
"g_rubescens"))

df_net <- data.frame("data_type" = rep("Net", 5),
"n_pachyderma" = c(1:5),
"conglobatus" = c(1:5),
"g_rubescens" = c(1:5))


## get_species_names() ----

test_that("Test get_species_names() for success", {

species <- get_species_names(df_cpr)

expect_true(class(species) == "character")
expect_true(length(species) == 3L)

expect_equal(species[1], "n_pachyderma")
expect_equal(species[2], "conglobatus")
expect_equal(species[3], "g_rubescens")

species <- get_species_names(df_net)

expect_true(class(species) == "character")
expect_true(length(species) == 3L)

expect_equal(species[1], "n_pachyderma")
expect_equal(species[2], "conglobatus")
expect_equal(species[3], "g_rubescens")
})

0 comments on commit fe51592

Please sign in to comment.