Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:17

0001 /*
0002  *  tsetprocessor_t.cc
0003  *  EDMProto
0004  *
0005  *  Created by Chris Jones on 5/1/18.
0006  *
0007  */
0008 
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/TestProcessor/interface/TestProcessor.h"
0011 
0012 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0013 #include "DataFormats/TestObjects/interface/ThingCollection.h"
0014 
0015 #include "FWCore/Integration/interface/ESTestData.h"
0016 #include "FWCore/Integration/interface/ESTestRecords.h"
0017 
0018 #include <cppunit/extensions/HelperMacros.h>
0019 
0020 #include <memory>
0021 
0022 #include <iostream>
0023 #include <sstream>
0024 #include <stdexcept>
0025 #include <string>
0026 #include <vector>
0027 #include <algorithm>
0028 #include <set>
0029 
0030 class testTestProcessor : public CppUnit::TestFixture {
0031   CPPUNIT_TEST_SUITE(testTestProcessor);
0032   CPPUNIT_TEST(simpleProcessTest);
0033   CPPUNIT_TEST(addProductTest);
0034   CPPUNIT_TEST(missingProductTest);
0035   CPPUNIT_TEST(filterTest);
0036   CPPUNIT_TEST(extraProcessTest);
0037   CPPUNIT_TEST(eventSetupTest);
0038   CPPUNIT_TEST(eventSetupPutTest);
0039   CPPUNIT_TEST(lumiTest);
0040   CPPUNIT_TEST(taskTest);
0041   CPPUNIT_TEST(emptyProcessBlockTest);
0042   CPPUNIT_TEST(emptyRunTest);
0043   CPPUNIT_TEST(emptyLumiTest);
0044   CPPUNIT_TEST(processBlockProductTest);
0045   CPPUNIT_TEST(processBlockEndProductTest);
0046   CPPUNIT_TEST(runProductTest);
0047   CPPUNIT_TEST(lumiProductTest);
0048 
0049   CPPUNIT_TEST_SUITE_END();
0050 
0051 public:
0052   void setUp() {}
0053   void tearDown() {}
0054   void simpleProcessTest();
0055   void addProductTest();
0056   void missingProductTest();
0057   void filterTest();
0058   void extraProcessTest();
0059   void eventSetupTest();
0060   void eventSetupPutTest();
0061   void lumiTest();
0062   void taskTest();
0063   void emptyProcessBlockTest();
0064   void emptyRunTest();
0065   void emptyLumiTest();
0066   void processBlockProductTest();
0067   void processBlockEndProductTest();
0068   void runProductTest();
0069   void lumiProductTest();
0070 
0071 private:
0072 };
0073 
0074 ///registration of the test so that the runner can find it
0075 CPPUNIT_TEST_SUITE_REGISTRATION(testTestProcessor);
0076 
0077 void testTestProcessor::simpleProcessTest() {
0078   char const* kTest =
0079       "from FWCore.TestProcessor.TestProcess import *\n"
0080       "process = TestProcess()\n"
0081       "process.foo = cms.EDProducer('IntProducer', ivalue=cms.int32(1))\n"
0082       "process.moduleToTest(process.foo)\n";
0083   edm::test::TestProcessor::Config config(kTest);
0084   edm::test::TestProcessor tester(config);
0085   CPPUNIT_ASSERT(tester.labelOfTestModule() == "foo");
0086 
0087   auto event = tester.test();
0088 
0089   CPPUNIT_ASSERT(event.get<edmtest::IntProduct>()->value == 1);
0090 
0091   CPPUNIT_ASSERT(not event.get<edmtest::IntProduct>("doesNotExist"));
0092   CPPUNIT_ASSERT_THROW(*event.get<edmtest::IntProduct>("doesNotExist"), cms::Exception);
0093 }
0094 
0095 void testTestProcessor::addProductTest() {
0096   char const* kTest =
0097       "from FWCore.TestProcessor.TestProcess import *\n"
0098       "process = TestProcess()\n"
0099       "process.add = cms.EDProducer('AddIntsProducer', labels=cms.VInputTag('in'))\n"
0100       "process.moduleToTest(process.add)\n";
0101   edm::test::TestProcessor::Config config(kTest);
0102   auto token = config.produces<edmtest::IntProduct>("in");
0103 
0104   edm::test::TestProcessor tester(config);
0105 
0106   {
0107     auto event = tester.test(std::make_pair(token, std::make_unique<edmtest::IntProduct>(1)));
0108 
0109     CPPUNIT_ASSERT(event.get<edmtest::IntProduct>()->value == 1);
0110   }
0111 
0112   {
0113     auto event = tester.test(std::make_pair(token, std::make_unique<edmtest::IntProduct>(2)));
0114 
0115     CPPUNIT_ASSERT(event.get<edmtest::IntProduct>()->value == 2);
0116   }
0117 
0118   //Check that event gets reset so the data product is not available
0119   CPPUNIT_ASSERT_THROW(tester.test(), cms::Exception);
0120 }
0121 
0122 void testTestProcessor::missingProductTest() {
0123   char const* kTest =
0124       "from FWCore.TestProcessor.TestProcess import *\n"
0125       "process = TestProcess()\n"
0126       "process.add = cms.EDProducer('AddIntsProducer', labels=cms.VInputTag('in'))\n"
0127       "process.moduleToTest(process.add)\n";
0128   edm::test::TestProcessor::Config config(kTest);
0129 
0130   edm::test::TestProcessor tester(config);
0131 
0132   CPPUNIT_ASSERT_THROW(tester.test(), cms::Exception);
0133 }
0134 
0135 void testTestProcessor::filterTest() {
0136   char const* kTest =
0137       "from FWCore.TestProcessor.TestProcess import *\n"
0138       "process = TestProcess()\n"
0139       "process.foo = cms.EDFilter('TestFilterModule', acceptValue=cms.untracked.int32(2),\n"
0140       "   onlyOne = cms.untracked.bool(True))\n"
0141       "process.moduleToTest(process.foo)\n";
0142   edm::test::TestProcessor::Config config(kTest);
0143   edm::test::TestProcessor tester(config);
0144   CPPUNIT_ASSERT(tester.labelOfTestModule() == "foo");
0145 
0146   CPPUNIT_ASSERT(not tester.test().modulePassed());
0147   CPPUNIT_ASSERT(tester.test().modulePassed());
0148   CPPUNIT_ASSERT(not tester.test().modulePassed());
0149   CPPUNIT_ASSERT(tester.test().modulePassed());
0150 }
0151 
0152 void testTestProcessor::extraProcessTest() {
0153   char const* kTest =
0154       "from FWCore.TestProcessor.TestProcess import *\n"
0155       "process = TestProcess()\n"
0156       "process.add = cms.EDProducer('AddIntsProducer', labels=cms.VInputTag('in'))\n"
0157       "process.moduleToTest(process.add)\n";
0158   edm::test::TestProcessor::Config config(kTest);
0159   auto processToken = config.addExtraProcess("HLT");
0160   auto token = config.produces<edmtest::IntProduct>("in", "", processToken);
0161 
0162   edm::test::TestProcessor tester(config);
0163 
0164   {
0165     auto event = tester.test(std::make_pair(token, std::make_unique<edmtest::IntProduct>(1)));
0166 
0167     CPPUNIT_ASSERT(event.get<edmtest::IntProduct>()->value == 1);
0168   }
0169 }
0170 
0171 void testTestProcessor::eventSetupTest() {
0172   char const* kTest =
0173       "from FWCore.TestProcessor.TestProcess import *\n"
0174       "process = TestProcess()\n"
0175       "process.emptyESSourceA1 = cms.ESSource('EmptyESSource',"
0176       "recordName = cms.string('ESTestRecordA'),"
0177       "firstValid = cms.vuint32(1,2),"
0178       "iovIsRunNotTime = cms.bool(True)"
0179       ")\n"
0180 
0181       "process.add_(cms.ESProducer('ESTestProducerA') )\n"
0182       "process.add = cms.EDAnalyzer('ESTestAnalyzerA', runsToGetDataFor = cms.vint32(1,2), "
0183       "expectedValues=cms.untracked.vint32(1,2))\n"
0184       "process.moduleToTest(process.add)\n";
0185   edm::test::TestProcessor::Config config(kTest);
0186 
0187   edm::test::TestProcessor tester(config);
0188 
0189   (void)tester.test();
0190 
0191   tester.setRunNumber(2);
0192   (void)tester.test();
0193 }
0194 
0195 void testTestProcessor::eventSetupPutTest() {
0196   char const* kTest =
0197       "from FWCore.TestProcessor.TestProcess import *\n"
0198       "process = TestProcess()\n"
0199       "process.add = cms.EDAnalyzer('ESTestAnalyzerA', runsToGetDataFor = cms.vint32(1,2,3), "
0200       "expectedValues=cms.untracked.vint32(1,2,2))\n"
0201       "process.moduleToTest(process.add)\n";
0202   edm::test::TestProcessor::Config config(kTest);
0203   auto estoken = config.esProduces<ESTestRecordA, edmtest::ESTestDataA>();
0204 
0205   edm::test::TestProcessor tester(config);
0206 
0207   (void)tester.test(std::make_pair(estoken, std::make_unique<edmtest::ESTestDataA>(1)));
0208 
0209   tester.setRunNumber(2);
0210   (void)tester.test(std::make_pair(estoken, std::make_unique<edmtest::ESTestDataA>(2)));
0211 
0212   tester.setRunNumber(3);
0213   CPPUNIT_ASSERT_THROW(tester.test(), cms::Exception);
0214 }
0215 
0216 void testTestProcessor::lumiTest() {
0217   auto const kTest = R"_(from FWCore.TestProcessor.TestProcess import *
0218 process = TestProcess()
0219 process.toTest = cms.EDProducer('ThingProducer')
0220 process.moduleToTest(process.toTest)
0221 )_";
0222   edm::test::TestProcessor::Config config(kTest);
0223 
0224   edm::test::TestProcessor tester(config);
0225   (void)tester.test();
0226   tester.setLuminosityBlockNumber(2);
0227   (void)tester.test();
0228 }
0229 
0230 void testTestProcessor::taskTest() {
0231   char const* kTest =
0232       "from FWCore.TestProcessor.TestProcess import *\n"
0233       "process = TestProcess()\n"
0234       "process.mid = cms.EDProducer('AddIntsProducer', labels=cms.VInputTag('in'))\n"
0235       "process.add = cms.EDProducer('AddIntsProducer', labels=cms.VInputTag('mid','in'))\n"
0236       "process.moduleToTest(process.add,cms.Task(process.mid))\n";
0237   edm::test::TestProcessor::Config config(kTest);
0238   auto token = config.produces<edmtest::IntProduct>("in");
0239 
0240   edm::test::TestProcessor tester(config);
0241 
0242   {
0243     auto event = tester.test(std::make_pair(token, std::make_unique<edmtest::IntProduct>(1)));
0244 
0245     CPPUNIT_ASSERT(event.get<edmtest::IntProduct>()->value == 2);
0246   }
0247 }
0248 
0249 void testTestProcessor::emptyProcessBlockTest() {
0250   auto const kTest = R"_(from FWCore.TestProcessor.TestProcess import *
0251 process = TestProcess()
0252 process.toTest = cms.EDAnalyzer('RunLumiEventChecker',
0253         eventSequence = cms.untracked.VEventID()
0254                                 )
0255 process.moduleToTest(process.toTest)
0256 )_";
0257   edm::test::TestProcessor::Config config(kTest);
0258   edm::test::TestProcessor tester(config);
0259 
0260   tester.testWithNoRuns();
0261 }
0262 void testTestProcessor::emptyRunTest() {
0263   auto const kTest = R"_(from FWCore.TestProcessor.TestProcess import *
0264 process = TestProcess()
0265 process.toTest = cms.EDAnalyzer('RunLumiEventChecker',
0266         eventSequence = cms.untracked.VEventID(cms.EventID(1,0,0), cms.EventID(1,0,0))
0267                                 )
0268 process.moduleToTest(process.toTest)
0269 )_";
0270   edm::test::TestProcessor::Config config(kTest);
0271 
0272   edm::test::TestProcessor tester(config);
0273 
0274   tester.testRunWithNoLuminosityBlocks();
0275 }
0276 void testTestProcessor::emptyLumiTest() {
0277   auto const kTest = R"_(from FWCore.TestProcessor.TestProcess import *
0278 process = TestProcess()
0279 process.toTest = cms.EDAnalyzer('RunLumiEventChecker',
0280                                   eventSequence = cms.untracked.VEventID(cms.EventID(1,0,0), cms.EventID(1,1,0),
0281                                                                          cms.EventID(1,1,0), cms.EventID(1,0,0))
0282                                   )
0283 process.moduleToTest(process.toTest)
0284 )_";
0285   edm::test::TestProcessor::Config config(kTest);
0286 
0287   edm::test::TestProcessor tester(config);
0288 
0289   tester.testLuminosityBlockWithNoEvents();
0290 }
0291 
0292 void testTestProcessor::processBlockProductTest() {
0293   auto const kTest = R"_(from FWCore.TestProcessor.TestProcess import *
0294 process = TestProcess()
0295 process.intProducerBeginProcessBlock = cms.EDProducer("IntProducerBeginProcessBlock", ivalue = cms.int32(10000))
0296 process.moduleToTest(process.intProducerBeginProcessBlock)
0297 )_";
0298   edm::test::TestProcessor::Config config(kTest);
0299 
0300   edm::test::TestProcessor tester(config);
0301   {
0302     auto processBlock = tester.testBeginProcessBlock();
0303     CPPUNIT_ASSERT(processBlock.get<edmtest::IntProduct>()->value == 10000);
0304   }
0305   {
0306     auto processBlock = tester.testEndProcessBlock();
0307     CPPUNIT_ASSERT(processBlock.get<edmtest::IntProduct>()->value == 10000);
0308   }
0309 }
0310 
0311 void testTestProcessor::processBlockEndProductTest() {
0312   auto const kTest = R"_(from FWCore.TestProcessor.TestProcess import *
0313 process = TestProcess()
0314 process.intProducerEndProcessBlock = cms.EDProducer("IntProducerEndProcessBlock", ivalue = cms.int32(10001))
0315 process.moduleToTest(process.intProducerEndProcessBlock)
0316 )_";
0317   edm::test::TestProcessor::Config config(kTest);
0318 
0319   edm::test::TestProcessor tester(config);
0320   {
0321     auto processBlock = tester.testBeginProcessBlock();
0322     CPPUNIT_ASSERT_THROW(*processBlock.get<edmtest::IntProduct>(), cms::Exception);
0323   }
0324   {
0325     auto processBlock = tester.testEndProcessBlock();
0326     CPPUNIT_ASSERT(processBlock.get<edmtest::IntProduct>()->value == 10001);
0327   }
0328 }
0329 
0330 void testTestProcessor::runProductTest() {
0331   auto const kTest = R"_(from FWCore.TestProcessor.TestProcess import *
0332 process = TestProcess()
0333 process.toTest = cms.EDProducer('ThingProducer')
0334 process.moduleToTest(process.toTest)
0335 )_";
0336   edm::test::TestProcessor::Config config(kTest);
0337 
0338   edm::test::TestProcessor tester(config);
0339   {
0340     auto run = tester.testBeginRun(1);
0341     CPPUNIT_ASSERT(run.get<edmtest::ThingCollection>("beginRun")->size() == 20);
0342   }
0343 
0344   {
0345     auto run = tester.testEndRun();
0346     CPPUNIT_ASSERT(run.get<edmtest::ThingCollection>("beginRun")->size() == 20);
0347     CPPUNIT_ASSERT(run.get<edmtest::ThingCollection>("endRun")->size() == 20);
0348   }
0349 
0350   {
0351     auto run = tester.testBeginRun(2);
0352     CPPUNIT_ASSERT(run.get<edmtest::ThingCollection>("beginRun")->size() == 20);
0353   }
0354 }
0355 
0356 void testTestProcessor::lumiProductTest() {
0357   auto const kTest = R"_(from FWCore.TestProcessor.TestProcess import *
0358 process = TestProcess()
0359 process.toTest = cms.EDProducer('ThingProducer')
0360 process.moduleToTest(process.toTest)
0361 )_";
0362   edm::test::TestProcessor::Config config(kTest);
0363 
0364   edm::test::TestProcessor tester(config);
0365   {
0366     auto lumi = tester.testBeginLuminosityBlock(1);
0367     CPPUNIT_ASSERT(lumi.get<edmtest::ThingCollection>("beginLumi")->size() == 20);
0368   }
0369 
0370   {
0371     auto lumi = tester.testEndLuminosityBlock();
0372     CPPUNIT_ASSERT(lumi.get<edmtest::ThingCollection>("beginLumi")->size() == 20);
0373     CPPUNIT_ASSERT(lumi.get<edmtest::ThingCollection>("endLumi")->size() == 20);
0374   }
0375 
0376   {
0377     auto lumi = tester.testBeginLuminosityBlock(2);
0378     CPPUNIT_ASSERT(lumi.get<edmtest::ThingCollection>("beginLumi")->size() == 20);
0379   }
0380 }
0381 
0382 #include <Utilities/Testing/interface/CppUnit_testdriver.icpp>