test_doc.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import datetime
  2. import os
  3. import unittest
  4. try:
  5. from urllib.request import pathname2url
  6. except ImportError:
  7. from urllib import pathname2url
  8. from odml import Document, Section, Property
  9. from odml.doc import BaseDocument
  10. from odml.dtypes import FORMAT_DATE
  11. class TestSection(unittest.TestCase):
  12. def setUp(self):
  13. pass
  14. def test_simple_attributes(self):
  15. author = "HPL"
  16. version = "4.8.15"
  17. doc = Document(author=author, version=version)
  18. self.assertEqual(doc.author, author)
  19. self.assertEqual(doc.version, version)
  20. doc.author = ""
  21. doc.version = ""
  22. self.assertIsNone(doc.author)
  23. self.assertIsNone(doc.version)
  24. doc.author = author
  25. doc.version = version
  26. self.assertEqual(doc.author, author)
  27. self.assertEqual(doc.version, version)
  28. def test_id(self):
  29. doc = Document()
  30. self.assertIsNotNone(doc.id)
  31. doc = Document("D", oid="79b613eb-a256-46bf-84f6-207df465b8f7")
  32. self.assertEqual(doc.id, "79b613eb-a256-46bf-84f6-207df465b8f7")
  33. doc = Document("D", oid="id")
  34. self.assertNotEqual(doc.id, "id")
  35. # Make sure id cannot be reset programmatically.
  36. with self.assertRaises(AttributeError):
  37. doc.id = "someId"
  38. def test_new_id(self):
  39. doc = Document()
  40. old_id = doc.id
  41. # Test assign new generated id.
  42. doc.new_id()
  43. self.assertNotEqual(old_id, doc.id)
  44. # Test assign new custom id.
  45. old_id = doc.id
  46. doc.new_id("79b613eb-a256-46bf-84f6-207df465b8f7")
  47. self.assertNotEqual(old_id, doc.id)
  48. self.assertEqual("79b613eb-a256-46bf-84f6-207df465b8f7", doc.id)
  49. # Test invalid custom id exception.
  50. with self.assertRaises(ValueError):
  51. doc.new_id("crash and burn")
  52. def test_date(self):
  53. datestring = "2000-01-02"
  54. doc = Document(date=datestring)
  55. self.assertIsInstance(doc.date, datetime.date)
  56. self.assertEqual(doc.date,
  57. datetime.datetime.strptime(datestring, FORMAT_DATE).date())
  58. doc.date = None
  59. self.assertIsNone(doc.date)
  60. doc.date = datestring
  61. self.assertIsInstance(doc.date, datetime.date)
  62. self.assertEqual(doc.date,
  63. datetime.datetime.strptime(datestring, FORMAT_DATE).date())
  64. doc.date = []
  65. self.assertIsNone(doc.date)
  66. doc.date = {}
  67. self.assertIsNone(doc.date)
  68. doc.date = ()
  69. self.assertIsNone(doc.date)
  70. doc.date = ""
  71. self.assertIsNone(doc.date)
  72. with self.assertRaises(ValueError):
  73. doc.date = "some format"
  74. def test_get_terminology_equivalent(self):
  75. dir_path = os.path.dirname(os.path.realpath(__file__))
  76. repo_file = os.path.join(dir_path, "resources",
  77. "local_repository_file_v1.1.xml")
  78. local_url = "file://%s" % pathname2url(repo_file)
  79. doc = Document(repository=local_url)
  80. teq = doc.get_terminology_equivalent()
  81. self.assertIsInstance(teq, BaseDocument)
  82. self.assertEqual(len(teq), 1)
  83. self.assertEqual(teq.sections[0].name, "Repository test")
  84. doc.repository = None
  85. self.assertIsNone(doc.get_terminology_equivalent())
  86. def test_append(self):
  87. doc = Document()
  88. self.assertListEqual(doc.sections, [])
  89. # Test append Section
  90. sec = Section(name="sec_one")
  91. doc.append(sec)
  92. self.assertEqual(len(doc.sections), 1)
  93. self.assertEqual(sec.parent, doc)
  94. # Test fail on Section list or tuple append
  95. with self.assertRaises(ValueError):
  96. doc.append([Section(name="sec_two"), Section(name="sec_three")])
  97. with self.assertRaises(ValueError):
  98. doc.append((Section(name="sec_two"), Section(name="sec_three")))
  99. self.assertEqual(len(doc.sections), 1)
  100. # Test fail on unsupported value
  101. with self.assertRaises(ValueError):
  102. doc.append(Document())
  103. with self.assertRaises(ValueError):
  104. doc.append("Section")
  105. with self.assertRaises(ValueError):
  106. doc.append(Property(name="prop"))
  107. # Test fail on same name entities
  108. with self.assertRaises(KeyError):
  109. doc.append(Section(name="sec_one"))
  110. self.assertEqual(len(doc.sections), 1)
  111. def test_extend(self):
  112. doc = Document()
  113. self.assertListEqual(doc.sections, [])
  114. # Test extend with Section list
  115. doc.extend([Section(name="sec_one"), Section(name="sec_two")])
  116. self.assertEqual(len(doc), 2)
  117. self.assertEqual(len(doc.sections), 2)
  118. self.assertEqual(doc.sections[0].name, "sec_one")
  119. # Test fail on non iterable
  120. with self.assertRaises(TypeError):
  121. doc.extend(1)
  122. self.assertEqual(len(doc.sections), 2)
  123. # Test fail on non Section entry
  124. with self.assertRaises(ValueError):
  125. doc.extend([Document()])
  126. with self.assertRaises(ValueError):
  127. doc.extend([Property(name="prop")])
  128. with self.assertRaises(ValueError):
  129. doc.extend([5])
  130. self.assertEqual(len(doc.sections), 2)
  131. # Test fail on same name entities
  132. with self.assertRaises(KeyError):
  133. doc.extend([Section(name="sec_three"), Section(name="sec_one")])
  134. self.assertEqual(len(doc.sections), 2)
  135. def test_insert(self):
  136. doc = Document()
  137. sec_one = Section(name="sec_one", parent=doc)
  138. sec_two = Section(name="sec_two", parent=doc)
  139. subsec = Section(name="sec_three")
  140. self.assertNotEqual(doc.sections[1].name, subsec.name)
  141. doc.insert(1, subsec)
  142. self.assertEqual(len(doc.sections), 3)
  143. self.assertEqual(doc.sections[1].name, subsec.name)
  144. self.assertEqual(doc.sections[0].name, sec_one.name)
  145. self.assertEqual(doc.sections[2].name, sec_two.name)
  146. self.assertEqual(subsec.parent, doc)
  147. # Test invalid object
  148. with self.assertRaises(ValueError):
  149. doc.insert(1, Document())
  150. with self.assertRaises(ValueError):
  151. doc.insert(1, Property(name="prop_one"))
  152. with self.assertRaises(ValueError):
  153. doc.insert(1, "some info")
  154. self.assertEqual(len(doc), 3)
  155. # Test same name entries
  156. with self.assertRaises(ValueError):
  157. doc.insert(0, subsec)
  158. self.assertEqual(len(doc), 3)
  159. def test_comparison(self):
  160. doc_auth = "author"
  161. doc_ver = "ver1.0"
  162. doc_a = Document(author=doc_auth, version=doc_ver)
  163. doc_b = Document(author=doc_auth, version=doc_ver)
  164. self.assertEqual(doc_a, doc_b)
  165. doc_b.author = "someone else"
  166. self.assertNotEqual(doc_a, doc_b)
  167. doc_b.author = doc_auth
  168. # Test section equality with subsections
  169. # Test equality with subsection of different entities
  170. # with same content and same children order
  171. sec_type = "sec type"
  172. sec_def = "an odml test section"
  173. sec_ref = "from over there"
  174. subsec_aa = Section(name="subsecA", type=sec_type,
  175. definition=sec_def, reference=sec_ref)
  176. subsec_ab = Section(name="subsecB", type=sec_type,
  177. definition=sec_def, reference=sec_ref)
  178. subsec_ba = Section(name="subsecA", type=sec_type,
  179. definition=sec_def, reference=sec_ref)
  180. subsec_bb = Section(name="subsecB", type=sec_type,
  181. definition=sec_def, reference=sec_ref)
  182. doc_a.extend([subsec_aa, subsec_ab])
  183. doc_b.extend([subsec_ba, subsec_bb])
  184. self.assertEqual(doc_a, doc_b)
  185. self.assertEqual(doc_a.sections, doc_b.sections)
  186. doc_b.sections[0].name = "newSubsecA"
  187. self.assertNotEqual(doc_a, doc_b)
  188. self.assertNotEqual(doc_a.sections, doc_b.sections)
  189. doc_b.sections[0].name = "subsecA"
  190. # Test inequality with different number of children
  191. doc_b.remove(doc_b.sections[1])
  192. self.assertNotEqual(doc_a, doc_b)
  193. self.assertNotEqual(doc_a.sections, doc_b.sections)
  194. # Test equality with subsection of different entities
  195. # with same content and different children order
  196. doc_b.remove(doc_b.sections[0])
  197. doc_b.extend([subsec_bb, subsec_ba])
  198. self.assertEqual(doc_a, doc_b)
  199. self.assertEqual(doc_a.sections, doc_b.sections)
  200. doc_b.sections[0].name = "newSubsecB"
  201. self.assertNotEqual(doc_a, doc_b)
  202. self.assertNotEqual(doc_a.sections, doc_b.sections)
  203. doc_b.sections[0].name = "subsecB"
  204. # Test section equality with properties
  205. # Test equality with properties of different entities
  206. # with same content and same children order
  207. prop_aa = Property(name="propA", definition="propDef")
  208. prop_ab = Property(name="propB", definition="propDef")
  209. prop_ba = Property(name="propA", definition="propDef")
  210. prop_bb = Property(name="propB", definition="propDef")
  211. doc_a.sections["subsecA"].extend([prop_aa, prop_ab])
  212. doc_b.sections["subsecA"].extend([prop_ba, prop_bb])
  213. self.assertEqual(doc_a, doc_b)
  214. doc_b.sections["subsecA"].properties[0].name = "newPropA"
  215. self.assertNotEqual(doc_a, doc_b)
  216. doc_b.sections["subsecA"].properties[0].name = "propA"
  217. # Test inequality with different number of children
  218. doc_b.sections["subsecA"].remove(doc_b.sections["subsecA"].properties[1])
  219. self.assertNotEqual(doc_a, doc_b)
  220. # Test equality with properties of different entities
  221. # with same content and different children order
  222. doc_b.sections["subsecA"].remove(doc_b.sections["subsecA"].properties[0])
  223. doc_b.sections["subsecA"].extend([prop_bb, prop_ba])
  224. self.assertEqual(doc_a, doc_b)
  225. doc_b.sections["subsecA"].properties[0].name = "newPropB"
  226. self.assertNotEqual(doc_a, doc_b)
  227. def test_create_section(self):
  228. root = Document()
  229. self.assertEqual(len(root.sections), 0)
  230. name = "subsec"
  231. type = "subtype"
  232. oid = "79b613eb-a256-46bf-84f6-207df465b8f7"
  233. subsec = root.create_section(name, type, oid)
  234. self.assertEqual(len(root.sections), 1)
  235. self.assertEqual(subsec.parent, root)
  236. self.assertEqual(root.sections[name], subsec)
  237. self.assertEqual(root.sections[name].type, type)
  238. self.assertEqual(root.sections[name].oid, oid)
  239. name = "othersec"
  240. subsec = root.create_section(name)
  241. self.assertEqual(len(root.sections), 2)
  242. self.assertEqual(subsec.parent, root)
  243. self.assertEqual(root.sections[name], subsec)
  244. self.assertEqual(root.sections[name].type, "undefined")
  245. name = "subsubsec"
  246. subsec = root.sections[0].create_section(name)
  247. self.assertEqual(len(root.sections), 2)
  248. self.assertEqual(subsec.parent, root.sections[0])
  249. self.assertEqual(len(root.sections[0].sections), 1)
  250. self.assertEqual(root.sections[0].sections[0].name, name)