-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_B_check_coordinates.R
130 lines (105 loc) · 4.51 KB
/
test_B_check_coordinates.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
library(sapfluxnetQC1)
context('B1. Coordinates argument checks')
test_that('argument errors are correct', {
foo_data <- data.frame(
si_long = rnorm(5),
si_lat = rnorm(5),
si_country = c('CRI', 'ITA', 'NZL', 'CHE', 'BRA'),
si_name = c('bla', 'ble', 'bli', 'blo', 'blu')
)
expect_error(qc_check_coordinates(foo_data, 'foo_folder'),
'Maps folder does not exist')
expect_error(qc_check_coordinates(c(1,2,3)),
'Provided data object is not a data.frame')
expect_error(qc_check_coordinates(foo_data[,2:4]), 'There is no longitude variable')
expect_error(qc_check_coordinates(foo_data[,c(1, 3:4)]), 'There is no latitude variable')
expect_error(qc_check_coordinates(foo_data[,c(1:2, 4)]), 'There is no country variable')
expect_error(qc_check_coordinates(foo_data[,c(1:3)]), 'There is no site_name variable')
})
context('B2. Coordinates check')
test_that('checking is correct', {
foo_data <- data.frame(
si_long = c(-84, rnorm(4, 0, 0.1)),
si_lat = c(10, rnorm(4, 0, 0.1)),
si_country = c('CRI', 'ITA', 'NZL', 'CHE', 'BRA'),
si_name = c('bla', 'ble', 'bli', 'blo', 'blu')
)
qc_download_maps(foo_data)
expect_message(qc_check_coordinates(foo_data[2:5,]), '4 wrong coordinates in data')
expect_message(qc_check_coordinates(foo_data[1,]), '1 correct coordinates in data')
})
context('B3. Plotting wrong coordinates and report presence')
test_that('wrong coordinates are plotted correctly', {
foo_data <- data.frame(
si_long = c(-84, rnorm(4, 0, 0.1)),
si_lat = c(10, rnorm(4, 0, 0.1)),
si_country = c('CRI', 'ITA', 'NZL', 'CHE', 'BRA'),
si_name = c('bla', 'ble', 'bli', 'blo', 'blu')
)
foo_report <- qc_check_coordinates(foo_data[1:2,], plot = TRUE)
expect_false(file_test("-f", 'CRI_bla.pdf'))
expect_true(file_test("-f", 'ITA_ble.pdf'))
})
test_that('report is present and is a data.frame', {
foo_data <- data.frame(
si_long = c(-84, rnorm(4, 0, 0.1)),
si_lat = c(10, rnorm(4, 0, 0.1)),
si_country = c('CRI', 'ITA', 'NZL', 'CHE', 'BRA'),
si_name = c('bla', 'ble', 'bli', 'blo', 'blu')
)
foo_report <- qc_check_coordinates(foo_data[1,])
expect_is(foo_report, 'data.frame')
})
context('B4. Get biomes object')
test_that('argument checks work', {
expect_error(qc_get_biomes_spdf(merge_deserts = 25),
'merge_deserts must be logical')
expect_error(qc_get_biomes_spdf(merge_deserts = NA),
'merge_deserts must be either TRUE or FALSE')
})
test_that('biomes object is created', {
expect_is(qc_get_biomes_spdf(), 'SpatialPolygonsDataFrame')
expect_is(qc_get_biomes_spdf(merge_deserts = TRUE), 'SpatialPolygonsDataFrame')
})
context('B5. Get biome of a site')
foo_data <- data.frame(
si_code = c('A', 'B', 'C', 'D'),
si_lat = c(-36.785, 69.491822025, 41.4309888889, 34.3863888888889),
si_long = c(146.582, 27.2310822944, 2.07361111111111, -106.529444444444)
)
results_foo_data <- cbind(foo_data, data.frame(
si_mat = c(12.50, -0.98, 14.16, 12.38),
si_map = c(955.5, 407.4, 708.2, 264.5),
si_biome = c('Mediterranean', 'Boreal forest', 'Mediterranean', 'Temperate grassland desert')
))
results_foo_data_md <- cbind(foo_data, data.frame(
si_mat = c(12.50, -0.98, 14.16, 12.38),
si_map = c(955.5, 407.4, 708.2, 264.5),
si_biome = c('Mediterranean', 'Boreal forest', 'Mediterranean', 'Desert')
))
test_that('argument checks work', {
expect_error(qc_get_biome('foo'),
'Provided data object is not a data frame.')
expect_error(qc_get_biome(subset(foo_data, select=-si_long)),
'There is no longitude variable in this dataset.')
expect_error(qc_get_biome(subset(foo_data, select=-si_lat)),
'There is no latitude variable in this dataset.')
expect_error(qc_get_biome(foo_data,merge_deserts = 25),
'merge_deserts must be logical')
expect_error(qc_get_biome(foo_data,merge_deserts = NA),
'merge_deserts must be either TRUE or FALSE')
})
test_that('the new variables are added and their type is correct', {
expect_equal(
c(names(foo_data), 'si_mat', 'si_map', 'si_biome'),
names(qc_get_biome(foo_data))
)
expect_is(qc_get_biome(foo_data)$si_map, 'numeric')
expect_is(qc_get_biome(foo_data)$si_mat, 'numeric')
expect_is(qc_get_biome(foo_data)$si_biome, 'factor')
})
test_that('new data is correct', {
expect_equal(qc_get_biome(foo_data), results_foo_data, tolerance = .0001)
expect_equal(qc_get_biome(foo_data, merge_deserts = TRUE), results_foo_data_md, tolerance = .0001)
expect_is(qc_get_biome(foo_data), 'data.frame')
})