1
1

test_iterators.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import unittest
  2. from odml import Property, Section, Document
  3. class TestValidation(unittest.TestCase):
  4. def setUp(self):
  5. """
  6. doc -- <section sec_main> -- <section sub_main>
  7. \
  8. -- <section sec_branch> -- <section sub_branch>
  9. """
  10. doc = Document("author")
  11. sec_main = Section("sec_main", "maintype")
  12. doc.append(sec_main)
  13. sec_main.append(Property("strprop", "somestring"))
  14. sec_main.append(Property("txtprop", "some\ntext"))
  15. sub_main = Section("sub_main", "maintype")
  16. sec_main.append(sub_main)
  17. sub_main.append(Property("strprop", "somestring"))
  18. sub_main.append(Property("txtprop", "some\ntext"))
  19. sec_branch = Section("sec_branch", "branchtype")
  20. sec_main.append(sec_branch)
  21. sec_branch.append(Property("strprop", "otherstring"))
  22. sec_branch.append(Property("txtprop", "other\ntext"))
  23. sub_branch = Section("sub_branch", "branchtype")
  24. sec_branch.append(sub_branch)
  25. sub_branch.append(Property("strprop", "otherstring"))
  26. sub_branch.append(Property("txtprop", "other\ntext"))
  27. self.doc = doc
  28. def test_itersections(self):
  29. sec_all = list(self.doc.itersections())
  30. self.assertEqual(len(sec_all), 4)
  31. filter_func = lambda x: getattr(x, "name") == "sec_main"
  32. sec_filtered = list(self.doc.itersections(filter_func=filter_func))
  33. self.assertEqual(len(sec_filtered), 1)
  34. filter_func = lambda x: getattr(x, "type").find("branch") > -1
  35. sec_filtered = list(self.doc.itersections(filter_func=filter_func))
  36. self.assertEqual(len(sec_filtered), 2)
  37. sec_filtered = list(self.doc.itersections(max_depth=2))
  38. self.assertEqual(len(sec_filtered), 3)
  39. sec_filtered = list(self.doc.itersections(max_depth=1))
  40. self.assertEqual(len(sec_filtered), 1)
  41. sec_filtered = list(self.doc.itersections(max_depth=0))
  42. self.assertEqual(len(sec_filtered), 0)
  43. def test_iterproperties(self):
  44. prop_all = list(self.doc.iterproperties())
  45. self.assertEqual(len(prop_all), 8)
  46. filter_func = lambda x: getattr(x, "name").find("strprop") > -1
  47. prop_filtered = list(self.doc.iterproperties(filter_func=filter_func))
  48. self.assertEqual(len(prop_filtered), 4)
  49. prop_filtered = list(self.doc.iterproperties(filter_func=filter_func, max_depth=2))
  50. self.assertEqual(len(prop_filtered), 3)
  51. prop_filtered = list(self.doc.iterproperties(filter_func=filter_func, max_depth=1))
  52. self.assertEqual(len(prop_filtered), 1)
  53. def test_itervalues(self):
  54. val_all = list(self.doc.itervalues())
  55. self.assertEqual(len(val_all), 8)
  56. filter_func = lambda x: str(x).find("text") > -1
  57. val_filtered = list(self.doc.itervalues(filter_func=filter_func))
  58. self.assertEqual(len(val_filtered), 4)
  59. val_filtered = list(self.doc.itervalues(filter_func=filter_func, max_depth=2))
  60. self.assertEqual(len(val_filtered), 3)
  61. val_filtered = list(self.doc.itervalues(filter_func=filter_func, max_depth=1))
  62. self.assertEqual(len(val_filtered), 1)