<s> """<STR_LIT>""" <EOL> import unittest <EOL> from six import text_type , PY3 <EOL> from six . moves import cStringIO <EOL> if PY3 : <EOL> def py3fix ( s ) : <EOL> return s . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> def py3fix ( s ) : <EOL> return s <EOL> import numpy as np <EOL> from openmdao . api import Component , Problem , Group <EOL> class MyComp ( Component ) : <EOL> def __init__ ( self ) : <EOL> super ( MyComp , self ) . __init__ ( ) <EOL> self . add_param ( '<STR_LIT>' , <NUM_LIT:2> , pass_by_obj = True ) <EOL> self . add_output ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> class MyComp2 ( Component ) : <EOL> def __init__ ( self ) : <EOL> super ( MyComp2 , self ) . __init__ ( ) <EOL> self . add_output ( '<STR_LIT>' , [ <NUM_LIT:1.0> , <NUM_LIT> ] , pass_by_obj = True ) <EOL> self . add_param ( '<STR_LIT>' , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> class TestComponent ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . comp = Component ( ) <EOL> def test_not_impl ( self ) : <EOL> with self . assertRaises ( RuntimeError ) as cm : <EOL> self . comp . solve_nonlinear ( { } , { } , { } ) <EOL> expected_msg = py3fix ( "<STR_LIT>" ) <EOL> self . assertEqual ( str ( cm . exception ) , expected_msg ) <EOL> def test_param_name_errors ( self ) : <EOL> self . comp . add_param ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> with self . assertRaises ( RuntimeError ) as cm : <EOL> self . comp . add_param ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> expected_msg = py3fix ( "<STR_LIT>" ) <EOL> self . assertEqual ( str ( cm . exception ) , expected_msg ) <EOL> with self . assertRaises ( NameError ) as cm : <EOL> self . comp . add_param ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> expected_msg = py3fix ( "<STR_LIT>" ) <EOL> self . assertEqual ( str ( cm . exception ) , expected_msg ) <EOL> self . comp . _setup_variables ( ) <EOL> with self . assertRaises ( RuntimeError ) as cm : <EOL> self . comp . add_param ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> expected_msg = "<STR_LIT>" <EOL> self . assertEqual ( str ( cm . exception ) , expected_msg ) <EOL> def test_output_name_errors ( self ) : <EOL> self . comp . add_output ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> with self . assertRaises ( RuntimeError ) as cm : <EOL> self . comp . add_output ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> expected_msg = py3fix ( "<STR_LIT>" ) <EOL> self . assertEqual ( str ( cm . exception ) , expected_msg ) <EOL> with self . assertRaises ( NameError ) as cm : <EOL> self . comp . add_output ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> expected_msg = py3fix ( "<STR_LIT>" ) <EOL> self . assertEqual ( str ( cm . exception ) , expected_msg ) <EOL> self . comp . _setup_variables ( ) <EOL> with self . assertRaises ( RuntimeError ) as cm : <EOL> self . comp . add_output ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> expected_msg = "<STR_LIT>" <EOL> self . assertEqual ( str ( cm . exception ) , expected_msg ) <EOL> def test_promotes ( self ) : <EOL> p = Problem ( root = Group ( ) ) <EOL> p . root . add ( '<STR_LIT>' , self . comp ) <EOL> self . comp . add_param ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> self . comp . add_param ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> self . comp . add_output ( "<STR_LIT>" , - <NUM_LIT:1> ) <EOL> self . comp . add_output ( "<STR_LIT>" , np . zeros ( <NUM_LIT:10> ) ) <EOL> self . comp . _promotes = ( '<STR_LIT:*>' , ) <EOL> p . setup ( check = False ) <EOL> for name in self . comp . _init_params_dict : <EOL> self . assertTrue ( self . comp . _promoted ( name ) ) <EOL> for name in self . comp . _init_unknowns_dict : <EOL> self . assertTrue ( self . comp . _promoted ( name ) ) <EOL> self . assertFalse ( self . comp . _promoted ( '<STR_LIT>' ) ) <EOL> self . comp . _promotes = ( '<STR_LIT>' , ) <EOL> p . setup ( check = False ) <EOL> for name in self . comp . _init_params_dict : <EOL> if name . startswith ( '<STR_LIT:x>' ) : <EOL> self . assertTrue ( self . comp . _promoted ( name ) ) <EOL> else : <EOL> self . assertFalse ( self . comp . _promoted ( name ) ) <EOL> for name in self . comp . _init_unknowns_dict : <EOL> if name . startswith ( '<STR_LIT:x>' ) : <EOL> self . assertTrue ( self . comp . _promoted ( name ) ) <EOL> else : <EOL> self . assertFalse ( self . comp . _promoted ( name ) ) <EOL> self . comp . _promotes = ( '<STR_LIT>' , ) <EOL> p . setup ( check = False ) <EOL> for name in self . comp . _init_params_dict : <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> self . assertTrue ( self . comp . _promoted ( name ) ) <EOL> else : <EOL> self . assertFalse ( self . comp . _promoted ( name ) ) <EOL> for name in self . comp . _init_unknowns_dict : <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> self . assertTrue ( self . comp . _promoted ( name ) ) <EOL> else : <EOL> self . assertFalse ( self . comp . _promoted ( name ) ) <EOL> try : <EOL> self . comp . _promotes = ( '<STR_LIT:*>' ) <EOL> self . comp . _promoted ( '<STR_LIT>' ) <EOL> except Exception as err : <EOL> self . assertEqual ( text_type ( err ) , <EOL> "<STR_LIT>" ) <EOL> def test_add_params ( self ) : <EOL> self . comp . add_param ( "<STR_LIT:x>" , <NUM_LIT:0.0> ) <EOL> self . comp . add_param ( "<STR_LIT:y>" , <NUM_LIT:0.0> ) <EOL> self . comp . add_param ( "<STR_LIT:z>" , shape = ( <NUM_LIT:1> , ) ) <EOL> self . comp . add_param ( "<STR_LIT:t>" , shape = <NUM_LIT:2> ) <EOL> self . comp . add_param ( "<STR_LIT:u>" , shape = <NUM_LIT:1> ) <EOL> self . comp . add_param ( "<STR_LIT:w>" ) <EOL> prob = Problem ( ) <EOL> self . comp . _init_sys_data ( '<STR_LIT>' , prob . _probdata ) <EOL> params , unknowns = self . comp . _setup_variables ( ) <EOL> self . assertEqual ( [ "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" , "<STR_LIT:t>" , "<STR_LIT:u>" , '<STR_LIT:w>' ] , list ( params . keys ( ) ) ) <EOL> self . assertEqual ( params [ "<STR_LIT:x>" ] , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT:x>' , '<STR_LIT>' : <NUM_LIT:0.0> , '<STR_LIT:size>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( params [ "<STR_LIT:y>" ] , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT:y>' , '<STR_LIT>' : <NUM_LIT:0.0> , '<STR_LIT:size>' : <NUM_LIT:1> } ) <EOL> np . testing . assert_array_equal ( params [ "<STR_LIT:z>" ] [ "<STR_LIT>" ] , np . zeros ( ( <NUM_LIT:1> , ) ) ) <EOL> np . testing . assert_array_equal ( params [ "<STR_LIT:t>" ] [ "<STR_LIT>" ] , np . zeros ( ( <NUM_LIT:2> , ) ) ) <EOL> self . assertEqual ( params [ "<STR_LIT:u>" ] , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT:u>' , '<STR_LIT>' : <NUM_LIT:0.0> , '<STR_LIT:size>' : <NUM_LIT:1> } ) <EOL> prob = Problem ( ) <EOL> root = prob . root = Group ( ) <EOL> root . add ( '<STR_LIT>' , self . comp ) <EOL> with self . assertRaises ( RuntimeError ) as cm : <EOL> prob . setup ( check = False ) <EOL> self . assertTrue ( "<STR_LIT>" in str ( cm . exception ) ) <EOL> def test_add_outputs ( self ) : <EOL> self . comp . add_output ( "<STR_LIT:x>" , - <NUM_LIT:1> ) <EOL> self . comp . add_output ( "<STR_LIT:y>" , np . zeros ( <NUM_LIT:10> ) ) <EOL> self . comp . add_output ( "<STR_LIT:z>" , shape = ( <NUM_LIT:10> , ) ) <EOL> self . comp . add_output ( "<STR_LIT:t>" , shape = <NUM_LIT:2> ) <EOL> self . comp . add_output ( "<STR_LIT:u>" , shape = <NUM_LIT:1> ) <EOL> with self . assertRaises ( ValueError ) as cm : <EOL> self . comp . add_output ( "<STR_LIT:w>" ) <EOL> self . assertEqual ( str ( cm . exception ) , "<STR_LIT>" ) <EOL> prob = Problem ( ) <EOL> self . comp . _init_sys_data ( '<STR_LIT>' , prob . _probdata ) <EOL> params , unknowns = self . comp . _setup_variables ( ) <EOL> self . assertEqual ( [ "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" , "<STR_LIT:t>" , "<STR_LIT:u>" ] , list ( unknowns . keys ( ) ) ) <EOL> self . assertIsInstance ( unknowns [ "<STR_LIT:x>" ] [ "<STR_LIT>" ] , int ) <EOL> self . assertIsInstance ( unknowns [ "<STR_LIT:y>" ] [ "<STR_LIT>" ] , np . ndarray ) <EOL> self . assertIsInstance ( unknowns [ "<STR_LIT:z>" ] [ "<STR_LIT>" ] , np . ndarray ) <EOL> self . assertIsInstance ( unknowns [ "<STR_LIT:t>" ] [ "<STR_LIT>" ] , np . ndarray ) <EOL> self . assertIsInstance ( unknowns [ "<STR_LIT:u>" ] [ "<STR_LIT>" ] , float ) <EOL> self . assertEqual ( unknowns [ "<STR_LIT:x>" ] , { '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT:x>' , <EOL> '<STR_LIT>' : - <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:0> } ) <EOL> self . assertEqual ( list ( unknowns [ "<STR_LIT:y>" ] [ "<STR_LIT>" ] ) , <NUM_LIT:10> * [ <NUM_LIT:0> ] ) <EOL> np . testing . assert_array_equal ( unknowns [ "<STR_LIT:z>" ] [ "<STR_LIT>" ] , np . zeros ( ( <NUM_LIT:10> , ) ) ) <EOL> np . testing . assert_array_equal ( unknowns [ "<STR_LIT:t>" ] [ "<STR_LIT>" ] , np . zeros ( ( <NUM_LIT:2> , ) ) ) <EOL> self . assertEqual ( unknowns [ "<STR_LIT:u>" ] , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT:u>' , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , '<STR_LIT:size>' : <NUM_LIT:1> } ) <EOL> def test_add_states ( self ) : <EOL> self . comp . add_state ( "<STR_LIT>" , <NUM_LIT:0.0> ) <EOL> self . comp . add_state ( "<STR_LIT>" , <NUM_LIT> ) <EOL> self . comp . add_state ( "<STR_LIT>" , shape = ( <NUM_LIT:1> , ) ) <EOL> self . comp . add_state ( "<STR_LIT>" , shape = <NUM_LIT:2> ) <EOL> self . comp . add_state ( "<STR_LIT>" , shape = <NUM_LIT:1> ) <EOL> with self . assertRaises ( ValueError ) as cm : <EOL> self . comp . add_state ( "<STR_LIT>" ) <EOL> self . assertEqual ( str ( cm . exception ) , "<STR_LIT>" ) <EOL> prob = Problem ( ) <EOL> self . comp . _init_sys_data ( '<STR_LIT>' , prob . _probdata ) <EOL> params , unknowns = self . comp . _setup_variables ( ) <EOL> self . assertEqual ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , list ( unknowns . keys ( ) ) ) <EOL> self . assertEqual ( unknowns [ "<STR_LIT>" ] , { '<STR_LIT>' : <NUM_LIT:0.0> , '<STR_LIT:state>' : True , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( unknowns [ "<STR_LIT>" ] , { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT:state>' : True , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT:1> } ) <EOL> np . testing . assert_array_equal ( unknowns [ "<STR_LIT>" ] [ "<STR_LIT>" ] , np . zeros ( ( <NUM_LIT:1> , ) ) ) <EOL> np . testing . assert_array_equal ( unknowns [ "<STR_LIT>" ] [ "<STR_LIT>" ] , np . zeros ( ( <NUM_LIT:2> , ) ) ) <EOL> self . assertEqual ( unknowns [ "<STR_LIT>" ] , { '<STR_LIT>' : <NUM_LIT:0.0> , '<STR_LIT:state>' : True , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT:1> } ) <EOL> def test_generate_numpydocstring ( self ) : <EOL> self . comp . add_param ( "<STR_LIT:x>" , <NUM_LIT:0.0> ) <EOL> self . comp . add_param ( "<STR_LIT:y>" , shape = <NUM_LIT:2> ) <EOL> self . comp . add_output ( "<STR_LIT:z>" , - <NUM_LIT:1> ) <EOL> self . comp . add_state ( "<STR_LIT:s>" , <NUM_LIT:0.0> ) <EOL> test_string = self . comp . generate_docstring ( ) <EOL> original_string = """<STR_LIT>""" <EOL> for sorig , stest in zip ( original_string . split ( '<STR_LIT:\n>' ) , test_string . split ( '<STR_LIT:\n>' ) ) : <EOL> self . assertEqual ( sorig , stest ) <EOL> def test_add_var_pbo_check ( self ) : <EOL> p = Problem ( root = Group ( ) ) <EOL> root = p . root <EOL> root . add ( '<STR_LIT>' , MyComp ( ) ) <EOL> root . add ( '<STR_LIT>' , MyComp2 ( ) ) <EOL> s = cStringIO ( ) <EOL> p . setup ( out_stream = s ) <EOL> content = s . getvalue ( ) <EOL> self . assertTrue ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" in content ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
