Completed
Push — master ( 7ac7e1...e63902 )
by dotzero
01:43
created

HubResourceTest   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 122
rs 10
wmc 25
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
# Copyright (c) 2016 dotzero <[email protected]>
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining a copy
7
# of this software and associated documentation files (the "Software"), to deal
8
# in the Software without restriction, including without limitation the rights
9
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
# copies of the Software, and to permit persons to whom the Software is
11
# furnished to do so, subject to the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be included
14
# in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
# SOFTWARE.
23
24
"""This module contains a object that represents Tests for HubResource"""
25
26
import unittest
27
import sys
28
29
sys.path.append('.')
30
31
import habrahabr
32
from tests.base import BaseTest
33
from tests.base import MockRequest
34
35
36
class HubResourceTest(BaseTest, unittest.TestCase):
37
    """This object represents Tests for HubResource."""
38
39
    def setUp(self):
40
        auth = habrahabr.Auth(client='foo.bar', token='foobar')
41
42
        self.resource = habrahabr.HubResource(auth)
43
        self.resource._request = MockRequest()
44
45
    def test_info(self):
46
        """Test HubResource.info(alias) method"""
47
        self.resource._request.register_uri(
48
            'GET', '/hub/python/info', 'fixture_hub.json')
49
50
        response = self.resource.info('python')
51
52
        self.assertTrue('data' in response)
53
        self.assertTrue('server_time' in response)
54
55
    def test_info_fail(self):
56
        """Test Fail HubResource.info(alias) method"""
57
        with self.assertRaises(AssertionError):
58
            self.resource.info(-1)
59
60
    def test_habred(self):
61
        """Test HubResource.habred(alias, page) method"""
62
        self.resource._request.register_uri(
63
            'GET', '/hub/python/habred?page=2', 'fixture_hub.json')
64
65
        response = self.resource.habred('python', 2)
66
67
        self.assertTrue('data' in response)
68
        self.assertTrue('server_time' in response)
69
70
    def test_habred_fail(self):
71
        """Test Fail HubResource.habred(alias, page) method"""
72
        with self.assertRaises(AssertionError):
73
            self.resource.habred(-1)
74
75
        with self.assertRaises(AssertionError):
76
            self.resource.habred('foo', 'bar')
77
78
    def test_unhabred(self):
79
        """Test HubResource.unhabred(alias, page) method"""
80
        self.resource._request.register_uri(
81
            'GET', '/hub/python/unhabred?page=2', 'fixture_hub.json')
82
83
        response = self.resource.unhabred('python', 2)
84
85
        self.assertTrue('data' in response)
86
        self.assertTrue('server_time' in response)
87
88
    def test_unhabred_fail(self):
89
        """Test Fail HubResource.unhabred(alias, page) method"""
90
        with self.assertRaises(AssertionError):
91
            self.resource.unhabred(-1)
92
93
        with self.assertRaises(AssertionError):
94
            self.resource.unhabred('foo', 'bar')
95
96
    def test_new(self):
97
        """Test HubResource.new(alias, page) method"""
98
        self.resource._request.register_uri(
99
            'GET', '/hub/python/new?page=2', 'fixture_hub.json')
100
101
        response = self.resource.new('python', 2)
102
103
        self.assertTrue('data' in response)
104
        self.assertTrue('server_time' in response)
105
106
    def test_new_fail(self):
107
        """Test Fail HubResource.new(alias, page) method"""
108
        with self.assertRaises(AssertionError):
109
            self.resource.new(-1)
110
111
        with self.assertRaises(AssertionError):
112
            self.resource.new('foo', 'bar')
113
114
    def test_list(self):
115
        """Test HubResource.list(page) method"""
116
        self.resource._request.register_uri(
117
            'GET', '/hubs?page=2', 'fixture_hub.json')
118
119
        response = self.resource.list(2)
120
121
        self.assertTrue('data' in response)
122
        self.assertTrue('server_time' in response)
123
124
    def test_list_fail(self):
125
        """Test Fail HubResource.list(page) method"""
126
        with self.assertRaises(AssertionError):
127
            self.resource.list('foobar')
128
129
    def test_subscribe(self):
130
        """Test HubResource.subscribe(alias) method"""
131
        self.resource._request.register_uri(
132
            'PUT', '/hub/python')
133
134
        response = self.resource.subscribe('python')
135
136
        self.assertEqual(response['ok'], 1)
137
        self.assertTrue('server_time' in response)
138
139
    def test_subscribe_fail(self):
140
        """Test Fail HubResource.subscribe(alias) method"""
141
        with self.assertRaises(AssertionError):
142
            self.resource.subscribe(-1)
143
144
    def test_unsubscribe(self):
145
        """Test HubResource.unsubscribe(alias) method"""
146
        self.resource._request.register_uri(
147
            'DELETE', '/hub/python')
148
149
        response = self.resource.unsubscribe('python')
150
151
        self.assertEqual(response['ok'], 1)
152
        self.assertTrue('server_time' in response)
153
154
    def test_unsubscribe_fail(self):
155
        """Test Fail HubResource.unsubscribe(alias) method"""
156
        with self.assertRaises(AssertionError):
157
            self.resource.unsubscribe(-1)
158
159
160
if __name__ == '__main__':
161
    unittest.main()
162