File: sample.py

package info (click to toggle)
doublex 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 420 kB
  • ctags: 891
  • sloc: python: 3,750; makefile: 56
file content (193 lines) | stat: -rwxr-xr-x 5,894 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python
# -*- mode:python; coding:utf-8; tab-width:4 -*-

from unittest import TestCase
from doublex import (Stub, Spy, ProxySpy, Mock,
                     assert_that, called, ANY_ARG, never,
                     verify, any_order_verify)
from hamcrest import greater_than, anything, contains_string


class AlreadyExists(Exception):
    pass


class InvalidPassword(Exception):
    pass


class AccountStore:
    def save(self, login, password):
        pass

    def has_user(self, login):
        pass


class Group(set):
    def __init__(self, name):
        pass


class PasswordService:
    def generate(self):
        pass


class AccountService:
    def __init__(self, store, password_service):
        self.store = store
        self.password_service = password_service

    def create_user(self, login):
        if self.store.has_user(login):
            raise AlreadyExists()

        password = self.password_service.generate()
        if not password:
            raise InvalidPassword()

        self.store.save(login, password)

    def create_group(self, group_name, user_names):
        group = Group(group_name)
        for name in user_names:
            try:
                self.create_user(name)
            except AlreadyExists:
                pass
            group.add(name)


class AccountTests(TestCase):
    def test_account_creation__free_stub(self):
        with Stub() as password_service:
            password_service.generate().returns('some')

        store = Spy(AccountStore)
        service = AccountService(store, password_service)

        service.create_group('team', ['John', 'Peter', 'Alice'])

        assert_that(store.save, called())

    def test_account_creation__restricted_stub(self):
        with Stub(PasswordService) as password_service:
            password_service.generate().returns('some')

        store = Spy(AccountStore)
        service = AccountService(store, password_service)

        service.create_user('John')

        assert_that(store.save, called())

    def test_account_creation__3_accounts(self):
        with Stub(PasswordService) as password_service:
            password_service.generate().returns('some')

        store = Spy(AccountStore)
        service = AccountService(store, password_service)

        service.create_group('team', ['John', 'Peter', 'Alice'])

        assert_that(store.save, called().times(3))
        assert_that(store.save, called().times(greater_than(2)))

    def test_account_creation__argument_values(self):
        with Stub(PasswordService) as password_service:
            password_service.generate().returns('some')

        store = Spy(AccountStore)
        service = AccountService(store, password_service)

        service.create_user('John')

        assert_that(store.save, called().with_args('John', 'some'))
        assert_that(store.save, called().with_args('John', ANY_ARG))
        assert_that(store.save, never(called().with_args('Alice', anything())))
        assert_that(store.save,
                    called().with_args(contains_string('oh'), ANY_ARG))

    def test_account_creation__report_message(self):
        with Stub(PasswordService) as password_service:
            password_service.generate().returns('some')

        store = Spy(AccountStore)
        service = AccountService(store, password_service)

        service.create_group('team', ['John', 'Alice'])

        # assert_that(store.save, called().with_args('Peter'))

    def test_account_already_exists(self):
        with Stub(PasswordService) as password_service:
            password_service.generate().returns('some')

        with ProxySpy(AccountStore()) as store:
            store.has_user('John').returns(True)

        service = AccountService(store, password_service)

        with self.assertRaises(AlreadyExists):
            service.create_user('John')

    def test_account_behaviour_with_mock(self):
        with Stub(PasswordService) as password_service:
            password_service.generate().returns('some')

        with Mock(AccountStore) as store:
            store.has_user('John')
            store.save('John', 'some')
            store.has_user('Peter')
            store.save('Peter', 'some')

        service = AccountService(store, password_service)

        service.create_group('team', ['John', 'Peter'])

        assert_that(store, verify())

#    def test_account_behaviour_with_mock_any_order(self):
#        with Stub(PasswordService) as password_service:
#            password_service.generate().returns('some')
#
#        with Mock(AccountStore) as store:
#            store.has_user('John')
#            store.has_user('Peter')
#            store.save('John', 'some')
#            store.save('Peter', 'some')
#
#        service = AccountService(store, password_service)
#
#        service.create_user('John')
#        service.create_user('Peter')
#
#        assert_that(store, any_order_verify())

    def test_stub_delegates(self):
        def get_pass():
            return "12345"

        with Stub(PasswordService) as password_service:
            password_service.generate().delegates(get_pass)

        store = Spy(AccountStore)
        service = AccountService(store, password_service)

        service.create_user('John')

        assert_that(store.save, called().with_args('John', '12345'))

    def test_stub_delegates_list(self):
        with Stub(PasswordService) as password_service:
            password_service.generate().delegates(["12345", "mypass", "nothing"])

        store = Spy(AccountStore)
        service = AccountService(store, password_service)

        service.create_group('team', ['John', 'Peter', 'Alice'])

        assert_that(store.save, called().with_args('John', '12345'))
        assert_that(store.save, called().with_args('Peter', 'mypass'))
        assert_that(store.save, called().with_args('Alice', 'nothing'))