~gary/charms/oneiric/buildbot-master/dynamic-relationship

« back to all changes in this revision

Viewing changes to hooks/tests.py

  • Committer: Brad Crittenden
  • Date: 2012-02-03 14:21:51 UTC
  • mfrom: (8.1.11 wip)
  • Revision ID: bac@canonical.com-20120203142151-1xdzrpqismvjefpm
[r=gary_poster] Fix config-changed to save state and only modify items that have changed.  More helpers and tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from subprocess import CalledProcessError
 
2
import tempfile
1
3
import unittest
 
4
 
 
5
from helpers import (
 
6
    command,
 
7
    DictDiffer,
 
8
    load_pickle,
 
9
    save_pickle,
 
10
    unit_info,
 
11
    )
 
12
 
2
13
from subprocess import CalledProcessError
3
 
from helpers import command, unit_info
4
 
 
5
 
 
6
 
class testCommand(unittest.TestCase):
 
14
 
 
15
 
 
16
class TestCommand(unittest.TestCase):
7
17
 
8
18
    def testSimpleCommand(self):
9
19
        # Creating a simple command (ls) works and running the command
42
52
        ls = command('/bin/ls')
43
53
        ls('--help', '>')
44
54
 
45
 
 
46
 
class testUnit_info(unittest.TestCase):
 
55
    def testOneLongArgument(self):
 
56
        ls = command('/bin/ls')
 
57
        with self.assertRaises(CalledProcessError):
 
58
            ls('tests.py install')
 
59
 
 
60
    def testSplitStringWorks(self):
 
61
        ls = command('/bin/ls')
 
62
        ls(*'tests.py install'.split())
 
63
 
 
64
 
 
65
class TestDictDiffer(unittest.TestCase):
 
66
 
 
67
    def testStr(self):
 
68
        a = dict(cow='moo', pig='oink')
 
69
        b = dict(cow='moo', pig='oinkoink', horse='nay')
 
70
        diff = DictDiffer(b, a)
 
71
        s = str(diff)
 
72
        self.assertIn("added: {'horse': None} -> {'horse': 'nay'}", s)
 
73
        self.assertIn("removed: {} -> {}", s)
 
74
        self.assertIn("changed: {'pig': 'oink'} -> {'pig': 'oinkoink'}", s)
 
75
        self.assertIn("unchanged: ['cow']", s)
 
76
 
 
77
    def testStrUnmodified(self):
 
78
        a = dict(cow='moo', pig='oink')
 
79
        diff = DictDiffer(a, a)
 
80
        s = str(diff)
 
81
        self.assertEquals('no changes', s)
 
82
 
 
83
    def testAddedOrChanged(self):
 
84
        a = dict(cow='moo', pig='oink')
 
85
        b = dict(cow='moo', pig='oinkoink', horse='nay')
 
86
        diff = DictDiffer(b, a)
 
87
        expected = set(['horse', 'pig'])
 
88
        self.assertEquals(expected, diff.added_or_changed)
 
89
 
 
90
 
 
91
class TestPicklers(unittest.TestCase):
 
92
 
 
93
    def testSaveAndLoad(self):
 
94
        fd = tempfile.NamedTemporaryFile()
 
95
        fn = fd.name
 
96
        fd.close()
 
97
        orig = dict(a=1, b=2)
 
98
        save_pickle(orig, fn)
 
99
        retrieved = load_pickle(fn)
 
100
        self.assertEquals(orig, retrieved)
 
101
 
 
102
 
 
103
class TestUnit_info(unittest.TestCase):
47
104
 
48
105
    def make_data(self, state='started'):
49
106
        return {