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' ,
@@ -40,109 +30,143 @@ def test_model(self):
4030 'embeds' : []
4131 }
4232
43- obj = Url (data , 'preview' , 'http://google.com/' )
33+ def test_model (self ):
34+ obj = Url (self .data , 'preview' , 'http://google.com/' )
4435
45- self .assertTrue (len (obj ) is 16 )
46- self .assertTrue (len (obj .values ()) is 16 )
47- self .assertTrue (len (obj .keys ()) is 16 )
48- self .assertTrue (len (obj .items ()) is 16 )
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 )
4940
41+ # look for expected data
5042 self .assertTrue ('type' in obj .keys ())
5143 self .assertTrue ('html' in obj .values ())
5244
53- #Get the object
54- self .assertTrue (obj .type == 'html' )
55- self .assertTrue (obj ['type' ] == 'html' )
56- 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 ())
5751
58- #nope
59- 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 ())
6055
61- obj .nothing = 'something'
62- self .assertTrue (obj .nothing == 'something' )
56+ with self .assertRaises (KeyError ):
57+ obj ['method' ]
58+ with self .assertRaises (KeyError ):
59+ obj ['original_url' ]
6360
61+ # attr access = dict access
62+ obj .nothing = 'something'
63+ self .assertEqual (obj ['nothing' ], 'something' )
6464 obj ['nothing' ] = 'maybe'
65- self .assertTrue (obj [ ' nothing' ] == 'maybe' )
65+ self .assertEqual (obj . nothing , 'maybe' )
6666
6767 del obj ['nothing' ]
6868 self .assertTrue (obj .nothing is None )
6969
70- #Deep Get attrs
71- 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 )
7299 self .assertTrue (obj .images [0 ].nothing is None )
73100 self .assertTrue (obj .object .type is None )
74101
75102 def test_provider (self ):
76103 http = Embedly (self .key )
77104
78105 obj = http .oembed ('http://www.scribd.com/doc/13994900/Easter' )
79- self .assertTrue (obj .provider_url == 'http://www.scribd.com/' )
106+ self .assertEqual (obj .provider_url , 'http://www.scribd.com/' )
80107
81108 obj = http .oembed ('http://www.scribd.com/doc/28452730/Easter-Cards' )
82- self .assertTrue (obj .provider_url == 'http://www.scribd.com/' )
109+ self .assertEqual (obj .provider_url , 'http://www.scribd.com/' )
83110
84111 obj = http .oembed ('http://www.youtube.com/watch?v=Zk7dDekYej0' )
85- self .assertTrue (obj .provider_url == 'http://www.youtube.com/' )
112+ self .assertEqual (obj .provider_url , 'http://www.youtube.com/' )
86113
87114 obj = http .oembed ('http://yfrog.com/h22eu4j' )
88- self .assertTrue (obj .provider_url == 'http://yfrog.com' )
115+ self .assertEqual (obj .provider_url , 'http://yfrog.com' )
89116
90117 def test_providers (self ):
91118 http = Embedly (self .key )
92119
93120 objs = list (http .oembed (['http://www.scribd.com/doc/13994900/Easter' ,
94121 'http://www.scribd.com/doc/28452730/Easter-Cards' ]))
95- self .assertTrue (objs [0 ].provider_url == 'http://www.scribd.com/' )
96- 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/' )
97124
98125 objs = list (http .oembed (['http://www.youtube.com/watch?v=Zk7dDekYej0' ,
99126 'http://yfrog.com/h22eu4' ]))
100- self .assertTrue (objs [0 ].provider_url == 'http://www.youtube.com/' )
101- 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' )
102129
103130 def test_error (self ):
104131 http = Embedly (self .key )
105132
106133 obj = http .oembed ('http://www.embedly.com/this/is/a/bad/url' )
107- self .assertTrue (obj . error is True , obj . dict )
134+ self .assertTrue (obj [ ' error' ] )
108135 obj = http .oembed ('http://blog.embed.ly/lsbsdlfldsf/asdfkljlas/klajsdlfkasdf' )
109- self .assertTrue (obj . error is True , obj . dict )
136+ self .assertTrue (obj [ ' error' ] )
110137 obj = http .oembed ('http://twitpic/nothing/to/see/here' )
111- self .assertTrue (obj . error is True , obj . dict )
138+ self .assertTrue (obj [ ' error' ] )
112139
113140 def test_multi_errors (self ):
114141 http = Embedly (self .key )
115142
116143 objs = list (http .oembed (['http://www.embedly.com/this/is/a/bad/url' ,
117144 'http://blog.embed.ly/alsd/slsdlf/asdlfj' ]))
118- self .assertTrue (objs [0 ].type == 'error' , objs [ 0 ]. dict )
119- self .assertTrue (objs [1 ].type == 'error' , objs [ 1 ]. dict )
145+ self .assertEqual (objs [0 ].type , 'error' )
146+ self .assertEqual (objs [1 ].type , 'error' )
120147
121148 objs = list (http .oembed (['http://blog.embed.ly/lsbsdlfldsf/asdf/kl' ,
122149 'http://twitpic.com/nothing/to/see/here' ]))
123- self .assertTrue (objs [0 ].type == 'error' , objs [ 0 ]. dict )
124- self .assertTrue (objs [1 ].type == 'error' , objs [ 1 ]. dict )
150+ self .assertEqual (objs [0 ].type , 'error' )
151+ self .assertEqual (objs [1 ].type , 'error' )
125152
126153 objs = list (http .oembed (['http://blog.embed.ly/lsbsdlfldsf/asdf/kl' ,
127154 'http://yfrog.com/h22eu4j' ]))
128- self .assertTrue (objs [0 ].type == 'error' , objs [ 0 ]. dict )
129- self .assertTrue (objs [1 ].type == 'photo' , objs [ 1 ]. dict )
155+ self .assertEqual (objs [0 ].type , 'error' )
156+ self .assertEqual (objs [1 ].type , 'photo' )
130157
131158 objs = list (http .oembed (['http://yfrog.com/h22eu4j' ,
132159 'http://www.scribd.com/asdf/asdf/asdfasdf' ]))
133- self .assertTrue (objs [0 ].type == 'photo' , objs [ 0 ]. dict )
134- self .assertTrue (objs [1 ].type == 'error' , objs [ 1 ]. dict )
160+ self .assertEqual (objs [0 ].type , 'photo' )
161+ self .assertEqual (objs [1 ].type , 'error' )
135162
136- def test_too_many_urls (self ):
163+ def test_exception_on_too_many_urls (self ):
137164 http = Embedly (self .key )
138-
139165 urls = ['http://embed.ly' ] * 21
140- try :
166+
167+ with self .assertRaises (ValueError ):
141168 http .oembed (urls )
142- self .fail ('too many urls, should have thrown an error' )
143- except Exception as e :
144- self .assertTrue (type (e ), ValueError )
145169
146170
147171if __name__ == '__main__' :
148- unittest .main ()
172+ unittest .main ()
0 commit comments