Skip to content

Commit

Permalink
Better tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Apr 29, 2019
1 parent 82fd5c0 commit 739f851
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 6 deletions.
4 changes: 2 additions & 2 deletions R/S2_generate_RGB.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 24 additions & 3 deletions tests/testthat/test-S2_generate_RGB.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
})
17 changes: 17 additions & 0 deletions tests/testthat/test-S2_put_ROI.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})
60 changes: 59 additions & 1 deletion tests/testthat/test-S2_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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', {
Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -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'"
)
})
30 changes: 30 additions & 0 deletions tests/testthat/test-helper_functions.R
Original file line number Diff line number Diff line change
@@ -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'
)
})

0 comments on commit 739f851

Please sign in to comment.