From 739f85125c45397eb2120f1b6cbcc925eb93c210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BB=C3=B3=C5=82tak?= Date: Mon, 29 Apr 2019 12:11:36 +0200 Subject: [PATCH] Better tests coverage --- R/S2_generate_RGB.R | 4 +- tests/testthat/test-S2_generate_RGB.R | 27 ++++++++++-- tests/testthat/test-S2_put_ROI.R | 17 ++++++++ tests/testthat/test-S2_query.R | 60 +++++++++++++++++++++++++- tests/testthat/test-helper_functions.R | 30 +++++++++++++ 5 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/test-helper_functions.R diff --git a/R/S2_generate_RGB.R b/R/S2_generate_RGB.R index 43c6856..316d29c 100644 --- a/R/S2_generate_RGB.R +++ b/R/S2_generate_RGB.R @@ -54,10 +54,10 @@ S2_generate_RGB = function( query = S2_query_image(granuleId = granuleId, atmCorr = atmCorr) - if (length(query) == 0 && !isTRUE(atmCorr)) { + if (nrow(query) == 0 && !isTRUE(atmCorr)) { warning("Unable to process 'granuleId ", granuleId, "'. Not found in database!") return(invisible(NULL)) - } else if (length(query) == 0 && isTRUE(atmCorr)) { + } else if (nrow(query) == 0 && isTRUE(atmCorr)) { warning( "Unable to process 'granuleId ", granuleId, "'. Maybe its not (yet) ", "atmospherically corrected!\n" diff --git a/tests/testthat/test-S2_generate_RGB.R b/tests/testthat/test-S2_generate_RGB.R index 719f103..85c93c6 100644 --- a/tests/testthat/test-S2_generate_RGB.R +++ b/tests/testthat/test-S2_generate_RGB.R @@ -4,9 +4,30 @@ test_that('S2_generate_RGB() works', { imgs = S2_query_image(owned = TRUE, atmCorr = TRUE, resolution = 60, cloudCovMax = 10) %>% dplyr::filter(band %in% c('B02', 'B03', 'B04')) %>% dplyr::arrange(date, band) - file = tempfile() - S2_generate_RGB(imgs$granuleId[1], atmCorr = TRUE, resolution = 'lowest', destfile = file, overwrite = TRUE) + file = S2_generate_RGB(imgs$granuleId[1], atmCorr = TRUE, resolution = 'lowest', overwrite = TRUE) + on.exit({ + if (file.exists(file)) { + unlink(file) + } + }) expect_true(file.exists(file)) expect_gt(file.size(file), 1000000) - unlink(file) +}) + +test_that('S2_generate_RGB() exceptions', { + imgs = S2_query_image(owned = TRUE, atmCorr = TRUE, resolution = 60, cloudCovMax = 10) %>% + dplyr::filter(band %in% c('B02')) %>% + dplyr::arrange(date, band) + expect_error( + S2_generate_RGB(imgs$granuleId, atmCorr = TRUE, resolution = 'lowest', destfile = file, overwrite = TRUE), + 'granuleId has to be a vector of length one' + ) + + expect_null(suppressWarnings(S2_generate_RGB(-1, atmCorr = TRUE, resolution = 'lowest', destfile = file, overwrite = TRUE))) + expect_null(suppressWarnings(S2_generate_RGB(-1, atmCorr = FALSE, resolution = 'lowest', destfile = file, overwrite = TRUE))) + + expect_error( + S2_generate_RGB(imgs, atmCorr = TRUE, resolution = 'lowest', destfile = file, overwrite = TRUE), + 'granuleId has to be a vector of length one' + ) }) diff --git a/tests/testthat/test-S2_put_ROI.R b/tests/testthat/test-S2_put_ROI.R index d974cb6..f580e38 100644 --- a/tests/testthat/test-S2_put_ROI.R +++ b/tests/testthat/test-S2_put_ROI.R @@ -13,3 +13,20 @@ test_that('S2_put_roi() works', { expect_equal(nrow(d), 1) expect_equal(d$dateMax, '1900-02-02') }) + +test_that('S2_put_roi() exceptions', { + expect_error( + S2_put_roi('{"type": "Polygon", "coordinates": [[[16.5, 48.0], [16.6, 48.1], [16.6, 48.0], [16.5, 48.0]]]}', '__test__', 0, 'LAI', '1900-01-01'), + "Please supply 'dateMin' and 'dateMax'" + ) + + expect_error( + S2_put_roi('{"type": "Polygon", "coordinates": [[[16.5, 48.0], [16.6, 48.1], [16.6, 48.0], [16.5, 48.0]]]}', '__test__', 0, 'LAI', '2000-01-01', '1900-01-01'), + "'dateMin' .* larger than 'dateMax'" + ) + + expect_error( + S2_put_roi('{"type": "Polygon", "coordinates": [[[16.5, 48.0], [16.6, 48.1], [16.6, 48.0], [16.5, 48.0]]]}', cloudCovMax = 0, indicators = 'LAI', dateMin = '2000-01-01', dateMax = '3000-01-01'), + "'regionId' not specified" + ) +}) diff --git a/tests/testthat/test-S2_query.R b/tests/testthat/test-S2_query.R index b46cbfa..95ace12 100644 --- a/tests/testthat/test-S2_query.R +++ b/tests/testthat/test-S2_query.R @@ -17,6 +17,22 @@ test_that('S2_query_angle() works', { expect_true(all(data$utm == '33UXP')) expect_true(all(data$angleType == 'sun')) expect_true(all(data$band == 'all')) + + data = S2_query_angle( + cloudCovMin = 80, + dateSingle = '2016-01-07', + utm = '33UXP', + angleType = 'sun' + ) + expect_is(data, 'data.frame') + expect_gt(nrow(data), 0) + cols = c("angleId", "productId", "granuleId", "product", "granule", "date", "processDate", "utm", "orbit", "angleType", "band", "zenithAngle", "azimuthAngle", "url") + expect_equal(intersect(names(data), cols), cols) + expect_true(all(data$date >= '2016-01-07 00:00:00.000')) + expect_true(all(data$date <= '2016-01-07 23:59:59.999 ')) + expect_true(all(data$utm == '33UXP')) + expect_true(all(data$angleType == 'sun')) + expect_true(all(data$band == 'all')) }) test_that('S2_query_granule() works', { @@ -34,6 +50,20 @@ test_that('S2_query_granule() works', { expect_true(all(data$date >= '2016-06-01')) expect_true(all(data$date <= '2016-08-31')) expect_true(all(data$utm == '33UXP')) + + data = S2_query_granule( + cloudCovMin = 80, + dateSingle = '2016-06-15', + utm = '33UXP' + ) + expect_is(data, 'data.frame') + expect_gt(nrow(data), 0) + cols = c("granuleId", "productId", "product", "granule", "date", "processDate", "utm", "orbit", "cloudCov", "atmCorr", "broken", "url") + expect_equal(intersect(names(data), cols), cols) + expect_true(all(data$cloudCov >= 80)) + expect_true(all(data$date >= '2016-06-15')) + expect_true(all(data$date <= '2016-06-15 23:59:59')) + expect_true(all(data$utm == '33UXP')) }) test_that('S2_query_image() works', { @@ -97,11 +127,25 @@ test_that('S2_query_qiData() works', { expect_gt(nrow(data), 0) cols = c("qiDataId", "productId", "granuleId", "product", "granule", "date", "processDate", "utm", "orbit", "qiType", "band", "url") expect_equal(intersect(names(data), cols), cols) - expect_true(all(data$cloudCov >= 80)) expect_true(all(data$date >= '2016-06-01 00:00:00.000')) expect_true(all(data$date <= '2016-06-15 23:59:59.999')) expect_true(all(data$utm == '33UXP')) expect_true(all(data$qiType == 'satura')) + + data = S2_query_qiData( + cloudCovMin = 80, + dateSingle = '2016-06-15', + utm = '33UXP', + qiType = 'satura' + ) + expect_is(data, 'data.frame') + expect_gt(nrow(data), 0) + cols = c("qiDataId", "productId", "granuleId", "product", "granule", "date", "processDate", "utm", "orbit", "qiType", "band", "url") + expect_equal(intersect(names(data), cols), cols) + expect_true(all(data$date >= '2016-06-15 00:00:00.000')) + expect_true(all(data$date <= '2016-06-15 23:59:59.999')) + expect_true(all(data$utm == '33UXP')) + expect_true(all(data$qiType == 'satura')) }) test_that('S2_query_roi() works', { @@ -233,3 +277,17 @@ test_that('data frame is always returned', { expect_is(data, 'data.frame') expect_true('regionId' %in% names(data)) }) + +test_that('S2_query_granule() throws errors', { + expect_error( + S2_query_granule(dateMin = '2016-06-15',dateMax = '2016-06-14', utm = '33UXP'), + "'dateMin' .* larger than 'dateMax'" + ) +}) + +test_that('S2_query_qiData() throws errors', { + expect_error( + S2_query_qiData(dateMin = '2016-06-15',dateMax = '2016-06-14', utm = '33UXP'), + "'dateMin' .* larger than 'dateMax'" + ) +}) diff --git a/tests/testthat/test-helper_functions.R b/tests/testthat/test-helper_functions.R new file mode 100644 index 0000000..8a4198d --- /dev/null +++ b/tests/testthat/test-helper_functions.R @@ -0,0 +1,30 @@ +context('put roi') + +test_that('S2_generate_names() works', { + data = S2_query_image(imageId = 29392766, granuleId = 1380347) + nms = S2_generate_names(data, 'someDir') + expect_true(grepl('^someDir/IMAGE_2016-08-21_33UXP_79_B10_60m_Id29392766_L1C.jp2$', nms)) + + data = S2_query_granule(granuleId = 1380347) + nms = S2_generate_names(data, 'someDir') + expect_true(grepl('^someDir/GRANULE_2016-08-21_33UXP_79_Id1380347_L2A$', nms)) +}) + +test_that('S2_generate_names() works', { + data = S2_query_granule(granuleId = 1380347) + nms = S2_granule_naming(data) + expect_true(grepl('^GRANULE_2016-08-21_33UXP_79_Id1380347_L2A$', nms)) +}) + +test_that('S2_generate_names() works', { + data = S2_query_granule(granuleId = 1380347) + nms = S2_naming(data) + expect_true(grepl('^GRANULE_2016-08-21_33UXP_79_Id1380347_L2A$', nms)) +}) + +test_that('check_date() works', { + expect_error( + check_date('2019/01/01'), + 'Invalid date' + ) +})