-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-plot-input.R
119 lines (96 loc) · 2.99 KB
/
test-plot-input.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
library(testthat)
source("plot-input.R")
context("Plot input data generators")
test_that("while obtaining graph plot input data", {
word_corrs <- rbind(
data.frame(from="soros", to="terv", corr=0.99),
data.frame(from="soros", to="ceu", corr=0.78),
data.frame(from="soros", to="civil", corr=0.52),
data.frame(from="orbán", to="viktor", corr=0.98),
data.frame(from="orbán", to="fidesz", corr=0.6),
data.frame(from="orbán", to="békemenet", corr=0.3),
data.frame(from="gyurcsány", to="dk", corr=0.66)
)
test_that("comma-separated user input terms should be preprocessed", {
allowed_terms <- c("soros","orbán","gyurcsány","migráns")
expect_equal(
prepare_input_terms("", allowed_terms),
character(0),
)
expect_equal(
prepare_input_terms("soros", allowed_terms),
c("soros")
)
expect_equal(
prepare_input_terms("Orbán, Gyurcsány, migráns", allowed_terms),
c("orbán", "gyurcsány", "migráns")
)
expect_equal(
prepare_input_terms("idontexist, I dont either", allowed_terms),
character(0)
)
})
test_that("correct output is obtained with terms we have correlation measures for", {
expected_data = rbind(
data.frame(term1="soros", term2="terv", correlation=0.99),
data.frame(term1="soros", term2="ceu", correlation=0.78),
data.frame(term1="orbán", term2="viktor", correlation=0.98)
)
input_data = generate_graph_plot_input(word_corrs, c("soros", "orbán"), 0.78)
expect_equal(
nrow(input_data),
3
)
expect_equal(
colnames(input_data),
c("term1", "term2", "correlation")
)
for(i in 1:3){
expect_equal(
as.vector(input_data[,i]),
as.vector(expected_data[,i])
)
}
})
test_that("placeholder output is obtained from terms that do not have associated terms above the required threshold", {
expected_data = rbind(
data.frame(term1="gyurcsány", term2="gyurcsány", correlation=1),
data.frame(term1="orbán", term2="orbán", correlation=1)
)
input_data = generate_graph_plot_input(word_corrs, c("gyurcsány", "orbán"), 1)
expect_equal(
nrow(input_data),
2
)
expect_equal(
colnames(input_data),
c("term1", "term2", "correlation")
)
for(i in 1:3){
expect_equal(
as.vector(input_data[,i]),
as.vector(expected_data[,i])
)
}
})
test_that("empty output is obtained from empty user input terms", {
expected_data = rbind(
data.frame(term1="", term2="", correlation=1)
)
input_data = generate_graph_plot_input(word_corrs, character(0), 0.78)
expect_equal(
nrow(input_data),
1
)
expect_equal(
colnames(input_data),
c("term1", "term2", "correlation")
)
for(i in 1:3){
expect_equal(
as.vector(input_data[,i]),
as.vector(expected_data[,i])
)
}
})
})