Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.9 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
)
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
        r = qh.get_quota(holders=[holder])
143
        quotas = {(holder, source, resource1): (limit1, 0, limit1/2),
144
                  (holder, source, resource2): (limit2, 0, limit2),
145
                  }
146
        self.assertEqual(r, quotas)
147

    
148
        # resolve commission
149

    
150
        r = qh.get_pending_commissions(clientkey=self.client)
151
        self.assertEqual(len(r), 1)
152
        serial = r[0]
153
        r = qh.resolve_pending_commission(self.client, serial)
154
        self.assertEqual(r, True)
155
        r = qh.get_pending_commissions(clientkey=self.client)
156
        self.assertEqual(r, [])
157
        r = qh.resolve_pending_commission(self.client, serial)
158
        self.assertEqual(r, False)
159

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

    
166
        # resolve commissions
167

    
168
        serial = self.issue_commission([((holder, source, resource1), 1),
169
                                        ((holder, source, resource2), -1)])
170

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

    
177
        r = qh.resolve_pending_commission(self.client, serial, accept=False)
178
        self.assertEqual(r, True)
179

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

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

    
186
        serial3 = self.issue_commission([((holder, source, resource1), 1),
187
                                         ((holder, source, resource2), -1)])
188

    
189
        r = qh.resolve_pending_commissions(clientkey=self.client,
190
                                           accept_set=[serial1, serial2, 0],
191
                                           reject_set=[serial2, serial3])
192
        self.assertEqual(r, ([serial1], [serial3], [0], [serial2]))
193

    
194
        r = qh.get_pending_commissions(clientkey=self.client)
195
        self.assertEqual(r, [serial2])
196

    
197
        # forced commission
198

    
199
        r = qh.get_quota(holders=[holder])
200
        quotas = {
201
            (holder, source, resource1): (limit1, limit1/2+1, limit1/2+2),
202
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
203
        }
204
        self.assertEqual(r, quotas)
205

    
206
        with assertRaises(NoCapacityError):
207
            self.issue_commission(
208
                [((holder, source, resource1), limit1/2+1)])
209

    
210
        serial = self.issue_commission(
211
            [((holder, source, resource1), limit1/2+1)],
212
            force=True)
213

    
214
        r = qh.resolve_pending_commission(self.client, serial, accept=True)
215
        self.assertEqual(r, True)
216

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

    
224
        with assertRaises(NoQuantityError):
225
            self.issue_commission(
226
                [((holder, source, resource1), -2*limit1)],
227
                force=True)
228

    
229
        # release off upper limit
230

    
231
        serial = self.issue_commission([((holder, source, resource1), -1)])
232
        r = qh.resolve_pending_commission(self.client, serial)
233
        self.assertEqual(r, True)
234

    
235
        r = qh.get_quota(holders=[holder], resources=[resource1])
236
        quotas = {
237
            (holder, source, resource1): (limit1, limit1+1, limit1+2),
238
        }
239
        self.assertEqual(r, quotas)
240

    
241
    def test_020_empty_provisions(self):
242
        serial = self.issue_commission([])
243
        r = qh.resolve_pending_commission(self.client, serial)
244
        self.assertEqual(r, True)
245

    
246
    def test_030_set(self):
247
        holder = 'h0'
248
        source = 'system'
249
        resource1 = 'r1'
250
        resource2 = 'r2'
251
        limit1 = 10
252
        limit2 = 20
253

    
254
        models.Holding.objects.create(
255
            holder=holder, source=source, resource=resource1,
256
            usage_min=1, usage_max=1, limit=2)
257
        models.Holding.objects.create(
258
            holder=holder, source=source, resource=resource2,
259
            usage_min=2, usage_max=2, limit=22)
260

    
261
        qh.set_quota([((holder, source, resource1), limit1),
262
                      ((holder, source, resource1), limit2)])
263

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