Skip to content

Commit

Permalink
Allow use of empty string as a value for a Attribute
Browse files Browse the repository at this point in the history
tostr will always return a string regardless of input,
no check on value is needed as empty string is allowed.
  • Loading branch information
martin-pil committed Sep 13, 2024
1 parent 049d2fc commit c574acb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
6 changes: 2 additions & 4 deletions suds/mx/appender.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ def append(self, parent, content):
if content.tag.startswith('_') and isinstance(content.type, Attribute):
attr = content.tag[1:]
value = tostr(content.value)
if value:
parent.set(attr, value)
parent.set(attr, value)
else:
child = self.node(content)
child.setText(tostr(content.value))
Expand Down Expand Up @@ -275,8 +274,7 @@ def append(self, parent, content):
if content.tag.startswith('_') and isinstance(content.type, Attribute):
attr = content.tag[1:]
value = tostr(content.value)
if value:
parent.set(attr, value)
parent.set(attr, value)
else:
child = self.node(content)
child.setText(content.value)
Expand Down
83 changes: 83 additions & 0 deletions tests/test_mx_appender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-

# This program is free software; you can redistribute it and/or modify it under
# the terms of the (LGPL) GNU Lesser 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 Library Lesser General Public License
# for more details at ( http://www.gnu.org/licenses/lgpl.html ).
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: Jurko Gospodnetić ( jurko.gospodnetic@pke.hr )

"""
Suds MX appender unit tests.
Implemented using the 'pytest' testing framework.
"""

if __name__ == "__main__":
import testutils
testutils.run_using_pytest(globals())

from suds.mx.appender import PrimitiveAppender, TextAppender
from suds.xsd.sxbasic import Attribute

from unittest.mock import Mock, patch


class MockContent:
def __init__(self, tag, value):
self.tag = tag
self.value = value
self.type = Mock(spec=Attribute)


class TestPrimitiveAppender:

@patch("suds.mx.appender.Appender.__init__", return_value=None)
def test_append_string(self, _):
parent = Mock()
content = MockContent("_TEST_TAG", "")
primitive_appender = PrimitiveAppender()
primitive_appender.append(parent, content)
parent.set.assert_called_once_with(content.tag.lstrip("_"), content.value)

@patch("suds.mx.appender.Appender.__init__", return_value=None)
def test_append_string_to_child(self, _):
parent = Mock()
content = MockContent("TEST_TAG", "")
primitive_appender = PrimitiveAppender()
mock_node = Mock()
primitive_appender.node = Mock(return_value=mock_node)
primitive_appender.append(parent, content)
mock_node.setText.assert_called_once_with(content.value)
parent.append.assert_called_once_with(mock_node)


class TestTextAppender:

@patch("suds.mx.appender.Appender.__init__", return_value=None)
def test_append_string(self, _):
parent = Mock()
content = MockContent("_TEST_TAG", "")
primitive_appender = TextAppender()
primitive_appender.append(parent, content)
parent.set.assert_called_once_with(content.tag.lstrip("_"), content.value)

@patch("suds.mx.appender.Appender.__init__", return_value=None)
def test_append_string_to_child(self, _):
parent = Mock()
content = MockContent("TEST_TAG", "")
primitive_appender = TextAppender()
mock_node = Mock()
primitive_appender.node = Mock(return_value=mock_node)
primitive_appender.append(parent, content)
mock_node.setText.assert_called_once_with(content.value)
parent.append.assert_called_once_with(mock_node)

0 comments on commit c574acb

Please sign in to comment.