Ejemplo n.º 1
0
    def test_replicate(self):
        replicate('Name', None)  # does nothing, no exception/error
        source_1 = OrderedDictWithDefaults()
        source_1["name"] = "sea"
        source_1["origin"] = "kitchen"
        source_1.defaults_ = {"brewery": False}
        source_2 = OrderedDictWithDefaults()
        source_2["name"] = ["sea", "legume"]
        source_2["origin"] = "fridge"
        source_3 = OrderedDictWithDefaults()
        source_3["name"] = OrderedDictWithDefaults()
        source_3["name"]["value"] = "oil"
        source_3["name"]["key"] = 1
        source_3["name"].defaults_ = {"value": 1}

        target_1 = OrderedDictWithDefaults()
        target_2 = OrderedDictWithDefaults()
        target_3 = OrderedDictWithDefaults()

        replicate(target_1, source_1)
        replicate(target_2, source_2)
        replicate(target_3, source_3)

        # Note: assertDictEqual not available for Python 2.6
        self.assertEqual(str(source_1), str(target_1))
        self.assertEqual(str(source_2), str(target_2))
        self.assertEqual(str(source_3), str(target_3))
Ejemplo n.º 2
0
def test_defaults_getitem_nested():
    """It preserves the defaults_ behaviour of ODD in nested dicts."""
    this = ODD()
    that = ODD(a=2)
    both = DictTree(dict(a=this), dict(a=that))

    # default in this gets overridden by value in that
    this.defaults_ = dict(a=1)
    that.defaults_ = dict()
    assert both['a']['a'] == 2

    # default in first dict gets priority
    this.defaults_ = dict(b=1)
    that.defaults_ = dict(b=2)
    assert both['a']['b'] == 1
Ejemplo n.º 3
0
def test_defaults_getitem_dict():
    """It returns defaults if a value isn't set."""
    this = ODD()
    that = ODD(a=2)
    both = DictTree(this, that)

    # default in this gets overridden by value in that
    this.defaults_ = dict(a=1)
    that.defaults_ = dict()
    assert both['a'] == 2

    # default in first dict gets priority
    this.defaults_ = dict(b=1)
    that.defaults_ = dict(b=2)
    assert both['b'] == 1
Ejemplo n.º 4
0
 def test_contains(self):
     d = OrderedDictWithDefaults()
     self.assertEqual([], list(d.items()))
     d['key'] = 'Birds'
     d.defaults_ = {'value': '10'}
     self.assertTrue('key' in d)
     self.assertTrue('value' in d)
     self.assertFalse('test' in d)
Ejemplo n.º 5
0
 def test_getitem(self):
     d = OrderedDictWithDefaults()
     d['name'] = 'Joseph'
     d.defaults_ = {
         'surname': 'Wyndham'
     }
     self.assertEqual('Joseph', d['name'])
     self.assertEqual('Wyndham', d['surname'])
Ejemplo n.º 6
0
 def test_keys(self):
     d = OrderedDictWithDefaults()
     d['name'] = 'Andrew'
     d['surname'] = 'Gray'
     d.defaults_ = {'address': 'N/A'}
     keys = list(d.keys())
     self.assertTrue(len(keys) == 3)
     self.assertTrue('name' in keys)
     self.assertTrue('surname' in keys)
     self.assertTrue('address' in keys)
Ejemplo n.º 7
0
 def test_contains(self):
     d = OrderedDictWithDefaults()
     self.assertEqual([], list(d.items()))
     d['key'] = 'Birds'
     d.defaults_ = {
         'value': '10'
     }
     self.assertTrue('key' in d)
     self.assertTrue('value' in d)
     self.assertFalse('test' in d)
Ejemplo n.º 8
0
def test_odd_getitem():
    """It returns default values if none of the dicts contains the key."""
    this = ODD(a=1)
    this.defaults_ = dict(c=3)
    that = ODD(b=2)
    that.defaults_ = dict(d=4)
    both = DictTree(this, that)

    # key from this
    assert both['a'] == 1
    # key from that
    assert both['b'] == 2
    # default from this
    assert both['c'] == 3
    # default from that
    assert both['d'] == 4
    # missing value
    with pytest.raises(KeyError):
        both['e']
Ejemplo n.º 9
0
 def test_itervalues(self):
     d = OrderedDictWithDefaults()
     self.assertEqual([], list(d.items()))
     d['key'] = 'Birds'
     d['len'] = '89'
     d.defaults_ = {'surname': 'Wyndham'}
     count = 0
     for k in d.values():
         self.assertTrue(k in ['Birds', '89', 'Wyndham'])
         count += 1
     self.assertEqual(3, count)
Ejemplo n.º 10
0
    def test_pdeepcopy(self):
        """This is tested entirely by the tests in replicate as well"""
        source = OrderedDictWithDefaults()
        source["name"] = OrderedDictWithDefaults()
        source["name"]["value"] = "oil"
        source["name"]["key"] = 1
        source["name"].defaults_ = {"value": 1}

        target = pdeepcopy(source)

        self.assertEqual(source, target)
Ejemplo n.º 11
0
def test_platform_from_job_info_ordered_dict_comparison():
    """Check that we are only comparing set items in OrderedDictWithDefaults.
    """
    job = {'batch system': 'background', 'Made up key': 'Zaphod'}
    remote = {'host': 'hpc1', 'Made up key': 'Arthur'}
    # Set up a fake OrderedDictWith a fake unset default.
    platform = OrderedDictWithDefaults()
    platform.defaults_ = {k: None for k in PLATFORMS['hpc1-bg'].keys()}
    platform.defaults_['Made up key'] = {}
    platform.update(PLATFORMS['hpc1-bg'])
    platforms = {'hpc1-bg': platform, 'dobbie': PLATFORMS['sugar']}
    assert platform_from_job_info(platforms, job, remote) == 'hpc1-bg'
Ejemplo n.º 12
0
 def test_keys(self):
     d = OrderedDictWithDefaults()
     d['name'] = 'Andrew'
     d['surname'] = 'Gray'
     d.defaults_ = {
         'address': 'N/A'
     }
     keys = list(d.keys())
     self.assertTrue(len(keys) == 3)
     self.assertTrue('name' in keys)
     self.assertTrue('surname' in keys)
     self.assertTrue('address' in keys)
Ejemplo n.º 13
0
 def test_itervalues(self):
     d = OrderedDictWithDefaults()
     self.assertEqual([], list(d.items()))
     d['key'] = 'Birds'
     d['len'] = '89'
     d.defaults_ = {
         'surname': 'Wyndham'
     }
     count = 0
     for k in d.values():
         self.assertTrue(k in ['Birds', '89', 'Wyndham'])
         count += 1
     self.assertEqual(3, count)
Ejemplo n.º 14
0
def replicate(target, source):
    """Replicate source *into* target.

    Source elements need not exist in target already, so source overrides
    common elements in target and otherwise adds elements to it.
    """
    if not source:
        return
    if hasattr(source, "defaults_"):
        target.defaults_ = pdeepcopy(source.defaults_)
    for key, val in source.items():
        if isinstance(val, dict):
            if key not in target:
                target[key] = OrderedDictWithDefaults()
            if hasattr(val, 'defaults_'):
                target[key].defaults_ = pdeepcopy(val.defaults_)
            replicate(target[key], val)
        elif isinstance(val, list):
            target[key] = val[:]
        else:
            target[key] = val
Ejemplo n.º 15
0
Archivo: util.py Proyecto: cylc/cylc
def replicate(target, source):
    """Replicate source *into* target.

    Source elements need not exist in target already, so source overrides
    common elements in target and otherwise adds elements to it.
    """
    if not source:
        return
    if hasattr(source, "defaults_"):
        target.defaults_ = pdeepcopy(source.defaults_)
    for key, val in source.items():
        if isinstance(val, dict):
            if key not in target:
                target[key] = OrderedDictWithDefaults()
            if hasattr(val, 'defaults_'):
                target[key].defaults_ = pdeepcopy(val.defaults_)
            replicate(target[key], val)
        elif isinstance(val, list):
            target[key] = val[:]
        else:
            target[key] = val
Ejemplo n.º 16
0
 def test_getitem(self):
     d = OrderedDictWithDefaults()
     d['name'] = 'Joseph'
     d.defaults_ = {'surname': 'Wyndham'}
     self.assertEqual('Joseph', d['name'])
     self.assertEqual('Wyndham', d['surname'])