summaryrefslogtreecommitdiff
path: root/tests/unit/engines/test_soundcloud.py
blob: 3077d3b4b4dc3d8b18fbbeacba932636ad1b1e6e (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from collections import defaultdict
import mock
from searx.engines import soundcloud
from searx.testing import SearxTestCase
from searx.url_utils import quote_plus


class TestSoundcloudEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 1
        params = soundcloud.request(query, dicto)
        self.assertIn('url', params)
        self.assertIn(query, params['url'])
        self.assertIn('soundcloud.com', params['url'])

    def test_response(self):
        self.assertRaises(AttributeError, soundcloud.response, None)
        self.assertRaises(AttributeError, soundcloud.response, [])
        self.assertRaises(AttributeError, soundcloud.response, '')
        self.assertRaises(AttributeError, soundcloud.response, '[]')

        response = mock.Mock(text='{}')
        self.assertEqual(soundcloud.response(response), [])

        response = mock.Mock(text='{"data": []}')
        self.assertEqual(soundcloud.response(response), [])

        json = """
        {
        "collection": [
            {
            "kind": "track",
            "id": 159723640,
            "created_at": "2014/07/22 00:51:21 +0000",
            "user_id": 2976616,
            "duration": 303780,
            "commentable": true,
            "state": "finished",
            "original_content_size": 13236349,
            "last_modified": "2015/01/31 15:14:50 +0000",
            "sharing": "public",
            "tag_list": "seekae flume",
            "permalink": "seekae-test-recognise-flume-re-work",
            "streamable": true,
            "embeddable_by": "all",
            "downloadable": true,
            "purchase_url": "http://www.facebook.com/seekaemusic",
            "label_id": null,
            "purchase_title": "Seekae",
            "genre": "freedownload",
            "title": "This is the title",
            "description": "This is the content",
            "label_name": "Future Classic",
            "release": "",
            "track_type": "remix",
            "key_signature": "",
            "isrc": "",
            "video_url": null,
            "bpm": null,
            "release_year": 2014,
            "release_month": 7,
            "release_day": 22,
            "original_format": "mp3",
            "license": "all-rights-reserved",
            "uri": "https://api.soundcloud.com/tracks/159723640",
            "user": {
                "id": 2976616,
                "kind": "user",
                "permalink": "flume",
                "username": "Flume",
                "last_modified": "2014/11/24 19:21:29 +0000",
                "uri": "https://api.soundcloud.com/users/2976616",
                "permalink_url": "http://soundcloud.com/flume",
                "avatar_url": "https://i1.sndcdn.com/avatars-000044475439-4zi7ii-large.jpg"
            },
            "permalink_url": "http://soundcloud.com/this.is.the.url",
            "artwork_url": "https://i1.sndcdn.com/artworks-000085857162-xdxy5c-large.jpg",
            "waveform_url": "https://w1.sndcdn.com/DWrL1lAN8BkP_m.png",
            "stream_url": "https://api.soundcloud.com/tracks/159723640/stream",
            "download_url": "https://api.soundcloud.com/tracks/159723640/download",
            "playback_count": 2190687,
            "download_count": 54856,
            "favoritings_count": 49061,
            "comment_count": 826,
            "likes_count": 49061,
            "reposts_count": 15910,
            "attachments_uri": "https://api.soundcloud.com/tracks/159723640/attachments",
            "policy": "ALLOW"
            }
        ],
        "total_results": 375750,
        "next_href": "https://api.soundcloud.com/search?&q=test",
        "tx_id": ""
        }
        """
        response = mock.Mock(text=json)
        results = soundcloud.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'http://soundcloud.com/this.is.the.url')
        self.assertEqual(results[0]['content'], 'This is the content')
        self.assertIn(quote_plus('https://api.soundcloud.com/tracks/159723640'), results[0]['embedded'])

        json = """
        {
        "collection": [
            {
            "kind": "user",
            "id": 159723640,
            "created_at": "2014/07/22 00:51:21 +0000",
            "user_id": 2976616,
            "duration": 303780,
            "commentable": true,
            "state": "finished",
            "original_content_size": 13236349,
            "last_modified": "2015/01/31 15:14:50 +0000",
            "sharing": "public",
            "tag_list": "seekae flume",
            "permalink": "seekae-test-recognise-flume-re-work",
            "streamable": true,
            "embeddable_by": "all",
            "downloadable": true,
            "purchase_url": "http://www.facebook.com/seekaemusic",
            "label_id": null,
            "purchase_title": "Seekae",
            "genre": "freedownload",
            "title": "This is the title",
            "description": "This is the content",
            "label_name": "Future Classic",
            "release": "",
            "track_type": "remix",
            "key_signature": "",
            "isrc": "",
            "video_url": null,
            "bpm": null,
            "release_year": 2014,
            "release_month": 7,
            "release_day": 22,
            "original_format": "mp3",
            "license": "all-rights-reserved",
            "uri": "https://api.soundcloud.com/tracks/159723640",
            "user": {
                "id": 2976616,
                "kind": "user",
                "permalink": "flume",
                "username": "Flume",
                "last_modified": "2014/11/24 19:21:29 +0000",
                "uri": "https://api.soundcloud.com/users/2976616",
                "permalink_url": "http://soundcloud.com/flume",
                "avatar_url": "https://i1.sndcdn.com/avatars-000044475439-4zi7ii-large.jpg"
            },
            "permalink_url": "http://soundcloud.com/this.is.the.url",
            "artwork_url": "https://i1.sndcdn.com/artworks-000085857162-xdxy5c-large.jpg",
            "waveform_url": "https://w1.sndcdn.com/DWrL1lAN8BkP_m.png",
            "stream_url": "https://api.soundcloud.com/tracks/159723640/stream",
            "download_url": "https://api.soundcloud.com/tracks/159723640/download",
            "playback_count": 2190687,
            "download_count": 54856,
            "favoritings_count": 49061,
            "comment_count": 826,
            "likes_count": 49061,
            "reposts_count": 15910,
            "attachments_uri": "https://api.soundcloud.com/tracks/159723640/attachments",
            "policy": "ALLOW"
            }
        ],
        "total_results": 375750,
        "next_href": "https://api.soundcloud.com/search?&q=test",
        "tx_id": ""
        }
        """
        response = mock.Mock(text=json)
        results = soundcloud.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        json = """
        {
        "collection": [],
        "total_results": 375750,
        "next_href": "https://api.soundcloud.com/search?&q=test",
        "tx_id": ""
        }
        """
        response = mock.Mock(text=json)
        results = soundcloud.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)