<s> import unittest <EOL> from gluon import * <EOL> from gluon . contrib import simplejson as json <EOL> try : <EOL> from cStringIO import StringIO <EOL> except : <EOL> from StringIO import StringIO <EOL> from lxml import etree <EOL> from s3 import S3Hierarchy , s3_meta_fields , S3Represent , S3XMLFormat , IS_ONE_OF <EOL> class TreeBuilderTests ( unittest . TestCase ) : <EOL> def testEmptyTree ( self ) : <EOL> xml = current . xml <EOL> tree = xml . tree ( None ) <EOL> root = tree . getroot ( ) <EOL> attrib = root . attrib <EOL> self . assertEqual ( root . tag , xml . TAG . root ) <EOL> self . assertEqual ( len ( attrib ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( attrib [ "<STR_LIT:success>" ] , "<STR_LIT:false>" ) <EOL> def testIncludeMaxBounds ( self ) : <EOL> xml = current . xml <EOL> tree = xml . tree ( None , maxbounds = True ) <EOL> root = tree . getroot ( ) <EOL> attrib = root . attrib <EOL> self . assertEqual ( root . tag , xml . TAG . root ) <EOL> self . assertEqual ( len ( attrib ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( attrib [ "<STR_LIT:success>" ] , "<STR_LIT:false>" ) <EOL> self . assertTrue ( "<STR_LIT>" in attrib ) <EOL> self . assertTrue ( "<STR_LIT>" in attrib ) <EOL> self . assertTrue ( "<STR_LIT>" in attrib ) <EOL> self . assertTrue ( "<STR_LIT>" in attrib ) <EOL> class JSONMessageTests ( unittest . TestCase ) : <EOL> def testDefaultSuccessMessage ( self ) : <EOL> """<STR_LIT>""" <EOL> json_message = current . xml . json_message <EOL> msg = json_message ( ) <EOL> msg = json . loads ( msg ) <EOL> self . assertEqual ( len ( msg ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( msg [ "<STR_LIT:status>" ] , "<STR_LIT:success>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> def testDefaultErrorMessage ( self ) : <EOL> """<STR_LIT>""" <EOL> json_message = current . xml . json_message <EOL> msg = json_message ( False ) <EOL> msg = json . loads ( msg ) <EOL> self . assertEqual ( len ( msg ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( msg [ "<STR_LIT:status>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> def testExtendedSuccessMessage ( self ) : <EOL> """<STR_LIT>""" <EOL> json_message = current . xml . json_message <EOL> msg = json_message ( True , message = "<STR_LIT>" ) <EOL> msg = json . loads ( msg ) <EOL> self . assertEqual ( len ( msg ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( msg [ "<STR_LIT:status>" ] , "<STR_LIT:success>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT:message>" ] , "<STR_LIT>" ) <EOL> def testExtendedSuccessMessageWithResultNumber ( self ) : <EOL> """<STR_LIT>""" <EOL> json_message = current . xml . json_message <EOL> msg = json_message ( True , message = "<STR_LIT>" , results = <NUM_LIT> ) <EOL> msg = json . loads ( msg ) <EOL> self . assertEqual ( len ( msg ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( msg [ "<STR_LIT:status>" ] , "<STR_LIT:success>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT:message>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> def testExtendedSuccessMessageWithSenderID ( self ) : <EOL> """<STR_LIT>""" <EOL> json_message = current . xml . json_message <EOL> msg = json_message ( True , message = "<STR_LIT>" , sender = "<STR_LIT>" ) <EOL> msg = json . loads ( msg ) <EOL> self . assertEqual ( len ( msg ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( msg [ "<STR_LIT:status>" ] , "<STR_LIT:success>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT:message>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> def testExtendedSuccessMessageWithCreatedIDs ( self ) : <EOL> """<STR_LIT>""" <EOL> json_message = current . xml . json_message <EOL> msg = json_message ( True , message = "<STR_LIT>" , created = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> msg = json . loads ( msg ) <EOL> self . assertEqual ( len ( msg ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( msg [ "<STR_LIT:status>" ] , "<STR_LIT:success>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT:message>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def testExtendedSuccessMessageWithUpdatedIDs ( self ) : <EOL> """<STR_LIT>""" <EOL> json_message = current . xml . json_message <EOL> msg = json_message ( True , message = "<STR_LIT>" , updated = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> msg = json . loads ( msg ) <EOL> self . assertEqual ( len ( msg ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( msg [ "<STR_LIT:status>" ] , "<STR_LIT:success>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT:message>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def testExtendedErrorMessage ( self ) : <EOL> """<STR_LIT>""" <EOL> json_message = current . xml . json_message <EOL> msg = json_message ( False , <NUM_LIT> , message = "<STR_LIT>" ) <EOL> msg = json . loads ( msg ) <EOL> self . assertEqual ( len ( msg ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( msg [ "<STR_LIT:status>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT:message>" ] , "<STR_LIT>" ) <EOL> def testExtendedErrorMessageWithTree ( self ) : <EOL> """<STR_LIT>""" <EOL> json_message = current . xml . json_message <EOL> msg = json_message ( False , <NUM_LIT> , message = "<STR_LIT>" , tree = '<STR_LIT>' ) <EOL> msg = json . loads ( msg ) <EOL> self . assertEqual ( len ( msg ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( msg [ "<STR_LIT:status>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( msg [ "<STR_LIT:message>" ] , "<STR_LIT>" ) <EOL> self . assertTrue ( isinstance ( msg [ "<STR_LIT>" ] , dict ) ) <EOL> tree = msg [ "<STR_LIT>" ] <EOL> self . assertEqual ( len ( tree ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( tree [ "<STR_LIT:test>" ] , "<STR_LIT:value>" ) <EOL> class XMLFormatTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> xmlstr = """<STR_LIT>""" <EOL> stylesheet = """<STR_LIT>""" <EOL> self . tree = etree . ElementTree ( etree . fromstring ( xmlstr ) ) <EOL> self . stylesheet = S3XMLFormat ( StringIO ( stylesheet ) ) <EOL> def testSelectAllFields ( self ) : <EOL> include , exclude = self . stylesheet . get_fields ( "<STR_LIT>" ) <EOL> self . assertEqual ( include , None ) <EOL> self . assertEqual ( exclude , [ ] ) <EOL> def testSelectedFields ( self ) : <EOL> include , exclude = self . stylesheet . get_fields ( "<STR_LIT>" ) <EOL> self . assertTrue ( "<STR_LIT>" in include ) <EOL> self . assertTrue ( "<STR_LIT>" in include ) <EOL> self . assertEqual ( len ( include ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( exclude , [ ] ) <EOL> def testExcludedFields ( self ) : <EOL> include , exclude = self . stylesheet . get_fields ( "<STR_LIT>" ) <EOL> self . assertEqual ( include , None ) <EOL> self . assertEqual ( exclude , [ "<STR_LIT>" ] ) <EOL> def testCombinedSelectedAndExcludedFields ( self ) : <EOL> include , exclude = self . stylesheet . get_fields ( "<STR_LIT>" ) <EOL> self . assertEqual ( include , [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( exclude , [ ] ) <EOL> def testWildcard ( self ) : <EOL> include , exclude = self . stylesheet . get_fields ( "<STR_LIT>" ) <EOL> self . assertEqual ( include , [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( exclude , [ "<STR_LIT>" ] ) <EOL> include , exclude = self . stylesheet . get_fields ( "<STR_LIT>" ) <EOL> self . assertEqual ( include , None ) <EOL> self . assertEqual ( exclude , [ "<STR_LIT>" ] ) <EOL> def testTransformation ( self ) : <EOL> result = self . stylesheet . transform ( self . tree ) <EOL> root = result . getroot ( ) <EOL> self . assertEqual ( root . tag , "<STR_LIT:test>" ) <EOL> self . assertEqual ( len ( root ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( root . text , "<STR_LIT>" ) <EOL> class GetFieldOptionsTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> options = { <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" , <EOL> } <EOL> uuids = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> db = current . db <EOL> s3db = current . s3db <EOL> s3db . define_table ( "<STR_LIT>" , <EOL> Field ( "<STR_LIT:name>" ) , <EOL> Field ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> * s3_meta_fields ( ) ) <EOL> data = ( { "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT>" : cls . uuids [ <NUM_LIT:0> ] } , <EOL> { "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT>" : cls . uuids [ <NUM_LIT:1> ] } , <EOL> ) <EOL> table = db . fotest_lookup_table <EOL> for item in data : <EOL> table . insert ( ** item ) <EOL> options = cls . options <EOL> represent = S3Represent ( lookup = "<STR_LIT>" ) <EOL> s3db . define_table ( "<STR_LIT>" , <EOL> Field ( "<STR_LIT>" , "<STR_LIT>" , <EOL> requires = IS_EMPTY_OR ( IS_IN_SET ( options ) ) , <EOL> represent = S3Represent ( options = options ) , <EOL> ) , <EOL> Field ( "<STR_LIT>" , "<STR_LIT>" , <EOL> requires = IS_EMPTY_OR ( <EOL> IS_ONE_OF ( db , <EOL> "<STR_LIT>" , <EOL> represent , <EOL> ) ) , <EOL> represent = represent , <EOL> ) , <EOL> Field ( "<STR_LIT>" ) , <EOL> * s3_meta_fields ( ) ) <EOL> db . commit ( ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> db = current . db <EOL> db . fotest_table . drop ( ) <EOL> db . fotest_lookup_table . drop ( ) <EOL> db . commit ( ) <EOL> def setUp ( self ) : <EOL> current . auth . override = True <EOL> db = current . db <EOL> table = db . fotest_lookup_table <EOL> self . records = db ( table . uuid . belongs ( self . uuids ) ) . select ( ) . as_dict ( ) <EOL> def tearDown ( self ) : <EOL> db = current . db <EOL> table = db . fotest_lookup_table <EOL> db ( table . uuid == "<STR_LIT>" ) . delete ( ) <EOL> current . auth . override = False <EOL> def testFixedOptionsSet ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> table = current . db . fotest_table <EOL> xml = current . xml <EOL> fo = xml . get_field_options ( table , "<STR_LIT>" ) <EOL> assertTrue ( isinstance ( fo , etree . _Element ) ) <EOL> assertEqual ( fo . tag , "<STR_LIT>" ) <EOL> ATTRIBUTE = xml . ATTRIBUTE <EOL> VALUE = ATTRIBUTE . value <EOL> options = self . options <EOL> has_empty = False <EOL> self . assertEqual ( len ( fo ) , len ( options ) + <NUM_LIT:1> ) <EOL> for opt in fo : <EOL> assertEqual ( opt . tag , "<STR_LIT>" ) <EOL> attr = opt . attrib <EOL> assertTrue ( VALUE in attr ) <EOL> value = attr [ VALUE ] <EOL> if value == "<STR_LIT>" : <EOL> has_empty = True <EOL> assertEqual ( opt . text , "<STR_LIT>" ) <EOL> continue <EOL> else : <EOL> value = int ( value ) <EOL> assertTrue ( value in options ) <EOL> assertEqual ( opt . text , options [ value ] ) <EOL> assertTrue ( has_empty , msg = "<STR_LIT>" ) <EOL> def testForeignKey ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> table = current . db . fotest_table <EOL> xml = current . xml <EOL> fo = xml . get_field_options ( table , "<STR_LIT>" ) <EOL> assertTrue ( isinstance ( fo , etree . _Element ) ) <EOL> assertEqual ( fo . tag , "<STR_LIT>" ) <EOL> ATTRIBUTE = xml . ATTRIBUTE <EOL> VALUE = ATTRIBUTE . value <EOL> options = self . records <EOL> has_empty = False <EOL> self . assertEqual ( len ( fo ) , len ( options ) + <NUM_LIT:1> ) <EOL> for opt in fo : <EOL> assertEqual ( opt . tag , "<STR_LIT>" ) <EOL> attr = opt . attrib <EOL> assertTrue ( VALUE in attr ) <EOL> value = attr [ VALUE ] <EOL> if value == "<STR_LIT>" : <EOL> has_empty = True <EOL> assertEqual ( opt . text , "<STR_LIT>" ) <EOL> continue <EOL> else : <EOL> value = int ( value ) <EOL> assertTrue ( value in options ) <EOL> assertEqual ( opt . text , options [ value ] [ "<STR_LIT:name>" ] ) <EOL> assertTrue ( has_empty , msg = "<STR_LIT>" ) <EOL> def testFieldWithoutOptions ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> table = current . db . fotest_table <EOL> xml = current . xml <EOL> fo = xml . get_field_options ( table , "<STR_LIT>" ) <EOL> assertTrue ( isinstance ( fo , etree . _Element ) ) <EOL> assertEqual ( fo . tag , "<STR_LIT>" ) <EOL> assertEqual ( len ( fo ) , <NUM_LIT:0> , <EOL> msg = "<STR_LIT>" ) <EOL> def testShowUIDs ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> table = current . db . fotest_table <EOL> xml = current . xml <EOL> fo = xml . get_field_options ( table , "<STR_LIT>" , show_uids = True ) <EOL> assertTrue ( isinstance ( fo , etree . _Element ) ) <EOL> assertEqual ( fo . tag , "<STR_LIT>" ) <EOL> ATTRIBUTE = xml . ATTRIBUTE <EOL> VALUE = ATTRIBUTE . value <EOL> UID = xml . UID <EOL> options = self . records <EOL> has_empty = False <EOL> self . assertEqual ( len ( fo ) , len ( options ) + <NUM_LIT:1> ) <EOL> for opt in fo : <EOL> assertEqual ( opt . tag , "<STR_LIT>" ) <EOL> attr = opt . attrib <EOL> assertTrue ( VALUE in attr ) <EOL> value = attr [ VALUE ] <EOL> if value == "<STR_LIT>" : <EOL> has_empty = True <EOL> self . assertFalse ( UID in attr ) <EOL> assertEqual ( opt . text , "<STR_LIT>" ) <EOL> continue <EOL> else : <EOL> value = int ( value ) <EOL> assertTrue ( UID in attr ) <EOL> assertEqual ( attr [ UID ] , options [ value ] [ "<STR_LIT>" ] ) <EOL> assertTrue ( value in options ) <EOL> assertEqual ( opt . text , options [ value ] [ "<STR_LIT:name>" ] ) <EOL> assertTrue ( has_empty , msg = "<STR_LIT>" ) <EOL> def testWithHierarchyInfo ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> db = current . db <EOL> s3db = current . s3db <EOL> table = db . fotest_lookup_table <EOL> represent = S3Represent ( lookup = "<STR_LIT>" ) <EOL> table . parent . requires = IS_EMPTY_OR ( <EOL> IS_ONE_OF ( db , <EOL> "<STR_LIT>" , <EOL> represent , <EOL> ) ) <EOL> table . parent . requires = represent <EOL> s3db . configure ( "<STR_LIT>" , hierarchy = "<STR_LIT>" ) <EOL> S3Hierarchy . dirty ( "<STR_LIT>" ) <EOL> options = dict ( self . records ) <EOL> child_node = { "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : options . keys ( ) [ <NUM_LIT:0> ] , <EOL> } <EOL> child_id = table . insert ( ** child_node ) <EOL> options [ child_id ] = child_node <EOL> xml = current . xml <EOL> table = db . fotest_table <EOL> fo = xml . get_field_options ( table , "<STR_LIT>" , show_uids = True , hierarchy = True ) <EOL> assertTrue ( isinstance ( fo , etree . _Element ) ) <EOL> assertEqual ( fo . tag , "<STR_LIT>" ) <EOL> ATTRIBUTE = xml . ATTRIBUTE <EOL> VALUE = ATTRIBUTE . value <EOL> PARENT = ATTRIBUTE . parent <EOL> UID = xml . UID <EOL> has_empty = False <EOL> self . assertEqual ( len ( fo ) , len ( options ) + <NUM_LIT:1> ) <EOL> for opt in fo : <EOL> assertEqual ( opt . tag , "<STR_LIT>" ) <EOL> attr = opt . attrib <EOL> assertTrue ( VALUE in attr ) <EOL> value = attr [ VALUE ] <EOL> if value == "<STR_LIT>" : <EOL> has_empty = True <EOL> self . assertFalse ( UID in attr ) <EOL> assertEqual ( opt . text , "<STR_LIT>" ) <EOL> continue <EOL> else : <EOL> value = int ( value ) <EOL> assertTrue ( UID in attr ) <EOL> assertEqual ( attr [ UID ] , options [ value ] [ "<STR_LIT>" ] ) <EOL> assertTrue ( value in options ) <EOL> assertEqual ( opt . text , options [ value ] [ "<STR_LIT:name>" ] ) <EOL> if "<STR_LIT>" in options [ value ] and options [ value ] [ "<STR_LIT>" ] : <EOL> assertTrue ( PARENT in attr ) <EOL> assertEqual ( attr [ PARENT ] , str ( options [ value ] [ "<STR_LIT>" ] ) ) <EOL> assertTrue ( has_empty , msg = "<STR_LIT>" ) <EOL> def testExportOptionsXML ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> options = dict ( self . records ) <EOL> resource = current . s3db . resource ( "<STR_LIT>" ) <EOL> result = resource . export_options ( fields = [ "<STR_LIT>" ] ) <EOL> fo = etree . XML ( result ) <EOL> xml = current . xml <EOL> ATTRIBUTE = xml . ATTRIBUTE <EOL> VALUE = ATTRIBUTE . value <EOL> UID = xml . UID <EOL> has_empty = False <EOL> self . assertEqual ( len ( fo ) , len ( options ) + <NUM_LIT:1> ) <EOL> for opt in fo : <EOL> assertEqual ( opt . tag , "<STR_LIT>" ) <EOL> attr = opt . attrib <EOL> assertTrue ( VALUE in attr ) <EOL> value = attr [ VALUE ] <EOL> if value == "<STR_LIT>" : <EOL> has_empty = True <EOL> self . assertFalse ( UID in attr ) <EOL> assertEqual ( opt . text , None ) <EOL> continue <EOL> else : <EOL> value = int ( value ) <EOL> assertTrue ( value in options ) <EOL> assertEqual ( opt . text , options [ value ] [ "<STR_LIT:name>" ] ) <EOL> assertTrue ( has_empty , msg = "<STR_LIT>" ) <EOL> def testExportOptionsXMLHierarchy ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> db = current . db <EOL> table = db . fotest_lookup_table <EOL> represent = S3Represent ( lookup = "<STR_LIT>" ) <EOL> table . parent . requires = IS_EMPTY_OR ( <EOL> IS_ONE_OF ( db , <EOL> "<STR_LIT>" , <EOL> represent , <EOL> ) ) <EOL> table . parent . requires = represent <EOL> s3db = current . s3db <EOL> s3db . configure ( "<STR_LIT>" , hierarchy = "<STR_LIT>" ) <EOL> S3Hierarchy . dirty ( "<STR_LIT>" ) <EOL> options = dict ( self . records ) <EOL> child_node = { "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : options . keys ( ) [ <NUM_LIT:0> ] , <EOL> } <EOL> child_id = table . insert ( ** child_node ) <EOL> options [ child_id ] = child_node <EOL> resource = s3db . resource ( "<STR_LIT>" ) <EOL> result = resource . export_options ( fields = [ "<STR_LIT>" ] , hierarchy = True ) <EOL> fo = etree . XML ( result ) <EOL> xml = current . xml <EOL> ATTRIBUTE = xml . ATTRIBUTE <EOL> VALUE = ATTRIBUTE . value <EOL> PARENT = ATTRIBUTE . parent <EOL> UID = xml . UID <EOL> has_empty = False <EOL> self . assertEqual ( len ( fo ) , len ( options ) + <NUM_LIT:1> ) <EOL> for opt in fo : <EOL> assertEqual ( opt . tag , "<STR_LIT>" ) <EOL> attr = opt . attrib <EOL> assertTrue ( VALUE in attr ) <EOL> value = attr [ VALUE ] <EOL> if value == "<STR_LIT>" : <EOL> has_empty = True <EOL> self . assertFalse ( UID in attr ) <EOL> assertEqual ( opt . text , None ) <EOL> continue <EOL> else : <EOL> value = int ( value ) <EOL> assertTrue ( value in options ) <EOL> assertEqual ( opt . text , options [ value ] [ "<STR_LIT:name>" ] ) <EOL> if "<STR_LIT>" in options [ value ] and options [ value ] [ "<STR_LIT>" ] : <EOL> assertTrue ( PARENT in attr ) <EOL> assertEqual ( attr [ PARENT ] , str ( options [ value ] [ "<STR_LIT>" ] ) ) <EOL> assertTrue ( has_empty , msg = "<STR_LIT>" ) <EOL> def testExportOptionsAllOptsJSON ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> options = dict ( self . records ) <EOL> resource = current . s3db . resource ( "<STR_LIT>" ) <EOL> result = resource . export_options ( fields = [ "<STR_LIT>" ] , <EOL> as_json = True ) <EOL> fo = json . loads ( result ) <EOL> has_empty = False <EOL> assertTrue ( isinstance ( fo , dict ) ) <EOL> assertTrue ( "<STR_LIT>" in fo ) <EOL> assertTrue ( isinstance ( fo [ "<STR_LIT>" ] , list ) ) <EOL> assertEqual ( len ( fo [ "<STR_LIT>" ] ) , len ( options ) + <NUM_LIT:1> ) <EOL> for opt in fo [ "<STR_LIT>" ] : <EOL> value = opt [ "<STR_LIT>" ] <EOL> if value == "<STR_LIT>" : <EOL> has_empty = True <EOL> self . assertFalse ( "<STR_LIT:$>" in opt ) <EOL> continue <EOL> else : <EOL> value = int ( value ) <EOL> assertTrue ( value in options ) <EOL> assertEqual ( opt [ "<STR_LIT:$>" ] , options [ value ] [ "<STR_LIT:name>" ] ) <EOL> assertTrue ( has_empty , msg = "<STR_LIT>" ) <EOL> def testExportOptionsAllOptsJSONHierarchy ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> db = current . db <EOL> table = db . fotest_lookup_table <EOL> represent = S3Represent ( lookup = "<STR_LIT>" ) <EOL> table . parent . requires = IS_EMPTY_OR ( <EOL> IS_ONE_OF ( db , <EOL> "<STR_LIT>" , <EOL> represent , <EOL> ) ) <EOL> table . parent . requires = represent <EOL> s3db = current . s3db <EOL> s3db . configure ( "<STR_LIT>" , hierarchy = "<STR_LIT>" ) <EOL> S3Hierarchy . dirty ( "<STR_LIT>" ) <EOL> options = dict ( self . records ) <EOL> child_node = { "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : options . keys ( ) [ <NUM_LIT:0> ] , <EOL> } <EOL> child_id = table . insert ( ** child_node ) <EOL> options [ child_id ] = child_node <EOL> resource = s3db . resource ( "<STR_LIT>" ) <EOL> result = resource . export_options ( fields = [ "<STR_LIT>" ] , <EOL> hierarchy = True , <EOL> as_json = True ) <EOL> fo = json . loads ( result ) <EOL> has_empty = False <EOL> assertTrue ( isinstance ( fo , dict ) ) <EOL> assertTrue ( "<STR_LIT>" in fo ) <EOL> assertTrue ( isinstance ( fo [ "<STR_LIT>" ] , list ) ) <EOL> assertEqual ( len ( fo [ "<STR_LIT>" ] ) , len ( options ) + <NUM_LIT:1> ) <EOL> for opt in fo [ "<STR_LIT>" ] : <EOL> value = opt [ "<STR_LIT>" ] <EOL> if value == "<STR_LIT>" : <EOL> has_empty = True <EOL> self . assertFalse ( "<STR_LIT:$>" in opt ) <EOL> continue <EOL> else : <EOL> value = int ( value ) <EOL> assertTrue ( value in options ) <EOL> assertEqual ( opt [ "<STR_LIT:$>" ] , options [ value ] [ "<STR_LIT:name>" ] ) <EOL> if "<STR_LIT>" in options [ value ] and options [ value ] [ "<STR_LIT>" ] : <EOL> assertTrue ( "<STR_LIT>" in opt ) <EOL> assertEqual ( opt [ "<STR_LIT>" ] , str ( options [ value ] [ "<STR_LIT>" ] ) ) <EOL> assertTrue ( has_empty , msg = "<STR_LIT>" ) <EOL> def testExportOptionsLastOptJSON ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> options = dict ( self . records ) <EOL> db = current . db <EOL> table = db . fotest_lookup_table <EOL> last = db ( table . id > <NUM_LIT:0> ) . select ( limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) , <EOL> orderby = ~ table . id ) . first ( ) <EOL> self . assertNotEqual ( last , None ) <EOL> resource = current . s3db . resource ( "<STR_LIT>" ) <EOL> result = resource . export_options ( fields = [ "<STR_LIT>" ] , <EOL> only_last = True , <EOL> as_json = True ) <EOL> fo = json . loads ( result ) <EOL> assertTrue ( isinstance ( fo , dict ) ) <EOL> assertTrue ( "<STR_LIT>" in fo ) <EOL> assertTrue ( isinstance ( fo [ "<STR_LIT>" ] , list ) ) <EOL> assertEqual ( len ( fo [ "<STR_LIT>" ] ) , <NUM_LIT:1> ) <EOL> opt = fo [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> value = opt [ "<STR_LIT>" ] <EOL> assertEqual ( options [ value ] [ "<STR_LIT>" ] , last . uuid ) <EOL> assertEqual ( opt [ "<STR_LIT:$>" ] , options [ value ] [ "<STR_LIT:name>" ] ) <EOL> def testExportOptionsLastOptJSONHierarchy ( self ) : <EOL> assertEqual = self . assertEqual <EOL> assertTrue = self . assertTrue <EOL> db = current . db <EOL> s3db = current . s3db <EOL> table = db . fotest_lookup_table <EOL> represent = S3Represent ( lookup = "<STR_LIT>" ) <EOL> table . parent . requires = IS_EMPTY_OR ( <EOL> IS_ONE_OF ( db , <EOL> "<STR_LIT>" , <EOL> represent , <EOL> ) ) <EOL> table . parent . requires = represent <EOL> s3db . configure ( "<STR_LIT>" , hierarchy = "<STR_LIT>" ) <EOL> S3Hierarchy . dirty ( "<STR_LIT>" ) <EOL> options = dict ( self . records ) <EOL> child_node = { "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : options . keys ( ) [ <NUM_LIT:0> ] , <EOL> } <EOL> child_id = table . insert ( ** child_node ) <EOL> options [ child_id ] = child_node <EOL> table = db . fotest_lookup_table <EOL> last = db ( table . id > <NUM_LIT:0> ) . select ( limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) , <EOL> orderby = ~ table . id ) . first ( ) <EOL> self . assertNotEqual ( last , None ) <EOL> resource = s3db . resource ( "<STR_LIT>" ) <EOL> result = resource . export_options ( fields = [ "<STR_LIT>" ] , <EOL> only_last = True , <EOL> hierarchy = True , <EOL> as_json = True ) <EOL> fo = json . loads ( result ) <EOL> assertTrue ( isinstance ( fo , dict ) ) <EOL> assertTrue ( "<STR_LIT>" in fo ) <EOL> assertTrue ( isinstance ( fo [ "<STR_LIT>" ] , list ) ) <EOL> assertEqual ( len ( fo [ "<STR_LIT>" ] ) , <NUM_LIT:1> ) <EOL> opt = fo [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> value = opt [ "<STR_LIT>" ] <EOL> assertEqual ( options [ value ] [ "<STR_LIT>" ] , last . uuid ) <EOL> assertEqual ( opt [ "<STR_LIT:$>" ] , options [ value ] [ "<STR_LIT:name>" ] ) <EOL> assertTrue ( "<STR_LIT>" in opt ) <EOL> assertEqual ( opt [ "<STR_LIT>" ] , str ( options [ value ] [ "<STR_LIT>" ] ) ) <EOL> class S3JSONParsingTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def testValueParsing ( self ) : <EOL> """<STR_LIT>""" <EOL> assertEqual = self . assertEqual <EOL> json_str = """<STR_LIT>""" <EOL> tree = current . xml . json2tree ( StringIO ( json_str ) ) <EOL> root = tree . getroot ( ) <EOL> value_list = root . findall ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> v = value_list . get ( "<STR_LIT:value>" ) <EOL> assertEqual ( v , '<STR_LIT>' ) <EOL> assertEqual ( json . loads ( v ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> value_list = root . findall ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> v = value_list . get ( "<STR_LIT:value>" ) <EOL> assertEqual ( v , '<STR_LIT>' ) <EOL> assertEqual ( json . loads ( v ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> value_list = root . findall ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> v = value_list . get ( "<STR_LIT:value>" ) <EOL> assertEqual ( v , "<STR_LIT>" ) <EOL> value_list = root . findall ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> v = value_list . get ( "<STR_LIT:value>" ) <EOL> assertEqual ( v , "<STR_LIT:2>" ) <EOL> def run_suite ( * test_classes ) : <EOL> """<STR_LIT>""" <EOL> loader = unittest . TestLoader ( ) <EOL> suite = unittest . TestSuite ( ) <EOL> for test_class in test_classes : <EOL> tests = loader . loadTestsFromTestCase ( test_class ) <EOL> suite . addTests ( tests ) <EOL> if suite is not None : <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:2> ) . run ( suite ) <EOL> return <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_suite ( <EOL> TreeBuilderTests , <EOL> JSONMessageTests , <EOL> XMLFormatTests , <EOL> GetFieldOptionsTests , <EOL> S3JSONParsingTests , <EOL> ) </s>
