~davidc3/ubuntu-rest-scopes/testing-newyorktimes

« back to all changes in this revision

Viewing changes to src/scopes/tests/test_onlinemusic.py

  • Committer: David Callé
  • Date: 2014-02-20 15:31:35 UTC
  • mfrom: (3.1.45 ubuntu-rest-scopes)
  • Revision ID: davidc@framli.eu-20140220153135-q6akdx8g8ssa60p8
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf8 -*-
 
2
 
 
3
# Copyright 2014 Canonical
 
4
# All Rights Reserved
 
5
 
 
6
""" Test for onlinemusic scope """
 
7
 
 
8
from unittest import TestCase
 
9
 
 
10
from mock import patch, ANY
 
11
 
 
12
from src.scopes import onlinemusic
 
13
from src.scopes.tests import get_fixture
 
14
 
 
15
CONFIG = {'onlinemusic_server':
 
16
          'https://productsearch.ubuntu.com/smartscopes/v1'}
 
17
 
 
18
 
 
19
class SearchTestCase(TestCase):
 
20
    """The tests for the search part."""
 
21
 
 
22
    def test_simple_search(self):
 
23
        app = onlinemusic.App(CONFIG)
 
24
        with patch.object(onlinemusic, '_get_search') as mock:
 
25
            mock.return_value = get_fixture('onlinemusic-results.json')
 
26
            response = list(app.search(query=u'foo', locale='en_US', limit=10))
 
27
 
 
28
        mock.assert_called_with(ANY, u'foo', u'en_US', ANY, ANY)
 
29
 
 
30
        # category
 
31
        expect_cat = {'category': app.music_categories[app.CATEGORY_ONLINE]}
 
32
        self.assertEqual(response[0], expect_cat)
 
33
 
 
34
        # the result
 
35
        expect_res = {'result': {
 
36
            'cat_id': app.CATEGORY_ONLINE,
 
37
            'uri': u'http://produ.com/29',
 
38
            'dnd_uri': u'http://produ.com/29',
 
39
            'art': 'http://ubuntu.com/350.jpg',
 
40
            'artist': 'Various Artists',
 
41
            'title': u'Met',
 
42
            'real_result': ANY,
 
43
            'session': ANY,
 
44
            'ssid': ANY
 
45
        }}
 
46
        self.assertEqual(response[1], expect_res)
 
47
 
 
48
 
 
49
class PreviewTestCase(TestCase):
 
50
    """Tests for the preview function."""
 
51
 
 
52
    def test_ok(self):
 
53
        # fake a previous result
 
54
        real_res = {
 
55
            'metadata': {
 
56
                'id': '1234'
 
57
            }
 
58
        }
 
59
        result = {
 
60
            'uri': u'https://xyz',
 
61
            'dnd_uri': u'https://xyz',
 
62
            'art': "http://ubuntu.com/3.jpg",
 
63
            'title': u'Test Title',
 
64
            'real_result': real_res,
 
65
            'session': '1111',
 
66
            'ssid': 'xyz'
 
67
        }
 
68
        app = onlinemusic.App(CONFIG)
 
69
        with patch.object(onlinemusic, '_get_preview') as mock:
 
70
            mock.return_value = get_fixture('onlinemusic-preview.json')
 
71
            response = list(app.preview(result=result, locale='en_US'))
 
72
 
 
73
        # the preview
 
74
        expect_w1 = {'widget': {
 
75
            'id': 'art',
 
76
            'type': 'image',
 
77
            'source': u'https://one.com/0.jpg'
 
78
        }}
 
79
        self.assertEqual(response[1], expect_w1)
 
80
 
 
81
        expect_w2 = {'widget': {
 
82
            'id': 'header',
 
83
            'type': 'header',
 
84
            'title': u'legacy',
 
85
            'subtitle': 'foo'
 
86
        }}
 
87
        self.assertEqual(response[2], expect_w2)
 
88
 
 
89
        expect_w3 = {'widget': {
 
90
            'id': 'buttons',
 
91
            'type': 'actions',
 
92
            'actions': [{'id': 'https://products',
 
93
                        'icon': 'u1.svg',
 
94
                        'label': 'Download'}]
 
95
        }}
 
96
        self.assertEqual(response[3], expect_w3)
 
97
 
 
98
        expect_w4 = {'widget': {
 
99
            'id': 'tracks',
 
100
            'type': 'audio',
 
101
            'tracks': [{'title': 'Legacy',
 
102
                        'source': 'http://t1',
 
103
                        'length': 670}]
 
104
        }}
 
105
        self.assertEqual(response[4], expect_w4)