Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:38

0001 #define CATCH_CONFIG_MAIN
0002 #include "catch.hpp"
0003 
0004 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/TestProcessor/interface/TestProcessor.h"
0007 
0008 #include <iostream>
0009 #include <fmt/format.h>
0010 
0011 static constexpr auto s_tag = "[EDAlias]";
0012 
0013 TEST_CASE("Configuration", s_tag) {
0014   const std::string baseConfig{
0015       R"_(from FWCore.TestProcessor.TestProcess import *
0016 import FWCore.ParameterSet.Config as cms
0017 
0018 process = TestProcess()
0019 
0020 process.intprod = cms.EDProducer('IntProducer', ivalue = cms.int32(1))
0021 process.intalias = cms.EDAlias(intprod = cms.VPSet(cms.PSet(type = cms.string('edmtestIntProduct'))))
0022 
0023 # Module to be tested can not be an EDAlias
0024 process.test = cms.EDProducer('AddIntsProducer', labels = cms.VInputTag('intalias'))
0025 
0026 process.moduleToTest(process.test, cms.Task(process.intprod))
0027 )_"};
0028 
0029   edm::test::TestProcessor::Config config{baseConfig};
0030 
0031   SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }
0032 
0033   SECTION("No event data") {
0034     edm::test::TestProcessor tester(config);
0035     REQUIRE_NOTHROW(tester.test());
0036   }
0037 
0038   SECTION("beginJob and endJob only") {
0039     edm::test::TestProcessor tester(config);
0040     REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
0041   }
0042 
0043   SECTION("Run with no LuminosityBlocks") {
0044     edm::test::TestProcessor tester(config);
0045     REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
0046   }
0047 
0048   SECTION("LuminosityBlock with no Events") {
0049     edm::test::TestProcessor tester(config);
0050     REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
0051   }
0052 
0053   SECTION("Getting value") {
0054     edm::test::TestProcessor tester(config);
0055     auto event = tester.test();
0056     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0057   }
0058 }
0059 
0060 TEST_CASE("Configuration with two instance aliases to a single product", s_tag) {
0061   const std::string baseConfig{
0062       R"_(from FWCore.TestProcessor.TestProcess import *
0063 import FWCore.ParameterSet.Config as cms
0064 
0065 process = TestProcess()
0066 
0067 process.intprod = cms.EDProducer('IntProducer', ivalue = cms.int32(1))
0068 process.intalias = cms.EDAlias(intprod = cms.VPSet(cms.PSet(type = cms.string('edmtestIntProduct')),
0069                                                    cms.PSet(type = cms.string('edmtestIntProduct'),
0070                                                             fromProductInstance = cms.string(''),
0071                                                             toProductInstance = cms.string('bar'))
0072                                                   )
0073 )
0074 
0075 # Module to be tested can not be an EDAlias
0076 process.test = cms.EDProducer('AddIntsProducer', labels = cms.VInputTag('intalias', 'intalias:bar'))
0077 
0078 process.moduleToTest(process.test, cms.Task(process.intprod))
0079 )_"};
0080 
0081   edm::test::TestProcessor::Config config{baseConfig};
0082 
0083   SECTION("Alias with two instances pointing to the same product is not allowed") {
0084     REQUIRE_THROWS_WITH(
0085         edm::test::TestProcessor(config),
0086         Catch::Contains("EDAlias conflict") && Catch::Contains("is used for multiple products of type") &&
0087             Catch::Contains("with module label") && Catch::Contains("and instance name") &&
0088             Catch::Contains("alias has the instance name") && Catch::Contains("and the other has the instance name"));
0089   }
0090 }
0091 
0092 TEST_CASE("Configuration with two identical aliases pointing to different products") {
0093   const std::string baseConfig{
0094       R"_(from FWCore.TestProcessor.TestProcess import *
0095 import FWCore.ParameterSet.Config as cms
0096 
0097 process = TestProcess()
0098 
0099 process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2), values = cms.VPSet(cms.PSet(instance=cms.string('foo'),value=cms.int32(3))))
0100 process.intalias = cms.EDAlias(intprod = cms.VPSet(cms.PSet(type = cms.string('edmtestIntProduct'), toProductInstance = cms.string(''), fromProductInstance = cms.string('')),
0101                                                    cms.PSet(type = cms.string('edmtestIntProduct'), toProductInstance = cms.string(''), fromProductInstance = cms.string('foo'))
0102                                                   )
0103 )
0104 
0105 # Module to be tested can not be an EDAlias
0106 process.test = cms.EDProducer('AddIntsProducer', labels = cms.VInputTag('intalias'))
0107 
0108 process.moduleToTest(process.test, cms.Task(process.intprod))
0109 )_"};
0110 
0111   edm::test::TestProcessor::Config config{baseConfig};
0112 
0113   SECTION("Alias with two instances pointing to the same product is not allowed") {
0114     REQUIRE_THROWS_WITH(edm::test::TestProcessor(config),
0115                         Catch::Contains("EDAlias conflict") && Catch::Contains("and product instance alias") &&
0116                             Catch::Contains("are used for multiple products of type") &&
0117                             Catch::Contains("One has module label") && Catch::Contains("the other has module label"));
0118   }
0119 }
0120 
0121 ////////////////////////////////////////
0122 TEST_CASE("Configuration with all products of a module", s_tag) {
0123   const std::string baseConfig{
0124       R"_(from FWCore.TestProcessor.TestProcess import *
0125 import FWCore.ParameterSet.Config as cms
0126 
0127 process = TestProcess()
0128 
0129 process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2),
0130     values = cms.VPSet(
0131         cms.PSet(instance=cms.string('foo'),value=cms.int32(3)),
0132         cms.PSet(instance=cms.string('another'),value=cms.int32(4)),
0133     )
0134 )
0135 
0136 process.intalias = cms.EDAlias(intprod = cms.EDAlias.allProducts())
0137 
0138 # Module to be tested can not be an EDAlias
0139 process.test = cms.EDProducer('AddIntsProducer',
0140     labels = cms.VInputTag('intalias', ('intalias', 'foo'), ('intalias', 'another'))
0141 )
0142 
0143 process.moduleToTest(process.test, cms.Task(process.intprod))
0144 )_"};
0145 
0146   edm::test::TestProcessor::Config config{baseConfig};
0147 
0148   SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }
0149 
0150   SECTION("No event data") {
0151     edm::test::TestProcessor tester(config);
0152     REQUIRE_NOTHROW(tester.test());
0153   }
0154 
0155   SECTION("beginJob and endJob only") {
0156     edm::test::TestProcessor tester(config);
0157     REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
0158   }
0159 
0160   SECTION("Run with no LuminosityBlocks") {
0161     edm::test::TestProcessor tester(config);
0162     REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
0163   }
0164 
0165   SECTION("LuminosityBlock with no Events") {
0166     edm::test::TestProcessor tester(config);
0167     REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
0168   }
0169 
0170   SECTION("Getting value") {
0171     edm::test::TestProcessor tester(config);
0172     auto event = tester.test();
0173     REQUIRE(event.get<edmtest::IntProduct>()->value == 9);
0174   }
0175 }
0176 
0177 TEST_CASE("Configuration with all products of a module with a given product instance name", s_tag) {
0178   constexpr const std::string_view baseConfig{
0179       R"_(from FWCore.TestProcessor.TestProcess import *
0180 import FWCore.ParameterSet.Config as cms
0181 
0182 process = TestProcess()
0183 
0184 process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2),
0185     values = cms.VPSet(
0186         cms.PSet(instance=cms.string('foo'),value=cms.int32(3)),
0187         cms.PSet(instance=cms.string('another'),value=cms.int32(4)),
0188     )
0189 )
0190 
0191 process.intalias = cms.EDAlias(intprod = cms.VPSet(cms.PSet(type = cms.string('*'), fromProductInstance = cms.string('another'))))
0192 
0193 # Module to be tested can not be an EDAlias
0194 process.test = cms.EDProducer('AddIntsProducer',
0195     labels = cms.VInputTag({})
0196 )
0197 
0198 process.moduleToTest(process.test, cms.Task(process.intprod))
0199 )_"};
0200 
0201   edm::test::TestProcessor::Config config{fmt::format(baseConfig, "'intalias:another'")};
0202 
0203   SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }
0204 
0205   SECTION("No event data") {
0206     edm::test::TestProcessor tester(config);
0207     REQUIRE_NOTHROW(tester.test());
0208   }
0209 
0210   SECTION("beginJob and endJob only") {
0211     edm::test::TestProcessor tester(config);
0212     REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
0213   }
0214 
0215   SECTION("Run with no LuminosityBlocks") {
0216     edm::test::TestProcessor tester(config);
0217     REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
0218   }
0219 
0220   SECTION("LuminosityBlock with no Events") {
0221     edm::test::TestProcessor tester(config);
0222     REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
0223   }
0224 
0225   SECTION("Getting value") {
0226     edm::test::TestProcessor tester(config);
0227     auto event = tester.test();
0228     REQUIRE(event.get<edmtest::IntProduct>()->value == 4);
0229   }
0230 
0231   SECTION("Other product instances are not aliased") {
0232     {
0233       edm::test::TestProcessor::Config config{fmt::format(baseConfig, "'intalias:foo'")};
0234       edm::test::TestProcessor tester(config);
0235       REQUIRE_THROWS_WITH(tester.test(), Catch::Contains("ProductNotFound"));
0236     }
0237     {
0238       edm::test::TestProcessor::Config config{fmt::format(baseConfig, "'intalias'")};
0239       edm::test::TestProcessor tester(config);
0240       REQUIRE_THROWS_WITH(tester.test(), Catch::Contains("ProductNotFound"));
0241     }
0242   }
0243 }
0244 
0245 ////////////////////////////////////////
0246 TEST_CASE("Configuration with all products of two modules", s_tag) {
0247   const std::string baseConfig{
0248       R"_(from FWCore.TestProcessor.TestProcess import *
0249 import FWCore.ParameterSet.Config as cms
0250 
0251 process = TestProcess()
0252 
0253 process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2),
0254     values = cms.VPSet(
0255         cms.PSet(instance=cms.string('foo'),value=cms.int32(3)),
0256         cms.PSet(instance=cms.string('another'),value=cms.int32(4)),
0257     )
0258 )
0259 process.intprod2 = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(20),
0260     values = cms.VPSet(
0261         cms.PSet(instance=cms.string('foo2'),value=cms.int32(30)),
0262         cms.PSet(instance=cms.string('another2'),value=cms.int32(40)),
0263     )
0264 )
0265 
0266 process.intalias = cms.EDAlias(
0267     intprod = cms.EDAlias.allProducts(),
0268     # can't use allProducts() because the product instance '' would lead to duplicate brances to be aliased
0269     intprod2 = cms.VPSet(
0270         cms.PSet(type = cms.string('*'), fromProductInstance = cms.string('foo2')),
0271         cms.PSet(type = cms.string('*'), fromProductInstance = cms.string('another2')),
0272     )
0273 )
0274 
0275 # Module to be tested can not be an EDAlias
0276 process.test = cms.EDProducer('AddIntsProducer',
0277     labels = cms.VInputTag('intalias', ('intalias', 'foo'), ('intalias', 'another'), ('intalias', 'foo2'), ('intalias', 'another2'))
0278 )
0279 
0280 process.moduleToTest(process.test, cms.Task(process.intprod, process.intprod2))
0281 )_"};
0282 
0283   edm::test::TestProcessor::Config config{baseConfig};
0284 
0285   SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }
0286 
0287   SECTION("No event data") {
0288     edm::test::TestProcessor tester(config);
0289     REQUIRE_NOTHROW(tester.test());
0290   }
0291 
0292   SECTION("beginJob and endJob only") {
0293     edm::test::TestProcessor tester(config);
0294     REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
0295   }
0296 
0297   SECTION("Run with no LuminosityBlocks") {
0298     edm::test::TestProcessor tester(config);
0299     REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
0300   }
0301 
0302   SECTION("LuminosityBlock with no Events") {
0303     edm::test::TestProcessor tester(config);
0304     REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
0305   }
0306 
0307   SECTION("Getting value") {
0308     edm::test::TestProcessor tester(config);
0309     auto event = tester.test();
0310     REQUIRE(event.get<edmtest::IntProduct>()->value == 79);
0311   }
0312 }
0313 
0314 ////////////////////////////////////////
0315 TEST_CASE("No products found with wildcards", s_tag) {
0316   constexpr const std::string_view baseConfig{
0317       R"_(from FWCore.TestProcessor.TestProcess import *
0318 import FWCore.ParameterSet.Config as cms
0319 
0320 process = TestProcess()
0321 
0322 process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2),
0323     values = cms.VPSet(
0324         cms.PSet(instance=cms.string('foo'),value=cms.int32(3)),
0325         cms.PSet(instance=cms.string('another'),value=cms.int32(4)),
0326     )
0327 )
0328 
0329 process.intalias = cms.EDAlias({})
0330 
0331 # Module to be tested can not be an EDAlias
0332 process.test = cms.EDProducer('AddIntsProducer',
0333     labels = cms.VInputTag('intalias')
0334 )
0335 
0336 process.moduleToTest(process.test, cms.Task(process.intprod))
0337 )_"};
0338 
0339   SECTION("Type wildcard") {
0340     edm::test::TestProcessor::Config config{fmt::format(
0341         baseConfig,
0342         "intprod = cms.VPSet(cms.PSet(type = cms.string('*'), fromProductInstance = cms.string('nonexistent')))")};
0343 
0344     REQUIRE_THROWS_WITH(
0345         edm::test::TestProcessor(config),
0346         Catch::Contains("There are no products with module label 'intprod' and product instance name 'nonexistent'"));
0347   }
0348 
0349   SECTION("Instance wildcard") {
0350     edm::test::TestProcessor::Config config{
0351         fmt::format(baseConfig, "intprod = cms.VPSet(cms.PSet(type = cms.string('nonexistentType')))")};
0352 
0353     REQUIRE_THROWS_WITH(edm::test::TestProcessor(config),
0354                         Catch::Contains("There are no products of type 'nonexistentType'") &&
0355                             Catch::Contains("with module label 'intprod'"));
0356   }
0357 }