class TestPicklingEntity(unittest.TestCase): """ Pickle an entity that refers to other entities. """ def setUp(self): self.p = Project() self.c = Entity(self.p, title="c") self.e = Entity(self.p, title="t", c=self.c) def tearDown(self): self.c.self_destruct(self.p) self.e.self_destruct(self.p) def test_pickle(self): s = pickle.dumps(self.e) assert self.e['c'] == self.c, self.e['c'] assert isinstance(self.c, Entity) assert isinstance(self.e['c'], Entity) def test_unpickle(self): assert isinstance(self.e['c'], Entity) assert self.e.project s = pickle.dumps(self.e) e = pickle.loads(s) assert id(e) != id(self.e) assert e.uuid == self.e.uuid assert isinstance(self.c, Entity) assert e['c'] == self.c, e['c'] assert isinstance(self.e['c'], Entity) e.summarized_view e.detailed_view assert e.update_modified_time == True
def test_self_destruct_1(): """ Delete an entity. """ p = Project() p.pathname = '/tmp' e1 = Entity(p, title="e1", a=1, b=2) file_written = e1.to_yaml_file(p.pathname) files_deleted = e1.self_destruct(p) assert files_deleted == [file_written] assert not os.path.exists(file_written)
class TestPicklingProject(unittest.TestCase): def setUp(self): self.p = Project() self.c = Entity(self.p, title="c") self.e = Entity(self.p, title="t", c=self.c) def tearDown(self): self.c.self_destruct(self.p) self.e.self_destruct(self.p) if os.path.exists('/tmp/project.pickle'): os.remove('/tmp/project.pickle') def test_to_pickle1(self): self.assertRaises( ValueError, self.p.to_pickle) def test_to_pickle2(self): self.p.to_pickle('/tmp') assert os.path.exists('/tmp/project.pickle') def test_unpickle(self): assert not os.path.exists('/tmp/project.pickle') self.p.to_pickle('/tmp') assert os.path.exists('/tmp/project.pickle') new_p = Project.from_pickle('/tmp/project.pickle') assert self.p.length == new_p.length assert new_p.length for e in new_p: assert e.project
def test_self_destruct_2(): """ Delete an entity with activities and verify activities are gone too. """ p = Project() p.pathname = '/tmp' p.current_user = Person(p, title="matt") e1 = Entity(p, title="entity for test_self_destruct_2", a=1, b=2) file_written = e1.to_yaml_file(p.pathname) print("file_written is %s" % file_written) c = e1.comment(who_said_it="matt", title="blah", description="") comment_yaml_file = c.to_yaml_file(p.pathname) # Update an attribute (to generate an activity). e1.record_activity_on_changes = True e1['a'] = 11 assert e1.activities.length == 1, e1.activities.length a = e1.activities[0] activity_yaml_file = a.to_yaml_file(p.pathname) files_deleted = sorted(e1.self_destruct(p)) files_created = sorted( [file_written, comment_yaml_file, activity_yaml_file]) print("files_deleted is %s" % files_deleted) print("files_created is %s" % files_created) assert files_deleted == files_created assert not os.path.exists(file_written)