<s> """<STR_LIT>""" <EOL> import os <EOL> import uuid <EOL> from django . test import TestCase <EOL> from django . db . models import Manager <EOL> from seed . models import CanonicalBuilding , BuildingSnapshot <EOL> from seed . factory import SEEDFactory <EOL> from seed . lib . exporter import Exporter <EOL> import xlrd <EOL> import unicodecsv as csv <EOL> class TestExporters ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . snapshots = [ ] <EOL> for x in range ( <NUM_LIT:50> ) : <EOL> cb = CanonicalBuilding ( ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( canonical_building = cb ) <EOL> b . extra_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> b . save ( ) <EOL> self . snapshots . append ( b ) <EOL> def test_data_model_assumptions ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( hasattr ( BuildingSnapshot , '<STR_LIT>' ) ) <EOL> def test_csv_export ( self ) : <EOL> """<STR_LIT>""" <EOL> qs_filter = { "<STR_LIT>" : [ x . pk for x in self . snapshots ] } <EOL> qs = BuildingSnapshot . objects . filter ( ** qs_filter ) <EOL> export_id = str ( uuid . uuid4 ( ) ) <EOL> exporter = Exporter ( export_id , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fields = list ( Exporter . fields_from_queryset ( qs ) ) <EOL> fields . append ( "<STR_LIT>" ) <EOL> export_filename = exporter . export_csv ( qs , fields ) <EOL> self . assertTrue ( os . path . exists ( export_filename ) ) <EOL> export_file = open ( export_filename ) <EOL> reader = csv . reader ( export_file ) <EOL> header = reader . next ( ) <EOL> self . assertEqual ( header [ len ( fields ) - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> for i in range ( len ( self . snapshots ) ) : <EOL> row = reader . next ( ) <EOL> for j in range ( len ( fields ) ) : <EOL> field = fields [ j ] <EOL> components = field . split ( "<STR_LIT>" ) <EOL> qs_val = qs [ i ] <EOL> for component in components : <EOL> qs_val = getattr ( qs_val , component ) <EOL> if qs_val is None : <EOL> break <EOL> if isinstance ( qs_val , Manager ) or qs_val is None : <EOL> qs_val = u'<STR_LIT>' <EOL> else : <EOL> qs_val = unicode ( qs_val ) <EOL> csv_val = row [ j ] <EOL> self . assertEqual ( qs_val , csv_val ) <EOL> export_file . close ( ) <EOL> os . remove ( export_filename ) <EOL> def test_csv_export_extra_data ( self ) : <EOL> """<STR_LIT>""" <EOL> qs_filter = { "<STR_LIT>" : [ x . pk for x in self . snapshots ] } <EOL> qs = BuildingSnapshot . objects . filter ( ** qs_filter ) <EOL> export_id = str ( uuid . uuid4 ( ) ) <EOL> exporter = Exporter ( export_id , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fields = list ( Exporter . fields_from_queryset ( qs ) ) <EOL> fields . append ( "<STR_LIT>" ) <EOL> fields . append ( '<STR_LIT>' ) <EOL> export_filename = exporter . export_csv ( qs , fields ) <EOL> export_file = open ( export_filename ) <EOL> reader = csv . reader ( export_file ) <EOL> header = reader . next ( ) <EOL> self . assertEqual ( header [ len ( fields ) - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> for i in range ( len ( self . snapshots ) ) : <EOL> row = reader . next ( ) <EOL> for j in range ( len ( fields ) ) : <EOL> field = fields [ j ] <EOL> components = field . split ( "<STR_LIT>" ) <EOL> qs_val = qs [ i ] <EOL> for component in components : <EOL> try : <EOL> qs_val = getattr ( qs_val , component ) <EOL> except AttributeError : <EOL> qs_val = qs_val . extra_data . get ( component ) <EOL> if qs_val is None : <EOL> break <EOL> if isinstance ( qs_val , Manager ) or qs_val is None : <EOL> qs_val = u'<STR_LIT>' <EOL> else : <EOL> qs_val = unicode ( qs_val ) <EOL> csv_val = row [ j ] <EOL> self . assertEqual ( qs_val , csv_val ) <EOL> export_file . close ( ) <EOL> os . remove ( export_filename ) <EOL> def test_xls_export ( self ) : <EOL> """<STR_LIT>""" <EOL> qs_filter = { "<STR_LIT>" : [ x . pk for x in self . snapshots ] } <EOL> qs = BuildingSnapshot . objects . filter ( ** qs_filter ) <EOL> export_id = str ( uuid . uuid4 ( ) ) <EOL> exporter = Exporter ( export_id , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fields = list ( Exporter . fields_from_queryset ( qs ) ) <EOL> fields . append ( "<STR_LIT>" ) <EOL> export_filename = exporter . export_xls ( qs , fields ) <EOL> export_file = xlrd . open_workbook ( export_filename ) <EOL> worksheet = export_file . sheet_by_name ( export_file . sheet_names ( ) [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( worksheet . cell_value ( <NUM_LIT:0> , len ( fields ) - <NUM_LIT:1> ) , '<STR_LIT>' ) <EOL> for i in range ( len ( self . snapshots ) ) : <EOL> for j in range ( len ( fields ) ) : <EOL> field = fields [ j ] <EOL> components = field . split ( "<STR_LIT>" ) <EOL> qs_val = qs [ i ] <EOL> for component in components : <EOL> qs_val = getattr ( qs_val , component ) <EOL> if qs_val is None : <EOL> break <EOL> if isinstance ( qs_val , Manager ) or qs_val is None : <EOL> qs_val = u'<STR_LIT>' <EOL> else : <EOL> qs_val = unicode ( qs_val ) <EOL> xls_val = worksheet . cell_value ( i + <NUM_LIT:1> , j ) <EOL> self . assertEqual ( qs_val , xls_val ) <EOL> def tearDown ( self ) : <EOL> for x in self . snapshots : <EOL> x . delete ( ) </s>
