<s> """<STR_LIT>""" <EOL> import copy <EOL> import json <EOL> from unittest import skip <EOL> from django . core . cache import cache <EOL> from django . core . urlresolvers import reverse_lazy <EOL> from django . test import TestCase <EOL> from superperms . orgs . models import Organization , OrganizationUser <EOL> from audit_logs . models import AuditLog , LOG , ACTION_OPTIONS <EOL> from data_importer . models import ROW_DELIMITER , ImportFile , ImportRecord <EOL> from landing . models import SEEDUser as User <EOL> from seed import decorators <EOL> from seed . factory import SEEDFactory <EOL> from seed . models import ( <EOL> Column , <EOL> ColumnMapping , <EOL> CanonicalBuilding , <EOL> BuildingSnapshot , <EOL> StatusLabel , <EOL> Unit , <EOL> ASSESSED_RAW , <EOL> ASSESSED_BS , <EOL> COMPOSITE_BS , <EOL> PORTFOLIO_BS , <EOL> save_snapshot_match , <EOL> Project , <EOL> ProjectBuilding , <EOL> FLOAT , <EOL> ) <EOL> from seed . views . main import ( <EOL> DEFAULT_CUSTOM_COLUMNS , <EOL> _parent_tree_coparents , <EOL> ) <EOL> from seed . utils . mapping import _get_column_names <EOL> from seed . utils . constants import ASSESSOR_FIELDS <EOL> from seed . tests import util as test_util <EOL> class DataImporterViewTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> user_details = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> } <EOL> self . user = User . objects . create_superuser ( <EOL> email = '<STR_LIT>' , ** user_details ) <EOL> self . client . login ( ** user_details ) <EOL> def test_get_raw_column_names ( self ) : <EOL> """<STR_LIT>""" <EOL> import_record = ImportRecord . objects . create ( ) <EOL> expected_raw_columns = [ '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> expected_saved_format = ROW_DELIMITER . join ( expected_raw_columns ) <EOL> import_file = ImportFile . objects . create ( <EOL> import_record = import_record , <EOL> cached_first_row = expected_saved_format <EOL> ) <EOL> self . assertEqual ( import_file . cached_first_row , expected_saved_format ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> resp = self . client . post ( <EOL> url , data = json . dumps ( <EOL> { '<STR_LIT>' : import_file . pk } <EOL> ) , content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> self . assertEqual ( body . get ( '<STR_LIT>' , [ ] ) , expected_raw_columns ) <EOL> def test_get_first_five_rows ( self ) : <EOL> """<STR_LIT>""" <EOL> import_record = ImportRecord . objects . create ( ) <EOL> expected_raw_columns = [ '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> expected_raw_rows = [ <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> ] <EOL> expected = [ <EOL> dict ( zip ( expected_raw_columns , row ) ) for row in expected_raw_rows <EOL> ] <EOL> expected_saved_format = '<STR_LIT:\n>' . join ( [ <EOL> ROW_DELIMITER . join ( row ) for row in expected_raw_rows <EOL> ] ) <EOL> import_file = ImportFile . objects . create ( <EOL> import_record = import_record , <EOL> cached_first_row = ROW_DELIMITER . join ( expected_raw_columns ) , <EOL> cached_second_to_fifth_row = expected_saved_format <EOL> ) <EOL> self . assertEqual ( <EOL> import_file . cached_second_to_fifth_row , expected_saved_format <EOL> ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> resp = self . client . post ( <EOL> url , data = json . dumps ( <EOL> { '<STR_LIT>' : import_file . pk } <EOL> ) , content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> self . assertEqual ( body . get ( '<STR_LIT>' , [ ] ) , expected ) <EOL> class DefaultColumnsViewTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> user_details = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' <EOL> } <EOL> self . user = User . objects . create_superuser ( ** user_details ) <EOL> self . org = Organization . objects . create ( ) <EOL> OrganizationUser . objects . create ( user = self . user , organization = self . org ) <EOL> self . client . login ( ** user_details ) <EOL> def test_get_default_columns_with_set_columns ( self ) : <EOL> columns = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . user . default_custom_columns = columns <EOL> self . user . save ( ) <EOL> columns = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> response = self . client . get ( url ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , columns ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , False ) <EOL> def test_get_default_columns_initial_state ( self ) : <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> response = self . client . get ( url ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , DEFAULT_CUSTOM_COLUMNS ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , True ) <EOL> def test_set_default_columns ( self ) : <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> columns = [ '<STR_LIT:s>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> post_data = { <EOL> '<STR_LIT>' : columns , <EOL> '<STR_LIT>' : True <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> response = self . client . get ( url ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , columns ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> response = self . client . get ( url ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , True ) <EOL> post_data [ '<STR_LIT>' ] = False <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> response = self . client . get ( url ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , False ) <EOL> def test_get_columns ( self ) : <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> response = self . client . get ( <EOL> url , <EOL> { '<STR_LIT>' : self . org . id } <EOL> ) <EOL> data = json . loads ( response . content ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , { <EOL> u'<STR_LIT>' : False , <EOL> u'<STR_LIT:class>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : True , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : True , <EOL> u'<STR_LIT>' : False , <EOL> u'<STR_LIT:title>' : u'<STR_LIT>' , <EOL> u'<STR_LIT:type>' : u'<STR_LIT:string>' , <EOL> } ) <EOL> response = self . client . get ( <EOL> url , <EOL> { <EOL> '<STR_LIT>' : self . org . id , <EOL> '<STR_LIT>' : True <EOL> } <EOL> ) <EOL> data = json . loads ( response . content ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , { <EOL> u'<STR_LIT>' : False , <EOL> u'<STR_LIT:class>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : False , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : True , <EOL> u'<STR_LIT>' : False , <EOL> u'<STR_LIT>' : True , <EOL> u'<STR_LIT:title>' : u'<STR_LIT>' , <EOL> u'<STR_LIT:type>' : u'<STR_LIT:string>' , <EOL> } ) <EOL> def test_get_columns_project ( self ) : <EOL> """<STR_LIT>""" <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> response = self . client . get ( <EOL> url , <EOL> { '<STR_LIT>' : '<STR_LIT:true>' , '<STR_LIT>' : self . org . id } <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:title>' ] , '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> self . user . delete ( ) <EOL> class SearchViewTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> user_details = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' <EOL> } <EOL> self . user = User . objects . create_superuser ( ** user_details ) <EOL> self . org = Organization . objects . create ( ) <EOL> OrganizationUser . objects . create ( user = self . user , organization = self . org ) <EOL> self . client . login ( ** user_details ) <EOL> def test_seach_active_canonicalbuildings ( self ) : <EOL> """<STR_LIT>""" <EOL> NUMBER_ACTIVE = <NUM_LIT:50> <EOL> NUMBER_INACTIVE = <NUM_LIT> <EOL> NUMBER_WITHOUT_CANONICAL = <NUM_LIT:5> <EOL> NUMBER_PER_PAGE = <NUM_LIT:10> <EOL> for i in range ( NUMBER_ACTIVE ) : <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( canonical_building = cb ) <EOL> cb . canonical_snapshot = b <EOL> cb . save ( ) <EOL> b . super_organization = self . org <EOL> b . save ( ) <EOL> for i in range ( NUMBER_INACTIVE ) : <EOL> cb = CanonicalBuilding ( active = False ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( canonical_building = cb ) <EOL> cb . canonical_snapshot = b <EOL> cb . save ( ) <EOL> b . super_organization = self . org <EOL> b . save ( ) <EOL> for i in range ( NUMBER_WITHOUT_CANONICAL ) : <EOL> b = SEEDFactory . building_snapshot ( ) <EOL> b . super_organization = self . org <EOL> b . save ( ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> post_data = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : NUMBER_PER_PAGE , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:q>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( <EOL> BuildingSnapshot . objects . all ( ) . count ( ) , <EOL> NUMBER_ACTIVE + NUMBER_INACTIVE + NUMBER_WITHOUT_CANONICAL <EOL> ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , NUMBER_ACTIVE ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , NUMBER_PER_PAGE ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , NUMBER_PER_PAGE ) <EOL> def test_search_sort ( self ) : <EOL> """<STR_LIT>""" <EOL> NUMBER_ACTIVE = <NUM_LIT:10> <EOL> NUMBER_PER_PAGE = <NUM_LIT:10> <EOL> for i in range ( NUMBER_ACTIVE ) : <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( <EOL> canonical_building = cb , <EOL> tax_lot_id = "<STR_LIT:%s>" % i <EOL> ) <EOL> cb . canonical_snapshot = b <EOL> cb . save ( ) <EOL> b . super_organization = self . org <EOL> b . save ( ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> post_data = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : NUMBER_PER_PAGE , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:q>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT:0>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:9> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> post_data [ '<STR_LIT>' ] = True <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:9> ] [ '<STR_LIT>' ] , '<STR_LIT:0>' ) <EOL> def test_search_extra_data ( self ) : <EOL> """<STR_LIT>""" <EOL> NUMBER_ACTIVE = <NUM_LIT:10> <EOL> NUMBER_PER_PAGE = <NUM_LIT:10> <EOL> for i in range ( NUMBER_ACTIVE ) : <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( <EOL> canonical_building = cb , <EOL> tax_lot_id = "<STR_LIT:%s>" % i <EOL> ) <EOL> if i > <NUM_LIT:4> : <EOL> b . extra_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> cb . canonical_snapshot = b <EOL> cb . save ( ) <EOL> b . super_organization = self . org <EOL> b . save ( ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> post_data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : NUMBER_PER_PAGE , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:q>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' <EOL> ) <EOL> post_data [ '<STR_LIT>' ] = True <EOL> post_data [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:4> ] [ '<STR_LIT>' ] , '<STR_LIT:5>' ) <EOL> def test_sort_extra_data ( self ) : <EOL> """<STR_LIT>""" <EOL> float_unit = Unit . objects . create ( <EOL> unit_name = '<STR_LIT>' , <EOL> unit_type = FLOAT <EOL> ) <EOL> ed_col_name = '<STR_LIT>' <EOL> float_col = Column . objects . create ( <EOL> organization = self . org , <EOL> column_name = ed_col_name , <EOL> unit = float_unit , <EOL> ) <EOL> test_mapping = ColumnMapping . objects . create ( <EOL> super_organization = self . org , <EOL> ) <EOL> test_mapping . column_mapped . add ( float_col ) <EOL> NUMBER_ACTIVE = <NUM_LIT:10> <EOL> NUMBER_PER_PAGE = <NUM_LIT:10> <EOL> for i in range ( NUMBER_ACTIVE ) : <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( <EOL> organization = self . org , <EOL> canonical_building = cb , <EOL> tax_lot_id = "<STR_LIT:%s>" % i <EOL> ) <EOL> cb . canonical_snapshot = b <EOL> cb . save ( ) <EOL> b . super_organization = self . org <EOL> b . extra_data = { <EOL> ed_col_name : str ( i * <NUM_LIT> ) <EOL> } <EOL> b . save ( ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> post_data = { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : NUMBER_PER_PAGE , <EOL> '<STR_LIT>' : ed_col_name , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:q>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> float_col_data = map ( lambda b : b [ '<STR_LIT>' ] [ ed_col_name ] , data [ '<STR_LIT>' ] ) <EOL> expected_data = map ( <EOL> lambda b : b . extra_data [ ed_col_name ] , <EOL> BuildingSnapshot . objects . order_by ( '<STR_LIT>' ) <EOL> ) <EOL> self . assertEqual ( float_col_data , expected_data ) <EOL> post_data [ '<STR_LIT>' ] = True <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> float_col_data = map ( lambda b : b [ '<STR_LIT>' ] [ ed_col_name ] , data [ '<STR_LIT>' ] ) <EOL> expected_data . reverse ( ) <EOL> self . assertEqual ( float_col_data , expected_data ) <EOL> def test_search_filter_range ( self ) : <EOL> """<STR_LIT>""" <EOL> NUMBER_ACTIVE = <NUM_LIT:10> <EOL> NUMBER_PER_PAGE = <NUM_LIT:10> <EOL> for i in range ( NUMBER_ACTIVE ) : <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( <EOL> canonical_building = cb , <EOL> tax_lot_id = "<STR_LIT:%s>" % i , <EOL> year_built = ( i + <NUM_LIT:1> ) <EOL> ) <EOL> cb . canonical_snapshot = b <EOL> cb . save ( ) <EOL> b . super_organization = self . org <EOL> b . save ( ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> post_data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : <NUM_LIT:7> <EOL> } , <EOL> '<STR_LIT>' : NUMBER_PER_PAGE , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:q>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:3> ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <NUM_LIT:5> ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , <NUM_LIT:6> ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:2> ] [ '<STR_LIT>' ] , <NUM_LIT:7> ) <EOL> def test_apply_label_to_project_buildings ( self ) : <EOL> """<STR_LIT>""" <EOL> NUM_IN_PROJECT = <NUM_LIT:2> <EOL> NUM_NO_PROJECT = <NUM_LIT:2> <EOL> orange_label = StatusLabel . objects . create ( <EOL> color = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> super_organization = self . org <EOL> ) <EOL> project = Project . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> owner = self . user , <EOL> super_organization = self . org , <EOL> ) <EOL> for i in range ( NUM_NO_PROJECT ) : <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( canonical_building = cb ) <EOL> cb . canonical_snapshot = b <EOL> cb . save ( ) <EOL> b . super_organization = self . org <EOL> b . save ( ) <EOL> for i in range ( NUM_IN_PROJECT ) : <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( canonical_building = cb ) <EOL> cb . canonical_snapshot = b <EOL> cb . save ( ) <EOL> b . super_organization = self . org <EOL> b . save ( ) <EOL> ProjectBuilding . objects . create ( <EOL> building_snapshot = b , <EOL> project = project <EOL> ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> post_data = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:label>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : orange_label . pk , <EOL> '<STR_LIT:label>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : project . slug , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : project . slug <EOL> } , <EOL> '<STR_LIT>' : project . pk <EOL> } , <EOL> '<STR_LIT>' : True <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> response_data = json . loads ( response . content ) <EOL> project_buildings = ProjectBuilding . objects . filter ( <EOL> project = project <EOL> ) <EOL> non_project_buildings = ProjectBuilding . objects . exclude ( <EOL> project = project <EOL> ) <EOL> self . assertEquals ( response_data , { '<STR_LIT:status>' : '<STR_LIT:success>' } ) <EOL> for pb in project_buildings : <EOL> self . assertEqual ( pb . status_label , orange_label ) <EOL> self . assertEqual ( non_project_buildings . count ( ) , <NUM_LIT:0> ) <EOL> def test_apply_label_to_specific_project_buildings ( self ) : <EOL> """<STR_LIT>""" <EOL> NUM_BUILDINGS = <NUM_LIT:4> <EOL> orange_label = StatusLabel . objects . create ( <EOL> color = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> super_organization = self . org <EOL> ) <EOL> project = Project . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> owner = self . user , <EOL> super_organization = self . org , <EOL> ) <EOL> buildings = [ ] <EOL> for i in range ( NUM_BUILDINGS ) : <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> b = SEEDFactory . building_snapshot ( canonical_building = cb ) <EOL> cb . canonical_snapshot = b <EOL> cb . save ( ) <EOL> b . super_organization = self . org <EOL> b . save ( ) <EOL> ProjectBuilding . objects . create ( <EOL> building_snapshot = b , <EOL> project = project <EOL> ) <EOL> buildings . append ( b ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> to_label = buildings [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> unlabeled = buildings [ <NUM_LIT:2> : <NUM_LIT:4> ] <EOL> post_data = { <EOL> '<STR_LIT>' : map ( lambda bs : bs . pk , to_label ) , <EOL> '<STR_LIT:label>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : orange_label . pk , <EOL> '<STR_LIT:label>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : project . slug , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : project . slug <EOL> } , <EOL> '<STR_LIT>' : project . pk <EOL> } , <EOL> '<STR_LIT>' : False <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> data = json . dumps ( post_data ) <EOL> ) <EOL> response_data = json . loads ( response . content ) <EOL> labeled_buildings = ProjectBuilding . objects . filter ( <EOL> building_snapshot__in = to_label <EOL> ) <EOL> unlabeled_buildings = ProjectBuilding . objects . exclude ( <EOL> building_snapshot__in = to_label <EOL> ) <EOL> self . assertEquals ( response_data , { '<STR_LIT:status>' : '<STR_LIT:success>' } ) <EOL> for pb in labeled_buildings : <EOL> self . assertEqual ( pb . status_label , orange_label ) <EOL> for pb in unlabeled_buildings : <EOL> self . assertEqual ( pb . status_label , None ) <EOL> class BuildingDetailViewTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> user_details = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' <EOL> } <EOL> self . user = User . objects . create_user ( ** user_details ) <EOL> self . org = Organization . objects . create ( ) <EOL> OrganizationUser . objects . create ( user = self . user , organization = self . org ) <EOL> self . client . login ( ** user_details ) <EOL> import_record = ImportRecord . objects . create ( ) <EOL> import_file_1 = ImportFile . objects . create ( <EOL> import_record = import_record , <EOL> ) <EOL> import_file_1 . save ( ) <EOL> import_file_2 = ImportFile . objects . create ( <EOL> import_record = import_record , <EOL> ) <EOL> import_file_2 . save ( ) <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> parent_1 = SEEDFactory . building_snapshot ( <EOL> canonical_building = cb , <EOL> gross_floor_area = None <EOL> ) <EOL> cb . canonical_snapshot = parent_1 <EOL> cb . save ( ) <EOL> parent_1 . super_organization = self . org <EOL> parent_1 . import_file = import_file_1 <EOL> parent_1 . source_type = <NUM_LIT:2> <EOL> parent_1 . save ( ) <EOL> cb = CanonicalBuilding ( active = True ) <EOL> cb . save ( ) <EOL> parent_2 = SEEDFactory . building_snapshot ( canonical_building = cb ) <EOL> cb . canonical_snapshot = parent_2 <EOL> cb . save ( ) <EOL> parent_2 . super_organization = self . org <EOL> parent_2 . import_file = import_file_2 <EOL> parent_2 . source_type = <NUM_LIT:3> <EOL> parent_2 . save ( ) <EOL> self . import_record = import_record <EOL> self . import_file_1 = import_file_1 <EOL> self . import_file_2 = import_file_2 <EOL> self . parent_1 = parent_1 <EOL> self . parent_2 = parent_2 <EOL> def test_create_building ( self ) : <EOL> """<STR_LIT>""" <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> post_data = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : self . org . pk , <EOL> } <EOL> response = self . client . post ( <EOL> url , <EOL> data = json . dumps ( post_data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> b = BuildingSnapshot . objects . get ( pk = data [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( b . property_name , '<STR_LIT>' ) <EOL> self . assertNotEqual ( b . canonical_building , None ) <EOL> self . assertEqual ( b . canonical_building . canonical_snapshot , b ) <EOL> self . assertEqual ( b . property_name_source , b ) <EOL> def test_get_building ( self ) : <EOL> """<STR_LIT>""" <EOL> child = save_snapshot_match ( self . parent_1 . pk , self . parent_2 . pk ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> get_data = { <EOL> '<STR_LIT>' : child . canonical_building . pk , <EOL> '<STR_LIT>' : self . org . pk , <EOL> } <EOL> response = self . client . get ( <EOL> url , <EOL> get_data , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> child . pk <EOL> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> child . pk <EOL> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> self . import_file_1 . pk <EOL> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , <EOL> self . import_file_2 . pk <EOL> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . parent_1 . address_line_1 <EOL> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . parent_1 . pk <EOL> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . parent_2 . pk <EOL> ) <EOL> def test_get_building_with_project ( self ) : <EOL> """<STR_LIT>""" <EOL> child = save_snapshot_match ( self . parent_1 . pk , self . parent_2 . pk ) <EOL> project = Project . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> owner = self . user , <EOL> super_organization = self . org , <EOL> ) <EOL> ProjectBuilding . objects . create ( <EOL> building_snapshot = child , <EOL> project = project <EOL> ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> get_data = { <EOL> '<STR_LIT>' : child . canonical_building . pk , <EOL> '<STR_LIT>' : self . org . pk , <EOL> } <EOL> response = self . client . get ( <EOL> url , <EOL> get_data , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_get_building_with_deleted_dataset ( self ) : <EOL> """<STR_LIT>""" <EOL> child = save_snapshot_match ( self . parent_1 . pk , self . parent_2 . pk ) <EOL> url = reverse_lazy ( "<STR_LIT>" ) <EOL> get_data = { <EOL> '<STR_LIT>' : child . canonical_building . pk , <EOL> '<STR_LIT>' : self . org . pk , <EOL> } <EOL> self . import_record . delete ( ) <EOL> response = self . client . get ( <EOL> url , <EOL> get_data , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> json_string = response . content <EOL> data = json . loads ( json_string ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . parent_1 . address_line_1 <EOL> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . parent_1 . pk <EOL> ) <EOL> self . assertEqual ( <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . parent_2 . pk <EOL> ) <EOL> self . assertAlmostEqual ( <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . parent_2 . gross_floor_area , <EOL> places = <NUM_LIT:1> , <EOL> ) <EOL> def test_update_building_audit_log ( self ) : <EOL> """<STR_LIT>""" <EOL> building = self . parent_1 . to_dict ( ) <EOL> building [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . client . put ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT>' : building , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> self . assertEqual ( AuditLog . objects . count ( ) , <NUM_LIT:1> ) <EOL> audit_log = AuditLog . objects . first ( ) <EOL> self . assertEqual ( <EOL> audit_log . content_object , <EOL> self . parent_1 . canonical_building <EOL> ) <EOL> self . assertTrue ( '<STR_LIT>' in audit_log . action ) <EOL> self . assertEqual ( audit_log . audit_type , LOG ) <EOL> def test_save_match_audit_log ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . put ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT>' : self . parent_1 . pk , <EOL> '<STR_LIT>' : self . parent_2 . pk , <EOL> '<STR_LIT>' : True <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> self . assertEqual ( AuditLog . objects . count ( ) , <NUM_LIT:1> ) <EOL> audit_log = AuditLog . objects . first ( ) <EOL> self . assertEqual ( <EOL> audit_log . content_object , <EOL> self . parent_1 . canonical_building <EOL> ) <EOL> self . assertTrue ( '<STR_LIT>' in audit_log . action ) <EOL> self . assertEqual ( audit_log . action_note , '<STR_LIT>' ) <EOL> self . assertEqual ( audit_log . audit_type , LOG ) <EOL> def test_get_match_tree ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . put ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT>' : self . parent_1 . pk , <EOL> '<STR_LIT>' : self . parent_2 . pk , <EOL> '<STR_LIT>' : True <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> resp = self . client . get ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT>' : self . parent_1 . pk , <EOL> } , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> ids = [ b [ '<STR_LIT:id>' ] for b in body [ '<STR_LIT>' ] ] <EOL> self . assertIn ( self . parent_1 . pk , ids ) <EOL> self . assertIn ( self . parent_2 . pk , ids ) <EOL> self . assertIn ( self . parent_1 . children . first ( ) . pk , ids ) <EOL> def test_get_match_tree_from_child ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . put ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT>' : self . parent_1 . pk , <EOL> '<STR_LIT>' : self . parent_2 . pk , <EOL> '<STR_LIT>' : True <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> resp = self . client . get ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT>' : self . parent_1 . children . first ( ) . pk , <EOL> } , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> ids = [ b [ '<STR_LIT:id>' ] for b in body [ '<STR_LIT>' ] ] <EOL> self . assertIn ( self . parent_1 . pk , ids ) <EOL> self . assertIn ( self . parent_2 . pk , ids ) <EOL> self . assertIn ( self . parent_1 . children . first ( ) . pk , ids ) <EOL> def test_save_match_wrong_perms_org_id ( self ) : <EOL> """<STR_LIT>""" <EOL> new_org = Organization . objects . create ( ) <EOL> OrganizationUser . objects . create ( user = self . user , organization = new_org ) <EOL> resp = self . client . put ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : new_org . pk , <EOL> '<STR_LIT>' : self . parent_1 . pk , <EOL> '<STR_LIT>' : self . parent_2 . pk , <EOL> '<STR_LIT>' : True <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> self . assertEqual ( body , { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } ) <EOL> def test_save_match_invalid_org ( self ) : <EOL> """<STR_LIT>""" <EOL> new_org = Organization . objects . create ( ) <EOL> resp = self . client . put ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : new_org . pk , <EOL> '<STR_LIT>' : self . parent_1 . pk , <EOL> '<STR_LIT>' : self . parent_2 . pk , <EOL> '<STR_LIT>' : True <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> self . assertEqual ( body , { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> } ) <EOL> def test_save_match_wrong_perms_different_building_orgs ( self ) : <EOL> """<STR_LIT>""" <EOL> new_org = Organization . objects . create ( ) <EOL> self . parent_1 . super_organization = new_org <EOL> self . parent_1 . save ( ) <EOL> OrganizationUser . objects . create ( user = self . user , organization = new_org ) <EOL> resp = self . client . put ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT>' : self . parent_1 . pk , <EOL> '<STR_LIT>' : self . parent_2 . pk , <EOL> '<STR_LIT>' : True <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> self . assertEqual ( body , { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } ) <EOL> def test_save_unmatch_audit_log ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT>' : self . parent_1 . pk , <EOL> '<STR_LIT>' : self . parent_2 . pk , <EOL> '<STR_LIT>' : True <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT>' : self . parent_2 . pk , <EOL> '<STR_LIT>' : False <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> self . assertEqual ( AuditLog . objects . count ( ) , <NUM_LIT:2> ) <EOL> audit_log = AuditLog . objects . first ( ) <EOL> self . assertEqual ( <EOL> audit_log . content_object , <EOL> self . parent_2 . canonical_building <EOL> ) <EOL> self . assertTrue ( '<STR_LIT>' in audit_log . action ) <EOL> self . assertEqual ( audit_log . action_note , '<STR_LIT>' ) <EOL> self . assertEqual ( audit_log . audit_type , LOG ) <EOL> class TestMCMViews ( TestCase ) : <EOL> expected_mappings = { <EOL> u'<STR_LIT:address>' : [ u'<STR_LIT>' , <NUM_LIT> ] , <EOL> u'<STR_LIT>' : [ u'<STR_LIT>' , <NUM_LIT:64> ] , <EOL> u'<STR_LIT:name>' : [ u'<STR_LIT>' , <NUM_LIT> ] , <EOL> u'<STR_LIT>' : [ u'<STR_LIT>' , <NUM_LIT:50> ] <EOL> } <EOL> raw_columns_expected = { <EOL> u'<STR_LIT:status>' : u'<STR_LIT:success>' , <EOL> u'<STR_LIT>' : [ u'<STR_LIT:name>' , u'<STR_LIT:address>' , u'<STR_LIT>' , u'<STR_LIT>' ] <EOL> } <EOL> def assert_expected_mappings ( self , actual , expected ) : <EOL> """<STR_LIT>""" <EOL> for orig_col in actual : <EOL> expected_dest , expected_confidence = expected [ orig_col ] <EOL> dest_col , suggested_confidence = actual [ orig_col ] <EOL> self . assertEqual ( dest_col , expected_dest ) <EOL> def setUp ( self ) : <EOL> self . maxDiff = None <EOL> self . org = Organization . objects . create ( ) <EOL> user_details = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> } <EOL> self . user = User . objects . create_superuser ( ** user_details ) <EOL> OrganizationUser . objects . create ( user = self . user , organization = self . org ) <EOL> self . client . login ( ** user_details ) <EOL> self . import_record = ImportRecord . objects . create ( <EOL> owner = self . user <EOL> ) <EOL> self . import_record . super_organization = self . org <EOL> self . import_record . save ( ) <EOL> self . import_file = ImportFile . objects . create ( <EOL> import_record = self . import_record , <EOL> cached_first_row = ROW_DELIMITER . join ( <EOL> [ u'<STR_LIT:name>' , u'<STR_LIT:address>' , u'<STR_LIT>' , u'<STR_LIT>' ] <EOL> ) <EOL> ) <EOL> def test_get_column_mapping_suggestions ( self ) : <EOL> """<STR_LIT>""" <EOL> raw_col = Column . objects . create ( <EOL> organization = self . org , <EOL> column_name = '<STR_LIT:address>' <EOL> ) <EOL> model_col = Column . objects . create ( <EOL> organization = self . org , <EOL> column_name = '<STR_LIT>' <EOL> ) <EOL> mapping = ColumnMapping . objects . create ( <EOL> super_organization = self . org <EOL> ) <EOL> mapping . column_raw . add ( raw_col ) <EOL> mapping . column_mapped . add ( model_col ) <EOL> mapping . save ( ) <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . id , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> suggested_mappings = body [ '<STR_LIT>' ] <EOL> new_expected_mappings = copy . deepcopy ( self . expected_mappings ) <EOL> new_expected_mappings [ '<STR_LIT:address>' ] = [ '<STR_LIT>' , <NUM_LIT:100> ] <EOL> self . assert_expected_mappings ( <EOL> suggested_mappings , <EOL> new_expected_mappings <EOL> ) <EOL> self . assertEqual ( <EOL> suggested_mappings [ '<STR_LIT:address>' ] , <EOL> new_expected_mappings [ '<STR_LIT:address>' ] <EOL> ) <EOL> def test_get_column_mapping_suggestions_concat ( self ) : <EOL> """<STR_LIT>""" <EOL> column_raw = Column . objects . create ( <EOL> column_name = '<STR_LIT:name>' , organization = self . org <EOL> ) <EOL> column_raw2 = Column . objects . create ( <EOL> column_name = '<STR_LIT:address>' , organization = self . org <EOL> ) <EOL> column_mapping = ColumnMapping . objects . create ( <EOL> super_organization = self . org , <EOL> ) <EOL> column_mapping . column_raw . add ( column_raw , column_raw2 ) <EOL> column_mapping . column_mapped . add ( column_raw ) <EOL> new_expected_mappings = copy . deepcopy ( self . expected_mappings ) <EOL> new_expected_mappings [ '<STR_LIT:name>' ] = [ <EOL> [ u'<STR_LIT:name>' ] , <NUM_LIT:100> <EOL> ] <EOL> new_expected_mappings [ '<STR_LIT:address>' ] = [ <EOL> [ u'<STR_LIT:name>' ] , <NUM_LIT:100> <EOL> ] <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . id , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> suggested_mappings = body [ '<STR_LIT>' ] <EOL> self . assert_expected_mappings ( <EOL> suggested_mappings , <EOL> new_expected_mappings <EOL> ) <EOL> def test_get_raw_column_names ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . id , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> body = json . loads ( resp . content ) <EOL> self . assertDictEqual ( body , self . raw_columns_expected ) <EOL> def test_save_column_mappings ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <EOL> ColumnMapping . objects . filter ( super_organization = self . org ) . count ( ) , <EOL> <NUM_LIT:0> <EOL> ) <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . id , <EOL> '<STR_LIT>' : [ <EOL> [ "<STR_LIT:name>" , "<STR_LIT:name>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ] <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> self . assertDictEqual ( json . loads ( resp . content ) , { '<STR_LIT:status>' : '<STR_LIT:success>' } ) <EOL> test_mappings = ColumnMapping . objects . filter ( <EOL> super_organization = self . org <EOL> ) <EOL> energy_use_columns = Column . objects . filter ( <EOL> organization = self . org , <EOL> column_name = "<STR_LIT>" , <EOL> ) <EOL> self . assertEquals ( len ( energy_use_columns ) , <NUM_LIT:1> ) <EOL> eu_col = energy_use_columns . first ( ) <EOL> assert ( eu_col . unit is not None ) <EOL> self . assertEqual ( eu_col . unit . unit_type , FLOAT ) <EOL> def test_save_column_mappings_w_concat ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . id , <EOL> '<STR_LIT>' : [ <EOL> [ "<STR_LIT:name>" , [ "<STR_LIT:name>" , "<STR_LIT>" ] ] , <EOL> ] <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> self . assertDictEqual ( json . loads ( resp . content ) , { '<STR_LIT:status>' : '<STR_LIT:success>' } ) <EOL> test_mapping = ColumnMapping . objects . filter ( <EOL> super_organization = self . org <EOL> ) . first ( ) <EOL> raw_names = _get_column_names ( test_mapping ) <EOL> self . assertEquals ( <EOL> raw_names , <EOL> [ u'<STR_LIT:name>' , u'<STR_LIT>' ] <EOL> ) <EOL> def test_save_column_mappings_idempotent ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <EOL> ColumnMapping . objects . filter ( super_organization = self . org ) . count ( ) , <EOL> <NUM_LIT:0> <EOL> ) <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . id , <EOL> '<STR_LIT>' : [ <EOL> [ "<STR_LIT:name>" , "<STR_LIT:name>" ] , <EOL> ] <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> self . assertDictEqual ( json . loads ( resp . content ) , { '<STR_LIT:status>' : '<STR_LIT:success>' } ) <EOL> self . assertEqual ( <EOL> ColumnMapping . objects . filter ( super_organization = self . org ) . count ( ) , <EOL> <NUM_LIT:1> <EOL> ) <EOL> user_2_details = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> } <EOL> user_2 = User . objects . create_superuser ( ** user_2_details ) <EOL> OrganizationUser . objects . create ( <EOL> user = user_2 , organization = self . org <EOL> ) <EOL> self . client . login ( ** user_2_details ) <EOL> self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . id , <EOL> '<STR_LIT>' : [ <EOL> [ "<STR_LIT:name>" , "<STR_LIT:name>" ] , <EOL> ] <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> self . assertDictEqual ( json . loads ( resp . content ) , { '<STR_LIT:status>' : '<STR_LIT:success>' } ) <EOL> self . assertEqual ( <EOL> ColumnMapping . objects . filter ( super_organization = self . org ) . count ( ) , <EOL> <NUM_LIT:1> <EOL> ) <EOL> def test_progress ( self ) : <EOL> """<STR_LIT>""" <EOL> progress_key = decorators . get_prog_key ( '<STR_LIT>' , <NUM_LIT> ) <EOL> expected = <NUM_LIT> <EOL> cache . set ( progress_key , expected ) <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : progress_key , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> body = json . loads ( resp . content ) <EOL> self . assertEqual ( body . get ( '<STR_LIT>' , <NUM_LIT:0> ) , expected ) <EOL> self . assertEqual ( body . get ( '<STR_LIT>' , '<STR_LIT>' ) , progress_key ) <EOL> def test_remap_buildings ( self ) : <EOL> """<STR_LIT>""" <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> test_util . make_fake_snapshot ( self . import_file , { } , ASSESSED_RAW ) <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> test_util . make_fake_snapshot ( self . import_file , { } , ASSESSED_BS ) <EOL> self . import_file . mapping_done = True <EOL> self . import_file . mapping_progress = <NUM_LIT:100> <EOL> self . import_file . save ( ) <EOL> cache_key = decorators . get_prog_key ( '<STR_LIT>' , self . import_file . pk ) <EOL> cache . set ( cache_key , <NUM_LIT:100> ) <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . pk , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( <EOL> BuildingSnapshot . objects . filter ( <EOL> import_file = self . import_file , <EOL> source_type__in = ( ASSESSED_BS , PORTFOLIO_BS ) <EOL> ) . count ( ) , <EOL> <NUM_LIT:0> <EOL> ) <EOL> self . assertEqual ( <EOL> BuildingSnapshot . objects . filter ( <EOL> import_file = self . import_file , <EOL> ) . count ( ) , <EOL> <NUM_LIT:10> <EOL> ) <EOL> self . assertEqual ( cache . get ( cache_key ) , <NUM_LIT:0> ) <EOL> def test_reset_mapped_w_previous_matches ( self ) : <EOL> """<STR_LIT>""" <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> test_util . make_fake_snapshot ( self . import_file , { } , ASSESSED_RAW ) <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> test_util . make_fake_snapshot ( self . import_file , { } , ASSESSED_BS ) <EOL> child = test_util . make_fake_snapshot ( None , { } , COMPOSITE_BS ) <EOL> first = BuildingSnapshot . objects . filter ( <EOL> import_file = self . import_file <EOL> ) [ : <NUM_LIT:1> ] . get ( ) <EOL> first . children . add ( child ) <EOL> for item in BuildingSnapshot . objects . filter ( source_type = ASSESSED_BS ) : <EOL> item . property_name = '<STR_LIT>' <EOL> item . save ( ) <EOL> self . assertEqual ( <EOL> BuildingSnapshot . objects . filter ( property_name = '<STR_LIT>' ) . count ( ) , <EOL> <NUM_LIT:10> <EOL> ) <EOL> self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . pk , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> self . assertEqual ( <EOL> BuildingSnapshot . objects . filter ( property_name = '<STR_LIT>' ) . count ( ) , <EOL> <NUM_LIT:1> <EOL> ) <EOL> self . assertEqual ( <EOL> BuildingSnapshot . objects . get ( <EOL> property_name = '<STR_LIT>' <EOL> ) . children . all ( ) [ <NUM_LIT:0> ] , <EOL> child <EOL> ) <EOL> def test_reset_mapped_w_matching_done ( self ) : <EOL> """<STR_LIT>""" <EOL> self . import_file . matching_done = True <EOL> self . import_file . matching_progress = <NUM_LIT:100> <EOL> self . import_file . save ( ) <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> test_util . make_fake_snapshot ( self . import_file , { } , ASSESSED_BS ) <EOL> expected = { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . import_file . pk , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> self . assertDictEqual ( json . loads ( resp . content ) , expected ) <EOL> self . assertEqual ( <EOL> BuildingSnapshot . objects . filter ( <EOL> import_file = self . import_file <EOL> ) . count ( ) , <EOL> <NUM_LIT:10> <EOL> ) <EOL> def test_create_dataset ( self ) : <EOL> """<STR_LIT>""" <EOL> DATASET_NAME_1 = '<STR_LIT>' <EOL> DATASET_NAME_2 = '<STR_LIT>' <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT:name>' : DATASET_NAME_1 , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> data = json . loads ( resp . content ) <EOL> self . assertEqual ( data [ '<STR_LIT:name>' ] , DATASET_NAME_1 ) <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT:name>' : DATASET_NAME_2 , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> data = json . loads ( resp . content ) <EOL> self . assertEqual ( data [ '<STR_LIT:name>' ] , DATASET_NAME_2 ) <EOL> the_id = data [ '<STR_LIT:id>' ] <EOL> self . assertDictEqual ( data , { <EOL> '<STR_LIT:id>' : the_id , <EOL> '<STR_LIT:name>' : DATASET_NAME_2 , <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> } ) <EOL> resp = self . client . post ( <EOL> reverse_lazy ( "<STR_LIT>" ) , <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : self . org . pk , <EOL> '<STR_LIT:name>' : DATASET_NAME_1 , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> data_3 = json . loads ( resp . content ) <EOL> import_record = ImportRecord . objects . get ( pk = data_3 [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( data_3 [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> self . assertEqual ( data_3 [ '<STR_LIT:name>' ] , DATASET_NAME_1 ) <EOL> self . assertNotEqual ( data_3 [ '<STR_LIT:id>' ] , data [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( import_record . owner , self . user ) <EOL> self . assertEqual ( import_record . last_modified_by , self . user ) <EOL> self . assertEqual ( import_record . app , '<STR_LIT>' ) <EOL> self . assertEqual ( import_record . name , DATASET_NAME_1 ) <EOL> self . assertEqual ( self . org , import_record . super_organization ) <EOL> class MatchTreeTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> user_details = { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' <EOL> } <EOL> self . user = User . objects . create_superuser ( ** user_details ) <EOL> self . org = Organization . objects . create ( ) <EOL> OrganizationUser . objects . create ( user = self . user , organization = self . org ) <EOL> snapshots = { } <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> temp = BuildingSnapshot . objects . create ( <EOL> super_organization = self . org , <EOL> ) <EOL> key = '<STR_LIT>' . format ( i ) <EOL> snapshots [ key ] = temp <EOL> cb0 = CanonicalBuilding . objects . create ( <EOL> canonical_snapshot = snapshots [ '<STR_LIT>' ] <EOL> ) <EOL> cb1 = CanonicalBuilding . objects . create ( <EOL> canonical_snapshot = snapshots [ '<STR_LIT>' ] <EOL> ) <EOL> for key in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> temp = snapshots [ key ] <EOL> temp . canonical_building = cb0 <EOL> temp . save ( ) <EOL> bs3 = snapshots [ '<STR_LIT>' ] <EOL> bs3 . canonical_building = cb1 <EOL> bs3 . save ( ) <EOL> def link_child ( parent_key , child_key ) : <EOL> parent = snapshots [ parent_key ] <EOL> child = snapshots [ child_key ] <EOL> parent . children . add ( child ) <EOL> link_child ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> link_child ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> link_child ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> link_child ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> link_child ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> link_child ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> link_child ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> link_child ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> link_child ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . cb0 = CanonicalBuilding . objects . get ( pk = cb0 . pk ) <EOL> self . cb1 = CanonicalBuilding . objects . get ( pk = cb1 . pk ) <EOL> for k , bs in snapshots . iteritems ( ) : <EOL> reloaded = BuildingSnapshot . objects . get ( pk = bs . pk ) <EOL> setattr ( self , k , reloaded ) <EOL> def test_parent_tree_coparents ( self ) : <EOL> """<STR_LIT>""" <EOL> bs9_root , bs9_cps = _parent_tree_coparents ( self . bs9 ) <EOL> bs9_expected_parent_coparents = [ <EOL> self . bs0 , self . bs1 , self . bs3 , self . bs5 , self . bs8 <EOL> ] <EOL> self . assertEqual ( bs9_root , self . bs0 ) <EOL> self . assertItemsEqual ( bs9_cps , bs9_expected_parent_coparents ) <EOL> bs4_root , bs4_cps = _parent_tree_coparents ( self . bs4 ) <EOL> bs4_expected_parent_coparents = [ <EOL> self . bs0 , self . bs1 , self . bs3 <EOL> ] <EOL> self . assertEqual ( bs4_root , self . bs0 ) <EOL> self . assertItemsEqual ( bs4_cps , bs4_expected_parent_coparents ) <EOL> bs5_root , bs5_cps = _parent_tree_coparents ( self . bs5 ) <EOL> bs5_expected_parent_coparents = [ <EOL> self . bs0 , self . bs1 , self . bs3 <EOL> ] <EOL> self . assertEqual ( bs5_root , self . bs0 ) <EOL> self . assertItemsEqual ( bs5_cps , bs5_expected_parent_coparents ) <EOL> self . bs4 . canonical_building = self . cb1 <EOL> self . bs4 . save ( ) <EOL> self . cb1 . canonical_snapshot = self . bs4 <EOL> self . cb1 . save ( ) <EOL> bs4_root , bs4_cps = _parent_tree_coparents ( self . bs4 ) <EOL> bs4_expected_parent_coparents = [ <EOL> self . bs2 , self . bs3 <EOL> ] <EOL> self . assertEqual ( bs4_root , self . bs3 ) <EOL> self . assertItemsEqual ( bs4_cps , bs4_expected_parent_coparents ) </s>
