Esempio n. 1
0
  def test_lazy_initializer(self):
    lazy_evaluator = TestLazyEvaluator()
    builder = ConfigurationBindingsBuilder()
    builder.add_lazy_initializer('lazy', lazy_evaluator)
    builder.add_lazy_initializer('implied', lazy_evaluator)
    builder.add_lazy_initializer('overriden', lazy_evaluator)
    builder.add_lazy_initializer('has_default', lazy_evaluator)
    builder.set_default('has_default', 'default')

    bindings = builder.build()
    self.assertEqual(2, bindings['lazy'])
    self.assertEqual(1, bindings['IMPLIED'])  # already initialized

    bindings['overriden'] = 'Hello'
    self.assertEquals('Hello', bindings['overriden'])

    # Default is lower precedence than lazy initializer
    self.assertEquals(3, bindings['has_default'])

    with self.assertRaises(KeyError):
      self.assertIsNotNone(bindings['missing'])

    self.assertEquals([(bindings, 'implied'),
                       (bindings, 'lazy'),
                       (bindings, 'has_default')],
                      lazy_evaluator.called_with_bk)
Esempio n. 2
0
  def test_lazy_initializer(self):
    lazy_evaluator = TestLazyEvaluator()
    builder = ConfigurationBindingsBuilder()
    builder.add_lazy_initializer('lazy', lazy_evaluator)
    builder.add_lazy_initializer('implied', lazy_evaluator)
    builder.add_lazy_initializer('overriden', lazy_evaluator)
    builder.add_lazy_initializer('has_default', lazy_evaluator)
    builder.set_default('has_default', 'default')

    bindings = builder.build()
    self.assertEqual(2, bindings['lazy'])
    self.assertEqual(1, bindings['IMPLIED'])  # already initialized

    bindings['overriden'] = 'Hello'
    self.assertEquals('Hello', bindings['overriden'])

    # Default is lower precedence than lazy initializer
    self.assertEquals(3, bindings['has_default'])

    with self.assertRaises(KeyError):
      self.assertIsNotNone(bindings['missing'])

    self.assertEquals([(bindings, 'implied'),
                       (bindings, 'lazy'),
                       (bindings, 'has_default')],
                      lazy_evaluator.called_with_bk)
Esempio n. 3
0
  def test_override_default(self):
    builder = ConfigurationBindingsBuilder()
    builder.set_default('test', 'MyPreDefault')
    builder.set_override('test', 'MyOverride')
    builder.set_default('test', 'MyPostDefault')

    bindings = builder.build()
    self.assertEquals('MyOverride', bindings.get('test'))
Esempio n. 4
0
  def test_override_default(self):
    builder = ConfigurationBindingsBuilder()
    builder.set_default('test', 'MyPreDefault')
    builder.set_override('test', 'MyOverride')
    builder.set_default('test', 'MyPostDefault')

    bindings = builder.build()
    self.assertEquals('MyOverride', bindings.get('test'))
Esempio n. 5
0
 def test_from_file_for_class_with_defaults(self):
   builder = ConfigurationBindingsBuilder()
   builder.add_configs_for_class(ConfigurationBindingsTest)
   builder.set_default('missing', 'MISSING')
   builder.set_default('config_location', 'IGNORE')
   bindings = builder.build()
   self.assertEquals('MISSING', bindings.get('missing'))
   self.assertEquals('tests/base/ConfigurationBindingsTest',
                     bindings.get('config_location'))
Esempio n. 6
0
 def test_from_file_for_class_with_defaults(self):
   builder = ConfigurationBindingsBuilder()
   builder.add_configs_for_class(ConfigurationBindingsTest)
   builder.set_default('missing', 'MISSING')
   builder.set_default('config_location', 'IGNORE')
   bindings = builder.build()
   self.assertEquals('MISSING', bindings.get('missing'))
   self.assertEquals('tests/base/ConfigurationBindingsTest',
                     bindings.get('config_location'))
Esempio n. 7
0
  def test_environ_builder_value(self):
    builder = ConfigurationBindingsBuilder()
    builder.add_config_file(os.path.join(os.path.dirname(__file__),
                                         'bindings_test.config'))
    builder.set_default('up', '$TEST_UP')

    builder.set_override('down', '$TEST_DOWN')

    bindings = builder.build()
    os.environ['TEST_UP'] = '123'
    os.environ['TEST_DOWN'] = '321'
    self.assertEquals(os.environ.get('HOME'), bindings.get('test_home'))
    self.assertEquals('123', bindings.get('up'))
    self.assertEquals('321', bindings.get('down'))
Esempio n. 8
0
    def test_environ_builder_value(self):
        builder = ConfigurationBindingsBuilder()
        builder.add_config_file(
            os.path.join(os.path.dirname(__file__), 'bindings_test.config'))
        builder.set_default('up', '$TEST_UP')

        builder.set_override('down', '$TEST_DOWN')

        bindings = builder.build()
        os.environ['TEST_UP'] = '123'
        os.environ['TEST_DOWN'] = '321'
        self.assertEquals(os.environ.get('HOME'), bindings.get('test_home'))
        self.assertEquals('123', bindings.get('up'))
        self.assertEquals('321', bindings.get('down'))
Esempio n. 9
0
  def test_contains(self):
    lazy_evaluator = TestLazyEvaluator()
    builder = ConfigurationBindingsBuilder()
    builder.add_lazy_initializer('lazy', lazy_evaluator)
    builder.add_lazy_initializer('shared', lazy_evaluator)
    builder.add_configs_for_class(ConfigurationBindingsTest)
    builder.set_override('shared', 'OverridenValue')
    builder.set_override('overriden', 'OverridenValue')
    builder.set_default('default', 'DefaultValue')

    bindings = builder.build()
    self.assertTrue('lazy' in bindings)
    self.assertTrue('shared' in bindings)
    self.assertTrue('overriden' in bindings)
    self.assertTrue('default' in bindings)
    self.assertTrue('DeFaUlT' in bindings)
    self.assertTrue('read_configuration_bindings_test' in bindings)
    self.assertTrue('tests_base_param' in bindings)

    self.assertFalse('missing' in bindings)

    self.assertEquals([], lazy_evaluator.called_with_bk)
    self.assertFalse('implied' in bindings)
Esempio n. 10
0
  def test_contains(self):
    lazy_evaluator = TestLazyEvaluator()
    builder = ConfigurationBindingsBuilder()
    builder.add_lazy_initializer('lazy', lazy_evaluator)
    builder.add_lazy_initializer('shared', lazy_evaluator)
    builder.add_configs_for_class(ConfigurationBindingsTest)
    builder.set_override('shared', 'OverridenValue')
    builder.set_override('overriden', 'OverridenValue')
    builder.set_default('default', 'DefaultValue')

    bindings = builder.build()
    self.assertTrue('lazy' in bindings)
    self.assertTrue('shared' in bindings)
    self.assertTrue('overriden' in bindings)
    self.assertTrue('default' in bindings)
    self.assertTrue('DeFaUlT' in bindings)
    self.assertTrue('read_configuration_bindings_test' in bindings)
    self.assertTrue('tests_base_param' in bindings)

    self.assertFalse('missing' in bindings)

    self.assertEquals([], lazy_evaluator.called_with_bk)
    self.assertFalse('implied' in bindings)
Esempio n. 11
0
 def test_just_default(self):
   builder = ConfigurationBindingsBuilder()
   builder.set_default('test', 'MyDefault')
   bindings = builder.build()
   self.assertEquals('MyDefault', bindings.get('test'))
Esempio n. 12
0
 def test_just_default(self):
   builder = ConfigurationBindingsBuilder()
   builder.set_default('test', 'MyDefault')
   bindings = builder.build()
   self.assertEquals('MyDefault', bindings.get('test'))