Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.9 kB)

1 ff5edb80 Giorgos Korfiatis
# Copyright 2012-2014 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
    NoCommissionError,
41 0ee59d55 Giorgos Korfiatis
    NoQuantityError,
42 0ee59d55 Giorgos Korfiatis
    NoCapacityError,
43 0ee59d55 Giorgos Korfiatis
    NoHoldingError,
44 daa780fa Giorgos Korfiatis
)
45 0ee59d55 Giorgos Korfiatis
46 0ee59d55 Giorgos Korfiatis
47 7bed22fb Giorgos Korfiatis
class QuotaholderTest(TestCase):
48 0ee59d55 Giorgos Korfiatis
49 f6e7a826 Giorgos Korfiatis
    def issue_commission(self, provisions, name="", force=False):
50 0ee59d55 Giorgos Korfiatis
        return qh.issue_commission(clientkey=self.client,
51 0ee59d55 Giorgos Korfiatis
                                   name=name,
52 0ee59d55 Giorgos Korfiatis
                                   force=force,
53 0ee59d55 Giorgos Korfiatis
                                   provisions=provisions)
54 0ee59d55 Giorgos Korfiatis
55 7bed22fb Giorgos Korfiatis
    def test_010_qh(self):
56 7bed22fb Giorgos Korfiatis
        holder = 'h0'
57 7bed22fb Giorgos Korfiatis
        source = 'system'
58 7bed22fb Giorgos Korfiatis
        resource1 = 'r1'
59 7bed22fb Giorgos Korfiatis
        resource2 = 'r2'
60 7bed22fb Giorgos Korfiatis
        limit1 = 10
61 7bed22fb Giorgos Korfiatis
        limit2 = 20
62 7bed22fb Giorgos Korfiatis
63 7bed22fb Giorgos Korfiatis
        qh.set_quota([((holder, source, resource1), limit1),
64 7bed22fb Giorgos Korfiatis
                      ((holder, source, resource1), limit2)])
65 7bed22fb Giorgos Korfiatis
66 7bed22fb Giorgos Korfiatis
        r = qh.get_quota(holders=[holder], sources=[source],
67 7bed22fb Giorgos Korfiatis
                         resources=[resource1, resource2])
68 7bed22fb Giorgos Korfiatis
        self.assertEqual(r, {(holder, source, resource1): (limit2, 0, 0)})
69 7bed22fb Giorgos Korfiatis
70 7bed22fb Giorgos Korfiatis
        r = qh.get_quota()
71 7bed22fb Giorgos Korfiatis
        self.assertEqual(r, {(holder, source, resource1): (limit2, 0, 0)})
72 7bed22fb Giorgos Korfiatis
73 01b8fb9a Giorgos Korfiatis
        # issuing commissions
74 0ee59d55 Giorgos Korfiatis
75 0ee59d55 Giorgos Korfiatis
        qh.set_quota([((holder, source, resource1), limit1),
76 0ee59d55 Giorgos Korfiatis
                      ((holder, source, resource2), limit2)])
77 0ee59d55 Giorgos Korfiatis
78 0ee59d55 Giorgos Korfiatis
        s1 = self.issue_commission([((holder, source, resource1), limit1/2),
79 0ee59d55 Giorgos Korfiatis
                                    ((holder, source, resource2), limit2)],
80 0ee59d55 Giorgos Korfiatis
                                   name="initial")
81 aba462a2 Giorgos Korfiatis
        assertGreater(s1, 0)
82 0ee59d55 Giorgos Korfiatis
83 0ee59d55 Giorgos Korfiatis
        r = qh.get_commission(self.client, s1)
84 0ee59d55 Giorgos Korfiatis
        provisions = [
85 0ee59d55 Giorgos Korfiatis
            {'holder': holder,
86 0ee59d55 Giorgos Korfiatis
             'source': source,
87 0ee59d55 Giorgos Korfiatis
             'resource': resource1,
88 0ee59d55 Giorgos Korfiatis
             'quantity': limit1/2,
89 0ee59d55 Giorgos Korfiatis
             },
90 0ee59d55 Giorgos Korfiatis
            {'holder': holder,
91 0ee59d55 Giorgos Korfiatis
             'source': source,
92 0ee59d55 Giorgos Korfiatis
             'resource': resource2,
93 0ee59d55 Giorgos Korfiatis
             'quantity': limit2,
94 0ee59d55 Giorgos Korfiatis
             }
95 0ee59d55 Giorgos Korfiatis
        ]
96 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r['serial'], s1)
97 0ee59d55 Giorgos Korfiatis
        ps = r['provisions']
98 0ee59d55 Giorgos Korfiatis
        for p in ps:
99 aba462a2 Giorgos Korfiatis
            assertIn(p, provisions)
100 0ee59d55 Giorgos Korfiatis
101 aba462a2 Giorgos Korfiatis
        with assertRaises(NoCommissionError):
102 0ee59d55 Giorgos Korfiatis
            qh.get_commission(self.client, s1+1)
103 0ee59d55 Giorgos Korfiatis
104 7bed22fb Giorgos Korfiatis
        # commission exceptions
105 0ee59d55 Giorgos Korfiatis
106 aba462a2 Giorgos Korfiatis
        with assertRaises(NoCapacityError) as cm:
107 0ee59d55 Giorgos Korfiatis
            self.issue_commission([((holder, source, resource1), 1),
108 0ee59d55 Giorgos Korfiatis
                                   ((holder, source, resource2), 1)])
109 0ee59d55 Giorgos Korfiatis
110 0ee59d55 Giorgos Korfiatis
        e = cm.exception
111 0ee59d55 Giorgos Korfiatis
        provision = e.data['provision']
112 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['holder'], holder)
113 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['source'], source)
114 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['resource'], resource2)
115 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['quantity'], 1)
116 0ee59d55 Giorgos Korfiatis
        self.assertEqual(e.data['usage'], limit2)
117 0ee59d55 Giorgos Korfiatis
        self.assertEqual(e.data['limit'], limit2)
118 0ee59d55 Giorgos Korfiatis
119 aba462a2 Giorgos Korfiatis
        with assertRaises(NoQuantityError) as cm:
120 0ee59d55 Giorgos Korfiatis
            self.issue_commission([((holder, source, resource1), -1)])
121 0ee59d55 Giorgos Korfiatis
122 0ee59d55 Giorgos Korfiatis
        e = cm.exception
123 0ee59d55 Giorgos Korfiatis
        provision = e.data['provision']
124 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['holder'], holder)
125 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['source'], source)
126 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['resource'], resource1)
127 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['quantity'], -1)
128 3a8e56bc Giorgos Korfiatis
        self.assertEqual(e.data['usage'], 0)
129 3a8e56bc Giorgos Korfiatis
        self.assertEqual(e.data['limit'], 0)
130 0ee59d55 Giorgos Korfiatis
131 aba462a2 Giorgos Korfiatis
        with assertRaises(NoHoldingError) as cm:
132 0ee59d55 Giorgos Korfiatis
            self.issue_commission([((holder, source, resource1), 1),
133 0ee59d55 Giorgos Korfiatis
                                   (('nonex', source, resource1), 1)])
134 0ee59d55 Giorgos Korfiatis
135 0ee59d55 Giorgos Korfiatis
        e = cm.exception
136 0ee59d55 Giorgos Korfiatis
        provision = e.data['provision']
137 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['holder'], 'nonex')
138 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['source'], source)
139 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['resource'], resource1)
140 0ee59d55 Giorgos Korfiatis
        self.assertEqual(provision['quantity'], 1)
141 0ee59d55 Giorgos Korfiatis
142 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
143 0ee59d55 Giorgos Korfiatis
        quotas = {(holder, source, resource1): (limit1, 0, limit1/2),
144 0ee59d55 Giorgos Korfiatis
                  (holder, source, resource2): (limit2, 0, limit2),
145 0ee59d55 Giorgos Korfiatis
                  }
146 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
147 0ee59d55 Giorgos Korfiatis
148 7bed22fb Giorgos Korfiatis
        # resolve commission
149 0ee59d55 Giorgos Korfiatis
150 0ee59d55 Giorgos Korfiatis
        r = qh.get_pending_commissions(clientkey=self.client)
151 0ee59d55 Giorgos Korfiatis
        self.assertEqual(len(r), 1)
152 0ee59d55 Giorgos Korfiatis
        serial = r[0]
153 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial)
154 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, True)
155 0ee59d55 Giorgos Korfiatis
        r = qh.get_pending_commissions(clientkey=self.client)
156 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, [])
157 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial)
158 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, False)
159 0ee59d55 Giorgos Korfiatis
160 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
161 0ee59d55 Giorgos Korfiatis
        quotas = {(holder, source, resource1): (limit1, limit1/2, limit1/2),
162 0ee59d55 Giorgos Korfiatis
                  (holder, source, resource2): (limit2, limit2, limit2),
163 0ee59d55 Giorgos Korfiatis
                  }
164 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
165 0ee59d55 Giorgos Korfiatis
166 7bed22fb Giorgos Korfiatis
        # resolve commissions
167 0ee59d55 Giorgos Korfiatis
168 0ee59d55 Giorgos Korfiatis
        serial = self.issue_commission([((holder, source, resource1), 1),
169 0ee59d55 Giorgos Korfiatis
                                        ((holder, source, resource2), -1)])
170 0ee59d55 Giorgos Korfiatis
171 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
172 0ee59d55 Giorgos Korfiatis
        quotas = {(holder, source, resource1): (limit1, limit1/2, limit1/2+1),
173 0ee59d55 Giorgos Korfiatis
                  (holder, source, resource2): (limit2, limit2-1, limit2),
174 0ee59d55 Giorgos Korfiatis
                  }
175 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
176 0ee59d55 Giorgos Korfiatis
177 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial, accept=False)
178 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, True)
179 0ee59d55 Giorgos Korfiatis
180 0ee59d55 Giorgos Korfiatis
        serial1 = self.issue_commission([((holder, source, resource1), 1),
181 0ee59d55 Giorgos Korfiatis
                                         ((holder, source, resource2), -1)])
182 0ee59d55 Giorgos Korfiatis
183 0ee59d55 Giorgos Korfiatis
        serial2 = self.issue_commission([((holder, source, resource1), 1),
184 0ee59d55 Giorgos Korfiatis
                                         ((holder, source, resource2), -1)])
185 0ee59d55 Giorgos Korfiatis
186 0ee59d55 Giorgos Korfiatis
        serial3 = self.issue_commission([((holder, source, resource1), 1),
187 0ee59d55 Giorgos Korfiatis
                                         ((holder, source, resource2), -1)])
188 0ee59d55 Giorgos Korfiatis
189 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commissions(clientkey=self.client,
190 0ee59d55 Giorgos Korfiatis
                                           accept_set=[serial1, serial2, 0],
191 0ee59d55 Giorgos Korfiatis
                                           reject_set=[serial2, serial3])
192 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, ([serial1], [serial3], [0], [serial2]))
193 0ee59d55 Giorgos Korfiatis
194 0ee59d55 Giorgos Korfiatis
        r = qh.get_pending_commissions(clientkey=self.client)
195 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, [serial2])
196 0ee59d55 Giorgos Korfiatis
197 7bed22fb Giorgos Korfiatis
        # forced commission
198 0ee59d55 Giorgos Korfiatis
199 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
200 0ee59d55 Giorgos Korfiatis
        quotas = {
201 0ee59d55 Giorgos Korfiatis
            (holder, source, resource1): (limit1, limit1/2+1, limit1/2+2),
202 0ee59d55 Giorgos Korfiatis
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
203 0ee59d55 Giorgos Korfiatis
        }
204 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
205 0ee59d55 Giorgos Korfiatis
206 aba462a2 Giorgos Korfiatis
        with assertRaises(NoCapacityError):
207 0ee59d55 Giorgos Korfiatis
            self.issue_commission(
208 0ee59d55 Giorgos Korfiatis
                [((holder, source, resource1), limit1/2+1)])
209 0ee59d55 Giorgos Korfiatis
210 0ee59d55 Giorgos Korfiatis
        serial = self.issue_commission(
211 0ee59d55 Giorgos Korfiatis
            [((holder, source, resource1), limit1/2+1)],
212 0ee59d55 Giorgos Korfiatis
            force=True)
213 0ee59d55 Giorgos Korfiatis
214 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial, accept=True)
215 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, True)
216 0ee59d55 Giorgos Korfiatis
217 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
218 0ee59d55 Giorgos Korfiatis
        quotas = {
219 0ee59d55 Giorgos Korfiatis
            (holder, source, resource1): (limit1, limit1+2, limit1+3),
220 0ee59d55 Giorgos Korfiatis
            (holder, source, resource2): (limit2, limit2-2, limit2-1),
221 0ee59d55 Giorgos Korfiatis
        }
222 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
223 0ee59d55 Giorgos Korfiatis
224 01b8fb9a Giorgos Korfiatis
        with assertRaises(NoQuantityError):
225 01b8fb9a Giorgos Korfiatis
            self.issue_commission(
226 01b8fb9a Giorgos Korfiatis
                [((holder, source, resource1), -2*limit1)],
227 01b8fb9a Giorgos Korfiatis
                force=True)
228 01b8fb9a Giorgos Korfiatis
229 7bed22fb Giorgos Korfiatis
        # release off upper limit
230 0ee59d55 Giorgos Korfiatis
231 0ee59d55 Giorgos Korfiatis
        serial = self.issue_commission([((holder, source, resource1), -1)])
232 0ee59d55 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial)
233 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, True)
234 0ee59d55 Giorgos Korfiatis
235 0ee59d55 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder], resources=[resource1])
236 0ee59d55 Giorgos Korfiatis
        quotas = {
237 0ee59d55 Giorgos Korfiatis
            (holder, source, resource1): (limit1, limit1+1, limit1+2),
238 0ee59d55 Giorgos Korfiatis
        }
239 0ee59d55 Giorgos Korfiatis
        self.assertEqual(r, quotas)
240 0ee59d55 Giorgos Korfiatis
241 7585a768 Giorgos Korfiatis
    def test_020_empty_provisions(self):
242 7585a768 Giorgos Korfiatis
        serial = self.issue_commission([])
243 7585a768 Giorgos Korfiatis
        r = qh.resolve_pending_commission(self.client, serial)
244 7585a768 Giorgos Korfiatis
        self.assertEqual(r, True)
245 20c6de35 Giorgos Korfiatis
246 20c6de35 Giorgos Korfiatis
    def test_030_set(self):
247 20c6de35 Giorgos Korfiatis
        holder = 'h0'
248 20c6de35 Giorgos Korfiatis
        source = 'system'
249 20c6de35 Giorgos Korfiatis
        resource1 = 'r1'
250 20c6de35 Giorgos Korfiatis
        resource2 = 'r2'
251 20c6de35 Giorgos Korfiatis
        limit1 = 10
252 20c6de35 Giorgos Korfiatis
        limit2 = 20
253 20c6de35 Giorgos Korfiatis
254 20c6de35 Giorgos Korfiatis
        models.Holding.objects.create(
255 20c6de35 Giorgos Korfiatis
            holder=holder, source=source, resource=resource1,
256 20c6de35 Giorgos Korfiatis
            usage_min=1, usage_max=1, limit=2)
257 20c6de35 Giorgos Korfiatis
        models.Holding.objects.create(
258 20c6de35 Giorgos Korfiatis
            holder=holder, source=source, resource=resource2,
259 20c6de35 Giorgos Korfiatis
            usage_min=2, usage_max=2, limit=22)
260 20c6de35 Giorgos Korfiatis
261 20c6de35 Giorgos Korfiatis
        qh.set_quota([((holder, source, resource1), limit1),
262 20c6de35 Giorgos Korfiatis
                      ((holder, source, resource1), limit2)])
263 20c6de35 Giorgos Korfiatis
264 20c6de35 Giorgos Korfiatis
        r = qh.get_quota(holders=[holder])
265 20c6de35 Giorgos Korfiatis
        self.assertEqual(r, {(holder, source, resource1): (limit2, 1, 1),
266 20c6de35 Giorgos Korfiatis
                             (holder, source, resource2): (22, 2, 2)})