Statistics
| Branch: | Tag: | Revision:

root / snf-quotaholder-app / quotaholder_django / test / limits.py @ 763b4da1

History | View | Annotate | Download (5 kB)

1 3e5941c1 Giorgos Korfiatis
# Copyright 2012 GRNET S.A. All rights reserved.
2 3e5941c1 Giorgos Korfiatis
#
3 3e5941c1 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 3e5941c1 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 3e5941c1 Giorgos Korfiatis
# conditions are met:
6 3e5941c1 Giorgos Korfiatis
#
7 3e5941c1 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 3e5941c1 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 3e5941c1 Giorgos Korfiatis
#      disclaimer.
10 3e5941c1 Giorgos Korfiatis
#
11 3e5941c1 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 3e5941c1 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 3e5941c1 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 3e5941c1 Giorgos Korfiatis
#      provided with the distribution.
15 3e5941c1 Giorgos Korfiatis
#
16 3e5941c1 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 3e5941c1 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 3e5941c1 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 3e5941c1 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 3e5941c1 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 3e5941c1 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 3e5941c1 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 3e5941c1 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 3e5941c1 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 3e5941c1 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 3e5941c1 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 3e5941c1 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 3e5941c1 Giorgos Korfiatis
#
29 3e5941c1 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 3e5941c1 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 3e5941c1 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 3e5941c1 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 3e5941c1 Giorgos Korfiatis
34 fc438cf2 Christos KK Loverdos
from config import QHTestCase
35 fc438cf2 Christos KK Loverdos
from config import run_test_case
36 fc438cf2 Christos KK Loverdos
from config import printf
37 5d655ae9 Christos KK Loverdos
from config import rand_string
38 5d655ae9 Christos KK Loverdos
from random import randint
39 5d655ae9 Christos KK Loverdos
40 c803f606 Giorgos Korfiatis
from synnefo.lib.quotaholder.api import InvalidDataError
41 45cdab81 Giorgos Korfiatis
42 5d655ae9 Christos KK Loverdos
class Data:
43 5d655ae9 Christos KK Loverdos
    def __init__(self, parent, **kwd):
44 5d655ae9 Christos KK Loverdos
        self.context  = kwd.get('context', parent.context)
45 5d655ae9 Christos KK Loverdos
        self.policy   = kwd.get('policy', parent.policy)
46 5d655ae9 Christos KK Loverdos
        self.capacity = kwd.get('capacity', parent.capacity)
47 5d655ae9 Christos KK Loverdos
        self.quantity = kwd.get('quantity', parent.quantity)
48 5d655ae9 Christos KK Loverdos
        self.import_limit = kwd.get('import_limit', parent.import_limit)
49 5d655ae9 Christos KK Loverdos
        self.export_limit = kwd.get('export_limit', parent.export_limit)
50 9a69d21c Christos KK Loverdos
        # set_limits_has_rejected indicates whether the test expects a non-empty rejected list
51 9a69d21c Christos KK Loverdos
        # after calling set_limits
52 9a69d21c Christos KK Loverdos
        self.set_limits_has_rejected = kwd.get('set_limits_has_rejected', False)
53 9a69d21c Christos KK Loverdos
        # get_limits_expected_length indicates the exact length of the limits list
54 9a69d21c Christos KK Loverdos
        # returned by get_limits
55 9a69d21c Christos KK Loverdos
        self.get_limits_expected_length = kwd.get('get_limits_expected_length', 1)
56 902f1fb9 Christos KK Loverdos
57 902f1fb9 Christos KK Loverdos
class LimitsTest(QHTestCase):
58 5d655ae9 Christos KK Loverdos
    context = {}
59 5d655ae9 Christos KK Loverdos
    policy = rand_string()
60 c7780c56 Christos KK Loverdos
    capacity1 = 0
61 c7780c56 Christos KK Loverdos
    capacity2 = 100
62 c7780c56 Christos KK Loverdos
    capacity = capacity1
63 c7780c56 Christos KK Loverdos
    quantity1 = capacity2
64 c7780c56 Christos KK Loverdos
    quantity2 = capacity1
65 c7780c56 Christos KK Loverdos
    quantity = quantity1
66 5d655ae9 Christos KK Loverdos
    import_limit_empty = 0
67 5d655ae9 Christos KK Loverdos
    import_limit_full_capacity = capacity
68 5d655ae9 Christos KK Loverdos
    import_limit_half_capacity = capacity / 2
69 5d655ae9 Christos KK Loverdos
    import_limit = import_limit_half_capacity
70 5d655ae9 Christos KK Loverdos
    export_limit_empty = 0
71 5d655ae9 Christos KK Loverdos
    export_limit_full_capacity = capacity
72 5d655ae9 Christos KK Loverdos
    export_limit_half_capacity = capacity / 2
73 5d655ae9 Christos KK Loverdos
    export_limit = export_limit_half_capacity
74 5d655ae9 Christos KK Loverdos
75 5d655ae9 Christos KK Loverdos
    def helper_set_limits(self, **kwd):
76 9a69d21c Christos KK Loverdos
        """
77 9a69d21c Christos KK Loverdos
        Calls set_limits and returns the rejected list (from the original API).
78 9a69d21c Christos KK Loverdos
        """
79 5d655ae9 Christos KK Loverdos
        data = Data(self, **kwd)
80 902f1fb9 Christos KK Loverdos
        rejected = self.qh.set_limits(
81 5d655ae9 Christos KK Loverdos
            context = data.context,
82 902f1fb9 Christos KK Loverdos
            set_limits = [
83 9a69d21c Christos KK Loverdos
                (data.policy,
84 9a69d21c Christos KK Loverdos
                 data.quantity,
85 9a69d21c Christos KK Loverdos
                 data.capacity,
86 9a69d21c Christos KK Loverdos
                 data.import_limit,
87 9a69d21c Christos KK Loverdos
                 data.export_limit)
88 902f1fb9 Christos KK Loverdos
            ]
89 902f1fb9 Christos KK Loverdos
        )
90 902f1fb9 Christos KK Loverdos
91 9a69d21c Christos KK Loverdos
        if data.set_limits_has_rejected:
92 5d655ae9 Christos KK Loverdos
            self.assertTrue(len(rejected) > 1)
93 5d655ae9 Christos KK Loverdos
        else:
94 5d655ae9 Christos KK Loverdos
            self.assertTrue(len(rejected) == 0)
95 902f1fb9 Christos KK Loverdos
96 9a69d21c Christos KK Loverdos
        return rejected
97 9a69d21c Christos KK Loverdos
98 5d655ae9 Christos KK Loverdos
    def helper_get_limits(self, **kwd):
99 5d655ae9 Christos KK Loverdos
        """
100 9a69d21c Christos KK Loverdos
        Calls get_limits and returns the limits list (from the original API)..
101 5d655ae9 Christos KK Loverdos
        """
102 5d655ae9 Christos KK Loverdos
        data = Data(self, **kwd)
103 902f1fb9 Christos KK Loverdos
        limits = self.qh.get_limits(
104 5d655ae9 Christos KK Loverdos
            context = data.context,
105 5d655ae9 Christos KK Loverdos
            get_limits = [data.policy]
106 902f1fb9 Christos KK Loverdos
        )
107 902f1fb9 Christos KK Loverdos
108 9a69d21c Christos KK Loverdos
        self.assertEqual(len(limits), data.get_limits_expected_length)
109 9a69d21c Christos KK Loverdos
110 5d655ae9 Christos KK Loverdos
        return limits
111 5d655ae9 Christos KK Loverdos
112 c7780c56 Christos KK Loverdos
    def test_010_set_get(self):
113 c7780c56 Christos KK Loverdos
        """
114 c7780c56 Christos KK Loverdos
        quantity = 0, capacity = 100
115 c7780c56 Christos KK Loverdos
        """
116 c7780c56 Christos KK Loverdos
        self.helper_set_limits(quantity = 0, capacity = 100, should_have_rejected = False)
117 9a69d21c Christos KK Loverdos
        self.helper_get_limits(get_limits_expected_length = 1)
118 9a69d21c Christos KK Loverdos
119 c7780c56 Christos KK Loverdos
    def test_011_set_get(self):
120 c7780c56 Christos KK Loverdos
        """
121 c7780c56 Christos KK Loverdos
        quantity = 100, capacity = 0
122 c7780c56 Christos KK Loverdos
        """
123 c7780c56 Christos KK Loverdos
        self.helper_set_limits(quantity = 100, capacity = 0, should_have_rejected = False)
124 c7780c56 Christos KK Loverdos
        self.helper_get_limits(get_limits_expected_length = 1)
125 f6a99346 Christos KK Loverdos
126 c7780c56 Christos KK Loverdos
    def test_020_set_get_empty_policy_name(self):
127 bba797bc Christos KK Loverdos
        """
128 bba797bc Christos KK Loverdos
        Tests empty policy name
129 bba797bc Christos KK Loverdos
        """
130 45cdab81 Giorgos Korfiatis
        with self.assertRaises(InvalidDataError):
131 45cdab81 Giorgos Korfiatis
            self.helper_set_limits(policy = '', set_limits_has_rejected = False)
132 45cdab81 Giorgos Korfiatis
            self.helper_get_limits(policy = '', get_limits_expected_length = 1)
133 bba797bc Christos KK Loverdos
134 6c4d8722 Christos KK Loverdos
135 f6a99346 Christos KK Loverdos
if __name__ == "__main__":
136 7f479bb1 Christos KK Loverdos
    import sys
137 a812c719 Christos KK Loverdos
    printf("Using {0}", sys.executable)
138 5d655ae9 Christos KK Loverdos
    run_test_case(LimitsTest)