66
77
88class EmbedlyTestCase (unittest .TestCase ):
9-
10- def __init__ (self , * args , ** kwargs ):
9+ def setUp (self ):
1110 self .key = 'internal'
12-
13- if not self .key :
14- raise ValueError ('Set envirnomental varible EMBEDLY_API_KEY ' +
15- 'before running these tests like so: $ export ' +
16- 'EMBEDLY_API_KEY=key' )
17-
18- super (EmbedlyTestCase , self ).__init__ (* args , ** kwargs )
19-
20- def test_model (self ):
21- data = {
11+ self .data = {
2212 'provider_url' : 'http://www.google.com/' ,
2313 'safe' : True ,
2414 'description' : 'Google' ,
25- 'original_url' : 'http://google.com/' ,
2615 'url' : 'http://www.google.com/' ,
2716 'type' : 'html' ,
2817 'object' : {},
@@ -41,109 +30,143 @@ def test_model(self):
4130 'embeds' : []
4231 }
4332
44- obj = Url (data , 'preview' , 'http://google.com/' )
33+ def test_model (self ):
34+ obj = Url (self .data , 'preview' , 'http://google.com/' )
4535
46- self .assertTrue (len (obj ) is 17 )
47- self .assertTrue (len (obj .values ()) is 17 )
48- self .assertTrue (len (obj .keys ()) is 17 )
49- self .assertTrue (len (obj .items ()) is 17 )
36+ self .assertEqual (len (obj ), 16 )
37+ self .assertEqual (len (obj .values ()), 16 )
38+ self .assertEqual (len (obj .keys ()), 16 )
39+ self .assertEqual (len (obj .items ()), 16 )
5040
41+ # look for expected data
5142 self .assertTrue ('type' in obj .keys ())
5243 self .assertTrue ('html' in obj .values ())
5344
54- #Get the object
55- self .assertTrue (obj .type == 'html' )
56- self .assertTrue (obj ['type' ] == 'html' )
57- self .assertTrue (obj .get ('type' ) == 'html' )
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 ())
5851
59- #nope
60- self .assertTrue (obj .nothing is None )
52+ # our special attrs shouldn't be in the data dict
53+ self .assertFalse ('method' in obj .keys ())
54+ self .assertFalse ('original_url' in obj .keys ())
6155
62- obj .nothing = 'something'
63- self .assertTrue (obj .nothing == 'something' )
56+ with self .assertRaises (KeyError ):
57+ obj ['method' ]
58+ with self .assertRaises (KeyError ):
59+ obj ['original_url' ]
6460
61+ # attr access = dict access
62+ obj .nothing = 'something'
63+ self .assertEqual (obj ['nothing' ], 'something' )
6564 obj ['nothing' ] = 'maybe'
66- self .assertTrue (obj [ ' nothing' ] == 'maybe' )
65+ self .assertEqual (obj . nothing , 'maybe' )
6766
6867 del obj ['nothing' ]
6968 self .assertTrue (obj .nothing is None )
7069
71- #Deep Get attrs
72- self .assertTrue (obj .images [0 ].width is 275 )
70+ def test_model_attributes (self ):
71+ obj = Url (self .data , 'preview' , 'http://original.url.com/' )
72+
73+ self .assertEqual (obj .method , 'preview' )
74+ self .assertEqual (obj .original_url , 'http://original.url.com/' )
75+
76+ def test_model_with_empty_data (self ):
77+ obj = Url ()
78+
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 )
7399 self .assertTrue (obj .images [0 ].nothing is None )
74100 self .assertTrue (obj .object .type is None )
75101
76102 def test_provider (self ):
77103 http = Embedly (self .key )
78104
79105 obj = http .oembed ('http://www.scribd.com/doc/13994900/Easter' )
80- self .assertTrue (obj .provider_url == 'http://www.scribd.com/' )
106+ self .assertEqual (obj .provider_url , 'http://www.scribd.com/' )
81107
82108 obj = http .oembed ('http://www.scribd.com/doc/28452730/Easter-Cards' )
83- self .assertTrue (obj .provider_url == 'http://www.scribd.com/' )
109+ self .assertEqual (obj .provider_url , 'http://www.scribd.com/' )
84110
85111 obj = http .oembed ('http://www.youtube.com/watch?v=Zk7dDekYej0' )
86- self .assertTrue (obj .provider_url == 'http://www.youtube.com/' )
112+ self .assertEqual (obj .provider_url , 'http://www.youtube.com/' )
87113
88114 obj = http .oembed ('http://yfrog.com/h22eu4j' )
89- self .assertTrue (obj .provider_url == 'http://yfrog.com' )
115+ self .assertEqual (obj .provider_url , 'http://yfrog.com' )
90116
91117 def test_providers (self ):
92118 http = Embedly (self .key )
93119
94120 objs = list (http .oembed (['http://www.scribd.com/doc/13994900/Easter' ,
95121 'http://www.scribd.com/doc/28452730/Easter-Cards' ]))
96- self .assertTrue (objs [0 ].provider_url == 'http://www.scribd.com/' )
97- self .assertTrue (objs [1 ].provider_url == 'http://www.scribd.com/' )
122+ self .assertEqual (objs [0 ].provider_url , 'http://www.scribd.com/' )
123+ self .assertEqual (objs [1 ].provider_url , 'http://www.scribd.com/' )
98124
99125 objs = list (http .oembed (['http://www.youtube.com/watch?v=Zk7dDekYej0' ,
100126 'http://yfrog.com/h22eu4' ]))
101- self .assertTrue (objs [0 ].provider_url == 'http://www.youtube.com/' )
102- self .assertTrue (objs [1 ].provider_url == 'http://yfrog.com' )
127+ self .assertEqual (objs [0 ].provider_url , 'http://www.youtube.com/' )
128+ self .assertEqual (objs [1 ].provider_url , 'http://yfrog.com' )
103129
104130 def test_error (self ):
105131 http = Embedly (self .key )
106132
107133 obj = http .oembed ('http://www.embedly.com/this/is/a/bad/url' )
108- self .assertTrue (obj . error is True , obj . dict )
134+ self .assertTrue (obj [ ' error' ] )
109135 obj = http .oembed ('http://blog.embed.ly/lsbsdlfldsf/asdfkljlas/klajsdlfkasdf' )
110- self .assertTrue (obj . error is True , obj . dict )
136+ self .assertTrue (obj [ ' error' ] )
111137 obj = http .oembed ('http://twitpic/nothing/to/see/here' )
112- self .assertTrue (obj . error is True , obj . dict )
138+ self .assertTrue (obj [ ' error' ] )
113139
114140 def test_multi_errors (self ):
115141 http = Embedly (self .key )
116142
117143 objs = list (http .oembed (['http://www.embedly.com/this/is/a/bad/url' ,
118144 'http://blog.embed.ly/alsd/slsdlf/asdlfj' ]))
119- self .assertTrue (objs [0 ].type == 'error' , objs [ 0 ]. dict )
120- self .assertTrue (objs [1 ].type == 'error' , objs [ 1 ]. dict )
145+ self .assertEqual (objs [0 ].type , 'error' )
146+ self .assertEqual (objs [1 ].type , 'error' )
121147
122148 objs = list (http .oembed (['http://blog.embed.ly/lsbsdlfldsf/asdf/kl' ,
123149 'http://twitpic.com/nothing/to/see/here' ]))
124- self .assertTrue (objs [0 ].type == 'error' , objs [ 0 ]. dict )
125- self .assertTrue (objs [1 ].type == 'error' , objs [ 1 ]. dict )
150+ self .assertEqual (objs [0 ].type , 'error' )
151+ self .assertEqual (objs [1 ].type , 'error' )
126152
127153 objs = list (http .oembed (['http://blog.embed.ly/lsbsdlfldsf/asdf/kl' ,
128154 'http://yfrog.com/h22eu4j' ]))
129- self .assertTrue (objs [0 ].type == 'error' , objs [ 0 ]. dict )
130- self .assertTrue (objs [1 ].type == 'photo' , objs [ 1 ]. dict )
155+ self .assertEqual (objs [0 ].type , 'error' )
156+ self .assertEqual (objs [1 ].type , 'photo' )
131157
132158 objs = list (http .oembed (['http://yfrog.com/h22eu4j' ,
133159 'http://www.scribd.com/asdf/asdf/asdfasdf' ]))
134- self .assertTrue (objs [0 ].type == 'photo' , objs [ 0 ]. dict )
135- self .assertTrue (objs [1 ].type == 'error' , objs [ 1 ]. dict )
160+ self .assertEqual (objs [0 ].type , 'photo' )
161+ self .assertEqual (objs [1 ].type , 'error' )
136162
137- def test_too_many_urls (self ):
163+ def test_exception_on_too_many_urls (self ):
138164 http = Embedly (self .key )
139-
140165 urls = ['http://embed.ly' ] * 21
141- try :
166+
167+ with self .assertRaises (ValueError ):
142168 http .oembed (urls )
143- self .fail ('too many urls, should have thrown an error' )
144- except Exception as e :
145- self .assertTrue (type (e ), ValueError )
146169
147170
148171if __name__ == '__main__' :
149- unittest .main ()
172+ unittest .main ()
0 commit comments