test_doc.py 11 KB

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