<s> from io import StringIO <EOL> import pandas as pd <EOL> import pytest <EOL> from calliope import locations , utils <EOL> class TestLocations : <EOL> @ pytest . fixture <EOL> def sample_locations ( self ) : <EOL> setup = StringIO ( """<STR_LIT>""" ) <EOL> return utils . AttrDict . from_yaml ( setup ) <EOL> @ pytest . fixture <EOL> def sample_unexploded_locations ( self ) : <EOL> setup = StringIO ( """<STR_LIT>""" ) <EOL> return utils . AttrDict . from_yaml ( setup ) <EOL> @ pytest . fixture <EOL> def sample_nested_locations ( self ) : <EOL> setup = StringIO ( """<STR_LIT>""" ) <EOL> return utils . AttrDict . from_yaml ( setup ) <EOL> @ pytest . fixture <EOL> def sample_overlapping_locations ( self ) : <EOL> setup = StringIO ( """<STR_LIT>""" ) <EOL> return utils . AttrDict . from_yaml ( setup ) <EOL> def test_generate_location ( self , sample_locations ) : <EOL> location = '<STR_LIT:test>' <EOL> items = sample_locations [ location ] <EOL> techs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result = locations . _generate_location ( location , items , techs ) <EOL> wanted_cols = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert sorted ( result . keys ( ) ) == wanted_cols <EOL> def test_generate_location_lacking_techs ( self , sample_locations ) : <EOL> location = '<STR_LIT:test>' <EOL> items = sample_locations [ location ] <EOL> techs = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> result = locations . _generate_location ( location , items , techs ) <EOL> wanted_cols = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert sorted ( result . keys ( ) ) == wanted_cols <EOL> def test_explode_location_single ( self ) : <EOL> assert locations . explode_locations ( '<STR_LIT:a>' ) == [ '<STR_LIT:a>' ] <EOL> def test_explode_location_range ( self ) : <EOL> assert locations . explode_locations ( '<STR_LIT>' ) == [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] <EOL> def test_explode_location_range_backwards ( self ) : <EOL> with pytest . raises ( KeyError ) : <EOL> locations . explode_locations ( '<STR_LIT>' ) <EOL> def test_explode_location_range_nonnumeric ( self ) : <EOL> with pytest . raises ( ValueError ) : <EOL> locations . explode_locations ( '<STR_LIT>' ) <EOL> def test_explore_location_list ( self ) : <EOL> assert locations . explode_locations ( '<STR_LIT>' ) == [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] <EOL> def test_explode_location_mixed ( self ) : <EOL> assert ( locations . explode_locations ( '<STR_LIT>' ) <EOL> == [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' , '<STR_LIT:c>' ] ) <EOL> def test_explode_location_empty ( self ) : <EOL> with pytest . raises ( KeyError ) : <EOL> assert locations . explode_locations ( '<STR_LIT>' ) <EOL> def test_explode_location_invalid ( self ) : <EOL> with pytest . raises ( AssertionError ) : <EOL> assert locations . explode_locations ( [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) <EOL> def test_process_locations ( self , sample_unexploded_locations ) : <EOL> fixture = sample_unexploded_locations <EOL> o = locations . process_locations ( fixture ) <EOL> assert '<STR_LIT>' in o <EOL> assert '<STR_LIT:x>' in o <EOL> assert len ( list ( o . keys ( ) ) ) == <NUM_LIT:20> <EOL> def test_process_locations_overlap ( self , sample_overlapping_locations ) : <EOL> fixture = sample_overlapping_locations <EOL> o = locations . process_locations ( fixture ) <EOL> assert o [ '<STR_LIT:1>' ] . level == <NUM_LIT:0> <EOL> assert o [ '<STR_LIT:1>' ] . override . bar == '<STR_LIT>' <EOL> def test_process_locations_levels ( self , sample_nested_locations ) : <EOL> fixture = sample_nested_locations <EOL> o = locations . process_locations ( fixture ) <EOL> assert o [ '<STR_LIT:1>' ] . level == <NUM_LIT:0> <EOL> assert o [ '<STR_LIT:bar>' ] . level == <NUM_LIT:1> <EOL> assert o [ '<STR_LIT>' ] . level == <NUM_LIT:2> <EOL> def test_generate_location_matrix_cols ( self , sample_locations ) : <EOL> techs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> df = locations . generate_location_matrix ( sample_locations , techs ) <EOL> wanted_cols = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert sorted ( df . columns ) == wanted_cols <EOL> def test_generate_location_matrix_index ( self , sample_locations ) : <EOL> techs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> df = locations . generate_location_matrix ( sample_locations , techs ) <EOL> assert df . index . tolist ( ) == [ '<STR_LIT:test>' , '<STR_LIT>' ] <EOL> def test_generate_location_matrix_values ( self , sample_locations ) : <EOL> techs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> df = locations . generate_location_matrix ( sample_locations , techs ) <EOL> assert df . at [ '<STR_LIT:test>' , '<STR_LIT>' ] == <NUM_LIT:1> <EOL> assert df . at [ '<STR_LIT>' , '<STR_LIT>' ] == <NUM_LIT:0> <EOL> assert ( df . at [ '<STR_LIT:test>' , '<STR_LIT>' ] <EOL> == '<STR_LIT>' ) <EOL> assert pd . isnull ( df . at [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_generate_location_matrix_additional_techs ( self , sample_locations ) : <EOL> techs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:foo>' ] <EOL> df = locations . generate_location_matrix ( sample_locations , techs ) <EOL> assert sum ( df [ '<STR_LIT:foo>' ] ) == <NUM_LIT:0> <EOL> def test_generate_location_matrix_missing_techs_cols ( self , sample_locations ) : <EOL> techs = [ '<STR_LIT>' ] <EOL> df = locations . generate_location_matrix ( sample_locations , techs ) <EOL> wanted_cols = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert sorted ( df . columns ) == wanted_cols <EOL> def test_generate_location_matrix_within_only_strings ( self , <EOL> sample_nested_locations ) : <EOL> techs = [ '<STR_LIT:foo>' ] <EOL> df = locations . generate_location_matrix ( sample_nested_locations , techs ) <EOL> for i in df [ '<STR_LIT>' ] . tolist ( ) : <EOL> assert ( i is None or isinstance ( i , str ) ) </s>
