1
1

test_dumper.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import unittest
  2. import sys
  3. try:
  4. from StringIO import StringIO
  5. except ImportError:
  6. from io import StringIO
  7. import odml
  8. from odml.tools.dumper import dump_doc
  9. class TestTypes(unittest.TestCase):
  10. def setUp(self):
  11. s_type = "type"
  12. self.doc = odml.Document(author='Rave', version='1.0')
  13. sec1 = odml.Section(name='Cell', type=s_type)
  14. prop1 = odml.Property(name='Type', values='Rechargeable')
  15. sec1.append(prop1)
  16. sec2 = odml.Section(name='Electrolyte', type=s_type)
  17. prop2 = odml.Property(name='Composition', values='Ni-Cd')
  18. sec2.append(prop2)
  19. sec1.append(sec2)
  20. sec3 = odml.Section(name='Electrode', type=s_type)
  21. prop3 = odml.Property(name='Material', values='Nickel')
  22. prop4 = odml.Property(name='Models', values=['AA', 'AAA'])
  23. sec3.append(prop3)
  24. sec3.append(prop4)
  25. sec2.append(sec3)
  26. self.doc.append(sec1)
  27. def test_dump_doc(self):
  28. # Capture the output printed by the functions to STDOUT, and use it for
  29. # testing purposes. It needs to be reset after the capture.
  30. captured_stdout = StringIO()
  31. sys.stdout = captured_stdout
  32. # This test dumps the whole document and checks it word by word.
  33. # If possible, maybe some better way of testing this?
  34. dump_doc(self.doc)
  35. output = [x.strip() for x in captured_stdout.getvalue().split('\n') if x]
  36. # Reset stdout
  37. sys.stdout = sys.__stdout__
  38. expected_output = ["*Cell (type='type')",
  39. ":Type (values=Rechargeable, dtype='string')",
  40. "*Electrolyte (type='type')",
  41. ":Composition (values=Ni-Cd, dtype='string')",
  42. "*Electrode (type='type')",
  43. ":Material (values=Nickel, dtype='string')",
  44. ":Models (values=[AA,AAA], dtype='string')"]
  45. self.assertEqual(len(output), len(expected_output))
  46. for i, _ in enumerate(output):
  47. self.assertEqual(output[i], expected_output[i])