def test_encode_decode(self): """verify that attribute saves to file, and is loaded back into node """ with pymm.Mindmap(self.filename, 'w') as mm: for key, val in self.attributes.items(): mm.root[key] = val self.assertTrue(mm.root.items() == self.attributes.items()) mm = None mm = pymm.Mindmap(self.filename) self.assertTrue(mm.root.items() == self.attributes.items())
def test_context_manager_abort(self): """Test that context manager writes to file even if an error occurs. Also verify that context-manager does not handle error, instead allowing error to propagate """ mm = None self.assertFalse(os.path.exists(self.filename)) with self.assertRaises(TypeError): with pymm.Mindmap(self.filename, 'w') as mm: mm.root.text = self.text mm.root.children.append(3, 3) # triggers TypeError self.assertTrue(os.path.exists(self.filename)) self.assertTrue(mm is not None) with pymm.Mindmap(self.filename) as mm: self.assertTrue(mm.root.text == self.text)
def test_lock_boolean(self): """file_locked can also act a boolean, returning true or false if a file is locked or not. """ self.assertFalse(pymm.file_locked(self.filename)) mm = pymm.Mindmap(self.filename, 'w') self.assertFalse(pymm.file_locked(self.filename))
def test_post_decode_removal(self): """test that elements remove themselves from hierarchy after decoding. Verify that before self-removing-element is created, mindmap looks normal. After self-removing-element is created, mindmap should have removed children """ self.test_post_encode_removal() with pymm.Mindmap(self.filename) as mm: self.assertTrue(mm.root.children) class Fake(pymm.element.BaseElement): @pymm.decode.post_decode def remove_self(self, parent): parent.children.remove(self) with pymm.Mindmap(self.filename) as mm: self.assertFalse(mm.root.children)
def test_pre_encode_removal(self): """create an element that removes itself from the hierarchy before encoding. Verify that multiple of these elements in a row are removed. This indicates no skipping of elemets. """ class Fake(pymm.element.BaseElement): @pymm.encode.pre_encode def remove_self(self, parent): parent.children.remove(self) with pymm.Mindmap(self.filename, 'w') as mm: mm.root.children = [Fake() for i in range(11)] self.assertTrue(mm.root.children) self.assertFalse(mm.root.children) # verify written file is affected with pymm.Mindmap(self.filename, 'r') as mm: self.assertFalse(mm.root.children)
def test_loads_default_hierarchy(self): """test that default hierarchy loads correctly. Should load one Mindmap with one one root node, (with text "new_mindmap") and several children. If Mindmap loads default hierarchy each time it is instantiated, python will enter a recursive loop that will end in error """ mm = pymm.Mindmap() self.assertTrue(mm.children != []) self.assertTrue(mm.root is not None) self.assertTrue(mm.root.text == "new_mindmap")
def test_post_encode_removal(self): """create an element that removes itself from hierarchy after encoding. Verify that multiple of thes elements in a row are removed which indicates no skipping of elements """ class Fake(pymm.element.BaseElement): @pymm.encode.post_encode def remove_self(self, parent): parent.children.remove(self) with pymm.Mindmap(self.filename, 'w') as mm: mm.root.children = [Fake() for i in range(11)] self.assertTrue(mm.root.children) self.assertFalse(mm.root.children) ch = pymm.factory.ConversionHandler() factory = ch.find_encode_factory(Fake()) # verify written file is NOT affected with pymm.Mindmap(self.filename) as mm: self.assertTrue(mm.root.children) for child in mm.root.children: self.assertTrue(isinstance(child, Fake))
def test_super(self): """verify that super() within a decorated function works: calling parent function of element, NOT of factory """ attr = 'x01234' value = 'asdf' self.assertFalse(hasattr(pymm.element.Node, attr)) class Fake(pymm.element.Node): @pymm.decode.post_decode def call_super(self, parent): super().tostring() # just call any function on parent element setattr(pymm.element.Node, attr, value) self.assertFalse(hasattr(pymm.element.Node, attr)) mm = pymm.Mindmap() self.assertTrue(getattr(pymm.element.Node, attr) == value) # cleanup delattr(pymm.element.Node, attr) self.assertFalse(hasattr(pymm.element.Node, attr)) class FakeOverride(pymm.element.Node): pass
def test_write_file(self): """Test that a "blank" mindmap can be written to file""" pymm.write(self.filename, pymm.Mindmap()) self.assertTrue(os.path.exists(self.filename))
def test_context_manager_read(self): """context manager should be able to read a file""" with pymm.Mindmap(self.filename, 'w') as mm: mm.root.text = self.text with pymm.Mindmap(self.filename, 'r') as mm: self.assertTrue(mm.root.text == self.text)
def test_context_manager_write(self): """verify that mindmap context manager writes to file""" with pymm.Mindmap(self.filename, 'w') as mm: mm.root.text = self.text self.assertTrue(os.path.exists(self.filename))