File: test_dataframes.py

package info (click to toggle)
python-cloudkittyclient 5.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 748 kB
  • sloc: python: 3,512; makefile: 26; sh: 2
file content (172 lines) | stat: -rw-r--r-- 6,032 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Copyright 2019 Objectif Libre
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
import json

from collections import OrderedDict

from cloudkittyclient import exc
from cloudkittyclient.tests.unit.v2 import base


class TestDataframes(base.BaseAPIEndpointTestCase):
    dataframes_data = """
    {
        "dataframes": [
            {
                "period": {
                    "begin": "20190723T122810Z",
                    "end": "20190723T132810Z"
                },
                "usage": {
                    "metric_one": [
                        {
                            "vol": {
                                "unit": "GiB",
                                "qty": 1.2
                            },
                            "rating": {
                                "price": 0.04
                            },
                            "groupby": {
                                "group_one": "one",
                                "group_two": "two"
                            },
                            "metadata": {
                                "attr_one": "one",
                                "attr_two": "two"
                            }
                        }
                    ],
                    "metric_two": [
                        {
                            "vol": {
                                "unit": "MB",
                                "qty": 200.4
                            },
                            "rating": {
                                "price": 0.06
                            },
                            "groupby": {
                                "group_one": "one",
                                "group_two": "two"
                            },
                            "metadata": {
                                "attr_one": "one",
                                "attr_two": "two"
                            }
                        }
                    ]
                }
            },
            {
                "period": {
                    "begin": "20190823T122810Z",
                    "end": "20190823T132810Z"
                },
                "usage": {
                    "metric_one": [
                        {
                            "vol": {
                                "unit": "GiB",
                                "qty": 2.4
                            },
                            "rating": {
                                "price": 0.08
                            },
                            "groupby": {
                                "group_one": "one",
                                "group_two": "two"
                            },
                            "metadata": {
                                "attr_one": "one",
                                "attr_two": "two"
                            }
                        }
                    ],
                    "metric_two": [
                        {
                            "vol": {
                                "unit": "MB",
                                "qty": 400.8
                            },
                            "rating": {
                                "price": 0.12
                            },
                            "groupby": {
                                "group_one": "one",
                                "group_two": "two"
                            },
                            "metadata": {
                                "attr_one": "one",
                                "attr_two": "two"
                            }
                        }
                    ]
                }
            }
        ]
    }
    """

    def test_add_dataframes_with_string(self):
        self.dataframes.add_dataframes(
            dataframes=self.dataframes_data,
        )
        self.api_client.post.assert_called_once_with(
            '/v2/dataframes',
            data=self.dataframes_data,
        )

    def test_add_dataframes_with_json_object(self):
        json_data = json.loads(self.dataframes_data)

        self.dataframes.add_dataframes(
            dataframes=json_data,
        )
        self.api_client.post.assert_called_once_with(
            '/v2/dataframes',
            data=json.dumps(json_data),
        )

    def test_add_dataframes_with_neither_string_nor_object_raises_exc(self):
        self.assertRaises(
            exc.InvalidArgumentError,
            self.dataframes.add_dataframes,
            dataframes=[open],
        )

    def test_add_dataframes_with_no_args_raises_exc(self):
        self.assertRaises(
            exc.ArgumentRequired,
            self.dataframes.add_dataframes)

    def test_get_dataframes(self):
        self.dataframes.get_dataframes()
        self.api_client.get.assert_called_once_with('/v2/dataframes')

    def test_get_dataframes_with_pagination_args(self):
        self.dataframes.get_dataframes(offset=10, limit=10)
        try:
            self.api_client.get.assert_called_once_with(
                '/v2/dataframes?limit=10&offset=10')
        except AssertionError:
            self.api_client.get.assert_called_once_with(
                '/v2/dataframes?offset=10&limit=10')

    def test_get_dataframes_filters(self):
        self.dataframes.get_dataframes(
            filters=OrderedDict([('one', 'two'), ('three', 'four')]))
        self.api_client.get.assert_called_once_with(
            '/v2/dataframes?filters=one%3Atwo%2Cthree%3Afour')