#!/usr/bin/env python
#
# spyne - Copyright (C) Spyne contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#



import logging
logging.basicConfig(level=logging.DEBUG)

import unittest

from spyne import MethodContext
from spyne.service import ServiceBase
from spyne.server import ServerBase
from spyne.application import Application
from spyne.decorator import srpc
from spyne.model.primitive import Unicode
from spyne.model.complex import ComplexModel
from spyne.model.complex import XmlAttribute
from spyne.service import ServiceBase
from spyne.interface.wsdl import Wsdl11
from spyne.protocol.xml import XmlDocument


from spyne.util.xml import get_xml_as_object
from lxml import etree


class TestXml(unittest.TestCase):
    def test_empty_string(self):
        class a(ComplexModel):
            b = Unicode

        elt = etree.fromstring('<a><b/></a>')
        o = get_xml_as_object(elt, a)

        assert o.b == ''

    def test_attribute_of(self):
        class a(ComplexModel):
            b = Unicode
            c = XmlAttribute(Unicode, attribute_of="b")

        class SomeService(ServiceBase):
            @srpc(_returns=a)
            def some_call():
                return a(b="foo",c="bar")

        app = Application([SomeService], "tns", in_protocol=XmlDocument(), out_protocol=XmlDocument())
        server = ServerBase(app)
        initial_ctx = MethodContext(server)
        initial_ctx.in_string = ['<some_call xmlns="tns"/>']

        ctx, = server.generate_contexts(initial_ctx)
        server.get_in_object(ctx)
        server.get_out_object(ctx)
        server.get_out_string(ctx)

        assert etree.fromstring(''.join(ctx.out_string)).xpath('//s0:b',
            namespaces=app.interface.nsmap)[0].attrib['c'] == "bar"

    def test_attribute_ns(self):
        class a(ComplexModel):
            b = Unicode
            c = XmlAttribute(Unicode, ns="spam", attribute_of="b")

        class SomeService(ServiceBase):
            @srpc(_returns=a)
            def some_call():
                return a(b="foo",c="bar")

        app = Application([SomeService], "tns", in_protocol=XmlDocument(), out_protocol=XmlDocument())
        server = ServerBase(app)
        initial_ctx = MethodContext(server)
        initial_ctx.in_string = ['<some_call xmlns="tns"/>']

        ctx, = server.generate_contexts(initial_ctx)
        server.get_in_object(ctx)
        server.get_out_object(ctx)
        server.get_out_string(ctx)

        assert etree.fromstring(''.join(ctx.out_string)).xpath('//s0:b',
            namespaces=app.interface.nsmap)[0].attrib['{%s}c'%app.interface.nsmap["s1"]] == "bar"


if __name__ == '__main__':
    unittest.main()

@@ -865,16 +865,40 @@
 ittest%0A%0A
+from lxml import etree%0A%0A
 from spy
@@ -1244,48 +1244,8 @@
 ase%0A
-from spyne.interface.wsdl import Wsdl11%0A
 from
@@ -1284,17 +1284,16 @@
 cument%0A%0A
-%0A
 from spy
@@ -1329,39 +1329,16 @@
 _object%0A
-from lxml import etree%0A
 %0A%0Aclass 
@@ -1604,33 +1604,33 @@
 :%0A        class 
-a
+C
 (ComplexModel):%0A
@@ -1633,33 +1633,33 @@
 l):%0A            
-b
+a
  = Unicode%0A     
@@ -1657,33 +1657,33 @@
 ode%0A            
-c
+b
  = XmlAttribute(
@@ -1697,33 +1697,33 @@
 , attribute_of=%22
-b
+a
 %22)%0A%0A        clas
@@ -1760,32 +1760,35 @@
           @srpc(
+C, 
 _returns=a)%0A    
@@ -1772,33 +1772,33 @@
 rpc(C, _returns=
-a
+C
 )%0A            de
@@ -1801,32 +1801,33 @@
   def some_call(
+c
 ):%0A             
@@ -1833,82 +1833,183 @@
     
-return a(b=%22foo%22,c=%22bar%22)%0A%0A        app = Application(%5BSomeService%5D, %22tns%22,
+assert c.a == 'a'%0A                assert c.b == 'b'%0A                return c%0A%0A        app = Application(%5BSomeService%5D, %22tns%22, name=%22test_attribute_of%22,%0A                       
  in_
@@ -2162,32 +2162,45 @@
 tx.in_string = %5B
+%0A            
 '%3Csome_call xmln
@@ -2198,35 +2198,152 @@
 call xmlns=%22tns%22
-/
 %3E'
+%0A                '%3Cc%3E'%0A                    '%3Ca b=%22b%22%3Ea%3C/a%3E'%0A                '%3C/c%3E'%0A            '%3C/some_call%3E'%0A        
 %5D%0A%0A        ctx, 
@@ -2487,38 +2487,37 @@
 g(ctx)%0A%0A        
-assert
+ret =
  etree.fromstrin
@@ -2547,33 +2547,33 @@
 g)).xpath('//s0:
-b
+a
 ',%0A            n
@@ -2597,32 +2597,136 @@
 erface.nsmap)%5B0%5D
+%0A        print etree.tostring(ret, pretty_print=True)%0A%0A        assert ret.text == %22a%22%0A        assert ret
 .attrib%5B'c'%5D == 
@@ -2722,17 +2722,17 @@
 ib%5B'
-c
+b
 '%5D == %22b
 ar%22%0A
@@ -2727,18 +2727,16 @@
 '%5D == %22b
-ar
 %22%0A%0A    d

modify attribute_of test to fail on round-trip message. will be fixed.