Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder_app / tests.py @ 548938f6

History | View | Annotate | Download (10.6 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
    InvalidDataError,
41
    NoCommissionError,
42
    NoQuantityError,
43
    NoCapacityError,
44
    NoHoldingError,
45
    DuplicateError)
46

    
47

    
48
class QuotaholderTest(TestCase):
49

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

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

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

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

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

    
74
        # issuing commissions
75

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

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

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

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

    
105
        # commission exceptions
106

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

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

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

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

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

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

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

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

    
154
        with assertRaises(InvalidDataError):
155
            self.issue_commission([((holder, source, resource1), 1),
156
                                   ((holder, source, resource1), "nan")])
157

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

    
164
        # resolve commission
165

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

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

    
182
        # resolve commissions
183

    
184
        serial = self.issue_commission([((holder, source, resource1), 1),
185
                                        ((holder, source, resource2), -1)])
186

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

    
193
        r = qh.resolve_pending_commission(self.client, serial, accept=False)
194
        self.assertEqual(r, True)
195

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

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

    
202
        serial3 = self.issue_commission([((holder, source, resource1), 1),
203
                                         ((holder, source, resource2), -1)])
204

    
205
        r = qh.resolve_pending_commissions(clientkey=self.client,
206
                                           accept_set=[serial1, serial2, 0],
207
                                           reject_set=[serial2, serial3])
208
        self.assertEqual(r, ([serial1], [serial3], [0], [serial2]))
209

    
210
        r = qh.get_pending_commissions(clientkey=self.client)
211
        self.assertEqual(r, [serial2])
212

    
213
        # forced commission
214

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

    
222
        with assertRaises(NoCapacityError):
223
            self.issue_commission(
224
                [((holder, source, resource1), limit1/2+1)])
225

    
226
        serial = self.issue_commission(
227
            [((holder, source, resource1), limit1/2+1)],
228
            force=True)
229

    
230
        r = qh.resolve_pending_commission(self.client, serial, accept=True)
231
        self.assertEqual(r, True)
232

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

    
240
        with assertRaises(NoQuantityError):
241
            self.issue_commission(
242
                [((holder, source, resource1), -2*limit1)],
243
                force=True)
244

    
245
        # release off upper limit
246

    
247
        serial = self.issue_commission([((holder, source, resource1), -1)])
248
        r = qh.resolve_pending_commission(self.client, serial)
249
        self.assertEqual(r, True)
250

    
251
        r = qh.get_quota(holders=[holder], resources=[resource1])
252
        quotas = {
253
            (holder, source, resource1): (limit1, limit1+1, limit1+2),
254
        }
255
        self.assertEqual(r, quotas)
256

    
257
    def test_020_empty_provisions(self):
258
        serial = self.issue_commission([])
259
        r = qh.resolve_pending_commission(self.client, serial)
260
        self.assertEqual(r, True)
261

    
262
    def test_030_set(self):
263
        holder = 'h0'
264
        source = 'system'
265
        resource1 = 'r1'
266
        resource2 = 'r2'
267
        limit1 = 10
268
        limit2 = 20
269

    
270
        models.Holding.objects.create(
271
            holder=holder, source=source, resource=resource1,
272
            usage_min=1, usage_max=1, limit=2)
273
        models.Holding.objects.create(
274
            holder=holder, source=source, resource=resource2,
275
            usage_min=2, usage_max=2, limit=22)
276

    
277
        qh.set_quota([((holder, source, resource1), limit1),
278
                      ((holder, source, resource1), limit2)])
279

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