iterators.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import unittest
  2. from datetime import datetime as dt
  3. from odml import Property, Section, Document
  4. longago = dt.strptime('2013-08-11 09:48:49', "%Y-%m-%d %H:%M:%S")
  5. recently = dt.strptime('2014-02-25 14:42:13', "%Y-%m-%d %H:%M:%S")
  6. class TestValidation(unittest.TestCase):
  7. def setUp(self):
  8. """
  9. doc -- <section foo> -- <section subfoo>
  10. \
  11. -- <section bar> -- <section subbar>
  12. """
  13. doc = Document("author")
  14. foo = Section("foo", "footype")
  15. doc.append(foo)
  16. foo.append(Property("strprop", "somestring"))
  17. foo.append(Property("txtprop", "some\ntext"))
  18. subfoo = Section("subfoo", "footype")
  19. foo.append(subfoo)
  20. subfoo.append(Property("strprop", "somestring"))
  21. subfoo.append(Property("txtprop", "some\ntext"))
  22. bar = Section("bar", "bartype")
  23. foo.append(bar)
  24. bar.append(Property("strprop", "otherstring"))
  25. bar.append(Property("txtprop", "other\ntext"))
  26. subbar = Section("subbar", "bartype")
  27. bar.append(subbar)
  28. subbar.append(Property("strprop", "otherstring"))
  29. subbar.append(Property("txtprop", "other\ntext"))
  30. self.doc = doc
  31. def test_itersections(self):
  32. sec_all = self.doc.itersections()
  33. assert(len([s for s in sec_all]) == 4)
  34. filter_func = lambda x: getattr(x, 'name') == "foo"
  35. sec_filt = self.doc.itersections(filter_func=filter_func)
  36. assert(len([s for s in sec_filt]) == 1)
  37. filter_func = lambda x: getattr(x, 'type').find("bar") > -1
  38. sec_filt = self.doc.itersections(filter_func=filter_func)
  39. assert(len([s for s in sec_filt]) == 2)
  40. sec_filt = self.doc.itersections(max_depth=2)
  41. assert(len([s for s in sec_filt]) == 3)
  42. sec_filt = self.doc.itersections(max_depth=1)
  43. assert(len([s for s in sec_filt]) == 1)
  44. sec_filt = self.doc.itersections(max_depth=0)
  45. assert(len([s for s in sec_filt]) == 0)
  46. def test_iterproperties(self):
  47. prop_all = self.doc.iterproperties()
  48. assert(len([p for p in prop_all]) == 8)
  49. filter_func = lambda x: getattr(x, 'name').find("strprop") > -1
  50. prop_filt = self.doc.iterproperties(filter_func=filter_func)
  51. assert(len([p for p in prop_filt]) == 4)
  52. prop_filt = self.doc.iterproperties(filter_func=filter_func, max_depth=2)
  53. assert(len([p for p in prop_filt]) == 3)
  54. prop_filt = self.doc.iterproperties(filter_func=filter_func, max_depth=1)
  55. assert(len([p for p in prop_filt]) == 1)
  56. def test_itervalues(self):
  57. val_all = self.doc.itervalues()
  58. assert(len([v for v in val_all]) == 8)
  59. filter_func = lambda x: str(getattr(x, 'data')).find("text") > -1
  60. val_filt = self.doc.itervalues(filter_func=filter_func)
  61. assert(len([v for v in val_filt]) == 4)
  62. val_filt = self.doc.itervalues(filter_func=filter_func, max_depth=2)
  63. assert(len([v for v in val_filt]) == 3)
  64. val_filt = self.doc.itervalues(filter_func=filter_func, max_depth=1)
  65. assert(len([v for v in val_filt]) == 1)
  66. if __name__ == '__main__':
  67. unittest.main()