[ADD]XML base tests

This commit is contained in:
Fabien BOURGEOIS 2021-02-22 12:55:45 +01:00
parent 44b61deb32
commit 6afa0b7f1b
1 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,118 @@
;; -*- coding: utf-8 -*-
;;
;; Copyright 2019-2021 Fabien Bourgeois <fabien@yaltik.com>
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
b" XML Helpers tests "
(require [hy-odoo.macros.test [*]])
(import unittest
[functools [partial]]
[xml.etree.ElementTree :as ET]
[os [unlink]]
[hy-odoo.xml-base [XMLDictElement xmln xmlroot xmlchild xml-write]])
(defclass TextXMLBase [(. unittest TestCase)]
b"XML Helpers tests"
(defn test-xmln [self]
b"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]
b"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 (. (first subchildren) tag) "subchild"))
(defn test-xmlroot [self]
b"Test xmlroot"
(setv root (xmlroot {"tag" "root" "attrs" {} "children" []}))
(o-assert-is-instance root (. ET Element))
(with [(o-assert-raises-regex TypeError "has no attribute")] (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]
b"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))))
(defmain [] (.main unittest))