From ac851828df94b49769f7bed38cca6182a60540f5 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Fri, 28 Jan 2011 22:10:18 -0500 Subject: Ported the persistence tests. --- tests/test_tree.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'tests/test_tree.py') diff --git a/tests/test_tree.py b/tests/test_tree.py index a3c4b3b..eac4e72 100644 --- a/tests/test_tree.py +++ b/tests/test_tree.py @@ -9,6 +9,8 @@ same markup, but all Beautiful Soup trees can be traversed with the methods tested here. """ +import copy +import cPickle as pickle import re from beautifulsoup import BeautifulSoup from beautifulsoup.element import SoupStrainer, Tag @@ -768,3 +770,48 @@ class TestElementObjects(SoupTest): soup = self.soup("") self.assertFalse(soup.b.string) + + +class TestPersistence(SoupTest): + "Testing features like pickle and deepcopy." + + def setUp(self): + super(TestPersistence, self).setUp() + self.page = """ + + + +Beautiful Soup: We called him Tortoise because he taught us. + + + + + + +foo +bar + +""" + self.tree = self.soup(self.page) + + def test_pickle_and_unpickle_identity(self): + # Pickling a tree, then unpickling it, yields a tree identical + # to the original. + dumped = pickle.dumps(self.tree, 2) + loaded = pickle.loads(dumped) + self.assertEqual(loaded.__class__, BeautifulSoup) + self.assertEqual(loaded.decode(), self.tree.decode()) + + def test_deepcopy_identity(self): + # Making a deepcopy of a tree yields an identical tree. + copied = copy.deepcopy(self.tree) + self.assertEqual(copied.decode(), self.tree.decode()) + + def test_unicode_pickle(self): + # A tree containing Unicode characters can be pickled. + html = u"\N{SNOWMAN}" + soup = self.soup(html) + dumped = pickle.dumps(soup, pickle.HIGHEST_PROTOCOL) + loaded = pickle.loads(dumped) + self.assertEqual(loaded.decode(), soup.decode()) -- cgit v1.2.3