forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ONCatEntityTest.h
137 lines (108 loc) · 5.4 KB
/
ONCatEntityTest.h
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
134
135
136
137
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2012 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
#include "MantidCatalog/Exception.h"
#include "MantidCatalog/ONCatEntity.h"
using Mantid::Catalog::Exception::MalformedRepresentationError;
using Mantid::Catalog::ONCat::ONCatEntity;
class ONCatEntityTest : public CxxTest::TestSuite {
public:
// CxxTest boilerplate.
static ONCatEntityTest *createSuite() { return new ONCatEntityTest(); }
static void destroySuite(ONCatEntityTest *suite) { delete suite; }
void test_basic_attributes() {
std::string dummyRepresentation = "{"
" \"id\": \"3fa1d522-f1b8-4134-a56b-b61f24d20510\","
" \"type\": \"dummy\""
"}";
std::stringstream ss;
ss << dummyRepresentation;
auto dummy = ONCatEntity::fromJSONStream(ss);
TS_ASSERT_EQUALS(dummy.id(), std::string("3fa1d522-f1b8-4134-a56b-b61f24d20510"));
TS_ASSERT_EQUALS(dummy.type(), std::string("dummy"));
}
void test_basic_attributes_of_entity_vector() {
std::string dummiesRepresentation = "["
" {"
" \"id\": \"3fa1d522-f1b8-4134-a56b-b61f24d20510\","
" \"type\": \"dummy\""
" },"
" {"
" \"id\": \"4b1dec2a-0f15-416d-8d23-e08901ac4634\","
" \"type\": \"dummy\""
" }"
"]";
std::stringstream ss;
ss << dummiesRepresentation;
auto dummies = ONCatEntity::vectorFromJSONStream(ss);
TS_ASSERT_EQUALS(dummies.size(), 2);
TS_ASSERT_EQUALS(dummies[0].id(), std::string("3fa1d522-f1b8-4134-a56b-b61f24d20510"));
TS_ASSERT_EQUALS(dummies[0].type(), std::string("dummy"));
TS_ASSERT_EQUALS(dummies[1].id(), std::string("4b1dec2a-0f15-416d-8d23-e08901ac4634"));
TS_ASSERT_EQUALS(dummies[1].type(), std::string("dummy"));
}
void test_throws_on_malformed_json() {
std::string malformedRepresentation = "{"
" \"id\": \"3fa1d522-f1b8-4134-a56b-b61f24d20510\","
" \"type\": \"dummy";
std::stringstream ss;
ss << malformedRepresentation;
TS_ASSERT_THROWS(ONCatEntity::fromJSONStream(ss), const MalformedRepresentationError &);
}
void test_throws_on_malformed_representation() {
std::string missingTypeRepresentation = "{"
" \"id\": \"3fa1d522-f1b8-4134-a56b-b61f24d20510\""
"}";
std::stringstream ss;
ss << missingTypeRepresentation;
TS_ASSERT_THROWS(ONCatEntity::fromJSONStream(ss), const MalformedRepresentationError &);
}
void test_nested_values_with_various_types() {
std::string dummyRepresentation = "{"
" \"id\": \"3fa1d522-f1b8-4134-a56b-b61f24d20510\","
" \"type\": \"dummy\","
" \"val\": {"
" \"a\": {"
" \"string\": \"value\","
" \"int\": 1234,"
" \"float\": 1234.5,"
" \"double\": 1234.5,"
" \"bool\": true"
" }"
" }"
"}";
std::stringstream ss;
ss << dummyRepresentation;
auto dummy = ONCatEntity::fromJSONStream(ss);
TS_ASSERT_EQUALS(dummy.get<std::string>("val.a.string"), std::string("value"));
TS_ASSERT_EQUALS(dummy.get<int>("val.a.int"), 1234);
TS_ASSERT_EQUALS(dummy.get<float>("val.a.float"), 1234.5f);
TS_ASSERT_EQUALS(dummy.get<double>("val.a.double"), 1234.5);
TS_ASSERT_EQUALS(dummy.get<bool>("val.a.bool"), true);
TS_ASSERT(!dummy.get<std::string>("a.string"));
TS_ASSERT(!dummy.get<int>("a.int"));
TS_ASSERT(!dummy.get<float>("a.float"));
TS_ASSERT(!dummy.get<double>("a.double"));
TS_ASSERT(!dummy.get<bool>("a.bool"));
}
void test_default_values_with_various_types() {
std::string dummyRepresentation = "{"
" \"id\": \"3fa1d522-f1b8-4134-a56b-b61f24d20510\","
" \"type\": \"dummy\""
" }"
"}";
std::stringstream ss;
ss << dummyRepresentation;
auto dummy = ONCatEntity::fromJSONStream(ss);
TS_ASSERT_EQUALS(dummy.get<std::string>("a.string", "val"), std::string("val"));
TS_ASSERT_EQUALS(dummy.get<int>("a.int", 1234), 1234);
TS_ASSERT_EQUALS(dummy.get<float>("a.float", 1234.5f), 1234.5f);
TS_ASSERT_EQUALS(dummy.get<double>("a.double", 1234.5), 1234.5);
TS_ASSERT_EQUALS(dummy.get<bool>("a.bool", true), true);
}
};