Macros

Line Code
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

#include "DataFormats/TestObjects/interface/ToyProducts.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/TestProcessor/interface/TestProcessor.h"

#include <iostream>
#include <fmt/format.h>

static constexpr auto s_tag = "[EDAlias]";

TEST_CASE("Configuration", s_tag) {
  const std::string baseConfig{
      R"_(from FWCore.TestProcessor.TestProcess import *
import FWCore.ParameterSet.Config as cms

process = TestProcess()

process.intprod = cms.EDProducer('IntProducer', ivalue = cms.int32(1))
process.intalias = cms.EDAlias(intprod = cms.VPSet(cms.PSet(type = cms.string('edmtestIntProduct'))))

# Module to be tested can not be an EDAlias
process.test = cms.EDProducer('AddIntsProducer', labels = cms.VInputTag('intalias'))

process.moduleToTest(process.test, cms.Task(process.intprod))
)_"};

  edm::test::TestProcessor::Config config{baseConfig};

  SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }

  SECTION("No event data") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.test());
  }

  SECTION("beginJob and endJob only") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
  }

  SECTION("Run with no LuminosityBlocks") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
  }

  SECTION("LuminosityBlock with no Events") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
  }

  SECTION("Getting value") {
    edm::test::TestProcessor tester(config);
    auto event = tester.test();
    REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
  }
}

TEST_CASE("Configuration with two instance aliases to a single product", s_tag) {
  const std::string baseConfig{
      R"_(from FWCore.TestProcessor.TestProcess import *
import FWCore.ParameterSet.Config as cms

process = TestProcess()

process.intprod = cms.EDProducer('IntProducer', ivalue = cms.int32(1))
process.intalias = cms.EDAlias(intprod = cms.VPSet(cms.PSet(type = cms.string('edmtestIntProduct')),
                                                   cms.PSet(type = cms.string('edmtestIntProduct'),
                                                            fromProductInstance = cms.string(''),
                                                            toProductInstance = cms.string('bar'))
                                                  )
)

# Module to be tested can not be an EDAlias
process.test = cms.EDProducer('AddIntsProducer', labels = cms.VInputTag('intalias', 'intalias:bar'))

process.moduleToTest(process.test, cms.Task(process.intprod))
)_"};

  edm::test::TestProcessor::Config config{baseConfig};

  SECTION("Alias with two instances pointing to the same product is not allowed") {
    REQUIRE_THROWS_WITH(
        edm::test::TestProcessor(config),
        Catch::Contains("EDAlias conflict") && Catch::Contains("is used for multiple products of type") &&
            Catch::Contains("with module label") && Catch::Contains("and instance name") &&
            Catch::Contains("alias has the instance name") && Catch::Contains("and the other has the instance name"));
  }
}

TEST_CASE("Configuration with two identical aliases pointing to different products") {
  const std::string baseConfig{
      R"_(from FWCore.TestProcessor.TestProcess import *
import FWCore.ParameterSet.Config as cms

process = TestProcess()

process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2), values = cms.VPSet(cms.PSet(instance=cms.string('foo'),value=cms.int32(3))))
process.intalias = cms.EDAlias(intprod = cms.VPSet(cms.PSet(type = cms.string('edmtestIntProduct'), toProductInstance = cms.string(''), fromProductInstance = cms.string('')),
                                                   cms.PSet(type = cms.string('edmtestIntProduct'), toProductInstance = cms.string(''), fromProductInstance = cms.string('foo'))
                                                  )
)

# Module to be tested can not be an EDAlias
process.test = cms.EDProducer('AddIntsProducer', labels = cms.VInputTag('intalias'))

process.moduleToTest(process.test, cms.Task(process.intprod))
)_"};

  edm::test::TestProcessor::Config config{baseConfig};

  SECTION("Alias with two instances pointing to the same product is not allowed") {
    REQUIRE_THROWS_WITH(edm::test::TestProcessor(config),
                        Catch::Contains("EDAlias conflict") && Catch::Contains("and product instance alias") &&
                            Catch::Contains("are used for multiple products of type") &&
                            Catch::Contains("One has module label") && Catch::Contains("the other has module label"));
  }
}

////////////////////////////////////////
TEST_CASE("Configuration with all products of a module", s_tag) {
  const std::string baseConfig{
      R"_(from FWCore.TestProcessor.TestProcess import *
import FWCore.ParameterSet.Config as cms

process = TestProcess()

process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2),
    values = cms.VPSet(
        cms.PSet(instance=cms.string('foo'),value=cms.int32(3)),
        cms.PSet(instance=cms.string('another'),value=cms.int32(4)),
    )
)

process.intalias = cms.EDAlias(intprod = cms.EDAlias.allProducts())

# Module to be tested can not be an EDAlias
process.test = cms.EDProducer('AddIntsProducer',
    labels = cms.VInputTag('intalias', ('intalias', 'foo'), ('intalias', 'another'))
)

process.moduleToTest(process.test, cms.Task(process.intprod))
)_"};

  edm::test::TestProcessor::Config config{baseConfig};

  SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }

  SECTION("No event data") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.test());
  }

  SECTION("beginJob and endJob only") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
  }

  SECTION("Run with no LuminosityBlocks") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
  }

  SECTION("LuminosityBlock with no Events") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
  }

  SECTION("Getting value") {
    edm::test::TestProcessor tester(config);
    auto event = tester.test();
    REQUIRE(event.get<edmtest::IntProduct>()->value == 9);
  }
}

TEST_CASE("Configuration with all products of a module with a given product instance name", s_tag) {
  constexpr const std::string_view baseConfig{
      R"_(from FWCore.TestProcessor.TestProcess import *
import FWCore.ParameterSet.Config as cms

process = TestProcess()

process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2),
    values = cms.VPSet(
        cms.PSet(instance=cms.string('foo'),value=cms.int32(3)),
        cms.PSet(instance=cms.string('another'),value=cms.int32(4)),
    )
)

process.intalias = cms.EDAlias(intprod = cms.VPSet(cms.PSet(type = cms.string('*'), fromProductInstance = cms.string('another'))))

# Module to be tested can not be an EDAlias
process.test = cms.EDProducer('AddIntsProducer',
    labels = cms.VInputTag({})
)

process.moduleToTest(process.test, cms.Task(process.intprod))
)_"};

  edm::test::TestProcessor::Config config{fmt::format(baseConfig, "'intalias:another'")};

  SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }

  SECTION("No event data") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.test());
  }

  SECTION("beginJob and endJob only") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
  }

  SECTION("Run with no LuminosityBlocks") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
  }

  SECTION("LuminosityBlock with no Events") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
  }

  SECTION("Getting value") {
    edm::test::TestProcessor tester(config);
    auto event = tester.test();
    REQUIRE(event.get<edmtest::IntProduct>()->value == 4);
  }

  SECTION("Other product instances are not aliased") {
    {
      edm::test::TestProcessor::Config config{fmt::format(baseConfig, "'intalias:foo'")};
      edm::test::TestProcessor tester(config);
      REQUIRE_THROWS_WITH(tester.test(), Catch::Contains("ProductNotFound"));
    }
    {
      edm::test::TestProcessor::Config config{fmt::format(baseConfig, "'intalias'")};
      edm::test::TestProcessor tester(config);
      REQUIRE_THROWS_WITH(tester.test(), Catch::Contains("ProductNotFound"));
    }
  }
}

////////////////////////////////////////
TEST_CASE("Configuration with all products of two modules", s_tag) {
  const std::string baseConfig{
      R"_(from FWCore.TestProcessor.TestProcess import *
import FWCore.ParameterSet.Config as cms

process = TestProcess()

process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2),
    values = cms.VPSet(
        cms.PSet(instance=cms.string('foo'),value=cms.int32(3)),
        cms.PSet(instance=cms.string('another'),value=cms.int32(4)),
    )
)
process.intprod2 = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(20),
    values = cms.VPSet(
        cms.PSet(instance=cms.string('foo2'),value=cms.int32(30)),
        cms.PSet(instance=cms.string('another2'),value=cms.int32(40)),
    )
)

process.intalias = cms.EDAlias(
    intprod = cms.EDAlias.allProducts(),
    # can't use allProducts() because the product instance '' would lead to duplicate brances to be aliased
    intprod2 = cms.VPSet(
        cms.PSet(type = cms.string('*'), fromProductInstance = cms.string('foo2')),
        cms.PSet(type = cms.string('*'), fromProductInstance = cms.string('another2')),
    )
)

# Module to be tested can not be an EDAlias
process.test = cms.EDProducer('AddIntsProducer',
    labels = cms.VInputTag('intalias', ('intalias', 'foo'), ('intalias', 'another'), ('intalias', 'foo2'), ('intalias', 'another2'))
)

process.moduleToTest(process.test, cms.Task(process.intprod, process.intprod2))
)_"};

  edm::test::TestProcessor::Config config{baseConfig};

  SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }

  SECTION("No event data") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.test());
  }

  SECTION("beginJob and endJob only") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
  }

  SECTION("Run with no LuminosityBlocks") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
  }

  SECTION("LuminosityBlock with no Events") {
    edm::test::TestProcessor tester(config);
    REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
  }

  SECTION("Getting value") {
    edm::test::TestProcessor tester(config);
    auto event = tester.test();
    REQUIRE(event.get<edmtest::IntProduct>()->value == 79);
  }
}

////////////////////////////////////////
TEST_CASE("No products found with wildcards", s_tag) {
  constexpr const std::string_view baseConfig{
      R"_(from FWCore.TestProcessor.TestProcess import *
import FWCore.ParameterSet.Config as cms

process = TestProcess()

process.intprod = cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2),
    values = cms.VPSet(
        cms.PSet(instance=cms.string('foo'),value=cms.int32(3)),
        cms.PSet(instance=cms.string('another'),value=cms.int32(4)),
    )
)

process.intalias = cms.EDAlias({})

# Module to be tested can not be an EDAlias
process.test = cms.EDProducer('AddIntsProducer',
    labels = cms.VInputTag('intalias')
)

process.moduleToTest(process.test, cms.Task(process.intprod))
)_"};

  SECTION("Type wildcard") {
    edm::test::TestProcessor::Config config{fmt::format(
        baseConfig,
        "intprod = cms.VPSet(cms.PSet(type = cms.string('*'), fromProductInstance = cms.string('nonexistent')))")};

    REQUIRE_THROWS_WITH(
        edm::test::TestProcessor(config),
        Catch::Contains("There are no products with module label 'intprod' and product instance name 'nonexistent'"));
  }

  SECTION("Instance wildcard") {
    edm::test::TestProcessor::Config config{
        fmt::format(baseConfig, "intprod = cms.VPSet(cms.PSet(type = cms.string('nonexistentType')))")};

    REQUIRE_THROWS_WITH(edm::test::TestProcessor(config),
                        Catch::Contains("There are no products of type 'nonexistentType'") &&
                            Catch::Contains("with module label 'intprod'"));
  }
}