示例#1
0
def test_element_lxml_name_with_namespace(string_type):
    surname_elem = Element('Surname', element_type=string_type)
    surname_elem.namespace = 'foo.bar.com'

    namespaces = {'foo.bar.com': 'f'}

    assert surname_elem.lxml_name(namespaces) == 'f:Surname'
示例#2
0
def test_element_add_child_must_add_properly(complex_type, string_type):
    name_elem = complex_type.children[0]
    surname_elem = Element('Surname', element_type=string_type)

    f = Element(name='Employee', element_type=complex_type)
    f.add_child(surname_elem)

    assert f.children == [name_elem, surname_elem]
示例#3
0
def test_element_with_in_operator(string_type):
    fields = [Element(name='Name', element_type=string_type)]

    f = Element(name='Name', element_type=string_type)
    f2 = Element(name='Surname', element_type=string_type)

    assert f in fields
    assert f2 not in fields
示例#4
0
def test_element_get_child_by_name_must_returns_expected_child():
    element_type = ElementType('xsd:string')
    name_elem = Element(name='Name', element_type=element_type)
    surname_elem = Element(name='Surname', element_type=element_type)
    f = ElementType(name='ModelType', children=[name_elem, surname_elem])

    assert f.get_child_by_name('Surname') == surname_elem
    assert f.get_child_by_name('SurnameNotFound') is None
示例#5
0
def test_element_type_same_children_in_different_order_must_have_different_hash(
):
    element_type = ElementType('xsd:string')
    child1 = Element(name='Name', element_type=element_type)
    child2 = Element(name='Owner', element_type=element_type)

    f = ElementType(name='ModelType', children=[child1, child2])
    f2 = ElementType(name='ModelType', children=[child2, child1])

    assert f != f2
    assert hash(f) != hash(f2)
示例#6
0
def test_element_type_change_children_must_recalculate_hash():
    element_type = ElementType('xsd:string')
    children = [Element(name='Name', element_type=element_type)]

    f = ElementType(name='ModelType', children=children)
    current_hash = hash(f)

    owner = Element(name='Owner', element_type=element_type)
    f.add_child(owner)

    assert hash(f) != current_hash
示例#7
0
def test_element_changed_method_must_recalculate_hash(complex_type,
                                                      string_type):
    f = Element(name='Employee', element_type=complex_type)
    current_hash = hash(f)

    complex_type.children = complex_type.children + [
        Element('Surname', element_type=string_type)
    ]
    assert hash(f) == current_hash

    f.changed()

    assert hash(f) != current_hash
示例#8
0
def test_element_similarity_with_equal_elements_must_return_as_expected():
    element_type = ElementType('xsd:string')

    name_elem = Element(name='Name', element_type=element_type)
    surname_elem = Element(name='Surname', element_type=element_type)

    first = ElementType(name='Contract', children=[name_elem, surname_elem])
    first.annotation = 'foo'

    second = ElementType(name='Contract', children=[name_elem, surname_elem])
    second.annotation = 'foo'

    assert first.similarity(second) == 1.0
示例#9
0
def test_element_type_add_child_must_add_item_properly():
    element_type = ElementType('xsd:string')
    children = [Element(name='Name', element_type=element_type)]

    f = ElementType(name='ModelType', children=children)
    current_hash = hash(f)

    code = Element(name='Code', element_type=element_type)
    f.add_child(code)

    assert hash(f) != current_hash

    assert f.children[0] == code
示例#10
0
def test_element_children_original_order_must_keep_order():
    element_type = ElementType('xsd:string')

    # A not alphabetical order...
    children = [
        Element(name='Surname', element_type=element_type),
        Element(name='Name', element_type=element_type),
        Element(name='City', element_type=element_type),
    ]

    f = ElementType(name='ModelType', children=children)

    assert f.children != children
    assert f.children_original_order == children
示例#11
0
def test_element_to_lxml_element_must_return_expected_tag(
        string_type, lxml_root):
    el = Element('Surname', element_type=string_type)

    el.to_lxml_element(lxml_root)

    result = et.tostring(lxml_root).decode()

    expected = (
        '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">'
        '<xsl:element name="Surname">autogenerated</xsl:element>'
        '</xsl:stylesheet>')

    assert result == expected
示例#12
0
def test_element_remove_child_must_remove_properly():
    element_type = ElementType('xsd:string')
    name_elem = Element(name='Name', element_type=element_type)
    surname_elem = Element(name='Surname', element_type=element_type)
    f = ElementType(name='ModelType', children=[name_elem, surname_elem])

    # Removing surname
    f.remove_child(surname_elem)

    assert f.children == [name_elem]

    # It's hash must be changed
    f_removed = ElementType(name='ModelType', children=[name_elem])
    assert f_removed == f
示例#13
0
def test_element_to_lxml_element_with_children_must_use_original_order_to_add_it(
        string_type, lxml_root):
    children = [
        Element('Name', element_type=string_type),
        Element('Surname', element_type=string_type),
        Element('Owner', element_type=string_type),
        Element('Country', element_type=string_type),
    ]

    el_type = ElementType('EmployeeType', children=children)

    el = Element('Employee', element_type=el_type)

    el.to_lxml_element(lxml_root)

    result = et.tostring(lxml_root).decode()

    # Same order that was added to element type
    expected = (
        '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">'
        '<xsl:element name="Employee">'
        '<xsl:element name="Name">autogenerated</xsl:element>'
        '<xsl:element name="Surname">autogenerated</xsl:element>'
        '<xsl:element name="Owner">autogenerated</xsl:element>'
        '<xsl:element name="Country">autogenerated</xsl:element>'
        '</xsl:element>'
        '</xsl:stylesheet>')

    assert result == expected
示例#14
0
def test_element_to_lxml_element_with_restriction_enum_filled_in_type_must_return_expected_tag_with_value(
        string_type, lxml_root):
    string_type.restriction_enum = ('Perez', 'Silva')
    el = Element('Surname', element_type=string_type)
    el.to_lxml_element(lxml_root)

    result = et.tostring(lxml_root).decode()

    expected = (
        '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">'
        '<!--CHECK! Created with first element of restriction from type spec-->'
        '<xsl:element name="Surname">Perez</xsl:element>'
        '</xsl:stylesheet>')

    assert result == expected
示例#15
0
def test_element_to_lxml_element_with_default_attr_must_return_expected_tag_with_value(
        string_type, lxml_root):
    el = Element('Surname',
                 element_type=string_type,
                 attrs={'default': 'Silva'})
    el.to_lxml_element(lxml_root)

    result = et.tostring(lxml_root).decode()

    expected = (
        '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">'
        '<!--Created with default value--><xsl:element name="Surname">Silva</xsl:element>'
        '</xsl:stylesheet>')

    assert result == expected
示例#16
0
def test_similarity_must_return_expected_ratio_considering_children():
    element_type = ElementType('xsd:string')

    name_elem = Element(name='Name', element_type=element_type)
    surname_elem = Element(name='Surname', element_type=element_type)

    f = ElementType(name='ContactType', children=[name_elem, surname_elem])

    other_name_elem = Element(name='Name', element_type=element_type)
    f2 = ElementType(name='OtherContactType',
                     children=[other_name_elem, surname_elem])

    assert f2.similarity(f) > 0.9

    other_name_elem.name = 'FirstName'

    assert f2.similarity(f) > 0.8
示例#17
0
def test_element_similarity_must_return_expected_ratio(string_type):
    e = Element(name='Name', element_type=string_type)
    new_e = Element(name='NewName', element_type=string_type)

    assert e.similarity(new_e) == 0.8636363636363636

    e = Element(name='Name', element_type=string_type)
    new_e = Element(name='NewOther', element_type=string_type)

    assert e.similarity(new_e) == 0.6666666666666666
示例#18
0
def test_create_new_element(string_type):
    f = Element(name='Name', element_type=string_type)

    assert f.name == 'Name'
    assert f.element_type == string_type
    assert f.children == []
    assert str(f) == 'Name-xsd:string'
    assert f.self_hash == hash(str(f))
    assert f.type_hash == hash(f.element_type)
示例#19
0
def test_element_type_with_same_elements_must_be_equal():
    element_type = ElementType('xsd:string')
    children = [Element(name='Name', element_type=element_type)]

    f = ElementType(name='ModelType', children=children)
    f2 = ElementType(name='ModelType', children=children)

    assert f == f2
    assert hash(f) == hash(f2)
示例#20
0
def test_element_fill_with_must_copy_all_data_from_other_element():
    element_type = ElementType('xsd:string')

    name_elem = Element(name='Name', element_type=element_type)
    surname_elem = Element(name='Surname', element_type=element_type)

    f = ElementType(name='ModelType', children=[name_elem, surname_elem])
    f.annotation = 'A simple documentation for type'

    f2 = ElementType('')

    f2.fill_with(f)

    assert f2.name == f.name
    assert f2.children == f.children

    assert f2.annotation == 'A simple documentation for type'
    assert f2.children_original_order == f.children_original_order
示例#21
0
def test_element_to_lxml_element_with_children_must_return_expected_tags(
        complex_type, string_type, lxml_root):
    complex_type.add_child(Element('Surname', element_type=string_type))
    f = Element('Employee', element_type=complex_type)

    f.to_lxml_element(lxml_root)

    result = et.tostring(lxml_root).decode()

    expected = (
        '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">'
        '<xsl:element name="Employee">'
        '<xsl:element name="Name">autogenerated</xsl:element>'
        '<xsl:element name="Surname">autogenerated</xsl:element>'
        '</xsl:element>'
        '</xsl:stylesheet>')

    assert result == expected
示例#22
0
def test_element_get_child_by_name(complex_type, string_type):
    surname = Element('Surname', element_type=string_type)

    f = Element(name='Employee', element_type=complex_type)
    f.add_child(surname)

    assert f.get_child_by_name('Surname') == surname
    assert f.get_child_by_name('SurnameNotFound') is None
示例#23
0
def test_element_to_lxml_element_with_copy_of_must_return_expected_tags(
        string_type, lxml_root):
    el = Element('Surname', element_type=string_type)
    el.copy_of = '/Foo/Surname'

    el.to_lxml_element(lxml_root)

    result = et.tostring(lxml_root).decode()

    expected = (
        '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">'
        '<xsl:if test="/Foo/Surname">'
        '<xsl:copy-of select="/Foo/Surname"/>'
        '</xsl:if>'
        '<xsl:if test="not(/Foo/Surname)">'
        '<xsl:element name="Surname">autogenerated</xsl:element>'
        '</xsl:if>'
        '</xsl:stylesheet>')

    assert result == expected
示例#24
0
def test_element_children_original_order_must_keep_order_when_added_child():
    element_type = ElementType('xsd:string')

    # A not alphabetical order...
    children = [
        Element(name='Surname', element_type=element_type),
        Element(name='Name', element_type=element_type),
        Element(name='City', element_type=element_type),
    ]

    f = ElementType(name='ModelType', children=children)

    nick = Element(name='Nickname', element_type=element_type)

    f.add_child(nick)

    expected_children = children + [nick]

    assert f.children != expected_children
    assert f.children_original_order == expected_children
示例#25
0
def test_element_is_similar_must_return_true_for_equal_elements(complex_type):
    e = Element(name='Employee', element_type=complex_type)
    new_e = Element(name='Employee', element_type=complex_type)

    assert new_e.is_similar(e)
示例#26
0
def complex_type(string_type):
    return ElementType('EmployeeType',
                       children=[Element('Name', element_type=string_type)])
示例#27
0
def test_element_with_same_values_must_be_equal(string_type):
    f = Element(name='Model', element_type=string_type)
    f2 = Element(name='Model', element_type=string_type)

    assert f == f2
    assert hash(f) == hash(f2)
示例#28
0
def test_element_lxml_name(string_type):
    surname_elem = Element('Surname', element_type=string_type)

    assert surname_elem.lxml_name() == 'Surname'
示例#29
0
def test_element_of_complex_type_with_same_values_must_be_equal(complex_type):
    f = Element(name='Employee', element_type=complex_type)
    f2 = Element(name='Employee', element_type=complex_type)

    assert f == f2
    assert hash(f) == hash(f2)
示例#30
0
def test_element_similarity_with_equal_elements_must_return_as_expected(
        string_type):
    e = Element(name='Name', element_type=string_type)
    new_e = Element(name='Name', element_type=string_type)

    assert e.similarity(new_e) == 1.0