Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder_app / tests.py @ 7585a768

History | View | Annotate | Download (10.2 kB)

1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from django.test import TestCase
35

    
36
import astakos.quotaholder_app.callpoint as qh
37
from astakos.quotaholder_app.exception import (
38
    InvalidDataError,
39
    NoCommissionError,
40
    NoQuantityError,
41
    NoCapacityError,
42
    NoHoldingError,
43
    DuplicateError)
44

    
45

    
46
class QuotaholderTest(TestCase):
47

    
48
    def issue_commission(self, provisions, name="", force=False):
49
        return qh.issue_commission(clientkey=self.client,
50
                                   name=name,
51
                                   force=force,
52
                                   provisions=provisions)
53

    
54
    def test_010_qh(self):
55
        holder = 'h0'
56
        source = 'system'
57
        resource1 = 'r1'
58
        resource2 = 'r2'
59
        limit1 = 10
60
        limit2 = 20
61

    
62
        qh.set_quota([((holder, source, resource1), limit1),
63
                      ((holder, source, resource1), limit2)])
64

    
65
        r = qh.get_quota(holders=[holder], sources=[source],
66
                         resources=[resource1, resource2])
67
        self.assertEqual(r, {(holder, source, resource1): (limit2, 0, 0)})
68

    
69
        r = qh.get_quota()
70
        self.assertEqual(r, {(holder, source, resource1): (limit2, 0, 0)})
71

    
72
        # issueing commissions
73

    
74
        qh.set_quota([((holder, source, resource1), limit1),
75
                      ((holder, source, resource2), limit2)])
76

    
77
        s1 = self.issue_commission([((holder, source, resource1), limit1/2),
78
                                    ((holder, source, resource2), limit2)],
79
                                   name="initial")
80
        self.assertGreater(s1, 0)
81

    
82
        r = qh.get_commission(self.client, s1)
83
        provisions = [
84
            {'holder': holder,
85
             'source': source,
86
             'resource': resource1,
87
             'quantity': limit1/2,
88
             },
89
            {'holder': holder,
90
             'source': source,
91
             'resource': resource2,
92
             'quantity': limit2,
93
             }
94
        ]
95
        self.assertEqual(r['serial'], s1)
96
        ps = r['provisions']
97
        for p in ps:
98
            self.assertIn(p, provisions)
99

    
100
        with self.assertRaises(NoCommissionError):
101
            qh.get_commission(self.client, s1+1)
102

    
103
        # commission exceptions
104

    
105
        with self.assertRaises(NoCapacityError) as cm:
106
            self.issue_commission([((holder, source, resource1), 1),
107
                                   ((holder, source, resource2), 1)])
108

    
109
        e = cm.exception
110
        provision = e.data['provision']
111
        self.assertEqual(provision['holder'], holder)
112
        self.assertEqual(provision['source'], source)
113
        self.assertEqual(provision['resource'], resource2)
114
        self.assertEqual(provision['quantity'], 1)
115
        self.assertEqual(e.data['usage'], limit2)
116
        self.assertEqual(e.data['limit'], limit2)
117

    
118
        with self.assertRaises(NoQuantityError) as cm:
119
            self.issue_commission([((holder, source, resource1), -1)])
120

    
121
        e = cm.exception
122
        provision = e.data['provision']
123
        self.assertEqual(provision['holder'], holder)
124
        self.assertEqual(provision['source'], source)
125
        self.assertEqual(provision['resource'], resource1)
126
        self.assertEqual(provision['quantity'], -1)
127
        self.assertEqual(e.data['available'], 0)
128

    
129
        with self.assertRaises(NoHoldingError) as cm:
130
            self.issue_commission([((holder, source, resource1), 1),
131
                                   (('nonex', source, resource1), 1)])
132

    
133
        e = cm.exception
134
        provision = e.data['provision']
135
        self.assertEqual(provision['holder'], 'nonex')
136
        self.assertEqual(provision['source'], source)
137
        self.assertEqual(provision['resource'], resource1)
138
        self.assertEqual(provision['quantity'], 1)
139

    
140
        with self.assertRaises(DuplicateError) as cm:
141
            self.issue_commission([((holder, source, resource1), 1),
142
                                   ((holder, source, resource1), 2)])
143

    
144
        e = cm.exception
145
        provision = e.data['provision']
146
        self.assertEqual(provision['holder'], holder)
147
        self.assertEqual(provision['source'], source)
148
        self.assertEqual(provision['resource'], resource1)
149
        self.assertEqual(provision['quantity'], 2)
150

    
151
        with self.assertRaises(InvalidDataError):
152
            self.issue_commission([((holder, source, resource1), 1),
153
                                   ((holder, source, resource1), "nan")])
154

    
155
        r = qh.get_quota(holders=[holder])
156
        quotas = {(holder, source, resource1): (limit1, 0, limit1/2),
157
                  (holder, source, resource2): (limit2, 0, limit2),
158
                  }
159
        self.assertEqual(r, quotas)
160

    
161
        # resolve commission
162

    
163
        r = qh.get_pending_commissions(clientkey=self.client)
164
        self.assertEqual(len(r), 1)
165
        serial = r[0]
166
        r = qh.resolve_pending_commission(self.client, serial)
167
        self.assertEqual(r, True)
168
        r = qh.get_pending_commissions(clientkey=self.client)
169
        self.assertEqual(r, [])
170
        r = qh.resolve_pending_commission(self.client, serial)
171
        self.assertEqual(r, False)
172

    
173
        r = qh.get_quota(holders=[holder])
174
        quotas = {(holder, source, resource1): (limit1, limit1/2, limit1/2),
175
                  (holder, source, resource2): (limit2, limit2, limit2),
176
                  }
177
        self.assertEqual(r, quotas)
178

    
179
        # resolve commissions
180

    
181
        serial = self.issue_commission([((holder, source, resource1), 1),
182
                                        ((holder, source, resource2), -1)])
183

    
184
        r = qh.get_quota(holders=[holder])
185
        quotas = {(holder, source, resource1): (limit1, limit1/2, limit1/2+1),
186
                  (holder, source, resource2): (limit2, limit2-1, limit2),
187
                  }
188
        self.assertEqual(r, quotas)
189

    
190
        r = qh.resolve_pending_commission(self.client, serial, accept=False)
191
        self.assertEqual(r, True)
192

    
193
        serial1 = self.issue_commission([((holder, source, resource1), 1),
194
                                         ((holder, source, resource2), -1)])
195

    
196
        serial2 = self.issue_commission([((holder, source, resource1), 1),
197
                                         ((holder, source, resource2), -1)])
198

    
199
        serial3 = self.issue_commission([((holder, source, resource1), 1),
200
                                         ((holder, source, resource2), -1)])
201

    
202
        r = qh.resolve_pending_commissions(clientkey=self.client,
203
                                           accept_set=[serial1, serial2, 0],
204
                                           reject_set=[serial2, serial3])
205
        self.assertEqual(r, ([serial1], [serial3], [0], [serial2]))
206

    
207
        r = qh.get_pending_commissions(clientkey=self.client)
208
        self.assertEqual(r, [serial2])
209

    
210
        # forced commission
211

    
212
        r = qh.get_quota(holders=[holder])
213
        quotas = {
214
            (holder, source, resource1): (limit1, limit1/2+1, limit1/2+2),
215
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
216
        }
217
        self.assertEqual(r, quotas)
218

    
219
        with self.assertRaises(NoCapacityError):
220
            self.issue_commission(
221
                [((holder, source, resource1), limit1/2+1)])
222

    
223
        serial = self.issue_commission(
224
            [((holder, source, resource1), limit1/2+1)],
225
            force=True)
226

    
227
        r = qh.resolve_pending_commission(self.client, serial, accept=True)
228
        self.assertEqual(r, True)
229

    
230
        r = qh.get_quota(holders=[holder])
231
        quotas = {
232
            (holder, source, resource1): (limit1, limit1+2, limit1+3),
233
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
234
        }
235
        self.assertEqual(r, quotas)
236

    
237
        # release off upper limit
238

    
239
        serial = self.issue_commission([((holder, source, resource1), -1)])
240
        r = qh.resolve_pending_commission(self.client, serial)
241
        self.assertEqual(r, True)
242

    
243
        r = qh.get_quota(holders=[holder], resources=[resource1])
244
        quotas = {
245
            (holder, source, resource1): (limit1, limit1+1, limit1+2),
246
        }
247
        self.assertEqual(r, quotas)
248

    
249
        # add resource limit
250

    
251
        qh.add_resource_limit(sources=[source], resources=[resource1], diff=1)
252
        r = qh.get_quota(holders=[holder])
253
        quotas = {
254
            (holder, source, resource1): (limit1+1, limit1+1, limit1+2),
255
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
256
        }
257
        self.assertEqual(r, quotas)
258

    
259
        qh.add_resource_limit(holders=[holder, "nonex"], diff=10)
260
        r = qh.get_quota(holders=[holder, "nonex"])
261
        quotas = {
262
            (holder, source, resource1): (limit1+11, limit1+1, limit1+2),
263
            (holder, source, resource2): (limit2+10, limit2-2, limit2-1),
264
        }
265
        self.assertEqual(r, quotas)
266

    
267
    def test_020_empty_provisions(self):
268
        serial = self.issue_commission([])
269
        r = qh.resolve_pending_commission(self.client, serial)
270
        self.assertEqual(r, True)