def test_serialize__files(self): """ Tests that a video with associated files can still be serialized and deserialized. """ video = Video("http://www.youtube.com/watch?v=J_DV9b0x7v4") now = datetime.datetime.now() video.files = [VideoFile(url='http://google.com', expires=now, length=100, width=50, height=50, mime_type="video/x-flv"), VideoFile(url='http://xkcd.com', expires=now, length=75, width=80, height=80, mime_type="application/x-shockwave-flash"),] data = video.serialize() # verify that the data we expect is in the serialized version. self.assertEqual(data['files'][0]['url'], "http://google.com") self.assertEqual(data['files'][1]['mime_type'], "application/x-shockwave-flash") self.assertEqual(data['files'][0]['expires'], now.isoformat()) # Verify that the data can be deserialized as a video. new_video = Video.deserialize(data) self.assertEqual(dict(video.items()), dict(new_video.items()))
def test_serialize__pickle(self): """ Tests that serialized videos can be pickled and unpickled. """ video = Video("http://www.youtube.com/watch?v=J_DV9b0x7v4") # we load the video data this way to avoid depending on the network video_data = CARAMELL_DANSEN_API_DATA.copy() video_data['tags'] = list(video_data['tags']) video._apply(video_data) data = video.serialize() new_data = pickle.loads(pickle.dumps(data, pickle.HIGHEST_PROTOCOL)) self.assertEqual(new_data, data)
def test_serialize__json(self): """ Tests that serialized videos can be transformed to and from json. """ video = Video("http://www.youtube.com/watch?v=J_DV9b0x7v4") # we load the video data this way to avoid depending on the network video_data = CARAMELL_DANSEN_API_DATA.copy() video_data['tags'] = list(video_data['tags']) video._apply(video_data) data = video.serialize() new_data = json.loads(json.dumps(data)) self.assertEqual(new_data, data)
def test_serialize(self): video = Video("http://www.youtube.com/watch?v=J_DV9b0x7v4") # we load the video data this way to avoid depending on the network video_data = CARAMELL_DANSEN_API_DATA.copy() video_data['tags'] = list(video_data['tags']) video._apply(video_data) data = video.serialize() # verify that the data we expect is in the serialized version. self.assertEqual(data['url'], video.url) self.assertEqual(data['title'], video.title) self.assertEqual(data['publish_datetime'], video.publish_datetime.isoformat()) # Verify that the data can be deserialized as a video. new_video = Video.deserialize(data) self.assertEqual(video.url, new_video.url) self.assertEqual(dict(video.items()), dict(new_video.items()))
def test_m2m_errors(self): """ If video.save_m2m raises an exception during import, the video should be deleted and the error reraised. """ class FakeException(Exception): pass video = mock.MagicMock(save_m2m=mock.MagicMock( side_effect=FakeException)) kwargs = {'from_vidscraper_video.return_value': video} vidscraper_video = VidscraperVideo(None) with mock.patch('localtv.tasks.get_model'): with mock.patch('localtv.tasks.Video', **kwargs): with self.assertRaises(FakeException): video_from_vidscraper_video.apply(args=(vidscraper_video.serialize(), 1)) video.save.assert_called_once_with(update_index=False) video.save_m2m.assert_called_once_with() video.delete.assert_called_once_with()
def test_serialize__partial(self): """ Tests that a video with only some fields can still be serialized and deserialized. """ video = Video("http://www.youtube.com/watch?v=J_DV9b0x7v4", fields=('title', 'embed_code')) # we load the video data this way to avoid depending on the network video_data = CARAMELL_DANSEN_API_DATA.copy() video._apply(video_data) data = video.serialize() # verify that the data we expect is in the serialized version. self.assertEqual(data['url'], video.url) self.assertEqual(data['title'], video.title) self.assertEqual(data['embed_code'], video.embed_code) # Verify that the data can be deserialized as a video. new_video = Video.deserialize(data) self.assertEqual(video.url, new_video.url) self.assertEqual(dict(video.items()), dict(new_video.items()))
def test_m2m_errors(self): """ If video.save_m2m raises an exception during import, the video should be deleted and the error reraised. """ class FakeException(Exception): pass video = mock.MagicMock(save_m2m=mock.MagicMock( side_effect=FakeException)) kwargs = {'from_vidscraper_video.return_value': video} vidscraper_video = VidscraperVideo(None) with mock.patch('localtv.tasks.get_model'): with mock.patch('localtv.tasks.Video', **kwargs): with self.assertRaises(FakeException): video_from_vidscraper_video.apply( args=(vidscraper_video.serialize(), 1)) video.save.assert_called_once_with(update_index=False) video.save_m2m.assert_called_once_with() video.delete.assert_called_once_with()