Skip to content

Commit

Permalink
unit tests and pylint
Browse files Browse the repository at this point in the history
remake unit tests

added stringification unit test
  • Loading branch information
amaltaro committed Mar 7, 2022
1 parent 998aaf7 commit e985080
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions test/python/WMCore_t/MicroService_t/MSOutput_t/RelValPolicy_t.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Unit tests for the WMCore/MicroService/MSOutput/RelValPolicy.py module
"""
from __future__ import division, print_function

import unittest
import json
from WMCore.MicroService.MSOutput.RelValPolicy import RelValPolicy, RelValPolicyException


class RelValPolicyTests(unittest.TestCase):
"""Unit tests for the RelValPolicy module"""

def setUp(self):
"""Basic setup for each unit test"""
pass

def tearDown(self):
"""Basic tear down operation when leaving each unit test"""
pass

def testBrokenPolicy(self):
"""Tests for the RelValPolicy class with a broken policy"""
validDatatiers = ["GEN-SIM", "GEN", "SIM", "AOD"]
validRSEs = ["rse_1", "rse_2", "rse_3"]

# test output policies with a wrong data type
for testPolicy in [None, {}, "blah", 123]:
with self.assertRaises(RelValPolicyException):
RelValPolicy(testPolicy, validDatatiers, validRSEs)

# test internal structure with wrong data type for datatier
testPolicy = [{"datatier": ["tier1", "tier2"], "destinations": ["rse_1", "rse_2"]},
{"datatier": "default", "destinations": ["rse_1"]}]
with self.assertRaises(RelValPolicyException):
RelValPolicy(testPolicy, validDatatiers, validRSEs)

# test internal structure with wrong data type for destinations
testPolicy = [{"datatier": "tier1", "destinations": "rse_1"},
{"datatier": "default", "destinations": ["rse_1"]}]
with self.assertRaises(RelValPolicyException):
RelValPolicy(testPolicy, validDatatiers, validRSEs)

# test internal structure missing the required "default" key/value pair
testPolicy = [{"datatier": "GEN", "destinations": ["rse_1", "rse_2"]}]
with self.assertRaises(RelValPolicyException):
RelValPolicy(testPolicy, validDatatiers, validRSEs)

def testValidPolicy(self):
"""Tests for the RelValPolicy class with a valid policy"""
validDatatiers = ["GEN-SIM", "GEN", "SIM", "AOD"]
validRSEs = ["rse_1", "rse_2", "rse_3"]

testPolicy = [{"datatier": "SIM", "destinations": ["rse_1", "rse_2"]},
{"datatier": "GEN-SIM", "destinations": ["rse_1"]},
{"datatier": "default", "destinations": ["rse_2"]}]
policyObj = RelValPolicy(testPolicy, validDatatiers, validRSEs)

# now test the method to get destinations for a given dataset (datatier)
for policyItem in testPolicy:
resp = policyObj.getDestinationByDataset("/PD/ProcStr-v1/{}".format(policyItem['datatier']))
self.assertEqual(resp, policyItem['destinations'])

# and this should fallback to the 'default' case
resp = policyObj.getDestinationByDataset("/PD/ProcStr-v1/BLAH")
self.assertEqual(resp, ["rse_2"])

def testStringification(self):
"""Test the stringification of the RelValPolicy object"""
validDatatiers = []
validRSEs = ["rse_2"]
testPolicy = [{"datatier": "default", "destinations": ["rse_2"]}]

policyObj = str(RelValPolicy(testPolicy, validDatatiers, validRSEs))

self.assertTrue(isinstance(policyObj, str))
policyObj = json.loads(policyObj)
self.assertCountEqual(policyObj["originalPolicy"], testPolicy)
self.assertCountEqual(policyObj["mappedPolicy"], {"default": ["rse_2"]})


if __name__ == '__main__':
unittest.main()

0 comments on commit e985080

Please sign in to comment.