# Path: wirecurly/serialize.py
# class XMLFactory(object):
# 	"""Base factory for XML generation
# 
# 	:param data: A dict with the specified format for XML generation
# 	:type data: dict
# 	"""
# 	def __init__(self, data):
# 		super(XMLFactory, self).__init__()
# 		self.data = data
# 		
# 	
# 	def _typecastAttributes(self, d):
# 		''' Typecast attributes to string or unicode
# 
# 		:param d: the attributes dictionary
# 		:type d: dict
# 
# 		:rtype: dict -- The new dict with only strings and unicodes
# 		'''
# 		for key in d:
# 			val = d[key]
# 			if type(val) == bool:
# 				if val == True:
# 					d[key] = 'true'
# 				else:
# 					d[key] = 'false'
# 			elif type(val) == int:
# 				d[key] = str(val)
# 			elif type(val) == float:
# 				d[key] = str(val)
# 			elif val is None:
# 				d[key] = u''
# 		return d
# 
# 	def getXML(self):
# 		'''Get the XML based on the data provided.
# 
# 		:rtype: str - A string generated XML for `data`
# 		'''
# 		self.root = etree.Element(self.data.get('tag'), **self._typecastAttributes(self.data.get('attrs', {})))
# 		if self.data.get('children'):
# 			self._parseChildren(self.data.get('children'), self.root)
# 
# 		return etree.tostring(self.root, pretty_print=True)
# 
# 	def _parseChildren(self, children, parent):
# 		'''
# 			Parse children of the dict to create XML structure
# 		'''
# 		for child in children:
# 			el = etree.SubElement(parent, child.get('tag'), **self._typecastAttributes(child.get('attrs', {})))
# 			if child.get('children'):
# 				self._parseChildren(child.get('children'), el)
# 
# 	def convert(self):
# 		'''
# 			Abstract method that will serialize the XML as per the factory specification
# 		'''
# 		raise NotImplementedError


import logging
import pytest
from lxml import etree
from utils import xml2d
from wirecurly.serialize import XMLFactory
        {'tag': 'domain', 'children': [{'tag': 'params', 'children': [{'tag': 'param', 'attrs': {'name': 'test', 'data': True} ,'children': []}]}, {'tag': 'variables', 'children': []}, {'tag': 'users', 'children': []}], 'attrs': {'name': u'wirephone.com.ar'}},
        '<domain name="wirephone.com.ar"><users></users><params><param name="test" data="true"/></params><variables></variables></domain>'
    )
])
def test_xml_factory(data, expected):
    f = XMLFactory(data)
    response = f.getXML()

    try:
        root = etree.fromstring(response)
        expected_root = etree.fromstring(expected)
    except:
        logging.exception('Invalid XML..')
        pytest.fail("Invalid XML: %s" % response)

    assert xml2d(root) == xml2d(expected_root)


def test_dict_typecasting():
    '''
        Test that we are able to typecast all dict keys and values to string
    '''
    d = {
        'int': 2,
        'bool_true': True,
        'bool_false': False,
        'float': 0.59,
        'unicode': u'João Mesquita'
    }


# Path: wirecurly/serialize.py
# class XMLFactory(object):
# 	"""Base factory for XML generation
# 
# 	:param data: A dict with the specified format for XML generation
# 	:type data: dict
# 	"""
# 	def __init__(self, data):
# 		super(XMLFactory, self).__init__()
# 		self.data = data
# 		
# 	
# 	def _typecastAttributes(self, d):
# 		''' Typecast attributes to string or unicode
# 
# 		:param d: the attributes dictionary
# 		:type d: dict
# 
# 		:rtype: dict -- The new dict with only strings and unicodes
# 		'''
# 		for key in d:
# 			val = d[key]
# 			if type(val) == bool:
# 				if val == True:
# 					d[key] = 'true'
# 				else:
# 					d[key] = 'false'
# 			elif type(val) == int:
# 				d[key] = str(val)
# 			elif type(val) == float:
# 				d[key] = str(val)
# 			elif val is None:
# 				d[key] = u''
# 		return d
# 
# 	def getXML(self):
# 		'''Get the XML based on the data provided.
# 
# 		:rtype: str - A string generated XML for `data`
# 		'''
# 		self.root = etree.Element(self.data.get('tag'), **self._typecastAttributes(self.data.get('attrs', {})))
# 		if self.data.get('children'):
# 			self._parseChildren(self.data.get('children'), self.root)
# 
# 		return etree.tostring(self.root, pretty_print=True)
# 
# 	def _parseChildren(self, children, parent):
# 		'''
# 			Parse children of the dict to create XML structure
# 		'''
# 		for child in children:
# 			el = etree.SubElement(parent, child.get('tag'), **self._typecastAttributes(child.get('attrs', {})))
# 			if child.get('children'):
# 				self._parseChildren(child.get('children'), el)
# 
# 	def convert(self):
# 		'''
# 			Abstract method that will serialize the XML as per the factory specification
# 		'''
# 		raise NotImplementedError


# Path: tests/test_serialization/test_xmlfactory.py
import logging
import pytest
from lxml import etree
from utils import xml2d
from wirecurly.serialize import XMLFactory

        {'tag': 'domain', 'children': [{'tag': 'params', 'children': [{'tag': 'param', 'attrs': {'name': 'test', 'data': True} ,'children': []}]}, {'tag': 'variables', 'children': []}, {'tag': 'users', 'children': []}], 'attrs': {'name': u'wirephone.com.ar'}},
        '<domain name="wirephone.com.ar"><users></users><params><param name="test" data="true"/></params><variables></variables></domain>'
    )
])
def test_xml_factory(data, expected):
    f = XMLFactory(data)
    response = f.getXML()

    try:
        root = etree.fromstring(response)
        expected_root = etree.fromstring(expected)
    except:
        logging.exception('Invalid XML..')
        pytest.fail("Invalid XML: %s" % response)

    assert xml2d(root) == xml2d(expected_root)


def test_dict_typecasting():
    '''
        Test that we are able to typecast all dict keys and values to string
    '''
    d = {
        'int': 2,
        'bool_true': True,
        'bool_false': False,
        'float': 0.59,
        'unicode': u'João Mesquita'
    }


    f = XMLFactory({})