11from __future__ import unicode_literals
22import unittest
3+ import json
34
45from embedly .client import Embedly
56from embedly .models import Url
89class EmbedlyTestCase (unittest .TestCase ):
910 def setUp (self ):
1011 self .key = 'internal'
11- self .data = {
12+
13+ def test_model (self ):
14+ data = {
1215 'provider_url' : 'http://www.google.com/' ,
1316 'safe' : True ,
1417 'description' : 'Google' ,
@@ -29,103 +32,70 @@ def setUp(self):
2932 'cache_age' : 86400 ,
3033 'embeds' : []
3134 }
32-
33- def test_model (self ):
34- obj = Url (self .data , 'preview' , 'http://google.com/' )
35+ obj = Url (data , 'preview' , 'http://original.url.com/' )
3536
3637 self .assertEqual (len (obj ), 16 )
3738 self .assertEqual (len (obj .values ()), 16 )
3839 self .assertEqual (len (obj .keys ()), 16 )
3940 self .assertEqual (len (obj .items ()), 16 )
4041
41- # look for expected data
42+ # check for expected data
4243 self .assertTrue ('type' in obj .keys ())
4344 self .assertTrue ('html' in obj .values ())
44-
45- # adding new data
46- obj ['new_key' ] = "new value"
47- self .assertEqual (len (obj ), 17 )
48- self .assertEqual (len (obj .items ()), 17 )
49- self .assertTrue ('new_key' in obj .keys ())
50- self .assertTrue ('new value' in obj .values ())
45+ self .assertEqual (obj ['type' ], 'html' )
46+ self .assertEqual (obj .get ('type' ), 'html' )
47+ self .assertEqual (obj .data ['type' ], 'html' )
48+ self .assertEqual (obj .data .get ('type' ), 'html' )
5149
5250 # our special attrs shouldn't be in the data dict
5351 self .assertFalse ('method' in obj .keys ())
54- self .assertFalse ('original_url' in obj .keys ())
55-
5652 with self .assertRaises (KeyError ):
5753 obj ['method' ]
58- with self .assertRaises (KeyError ):
59- obj ['original_url' ]
60-
61- # attr access = dict access
62- obj .nothing = 'something'
63- self .assertEqual (obj ['nothing' ], 'something' )
64- obj ['nothing' ] = 'maybe'
65- self .assertEqual (obj .nothing , 'maybe' )
66-
67- del obj ['nothing' ]
68- self .assertTrue (obj .nothing is None )
69-
70- def test_model_attributes (self ):
71- obj = Url (self .data , 'preview' , 'http://original.url.com/' )
7254
73- self . assertEqual ( obj . method , 'preview' )
55+ # attrs and data dict values should be separate
7456 self .assertEqual (obj .original_url , 'http://original.url.com/' )
7557
76- def test_model_with_empty_data (self ):
77- obj = Url ()
58+ obj .new_attr = 'attr value'
59+ obj ['new_key' ] = 'dict value'
60+ self .assertEqual (obj .new_attr , 'attr value' )
61+ self .assertEqual (obj ['new_key' ], 'dict value' )
7862
79- self .assertEqual (obj .data , {})
80- self .assertEqual (obj .method , 'url' )
81- self .assertTrue (obj .original_url is None )
82-
83- def test_model_data (self ):
84- obj = Url (self .data , 'preview' , 'http://google.com/' )
85-
86- self .assertEqual (obj ['type' ], 'html' )
87- self .assertEqual (obj .get ('type' ), 'html' )
88- self .assertEqual (obj .data ['type' ], 'html' )
89- self .assertEqual (obj .data .get ('type' ), 'html' )
90-
91- obj ['nothing' ] = 'something'
92- self .assertEqual (obj ['nothing' ], 'something' )
93- self .assertEqual (obj .get ('nothing' ), 'something' )
94- self .assertEqual (obj .data ['nothing' ], 'something' )
95- self .assertEqual (obj .data .get ('nothing' ), 'something' )
96-
97- # deep get attrs
98- self .assertEqual (obj .images [0 ].width , 275 )
99- self .assertTrue (obj .images [0 ].nothing is None )
100- self .assertTrue (obj .object .type is None )
63+ def test_model_data_can_serialize (self ):
64+ obj = Url ({'a' : {'key' : 'value' }})
65+ unserialzed = json .loads (json .dumps (obj .data ))
66+ self .assertDictEqual (obj .data , unserialzed )
10167
10268 def test_provider (self ):
10369 http = Embedly (self .key )
10470
10571 obj = http .oembed ('http://www.scribd.com/doc/13994900/Easter' )
106- self .assertEqual (obj .provider_url , 'http://www.scribd.com/' )
72+
73+ self .assertEqual (obj ['provider_url' ], 'http://www.scribd.com/' )
10774
10875 obj = http .oembed ('http://www.scribd.com/doc/28452730/Easter-Cards' )
109- self .assertEqual (obj . provider_url , 'http://www.scribd.com/' )
76+ self .assertEqual (obj [ ' provider_url' ] , 'http://www.scribd.com/' )
11077
11178 obj = http .oembed ('http://www.youtube.com/watch?v=Zk7dDekYej0' )
112- self .assertEqual (obj . provider_url , 'http://www.youtube.com/' )
79+ self .assertEqual (obj [ ' provider_url' ] , 'http://www.youtube.com/' )
11380
11481 obj = http .oembed ('http://yfrog.com/h22eu4j' )
115- self .assertEqual (obj .provider_url , 'http://yfrog.com' )
82+ self .assertEqual (obj ['provider_url' ], 'http://yfrog.com' )
83+
11684
11785 def test_providers (self ):
11886 http = Embedly (self .key )
11987
12088 objs = list (http .oembed (['http://www.scribd.com/doc/13994900/Easter' ,
12189 'http://www.scribd.com/doc/28452730/Easter-Cards' ]))
122- self .assertEqual (objs [0 ].provider_url , 'http://www.scribd.com/' )
123- self .assertEqual (objs [1 ].provider_url , 'http://www.scribd.com/' )
90+
91+ self .assertEqual (objs [0 ]['provider_url' ], 'http://www.scribd.com/' )
92+ self .assertEqual (objs [1 ]['provider_url' ], 'http://www.scribd.com/' )
12493
12594 objs = list (http .oembed (['http://www.youtube.com/watch?v=Zk7dDekYej0' ,
12695 'http://yfrog.com/h22eu4' ]))
127- self .assertEqual (objs [0 ].provider_url , 'http://www.youtube.com/' )
128- self .assertEqual (objs [1 ].provider_url , 'http://yfrog.com' )
96+ self .assertEqual (objs [0 ]['provider_url' ], 'http://www.youtube.com/' )
97+ self .assertEqual (objs [1 ]['provider_url' ], 'http://yfrog.com' )
98+
12999
130100 def test_error (self ):
131101 http = Embedly (self .key )
@@ -142,23 +112,25 @@ def test_multi_errors(self):
142112
143113 objs = list (http .oembed (['http://www.embedly.com/this/is/a/bad/url' ,
144114 'http://blog.embed.ly/alsd/slsdlf/asdlfj' ]))
145- self .assertEqual (objs [0 ].type , 'error' )
146- self .assertEqual (objs [1 ].type , 'error' )
115+
116+ self .assertEqual (objs [0 ]['type' ], 'error' )
117+ self .assertEqual (objs [1 ]['type' ], 'error' )
147118
148119 objs = list (http .oembed (['http://blog.embed.ly/lsbsdlfldsf/asdf/kl' ,
149120 'http://twitpic.com/nothing/to/see/here' ]))
150- self .assertEqual (objs [0 ]. type , 'error' )
151- self .assertEqual (objs [1 ]. type , 'error' )
121+ self .assertEqual (objs [0 ][ ' type' ] , 'error' )
122+ self .assertEqual (objs [1 ][ ' type' ] , 'error' )
152123
153124 objs = list (http .oembed (['http://blog.embed.ly/lsbsdlfldsf/asdf/kl' ,
154125 'http://yfrog.com/h22eu4j' ]))
155- self .assertEqual (objs [0 ]. type , 'error' )
156- self .assertEqual (objs [1 ]. type , 'photo' )
126+ self .assertEqual (objs [0 ][ ' type' ] , 'error' )
127+ self .assertEqual (objs [1 ][ ' type' ] , 'photo' )
157128
158129 objs = list (http .oembed (['http://yfrog.com/h22eu4j' ,
159130 'http://www.scribd.com/asdf/asdf/asdfasdf' ]))
160- self .assertEqual (objs [0 ].type , 'photo' )
161- self .assertEqual (objs [1 ].type , 'error' )
131+ self .assertEqual (objs [0 ]['type' ], 'photo' )
132+ self .assertEqual (objs [1 ]['type' ], 'error' )
133+
162134
163135 def test_exception_on_too_many_urls (self ):
164136 http = Embedly (self .key )
0 commit comments