Skip to content

Commit ab4f7c5

Browse files
committed
refactor tests to remove direct construction call to CDataGatherer
1 parent 408e875 commit ab4f7c5

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

lib/model/unittest/CDetectionRuleTest.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,41 @@ BOOST_FIXTURE_TEST_CASE(testRuleActions, CTestFixture) {
794794
0, 0, 100));
795795
}
796796

797+
TMockModelPtr initializeModel(ml::model::CResourceMonitor& resourceMonitor) {
798+
core_t::TTime bucketLength{600};
799+
model::SModelParams params{bucketLength};
800+
model::CSearchKey key;
801+
model_t::TFeatureVec features;
802+
// Initialize mock model
803+
model::CAnomalyDetectorModel::TDataGathererPtr gatherer;
804+
805+
features.assign(1, model_t::E_IndividualSumByBucketAndPerson);
806+
807+
gatherer = std::make_shared<model::CDataGatherer>(
808+
model_t::analysisCategory(features[0]), model_t::E_None, params, EMPTY_STRING,
809+
EMPTY_STRING, "p", EMPTY_STRING, EMPTY_STRING, TStrVec{}, key, features, 0, 0);
810+
811+
std::string person("p1");
812+
bool addedPerson{false};
813+
gatherer->addPerson(person, resourceMonitor, addedPerson);
814+
815+
TMockModelPtr model{new model::CMockModel(
816+
params, gatherer, {/* we don't care about influence */})};
817+
818+
maths::time_series::CTimeSeriesDecomposition trend;
819+
maths::common::CNormalMeanPrecConjugate prior{
820+
maths::common::CNormalMeanPrecConjugate::nonInformativePrior(maths_t::E_ContinuousData)};
821+
maths::common::CModelParams timeSeriesModelParams{
822+
bucketLength, 1.0, 0.001, 0.2, 6 * core::constants::HOUR, 24 * core::constants::HOUR};
823+
std::unique_ptr<maths::time_series::CUnivariateTimeSeriesModel> timeSeriesModel =
824+
std::make_unique<maths::time_series::CUnivariateTimeSeriesModel>(
825+
timeSeriesModelParams, 0, trend, prior);
826+
model::CMockModel::TMathsModelUPtrVec models;
827+
models.emplace_back(std::move(timeSeriesModel));
828+
model->mockTimeSeriesModels(std::move(models));
829+
return model;
830+
}
831+
797832
BOOST_FIXTURE_TEST_CASE(testRuleTimeShiftShouldShiftTimeSeriesModelState, CTestFixture) {
798833

799834
test::CRandomNumbers rng;

lib/model/unittest/CEventRatePopulationDataGathererTest.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,11 @@ BOOST_FIXTURE_TEST_CASE(testCompressedLength, CTestFixture) {
475475

476476
CDataGatherer::TFeatureVec features;
477477
features.push_back(model_t::E_PopulationInfoContentByBucketPersonAndAttribute);
478-
SModelParams const params(bucketLength);
478+
SModelParams constparams(bucketLength);
479479
CDataGatherer dataGatherer = CDataGathererBuilder(model_t::E_PopulationEventRate,
480-
features, params, searchKey, startTime)
481-
.valueFieldName("value")
482-
.build();
480+
features, params, searchKey, startTime)
481+
.valueFieldName("value")
482+
.build();
483483
core_t::TTime time = startTime;
484484
for (std::size_t i = 0; i < numberBuckets; ++i, time += bucketLength) {
485485
TMessageVec messages;

lib/model/unittest/CHierarchicalResultsTest.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,12 +1446,13 @@ BOOST_AUTO_TEST_CASE(testWriter) {
14461446
model::SModelParams const params(modelConfig.bucketLength());
14471447
auto interimBucketCorrector =
14481448
std::make_shared<model::CInterimBucketCorrector>(modelConfig.bucketLength());
1449-
model::CSearchKey const key;
1450-
auto dataGatherer =
1451-
model::CDataGathererBuilder(model_t::E_EventRate,
1452-
{model_t::E_IndividualCountByBucketAndPerson},
1453-
params, key, modelConfig.bucketLength())
1454-
.buildSharedPtr();
1449+
model::CSearchKey key;
1450+
model::CAnomalyDetectorModel::TDataGathererPtr dataGatherer(
1451+
std::make_shared<model::CDataGatherer>(
1452+
model_t::E_EventRate, model_t::E_None, params, EMPTY_STRING,
1453+
EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, TStrVec{}, key,
1454+
model_t::TFeatureVec{model_t::E_IndividualCountByBucketAndPerson},
1455+
modelConfig.bucketLength(), 0));
14551456
model::CEventData dummy;
14561457
dataGatherer->addArrival(TStrCPtrVec(1, &EMPTY_STRING), dummy, resourceMonitor);
14571458
dummy.clear();

lib/model/unittest/ModelTestHelpers.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ class CDataGathererBuilder {
124124
return *this;
125125
}
126126

127+
std::shared_ptr<CDataGatherer> buildSharedPtr() const {
128+
return std::make_shared<CDataGatherer>(m_GathererType, m_SummaryMode, m_Params, m_SummaryCountFieldName,
129+
m_PartitionFieldValue, m_PersonFieldName, m_AttributeFieldName,
130+
m_ValueFieldName, m_InfluenceFieldNames, m_SearchKey, m_Features,
131+
m_StartTime, m_SampleCountOverride);
132+
}
133+
134+
CDataGathererBuilder& partitionFieldValue(const std::string& partitionFieldValue) {
135+
m_PartitionFieldValue = partitionFieldValue;
136+
return *this;
137+
}
138+
127139
CDataGathererBuilder& personFieldName(std::string_view personFieldName) {
128140
m_PersonFieldName = personFieldName;
129141
return *this;

0 commit comments

Comments
 (0)