-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathyaml_file.test.cpp
More file actions
133 lines (116 loc) · 4.75 KB
/
Copy pathyaml_file.test.cpp
File metadata and controls
133 lines (116 loc) · 4.75 KB
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
131
132
133
#include "config/source/yaml_file.h"
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include "config/config_key_path.h"
#include "silo/common/fmt_formatters.h"
using silo::config::ConfigKeyPath;
using silo::config::YamlFile;
TEST(YamlFile, simpleStringToConfigKeyPath) {
auto under_test = YamlFile::stringToConfigKeyPath("test");
ASSERT_EQ(under_test, (ConfigKeyPath::tryFrom({{{"test"}}})));
}
TEST(YamlFile, stringToConfigKeyPathWithDot) {
auto under_test = YamlFile::stringToConfigKeyPath("api.port");
ASSERT_EQ(under_test, (ConfigKeyPath::tryFrom({{"api"}, {"port"}})));
ASSERT_NE(under_test, (ConfigKeyPath::tryFrom({{"api", "port"}})));
}
TEST(YamlFile, stringToConfigKeyPathWithCamelCase) {
auto under_test = YamlFile::stringToConfigKeyPath("query.materializationCutoff");
ASSERT_EQ(under_test, (ConfigKeyPath::tryFrom({{"query"}, {"materialization", "cutoff"}})));
}
TEST(YamlFile, errorOnPascalCase) {
EXPECT_THAT(
[]() { YamlFile::stringToConfigKeyPath("Api.Port"); },
ThrowsMessage<std::runtime_error>(::testing::HasSubstr("'Api.Port' is not a valid YamlPath"))
);
}
TEST(YamlFile, configKeyPathToString) {
ASSERT_EQ(YamlFile::configKeyPathToString(ConfigKeyPath::tryFrom({{"test"}}).value()), "test");
ASSERT_EQ(
YamlFile::configKeyPathToString(
(ConfigKeyPath::tryFrom({{"query"}, {"materialization", "cutoff"}})).value()
),
"query.materializationCutoff"
);
}
TEST(YamlFile, validRoundTrip) {
auto under_test =
std::vector<std::string>{"test", "somethingElse.that.is.quiteLong", "a.2.3.4", "asd", "aa"};
for (const auto& string : under_test) {
ASSERT_EQ(YamlFile::configKeyPathToString(YamlFile::stringToConfigKeyPath(string)), string);
}
}
TEST(YamlFile, containsCorrectFieldsFromFlatYAML) {
const auto under_test = YamlFile::fromYAML(
"inline",
R"(
inputDirectory: "./testBaseData/exampleDataset/"
outputDirectory: "./output/"
ndjsonInputFilename: "input_file.ndjson"
lineageDefinitionFilenames: "lineage_definition.yaml"
phyloTreeFilename: "phylogenetic_tree.yaml"
referenceGenomeFilename: "reference_genomes.json"
)"
)
.getYamlFields();
const std::unordered_map<ConfigKeyPath, YAML::Node> expected_result{
{YamlFile::stringToConfigKeyPath("inputDirectory"),
YAML::Node{"./testBaseData/exampleDataset/"}},
{YamlFile::stringToConfigKeyPath("outputDirectory"), YAML::Node{"./output/"}},
{YamlFile::stringToConfigKeyPath("ndjsonInputFilename"), YAML::Node{"input_file.ndjson"}},
{YamlFile::stringToConfigKeyPath("lineageDefinitionFilenames"),
YAML::Node{"lineage_definition.yaml"}},
{YamlFile::stringToConfigKeyPath("phyloTreeFilename"), YAML::Node{"phylogenetic_tree.yaml"}},
{YamlFile::stringToConfigKeyPath("referenceGenomeFilename"),
YAML::Node{"reference_genomes.json"}},
};
for (const auto& [key, value] : expected_result) {
ASSERT_TRUE(under_test.contains(key));
ASSERT_EQ(under_test.at(key).as<std::string>(), value.as<std::string>());
}
for (const auto& [key, value] : under_test) {
ASSERT_TRUE(expected_result.contains(key));
ASSERT_EQ(expected_result.at(key).as<std::string>(), value.as<std::string>());
}
}
TEST(YamlFile, containsCorrectFieldsFromNestedYAML) {
const auto under_test = YamlFile::fromYAML("inline", R"(
dataDirectory: "test/directory"
api:
port: 1234)")
.getYamlFields();
const std::unordered_map<ConfigKeyPath, YAML::Node> expected_result{
{YamlFile::stringToConfigKeyPath("dataDirectory"), YAML::Node{"test/directory"}},
{YamlFile::stringToConfigKeyPath("api.port"), YAML::Node{1234}},
};
for (const auto& [key, value] : expected_result) {
ASSERT_TRUE(under_test.contains(key));
ASSERT_EQ(under_test.at(key).as<std::string>(), value.as<std::string>());
}
for (const auto& [key, value] : under_test) {
ASSERT_TRUE(expected_result.contains(key));
ASSERT_EQ(expected_result.at(key).as<std::string>(), value.as<std::string>());
}
}
TEST(YamlFile, shouldThrowExceptionWhenConfigFileDoesNotExist) {
EXPECT_THAT(
[]() { YamlFile::readFile("testBaseData/does_not_exist.yaml"); },
ThrowsMessage<std::runtime_error>(
::testing::HasSubstr("Could not open the YAML file: 'testBaseData/does_not_exist.yaml'")
)
);
}
TEST(YamlFile, shouldThrowExceptionWhenConfigFileCannotBeParsed) {
EXPECT_THAT(
[]() {
YamlFile::fromYAML("string", R"(
X
s:
)");
},
ThrowsMessage<std::runtime_error>(
::testing::HasSubstr("string does not contain valid YAML: yaml-cpp: error at line 3, "
"column 2: illegal map value")
)
);
}