Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.6 kB)

1 0ee59d55 Giorgos Korfiatis
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2 0ee59d55 Giorgos Korfiatis
#
3 0ee59d55 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 0ee59d55 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 0ee59d55 Giorgos Korfiatis
# conditions are met:
6 0ee59d55 Giorgos Korfiatis
#
7 0ee59d55 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 0ee59d55 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 0ee59d55 Giorgos Korfiatis
#      disclaimer.
10 0ee59d55 Giorgos Korfiatis
#
11 0ee59d55 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 0ee59d55 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 0ee59d55 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 0ee59d55 Giorgos Korfiatis
#      provided with the distribution.
15 0ee59d55 Giorgos Korfiatis
#
16 0ee59d55 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 0ee59d55 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 0ee59d55 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 0ee59d55 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 0ee59d55 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 0ee59d55 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 0ee59d55 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 0ee59d55 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 0ee59d55 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 0ee59d55 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 0ee59d55 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 0ee59d55 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 0ee59d55 Giorgos Korfiatis
#
29 0ee59d55 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 0ee59d55 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 0ee59d55 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 0ee59d55 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 0ee59d55 Giorgos Korfiatis
34 7bed22fb Giorgos Korfiatis
from django.test import TestCase
35 0ee59d55 Giorgos Korfiatis
36 aba462a2 Giorgos Korfiatis
from snf_django.utils.testing import assertGreater, assertIn, assertRaises
37 20c6de35 Giorgos Korfiatis
from astakos.quotaholder_app import models
38 ec5e00aa Giorgos Korfiatis
import astakos.quotaholder_app.callpoint as qh
39 ec5e00aa Giorgos Korfiatis
from astakos.quotaholder_app.exception import (
40 0ee59d55 Giorgos Korfiatis
    InvalidDataError,
41 0ee59d55 Giorgos Korfiatis
    NoCommissionError,
42 0ee59d55 Giorgos Korfiatis
    NoQuantityError,
43 0ee59d55 Giorgos Korfiatis
    NoCapacityError,
44 0ee59d55 Giorgos Korfiatis
    NoHoldingError,
45 0ee59d55 Giorgos Korfiatis
    DuplicateError)
46 0ee59d55 Giorgos Korfiatis
47 0ee59d55 Giorgos Korfiatis
48 7bed22fb Giorgos Korfiatis
class QuotaholderTest(TestCase):
49 0ee59d55 Giorgos Korfiatis
50 f6e7a826 Giorgos Korfiatis
    def issue_commission(self, provisions, name="", force=False):
51 0ee59d55 Giorgos Korfiatis
        return qh.issue_commission(clientkey=self.client,
52 0ee59d55 Giorgos Korfiatis
                                   name=name,
53 0ee59d55 Giorgos Korfiatis
                                   force=force,
54 0ee59d55 Giorgos Korfiatis
                                   provisions=provisions)
55 0ee59d55 Giorgos Korfiatis
56 7bed22fb Giorgos Korfiatis
    def test_010_qh(self):
57 7bed22fb Giorgos Korfiatis
        holder = 'h0'
58 7bed22fb Giorgos Korfiatis
        source = 'system'
59 7bed22fb Giorgos Korfiatis
        resource1 = 'r1'
60 7bed22fb Giorgos Korfiatis
        resource2 = 'r2'
61 7bed22fb Giorgos Korfiatis
        limit1 = 10
62 7bed22fb Giorgos Korfiatis
        limit2 = 20
63 7bed22fb Giorgos Korfiatis
64 7bed22fb Giorgos Korfiatis
        qh.set_quota([((holder, source, resource1), limit1),
65 7bed22fb Giorgos Korfiatis
                      ((holder, source, resource1), limit2)])
66 7bed22fb Giorgos Korfiatis
67 7bed22fb Giorgos Korfiatis
        r = qh.get_quota(holders=[holder], sources=[source],
68 7bed22fb Giorgos Korfiatis
                         resources=[resource1, resource2])
69 7bed22fb Giorgos Korfiatis
        self.assertEqual(r, {(holder, source, resource1): (limit2, 0, 0)})
70 7bed22fb Giorgos Korfiatis
71 7bed22fb Giorgos Korfiatis
        r = qh.get_quota()
72 7bed22fb Giorgos Korfiatis
        self.assertEqual(r, {(holder, source, resource1): (limit2, 0, 0)})
73 7bed22fb Giorgos Korfiatis
74 01b8fb9a Giorgos Korfiatis
        # issuing commissions
75 0ee59d55 Giorgos Korfiatis
76 0ee59d55 Giorgos Korfiatis
        qh.set_quota([((holder, source, resource1), limit1),
77 0ee59d55 Giorgos Korfiatis
                      ((holder, source, resource2), limit2)])
78 0ee59d55 Giorgos Korfiatis
79 0ee59d55 Giorgos Korfiatis
        s1 = self.issue_commission([((holder, source, resource1), limit1/2),
80 0ee59d55 Giorgos Korfiatis
                                    ((holder, source, resource2), limit2)],
81 0ee59d55 Giorgos Korfiatis
                                   name="initial")
82 aba462a2 Giorgos Korfiatis
        assertGreater(s1, 0)
83 0ee59d55 Giorgos Korfiatis
84 0ee59d55 Giorgos Korfiatis
        r = qh.get_commission(self.client, s1)
85 0ee59d55 Giorgos Korfiatis
        provisions = [
86 0ee59d55 Giorgos Korfiatis
            {'holder': holder,
87 0ee59d55 Giorgos Korfiatis
             'source': source,
88 0ee59d55 Giorgos Korfiatis
             'resource': resource1,
89 0ee59d55 Giorgos Korfiatis
             'quantity': limit1/2,
90 0ee59d55 Giorgos Korfiatis
             },
91 0ee59d55 Giorgos Korfiatis
            {'holder': holder,
92 0ee59d55 Giorgos Korfiatis
             'source': source,
93 0ee59d55 Giorgos Korfiatis
             'resource': resource2,
94 0ee59d55 Giorgos Korfiatis
             'quantity': limit2,
95 0ee59d55 Giorgos Korfiatis
             }
96 0ee59d55 Giorgos Korfiatis
        ]
97 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r['serial'], s1)
98 0ee59d55 Giorgos Korfiatis
        ps = r['provisions']
99 0ee59d55 Giorgos Korfiatis
        for p in ps:
100 aba462a2 Giorgos Korfiatis
            assertIn(p, provisions)
101 0ee59d55 Giorgos Korfiatis
102 aba462a2 Giorgos Korfiatis
        with assertRaises(NoCommissionError):
103 0ee59d55 Giorgos Korfiatis
            qh.get_commission(self.client, s1+1)
104 0ee59d55 Giorgos Korfiatis
105 7bed22fb Giorgos Korfiatis
        # commission exceptions
106 0ee59d55 Giorgos Korfiatis
107 aba462a2 Giorgos Korfiatis
        with assertRaises(NoCapacityError) as cm:
108 0ee59d55 Giorgos Korfiatis
            self.issue_commission([((holder, source, resource1), 1),
109 0ee59d55 Giorgos Korfiatis
                                   ((holder, source, resource2), 1)])
110 0ee59d55 Giorgos Korfiatis
111 0ee59d55 Giorgos Korfiatis
        e = cm.exception
112 0ee59d55 Giorgos Korfiatis
        provision = e.data['provision']
113 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['holder'], holder)
114 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['source'], source)
115 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['resource'], resource2)
116 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['quantity'], 1)
117 0ee59d55 Giorgos Korfiatis
        self.assertEqual(e.data['usage'], limit2)
118 0ee59d55 Giorgos Korfiatis
        self.assertEqual(e.data['limit'], limit2)
119 0ee59d55 Giorgos Korfiatis
120 aba462a2 Giorgos Korfiatis
        with assertRaises(NoQuantityError) as cm:
121 0ee59d55 Giorgos Korfiatis
            self.issue_commission([((holder, source, resource1), -1)])
122 0ee59d55 Giorgos Korfiatis
123 0ee59d55 Giorgos Korfiatis
        e = cm.exception
124 0ee59d55 Giorgos Korfiatis
        provision = e.data['provision']
125 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['holder'], holder)
126 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['source'], source)
127 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['resource'], resource1)
128 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['quantity'], -1)
129 3a8e56bc Giorgos Korfiatis
        self.assertEqual(e.data['usage'], 0)
130 3a8e56bc Giorgos Korfiatis
        self.assertEqual(e.data['limit'], 0)
131 0ee59d55 Giorgos Korfiatis
132 aba462a2 Giorgos Korfiatis
        with assertRaises(NoHoldingError) as cm:
133 0ee59d55 Giorgos Korfiatis
            self.issue_commission([((holder, source, resource1), 1),
134 0ee59d55 Giorgos Korfiatis
                                   (('nonex', source, resource1), 1)])
135 0ee59d55 Giorgos Korfiatis
136 0ee59d55 Giorgos Korfiatis
        e = cm.exception
137 0ee59d55 Giorgos Korfiatis
        provision = e.data['provision']
138 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['holder'], 'nonex')
139 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['source'], source)
140 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['resource'], resource1)
141 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['quantity'], 1)
142 0ee59d55 Giorgos Korfiatis
143 aba462a2 Giorgos Korfiatis
        with assertRaises(DuplicateError) as cm:
144 0ee59d55 Giorgos Korfiatis
            self.issue_commission([((holder, source, resource1), 1),
145 0ee59d55 Giorgos Korfiatis
                                   ((holder, source, resource1), 2)])
146 0ee59d55 Giorgos Korfiatis
147 0ee59d55 Giorgos Korfiatis
        e = cm.exception
148 0ee59d55 Giorgos Korfiatis
        provision = e.data['provision']
149 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['holder'], holder)
150 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['source'], source)
151 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['resource'], resource1)
152 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['quantity'], 2)
153 0ee59d55 Giorgos Korfiatis
154 aba462a2 Giorgos Korfiatis
        with assertRaises(InvalidDataError):
155 0ee59d55 Giorgos Korfiatis
            self.issue_commission([((holder, source, resource1), 1),
156 0ee59d55 Giorgos Korfiatis
                                   ((holder, source, resource1), "nan")])
157 0ee59d55 Giorgos Korfiatis
158 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
159 0ee59d55 Giorgos Korfiatis
        quotas = {(holder, source, resource1): (limit1, 0, limit1/2),
160 0ee59d55 Giorgos Korfiatis
                  (holder, source, resource2): (limit2, 0, limit2),
161 0ee59d55 Giorgos Korfiatis
                  }
162 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
163 0ee59d55 Giorgos Korfiatis
164 7bed22fb Giorgos Korfiatis
        # resolve commission
165 0ee59d55 Giorgos Korfiatis
166 0ee59d55 Giorgos Korfiatis
        r = qh.get_pending_commissions(clientkey=self.client)
167 0ee59d55 Giorgos Korfiatis
        self.assertEqual(len(r), 1)
168 0ee59d55 Giorgos Korfiatis
        serial = r[0]
169 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial)
170 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, True)
171 0ee59d55 Giorgos Korfiatis
        r = qh.get_pending_commissions(clientkey=self.client)
172 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, [])
173 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial)
174 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, False)
175 0ee59d55 Giorgos Korfiatis
176 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
177 0ee59d55 Giorgos Korfiatis
        quotas = {(holder, source, resource1): (limit1, limit1/2, limit1/2),
178 0ee59d55 Giorgos Korfiatis
                  (holder, source, resource2): (limit2, limit2, limit2),
179 0ee59d55 Giorgos Korfiatis
                  }
180 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
181 0ee59d55 Giorgos Korfiatis
182 7bed22fb Giorgos Korfiatis
        # resolve commissions
183 0ee59d55 Giorgos Korfiatis
184 0ee59d55 Giorgos Korfiatis
        serial = self.issue_commission([((holder, source, resource1), 1),
185 0ee59d55 Giorgos Korfiatis
                                        ((holder, source, resource2), -1)])
186 0ee59d55 Giorgos Korfiatis
187 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
188 0ee59d55 Giorgos Korfiatis
        quotas = {(holder, source, resource1): (limit1, limit1/2, limit1/2+1),
189 0ee59d55 Giorgos Korfiatis
                  (holder, source, resource2): (limit2, limit2-1, limit2),
190 0ee59d55 Giorgos Korfiatis
                  }
191 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
192 0ee59d55 Giorgos Korfiatis
193 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial, accept=False)
194 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, True)
195 0ee59d55 Giorgos Korfiatis
196 0ee59d55 Giorgos Korfiatis
        serial1 = self.issue_commission([((holder, source, resource1), 1),
197 0ee59d55 Giorgos Korfiatis
                                         ((holder, source, resource2), -1)])
198 0ee59d55 Giorgos Korfiatis
199 0ee59d55 Giorgos Korfiatis
        serial2 = self.issue_commission([((holder, source, resource1), 1),
200 0ee59d55 Giorgos Korfiatis
                                         ((holder, source, resource2), -1)])
201 0ee59d55 Giorgos Korfiatis
202 0ee59d55 Giorgos Korfiatis
        serial3 = self.issue_commission([((holder, source, resource1), 1),
203 0ee59d55 Giorgos Korfiatis
                                         ((holder, source, resource2), -1)])
204 0ee59d55 Giorgos Korfiatis
205 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commissions(clientkey=self.client,
206 0ee59d55 Giorgos Korfiatis
                                           accept_set=[serial1, serial2, 0],
207 0ee59d55 Giorgos Korfiatis
                                           reject_set=[serial2, serial3])
208 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, ([serial1], [serial3], [0], [serial2]))
209 0ee59d55 Giorgos Korfiatis
210 0ee59d55 Giorgos Korfiatis
        r = qh.get_pending_commissions(clientkey=self.client)
211 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, [serial2])
212 0ee59d55 Giorgos Korfiatis
213 7bed22fb Giorgos Korfiatis
        # forced commission
214 0ee59d55 Giorgos Korfiatis
215 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
216 0ee59d55 Giorgos Korfiatis
        quotas = {
217 0ee59d55 Giorgos Korfiatis
            (holder, source, resource1): (limit1, limit1/2+1, limit1/2+2),
218 0ee59d55 Giorgos Korfiatis
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
219 0ee59d55 Giorgos Korfiatis
        }
220 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
221 0ee59d55 Giorgos Korfiatis
222 aba462a2 Giorgos Korfiatis
        with assertRaises(NoCapacityError):
223 0ee59d55 Giorgos Korfiatis
            self.issue_commission(
224 0ee59d55 Giorgos Korfiatis
                [((holder, source, resource1), limit1/2+1)])
225 0ee59d55 Giorgos Korfiatis
226 0ee59d55 Giorgos Korfiatis
        serial = self.issue_commission(
227 0ee59d55 Giorgos Korfiatis
            [((holder, source, resource1), limit1/2+1)],
228 0ee59d55 Giorgos Korfiatis
            force=True)
229 0ee59d55 Giorgos Korfiatis
230 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial, accept=True)
231 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, True)
232 0ee59d55 Giorgos Korfiatis
233 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
234 0ee59d55 Giorgos Korfiatis
        quotas = {
235 0ee59d55 Giorgos Korfiatis
            (holder, source, resource1): (limit1, limit1+2, limit1+3),
236 0ee59d55 Giorgos Korfiatis
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
237 0ee59d55 Giorgos Korfiatis
        }
238 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
239 0ee59d55 Giorgos Korfiatis
240 01b8fb9a Giorgos Korfiatis
        with assertRaises(NoQuantityError):
241 01b8fb9a Giorgos Korfiatis
            self.issue_commission(
242 01b8fb9a Giorgos Korfiatis
                [((holder, source, resource1), -2*limit1)],
243 01b8fb9a Giorgos Korfiatis
                force=True)
244 01b8fb9a Giorgos Korfiatis
245 7bed22fb Giorgos Korfiatis
        # release off upper limit
246 0ee59d55 Giorgos Korfiatis
247 0ee59d55 Giorgos Korfiatis
        serial = self.issue_commission([((holder, source, resource1), -1)])
248 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial)
249 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, True)
250 0ee59d55 Giorgos Korfiatis
251 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder], resources=[resource1])
252 0ee59d55 Giorgos Korfiatis
        quotas = {
253 0ee59d55 Giorgos Korfiatis
            (holder, source, resource1): (limit1, limit1+1, limit1+2),
254 0ee59d55 Giorgos Korfiatis
        }
255 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
256 0ee59d55 Giorgos Korfiatis
257 7585a768 Giorgos Korfiatis
    def test_020_empty_provisions(self):
258 7585a768 Giorgos Korfiatis
        serial = self.issue_commission([])
259 7585a768 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial)
260 7585a768 Giorgos Korfiatis
        self.assertEqual(r, True)
261 20c6de35 Giorgos Korfiatis
262 20c6de35 Giorgos Korfiatis
    def test_030_set(self):
263 20c6de35 Giorgos Korfiatis
        holder = 'h0'
264 20c6de35 Giorgos Korfiatis
        source = 'system'
265 20c6de35 Giorgos Korfiatis
        resource1 = 'r1'
266 20c6de35 Giorgos Korfiatis
        resource2 = 'r2'
267 20c6de35 Giorgos Korfiatis
        limit1 = 10
268 20c6de35 Giorgos Korfiatis
        limit2 = 20
269 20c6de35 Giorgos Korfiatis
270 20c6de35 Giorgos Korfiatis
        models.Holding.objects.create(
271 20c6de35 Giorgos Korfiatis
            holder=holder, source=source, resource=resource1,
272 20c6de35 Giorgos Korfiatis
            usage_min=1, usage_max=1, limit=2)
273 20c6de35 Giorgos Korfiatis
        models.Holding.objects.create(
274 20c6de35 Giorgos Korfiatis
            holder=holder, source=source, resource=resource2,
275 20c6de35 Giorgos Korfiatis
            usage_min=2, usage_max=2, limit=22)
276 20c6de35 Giorgos Korfiatis
277 20c6de35 Giorgos Korfiatis
        qh.set_quota([((holder, source, resource1), limit1),
278 20c6de35 Giorgos Korfiatis
                      ((holder, source, resource1), limit2)])
279 20c6de35 Giorgos Korfiatis
280 20c6de35 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
281 20c6de35 Giorgos Korfiatis
        self.assertEqual(r, {(holder, source, resource1): (limit2, 1, 1),
282 20c6de35 Giorgos Korfiatis
                             (holder, source, resource2): (22, 2, 2)})