23
23
#include < model/CSearchKey.h>
24
24
25
25
#include " Mocks.h"
26
+ #include " ModelTestHelpers.h"
26
27
27
28
#include < boost/test/unit_test.hpp>
28
29
29
30
#include < memory>
31
+ #include < ranges>
30
32
#include < vector>
31
33
32
34
BOOST_TEST_DONT_PRINT_LOG_VALUE (ml::model::CModelPlotData::TFeatureStrByFieldDataUMapUMapCItr);
@@ -35,115 +37,79 @@ BOOST_AUTO_TEST_SUITE(CModelDetailsViewTest)
35
37
36
38
using namespace ml;
37
39
38
- namespace {
39
-
40
- const std::string EMPTY_STRING;
41
-
42
- } // unnamed
43
-
44
40
class CTestFixture {
45
41
protected:
46
42
model::CResourceMonitor m_ResourceMonitor;
47
43
};
48
44
49
45
BOOST_FIXTURE_TEST_CASE (testModelPlot, CTestFixture) {
50
46
using TDoubleVec = std::vector<double >;
51
- using TStrVec = std::vector<std::string>;
52
47
using TMockModelPtr = std::unique_ptr<model::CMockModel>;
53
48
54
- core_t ::TTime bucketLength{600 };
55
- model::CSearchKey key;
56
- model::SModelParams params{bucketLength};
49
+ constexpr core_t ::TTime bucketLength{600 };
50
+ const model::CSearchKey key;
51
+ const model::SModelParams params{bucketLength};
57
52
model_t ::TFeatureVec features;
58
53
59
54
model::CAnomalyDetectorModel::TDataGathererPtr gatherer;
60
55
TMockModelPtr model;
61
56
62
57
auto setupTest = [&]() {
63
- gatherer = std::make_shared<model::CDataGatherer>(
64
- model_t::analysisCategory (features[0 ]), model_t ::E_None, params,
65
- EMPTY_STRING, EMPTY_STRING, " p" , EMPTY_STRING, EMPTY_STRING,
66
- TStrVec{}, key, features, 0 , 0 );
67
- std::string person11{" p11" };
68
- std::string person12{" p12" };
69
- std::string person21{" p21" };
70
- std::string person22{" p22" };
71
- bool addedPerson{false };
72
- gatherer->addPerson (person11, m_ResourceMonitor, addedPerson);
73
- gatherer->addPerson (person12, m_ResourceMonitor, addedPerson);
74
- gatherer->addPerson (person21, m_ResourceMonitor, addedPerson);
75
- gatherer->addPerson (person22, m_ResourceMonitor, addedPerson);
76
-
77
- model.reset (new model::CMockModel{params, gatherer, {/* we don't care about influence*/ }});
78
-
79
- maths::time_series::CTimeSeriesDecomposition trend;
80
- maths::common::CNormalMeanPrecConjugate prior{
58
+ gatherer = model::CDataGathererBuilder (model_t::analysisCategory (features[0 ]),
59
+ features, params, key, 0 )
60
+ .personFieldName (" p" )
61
+ .buildSharedPtr ();
62
+ for (const std::vector<std::string> persons{" p11" , " p12" , " p21" , " p22" };
63
+ const auto & person : persons) {
64
+ bool addedPerson{false };
65
+ gatherer->addPerson (person, m_ResourceMonitor, addedPerson);
66
+ }
67
+
68
+ const model::CMockModel::TFeatureInfluenceCalculatorCPtrPrVecVec influenceCalculators;
69
+ model = std::make_unique<model::CMockModel>(params, gatherer, influenceCalculators);
70
+
71
+ const maths::time_series::CTimeSeriesDecomposition trend;
72
+ const maths::common::CNormalMeanPrecConjugate prior{
81
73
maths::common::CNormalMeanPrecConjugate::nonInformativePrior (maths_t ::E_ContinuousData)};
82
- maths::common::CModelParams timeSeriesModelParams{
74
+ const maths::common::CModelParams timeSeriesModelParams{
83
75
bucketLength, 1.0 , 0.001 , 0.2 , 6 * core::constants::HOUR, 24 * core::constants::HOUR};
84
- maths::time_series::CUnivariateTimeSeriesModel timeSeriesModel{
76
+ const maths::time_series::CUnivariateTimeSeriesModel timeSeriesModel{
85
77
timeSeriesModelParams, 0 , trend, prior};
86
78
model::CMockModel::TMathsModelUPtrVec models;
87
- models.emplace_back (timeSeriesModel.clone (0 ));
88
- models.emplace_back (timeSeriesModel.clone (1 ));
89
- models.emplace_back (timeSeriesModel.clone (2 ));
90
- models.emplace_back (timeSeriesModel.clone (3 ));
79
+ for (int i = 0 ; i < 4 ; ++i) {
80
+ models.emplace_back (timeSeriesModel.clone (i));
81
+ }
91
82
model->mockTimeSeriesModels (std::move (models));
92
83
};
93
84
94
- LOG_DEBUG (<< " Individual sum" );
95
- {
96
- features.assign (1 , model_t ::E_IndividualSumByBucketAndPerson);
85
+ auto testModelPlot = [&](model_t ::EFeature feature, const TDoubleVec& values) {
86
+ features.assign (1 , feature);
97
87
setupTest ();
98
-
99
- TDoubleVec values{2.0 , 3.0 , 0.0 , 0.0 };
100
88
std::size_t pid{0 };
101
89
for (auto value : values) {
102
- model->mockAddBucketValue (model_t ::E_IndividualSumByBucketAndPerson,
103
- pid++, 0 , 0 , {value});
90
+ model->mockAddBucketValue (feature, pid++, 0 , 0 , {value});
104
91
}
105
92
106
93
model::CModelPlotData plotData;
107
94
model->details ()->modelPlot (0 , 90.0 , {}, plotData);
108
95
BOOST_TEST_REQUIRE (plotData.begin () != plotData.end ());
109
- for (const auto & featureByFieldData : plotData) {
110
- BOOST_REQUIRE_EQUAL (values.size (), featureByFieldData. second .size ());
111
- for (const auto & byFieldData : featureByFieldData. second ) {
112
- BOOST_TEST_REQUIRE (gatherer->personId (byFieldData. first , pid));
113
- BOOST_REQUIRE_EQUAL (1 , byFieldData. second .s_ValuesPerOverField .size ());
114
- for (const auto & currentBucketValue : byFieldData. second .s_ValuesPerOverField ) {
115
- BOOST_REQUIRE_EQUAL (values[pid], currentBucketValue. second );
96
+ for (const auto & [ _, plotDataValues ] : plotData) {
97
+ BOOST_REQUIRE_EQUAL (values.size (), plotDataValues .size ());
98
+ for (const auto & [ fst, snd ] : plotDataValues ) {
99
+ BOOST_TEST_REQUIRE (gatherer->personId (fst , pid));
100
+ BOOST_REQUIRE_EQUAL (1 , snd .s_ValuesPerOverField .size ());
101
+ for (const auto & [ field_name, val ] : snd .s_ValuesPerOverField ) {
102
+ BOOST_REQUIRE_EQUAL (values[pid], val );
116
103
}
117
104
}
118
105
}
119
- }
120
-
121
- LOG_DEBUG (<< " Individual count" );
122
- {
123
- features.assign (1 , model_t ::E_IndividualCountByBucketAndPerson);
124
- setupTest ();
106
+ };
125
107
126
- TDoubleVec values{0.0 , 1.0 , 3.0 };
127
- std::size_t pid{0 };
128
- for (auto value : values) {
129
- model->mockAddBucketValue (model_t ::E_IndividualCountByBucketAndPerson,
130
- pid++, 0 , 0 , {value});
131
- }
108
+ LOG_DEBUG (<< " Individual sum" );
109
+ testModelPlot (model_t ::E_IndividualSumByBucketAndPerson, {2.0 , 3.0 , 0.0 , 0.0 });
132
110
133
- model::CModelPlotData plotData;
134
- model->details ()->modelPlot (0 , 90.0 , {}, plotData);
135
- BOOST_TEST_REQUIRE (plotData.begin () != plotData.end ());
136
- for (const auto & featureByFieldData : plotData) {
137
- BOOST_REQUIRE_EQUAL (values.size (), featureByFieldData.second .size ());
138
- for (const auto & byFieldData : featureByFieldData.second ) {
139
- BOOST_TEST_REQUIRE (gatherer->personId (byFieldData.first , pid));
140
- BOOST_REQUIRE_EQUAL (1 , byFieldData.second .s_ValuesPerOverField .size ());
141
- for (const auto & currentBucketValue : byFieldData.second .s_ValuesPerOverField ) {
142
- BOOST_REQUIRE_EQUAL (values[pid], currentBucketValue.second );
143
- }
144
- }
145
- }
146
- }
111
+ LOG_DEBUG (<< " Individual count" );
112
+ testModelPlot (model_t ::E_IndividualCountByBucketAndPerson, {0.0 , 1.0 , 3.0 });
147
113
}
148
114
149
115
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments