def test_prop_dict_ylabel(self): fig = plot_single_material_vs_depth((self.a, self.b), self.xlabels, prop_dict={'ylabel': 'hello'}) ax1 = fig.get_axes()[0] assert_equal(ax1.get_ylabel(), 'hello')
def test_to_dict_recursive_circular(): class Brother(TInterface): _dict_attrs = ( 'name', 'siblings' ) def __init__(self, name): self.name = name self.siblings = [] def __repr__(self): return 'Brother: %s' % (self.name) tom = Brother('tom') jerry = Brother('jerry') tom.siblings.append(jerry) jerry.siblings.append(tom) result = to_dict_recursive(tom) result2 = to_dict_recursive(jerry) assert_equal( result, {'name': 'tom', 'siblings': [{'name': 'jerry', 'siblings': []}]} ) assert_equal( result2, {'name': 'jerry', 'siblings': [{'name': 'tom', 'siblings': []}]} )
def test_area(self): assert_equal(integrate_f_over_polygon_code(1).splitlines(), 'def ifxy(pts):\n "Integrate f = 1 over ' 'polygon"\n\n x, y, z = xyz_from_pts(pts, True)' '\n\n return np.sum((x[:-1]/2 + x[1:]/2)*(-y[:-1] ' '+ y[1:]))'.splitlines())
def test_defaults(self): fig = plot_single_material_vs_depth((self.a, self.b), self.xlabels) assert_equal(len(fig.get_axes()), 2) ax1 = fig.get_axes()[0] line1= ax1.get_lines()[0] ax2 = fig.get_axes()[1] line2 = ax2.get_lines()[0] assert_allclose(line1.get_xydata()[0], np.array([ 1., 0.])) assert_allclose(line1.get_xydata()[-1], np.array([ 2., 1])) assert_allclose(line2.get_xydata()[0], np.array([ 2., 0.])) assert_allclose(line2.get_xydata()[-1], np.array([ 3., 1])) assert_equal(ax1.get_xlabel(), 'a') assert_equal(ax2.get_xlabel(), 'b') assert_equal(ax1.get_ylabel(), 'Depth, z') assert_equal(ax2.get_ylabel(), '')
def test_initialize_objects_attributes(): """test for initialize_objects_attributes function""" #initialize_objects_attributes(obj, attributes=[], defaults = dict(), not_found_value = None): a = EmptyClass() initialize_objects_attributes(a,attributes=['a','b'], defaults={'a': 6}) assert_equal([a.a,a.b],[6,None])
def test_to_dict_recursive_circular(): class Brother(TInterface): _dict_attrs = ('name', 'siblings') def __init__(self, name): self.name = name self.siblings = [] def __repr__(self): return 'Brother: %s' % (self.name) tom = Brother('tom') jerry = Brother('jerry') tom.siblings.append(jerry) jerry.siblings.append(tom) result = to_dict_recursive(tom) result2 = to_dict_recursive(jerry) assert_equal(result, { 'name': 'tom', 'siblings': [{ 'name': 'jerry', 'siblings': [] }] }) assert_equal(result2, { 'name': 'jerry', 'siblings': [{ 'name': 'tom', 'siblings': [] }] })
def test_check_attribute_pairs_have_equal_length(): """test for check_attribute_pairs_have_equal_length function""" #check_attribute_pairs_have_equal_length(obj, attributes=[]) a = EmptyClass() a.a = None a.b = [7, 8] a.c = [8] a.d = [6, 7] a.e = 8 # assert_raises(ValueError, check_attribute_pairs_have_equal_length, a, # attributes=[['a','b']]) assert_raises(ValueError, check_attribute_pairs_have_equal_length, a, attributes=[['b', 'c']]) assert_raises(TypeError, check_attribute_pairs_have_equal_length, a, attributes=[['b', 'e']]) assert_equal( check_attribute_pairs_have_equal_length(a, attributes=[['b', 'd']]), None)
def test_initialize_objects_attributes(): """test for initialize_objects_attributes function""" #initialize_objects_attributes(obj, attributes=[], defaults = dict(), not_found_value = None): a = EmptyClass() initialize_objects_attributes(a, attributes=['a', 'b'], defaults={'a': 6}) assert_equal([a.a, a.b], [6, None])
def test_RLzero(self): fig = plot_generic_loads([[self.triple1], [self.triple2]], load_names=self.load_names, H=2.0, RLzero=1) ax1 = fig.get_axes()[0] line1 = ax1.get_lines()[0] ax2 = fig.get_axes()[1] line2 = ax2.get_lines()[0] ax3 = fig.get_axes()[2] line3 = ax3.get_lines()[0] ax4 = fig.get_axes()[3] line4 = ax4.get_lines()[0] #first row of charts assert_allclose(line1.get_xydata()[0], np.array([0, 0])) assert_allclose(line1.get_xydata()[-1], np.array([10, 1 * np.cos(0.5 * 10 + 0.3)])) assert_allclose(line2.get_xydata()[0], np.array([1, 1])) assert_allclose(line2.get_xydata()[-1], np.array([0.5, -1])) #2nd row of charts assert_allclose(line3.get_xydata()[0], np.array([0, 0])) assert_allclose(line3.get_xydata()[-1], np.array([9, 2])) assert_allclose(line4.get_xydata()[0], np.array([1, 1])) assert_allclose(line4.get_xydata()[-1], np.array([0.8, -1])) assert_equal(ax2.get_ylabel(), 'RL') assert_equal(ax4.get_ylabel(), 'RL')
def test_two_files(self): data = np.arange(6).reshape(3, 2) save_grid_data_to_file([{ 'data': data, 'name': 1 }, { 'data': 2 * data, 'name': 2 }], directory=self.tempdir.path, file_stem="qqq") assert_equal( self.tempdir.read(('qqq', 'qqq1.csv'), 'utf-8').splitlines(), textwrap.dedent("""\ idex,0,1 0,0,1 1,2,3 2,4,5""").splitlines()) assert_equal( self.tempdir.read(('qqq', 'qqq2.csv'), 'utf-8').splitlines(), textwrap.dedent("""\ idex,0,1 0,0,2 1,4,6 2,8,10""").splitlines())
def test_propdict_legend_prop_title(self): fig = plot_generic_loads([[self.triple1], [self.triple2]], load_names=self.load_names, prop_dict={'legend_prop':{'title':'abc'}}) ax1 = fig.get_axes()[0] ax3 = fig.get_axes()[2] assert_equal(ax1.get_legend().get_title().get_text(), 'abc') assert_equal(ax3.get_legend().get_title().get_text(), 'abc')
def test_area(self): assert_equal( integrate_f_over_polygon_code(1).splitlines(), 'def ifxy(pts):\n "Integrate f = 1 over ' 'polygon"\n\n x, y, z = xyz_from_pts(pts, True)' '\n\n return np.sum((x[:-1]/2 + x[1:]/2)*(-y[:-1] ' '+ y[1:]))'.splitlines())
def test_save_data_input_text(self): a = InputFileLoaderCheckerSaver() a._input_text= "hello" a.save_data_to_file=True a._save_data() assert_equal(self.tempdir.read( ('out0002','out0002_input_original.py'), 'utf-8').strip().splitlines(), 'hello'.splitlines())
def test_check_attributes_to_force_same_len(self): a = InputFileLoaderCheckerSaver() a.a = [4,5] a.c=None a._attributes_to_force_same_len = ['a c'.split()] a.check_input_attributes() assert_equal(a.c, [None, None])
def test_determine_output_stem_directory(self): a = InputFileLoaderCheckerSaver() a.directory = os.path.join(self.tempdir.path, 'what') a._determine_output_stem() assert_equal( a._file_stem, os.path.join(self.tempdir.path, 'what', 'out0002', 'out0002'))
def test_check_attributes_to_force_same_len(self): a = InputFileLoaderCheckerSaver() a.a = [4, 5] a.c = None a._attributes_to_force_same_len = ['a c'.split()] a.check_input_attributes() assert_equal(a.c, [None, None])
def test_propdict_legend_prop_title(self): fig = plot_vs_depth(self.x, self.z, ['a', 'b', 'c'], prop_dict={'legend_prop': { 'title': 'abc' }}) ax = fig.get_axes()[0] assert_equal(ax.get_legend().get_title().get_text(), 'abc')
def test_propdict_has_legend(self): fig = plot_vs_depth(self.x, self.z, ['a', 'b', 'c'], prop_dict={'has_legend': False}) ax = fig.get_axes()[0] assert_equal(ax.get_legend(), None)
def test_prop_dict_depth_axis_label(self): fig = plot_generic_loads([[self.triple1], [self.triple2]], load_names=self.load_names, prop_dict={'depth_axis_label': 'hello'}) ax2 = fig.get_axes()[1] ax4 = fig.get_axes()[3] assert_equal(ax2.get_xlabel(), '') assert_equal(ax4.get_xlabel(), 'hello')
def test_prop_dict_time_axis_label(self): fig = plot_generic_loads([[self.triple1], [self.triple2]], load_names=self.load_names, prop_dict={'time_axis_label': 'hello'}) ax1 = fig.get_axes()[0] ax3 = fig.get_axes()[2] assert_equal(ax1.get_xlabel(), '') assert_equal(ax3.get_xlabel(), 'hello')
def test_split_sequence_into_dict_and_nondicts(): """test for split_sequence_into_dict_and_nondicts""" #split_sequence_into_dict_and_nondicts(*args) assert_equal(split_sequence_into_dict_and_nondicts({'a': 2, 'b': 3}, 4, {'a':8, 'c':5}, 5), ([4,5], {'a': 8, 'b': 3, 'c':5}))
def test_copy_dict(): """test for copy_dict""" #copy_dict(source_dict, diffs) # ok_(copy_dict({'a':7, 'b':12}, {'c':13})=={'a':7, 'b':12, 'c':13}) assert_equal(copy_dict({'a':7, 'b':12}, {'c':13}), {'a':7, 'b':12, 'c':13}) # ok_(copy_dict({'a':7, 'b':12}, {'a':21, 'c':13})=={'a':21, 'b':12, 'c':13}) assert_equal(copy_dict({'a':7, 'b':12}, {'a':21, 'c':13}), {'a':21, 'b':12, 'c':13})
def test_check_attributes_that_should_be_lists(self): a = InputFileLoaderCheckerSaver() a.a=4 a.b=6 a._attributes_that_should_be_lists = ['b'] a.check_input_attributes() assert_equal(a.a, 4) assert_equal(a.b, [6])
def test_check_attributes_that_should_be_lists(self): a = InputFileLoaderCheckerSaver() a.a = 4 a.b = 6 a._attributes_that_should_be_lists = ['b'] a.check_input_attributes() assert_equal(a.a, 4) assert_equal(a.b, [6])
def test_propdict_xylabels(self): fig = plot_vs_depth(self.x, self.z, ['a', 'b', 'c'], prop_dict={'xlabel':'xxx', 'ylabel':'yyy'}) ax = fig.get_axes()[0] assert_equal(ax.get_xlabel(), 'xxx') assert_equal(ax.get_ylabel(), 'yyy')
def test_propdict_has_legend(self): fig = plot_generic_loads([[self.triple1], [self.triple2]], load_names=self.load_names, prop_dict={'has_legend': False}) ax1 = fig.get_axes()[0] ax3 = fig.get_axes()[2] assert_equal(ax1.get_legend(), None) assert_equal(ax3.get_legend(), None)
def test_copy_attributes_from_text_to_object(): """test for copy_attributes_from_text_to_object function""" #copy_attributes_from_text_to_object(reader,*args, **kwargs) reader = textwrap.dedent("""\ a = 2 b = 3 """) a = EmptyClass() copy_attributes_from_text_to_object(reader,a,['a','b', 'aa', 'bb'], {'bb': 27}) assert_equal([a.a, a.b, a.aa, a.bb], [2, 3, None, 27])
def test_default(): default_validator = konval.Default(konval.types.IsType(str), 'NOT A STRING!') string_value = '1234' assert_equal(default_validator(string_value), string_value) numerical_value = 1234 assert_equal(default_validator(numerical_value), 'NOT A STRING!')
def test_ylabels(self): fig = plot_generic_loads([[self.triple1], [self.triple2]], load_names=self.load_names, ylabels=['one', 'two']) ax1 = fig.get_axes()[0] ax3 = fig.get_axes()[2] assert_equal(ax1.get_ylabel(), 'one') assert_equal(ax3.get_ylabel(), 'two')
def test_if(): if_validator = konval.If(True, konval.types.ToType(int)) numerical_string = '1234' assert_equal(if_validator(numerical_string), 1234) if_validator = konval.If(False, konval.types.ToType(int)) assert_equal(if_validator(numerical_string), numerical_string)
def test_if_else(): if_else_validator = konval.IfElse(konval.types.IsType(str), konval.types.ToType(str)) string_value = '1234' assert_equal(if_else_validator(string_value), string_value) numerical_value = 1234 assert_equal(if_else_validator(numerical_value), string_value)
def test_plot_args_marker(self): fig = plt.figure() data = [([self.xa1, self.ya1,{'marker': 's'}], [self.xa2, self.ya2]), ([self.xb1, self.yb1],)] plot_data_in_grid(fig, data, self.gs) assert_equal(len(fig.get_axes()), 2) ax1 = fig.get_axes()[0] line1 = ax1.get_lines()[0] assert_equal(line1.get_marker(), 's')
def test_pattern_with_fobj(self): a = GenericInputFileArgParser(self._abc_fobj, True) args = '-d {0} -p *.txt'.format(self.tempdir.path).split() # print(args) a.main(argv=args) assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(), textwrap.dedent("""\ b1 b2""").splitlines())
def test_directory_with_path(self): a = GenericInputFileArgParser(self._abc_path, False) args = '-d {0} -p'.format(self.tempdir.path).split() print(args) a.main(argv=args) assert_equal(self.tempdir.read(('out.zebra'), 'utf-8').splitlines(), textwrap.dedent("""\ a1.py a2.py""").splitlines())
def test_data_dict_row_labels(self): data = np.arange(6).reshape(3,2) save_grid_data_to_file({'data': data, 'row_labels':[8,12,6]}, directory=self.tempdir.path) assert_equal(self.tempdir.read(('out_000','out_000.csv'), 'utf-8').splitlines(), textwrap.dedent("""\ idex,item,0,1 0,8,0,1 1,12,2,3 2,6,4,5""").splitlines())
def test_data_dict_name(self): data = np.arange(6).reshape(3,2) save_grid_data_to_file({'data': data, 'name':'xx'}, directory=self.tempdir.path) assert_equal(self.tempdir.read(('out_000','out_000xx.csv'), 'utf-8').splitlines(), textwrap.dedent("""\ idex,0,1 0,0,1 1,2,3 2,4,5""").splitlines())
def test_directory(self): data = np.arange(6).reshape(3,2) save_grid_data_to_file({'data': data}, directory=os.path.join(self.tempdir.path,'g')) assert_equal(self.tempdir.read(('g','out_000','out_000.csv'), 'utf-8').splitlines(), textwrap.dedent("""\ idex,0,1 0,0,1 1,2,3 2,4,5""").splitlines())
def test_data_dict_column_labels(self): data = np.arange(6).reshape(3,2) save_grid_data_to_file({'data': data, 'column_labels':['a', 'b']}, directory=self.tempdir.path) assert_equal(self.tempdir.read(('out_000','out_000.csv'), 'utf-8').splitlines(), textwrap.dedent("""\ idex,a,b 0,0,1 1,2,3 2,4,5""").splitlines())
def test_ylabels(self): fig = plot_generic_loads([[self.triple1], [self.triple2]], load_names=self.load_names, ylabels = ['one', 'two']) ax1 = fig.get_axes()[0] ax3 = fig.get_axes()[2] assert_equal(ax1.get_ylabel(), 'one') assert_equal(ax3.get_ylabel(), 'two')
def test_file_stem(self): data = np.arange(6).reshape(3,2) save_grid_data_to_file({'data': data}, directory=self.tempdir.path, file_stem="ppp") assert_equal(self.tempdir.read(('ppp','ppp.csv'), 'utf-8').splitlines(), textwrap.dedent("""\ idex,0,1 0,0,1 1,2,3 2,4,5""").splitlines())
def test_create_directory(self): data = np.arange(6).reshape(3,2) save_grid_data_to_file({'data': data}, directory=self.tempdir.path, create_directory=False) assert_equal(self.tempdir.read('out_000.csv', 'utf-8').splitlines(), textwrap.dedent("""\ idex,0,1 0,0,1 1,2,3 2,4,5""").splitlines())