def test_destroy_config(self): initialize_config(Namespace()) conf = config() with self.assertRaises(AttributeError): conf.tmp destroy_config() parser2 = ArgumentParser() parser2.add_argument("--tmp5", type=str) parser2.add_argument("--tmp6", type=int) cli_args3 = ["--tmp5", "val5", "--tmp6", "6"] args3 = parser2.parse_args(cli_args3) initialize_config(args3) conf3 = config() self.assertNotEqual(conf3, conf) self.assertEqual(conf3.tmp5, "val5") self.assertEqual(conf3.tmp6, 6) with self.assertRaises(AttributeError): config().tmp1 with self.assertRaises(AttributeError): config().tmp2
def _fake_config(**kwargs): destroy_config() namespace = Namespace() for k, v in kwargs.items(): setattr(namespace, k, v) initialize_config(namespace)
def test_config_already_initialized(self): namespace = Namespace() namespace.tmp = "tmp" initialize_config(namespace) with self.assertRaises(ConfigAlreadyInitializedException): initialize_config(Namespace()) self.assertEqual(namespace, config())
def test_config(self): namespace = Namespace() namespace.tmp1 = "val1" namespace.tmp2 = 2 initialize_config(namespace) conf = config() self.assertEqual(conf.tmp1, "val1") self.assertEqual(conf.tmp2, 2)
def test_single_exp(self): @condition(Exp(a="b")) def tmp(): return "returned" initialize_config(Namespace()) conf = config() conf._allow_overriding = True conf.a = "b" self.assertTrue(tmp() == "returned") conf.a = "c" self.assertIsNone(tmp())
def test_initialize_after(self): """ Test if initialization works after defining a method with a condition. """ namespace = Namespace() namespace.tmp1 = 1 @condition(Exp(lambda c: c.tmp1 == 1)) def tmp(): return "returned" initialize_config(namespace) self.assertEqual(tmp(), "returned")
def test_subparser_condition_wrong_order(self): parser = ArgumentParser() subparsers = parser.add_subparsers(dest="command") parser_sub1 = subparsers.add_parser('sub1') parser_sub1.add_argument('--sub1-tmp', type=int) parser_sub2 = subparsers.add_parser('sub2') parser_sub2.add_argument('--sub2-tmp', type=int) args = parser.parse_args(["sub1", "--sub1-tmp", "1"]) initialize_config(args) self.assertEqual(config().sub1_tmp, 1) @condition( and_(Exp(lambda c: c.sub2_tmp == 2), Exp(lambda c: c.command == "sub2"))) def tmp1(): return "returned" with self.assertRaises(ConditionRaisedException): tmp1()
def test_subparser_condition_right_order(self): parser = ArgumentParser() subparsers = parser.add_subparsers(dest="command") parser_sub1 = subparsers.add_parser('sub1') parser_sub1.add_argument('--sub1-tmp', type=int) parser_sub2 = subparsers.add_parser('sub2') parser_sub2.add_argument('--sub2-tmp', type=int) args = parser.parse_args(["sub1", "--sub1-tmp", "1"]) initialize_config(args) self.assertEqual(config().sub1_tmp, 1) @condition( and_(Exp(lambda c: c.command == "sub2"), Exp(lambda c: c.sub2_tmp == 2))) def tmp2(): return "returned" with self.assertNotRaises(AttributeError): tmp2() self.assertIsNone(tmp2())
from pyppy.args import fill_args from pyppy.config import initialize_config, config initialize_config() config().debug = True @fill_args() def debug_log(debug, message): if debug: return f"debugging: {message}" assert debug_log(message="useful logs") == "debugging: useful logs" config().debug = False assert not debug_log(message="useful logs")
from pyppy.conditions import condition, Exp, and_ import types from pyppy.config import initialize_config, config args = types.SimpleNamespace() args.log_level = "WARN" args.specific_log_level = "LEVEL_1" initialize_config(args) @condition( and_( Exp(log_level="WARN"), Exp(specific_log_level="LEVEL_1") ) ) def log_warn_level_1(): return "WARNING LEVEL 1" assert log_warn_level_1() == "WARNING LEVEL 1" config().log_level = "INFO" assert not log_warn_level_1()