Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder_app / tests.py @ fe4000a3

History | View | Annotate | Download (10.4 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
from snf_django.utils.testing import assertGreater, assertIn, assertRaises
37
from astakos.quotaholder_app import models
38
import astakos.quotaholder_app.callpoint as qh
39
from astakos.quotaholder_app.exception import (
40
    NoCommissionError,
41
    NoQuantityError,
42
    NoCapacityError,
43
    NoHoldingError,
44
    DuplicateError)
45

    
46

    
47
class QuotaholderTest(TestCase):
48

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

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

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

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

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

    
73
        # issuing commissions
74

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

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

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

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

    
104
        # commission exceptions
105

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

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

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

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

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

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

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

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

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

    
159
        # resolve commission
160

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

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

    
177
        # resolve commissions
178

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

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

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

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

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

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

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

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

    
208
        # forced commission
209

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

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

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

    
225
        r = qh.resolve_pending_commission(self.client, serial, accept=True)
226
        self.assertEqual(r, True)
227

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

    
235
        with assertRaises(NoQuantityError):
236
            self.issue_commission(
237
                [((holder, source, resource1), -2*limit1)],
238
                force=True)
239

    
240
        # release off upper limit
241

    
242
        serial = self.issue_commission([((holder, source, resource1), -1)])
243
        r = qh.resolve_pending_commission(self.client, serial)
244
        self.assertEqual(r, True)
245

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

    
252
    def test_020_empty_provisions(self):
253
        serial = self.issue_commission([])
254
        r = qh.resolve_pending_commission(self.client, serial)
255
        self.assertEqual(r, True)
256

    
257
    def test_030_set(self):
258
        holder = 'h0'
259
        source = 'system'
260
        resource1 = 'r1'
261
        resource2 = 'r2'
262
        limit1 = 10
263
        limit2 = 20
264

    
265
        models.Holding.objects.create(
266
            holder=holder, source=source, resource=resource1,
267
            usage_min=1, usage_max=1, limit=2)
268
        models.Holding.objects.create(
269
            holder=holder, source=source, resource=resource2,
270
            usage_min=2, usage_max=2, limit=22)
271

    
272
        qh.set_quota([((holder, source, resource1), limit1),
273
                      ((holder, source, resource1), limit2)])
274

    
275
        r = qh.get_quota(holders=[holder])
276
        self.assertEqual(r, {(holder, source, resource1): (limit2, 1, 1),
277
                             (holder, source, resource2): (22, 2, 2)})