yaltik_odoo_custom/hy_odoo/tests/test_xml.hy

121 lines
4.5 KiB
Hy
Raw Normal View History

;; Copyright 2019-2022 Fabien Bourgeois <fabien@yaltik.com>
;;
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at https://mozilla.org/MPL/2.0/.
" XML Helpers tests "
(require
hyrule.control [defmain]
odoo.addons.hy-odoo.mtest *)
(import unittest
functools [partial]
xml.etree.ElementTree :as ET
os [unlink]
odoo.tests [tagged]
odoo.tests.common [TransactionCase]
odoo.addons.hy-odoo.xml [XMLDictElement xmln xmlroot xmlchild xml-write])
(defclass [(tagged "hyodoo" "hyodoo.xml")] TextXMLBase [TransactionCase]
"XML Helpers tests"
(defn test-xmln [self]
"Text xmln"
; XMLDictElement
(o-assert-is-instance (xmln) XMLDictElement)
; Tags
(o-assert-dict-equal (._asdict (xmln)) {"tag" "" "attrs" {} "children" []})
(o-assert-equal (. (xmln "a tag") tag) "a tag")
; Attrs
(o-assert-dict-equal (. (xmln :attrs {"a good" "one"}) attrs) {"a good" "one"})
; Childrens
(o-assert-list-equal (. (xmln :children [1 2 3]) children) [1 2 3])
(o-assert-list-equal (. (xmln :children "Some text") children) ["Some text"])
(with [(o-assert-raises-regex TypeError "Invalid arguments")] (xmln :children False))
; Ensure that only children after tags is managed
(setv element (xmln "tag" {"something" "inside"}))
(o-assert-is-instance (. element attrs) dict)
(o-assert-is-instance (. element children) list)
(o-assert-is-instance element XMLDictElement)
(setv element (xmln "tag" ["something" "inside"]))
(o-assert-is-instance (. element attrs) dict)
(o-assert-is-instance (. element children) list)
(o-assert-is-instance element XMLDictElement))
(defn test-xmlchild [self]
"Test xmlchild"
(setv parent (xmlroot {"tag" "root" "attrs" {} "children" []})
xmlc-par (partial xmlchild parent))
; Bad arguments
(with [(o-assert-raises-regex TypeError "Invalid arguments for xmlchild")] (xmlc-par False))
; Need XMLDictElement, not dict
(with [(o-assert-raises-regex TypeError "Invalid arguments for xmlchild")]
(xmlc-par [{"tag" "t" "attrs" {"a" "b"} "children" []}]))
(xmlc-par ["some text"])
(o-assert-equal (. parent text) "some text")
(xmlc-par[(xmln "t" {"a" "b"} [])])
(setv child (next (.iter parent "t")))
(o-assert-equal (. child tag) "t")
(o-assert-dict-equal (. child attrib) {"a" "b"})
(o-assert-list-equal (list child) [])
(xmlc-par [(xmln "t2" {1 2} [])])
(setv child (next (.iter parent "t2")))
(o-assert-dict-equal (. child attrib) {"1" "2"})
(xmlc-par [(xmln "tchildren" {} [(xmln "subchild" {} [])])])
(setv child (next (.iter parent "tchildren"))
subchildren (list child))
(o-assert-equal (len subchildren) 1)
(o-assert-equal (. (get subchildren 0) tag) "subchild"))
(defn test-xmlroot [self]
"Test xmlroot"
(setv root (xmlroot {"tag" "root" "attrs" {} "children" []}))
(o-assert-is-instance root (. ET Element))
(with [(o-assert-raises-regex TypeError "is not subscriptable")] (xmlroot False))
(with [(o-assert-raises-regex KeyError "tag")] (xmlroot {}))
(with [(o-assert-raises-regex KeyError "attrs")] (xmlroot {"tag" "root"})))
(defn test-xml-write [self]
"Test xml-write"
(setv children [(xmln "child1" {"attr" "value"} [])
(xmln "child2" {} "Some text")]
tree (xmlroot {"tag" "root" "attrs" {} "children" children})
xmlw (fn [p] (xml_write p tree)))
(o-assert-is-none (xmlw "/badpath"))
(o-assert-is-none (xmlw "/bad.ext"))
(xmlw __file__)
(setv filepath (.replace __file__ ".hy" "_views.xml"))
(with [output-file (open filepath "r")]
(setv output-xml (.read output-file))
(o-assert-in "<?xml version" output-xml)
(o-assert-in "<root>" output-xml)
(o-assert-in "<child1 attr=\"value\"/>" output-xml)
(o-assert-in "<child2>Some text</child2>" output-xml)
(unlink filepath))
(xml-write __file__ tree :pretty True :suffix "_data")
(setv filepath (.replace __file__ ".hy" "_data.xml"))
(with [output-file (open filepath "r")]
(setv output-xml (.read output-file))
(o-assert-in "<?xml version" output-xml)
(o-assert-in "<root>" output-xml)
(o-assert-in "<child1 attr=\"value\"/>" output-xml)
(o-assert-in "<child2>Some text</child2>" output-xml)
(unlink filepath))))
(defmain [] (.main unittest))