def setUp(self): self.ndata = 10 self.data = np.random.rand(2, self.ndata) self.curve = GwyGraphCurve(data=self.data) self.description = "Curve" self.curve.meta['description'] = self.description patcher_lib = patch('pygwyfile.gwygraphcurve.lib', autospec=True) self.addCleanup(patcher_lib.stop) self.mock_lib = patcher_lib.start() self.gwycurve = Mock()
def test_positional_args_of_libgwyfile_func_call(self): """ Test positional args in gwyfile_object_graphcurvemodel_get call First arg is GwyGraphCurveModel* Second arg is GwyfileError** Last arg is NULL """ self.mock_lib.gwyfile_object_graphcurvemodel_get.side_effect = ( self._positional_args_side_effect) GwyGraphCurve._get_data(self.gwycurve, self.npoints)
def setUp(self): self.gwygraphmodel = Mock(spec=GwyGraphModel) self.ncurves = 2 self.gwygraphmodel.curves = [ GwyGraphCurve(np.random.rand(2, 10)) for curve in range(self.ncurves) ] self.gwygraphmodel.meta = { 'title': 'Title', 'top_label': '', 'left_label': 'y', 'right_label': '', 'bottom_label': 'x', 'x_unit': 'm', 'y_unit': 'm', 'x_min': 0., 'x_min_set': True, 'x_max': 1., 'x_max_set': True, 'x_is_logarithmic': False, 'y_is_logarithmic': False, 'label.visible': True, 'label.has_frame': True, 'label.reverse': False, 'label.frame_thickness': 1, 'label.position': 0, 'grid-type': 1 } self.gwygraphmodel.to_gwy = GwyGraphModel.to_gwy self.expected_return = Mock()
def test_getting_line_size_of_graphcurvemodel(self): """ Test getting 'line_size' field from GwyGraphCurveModel """ self.mock_lib.gwyfile_object_graphcurvemodel_get.side_effect = ( self._getting_line_size_side_effect) meta = GwyGraphCurve._get_meta(self.gwycurve) self.assertEqual(meta['line_size'], self.point_size)
def test_getting_null_description_of_gwycurvemodel(self): """ Test getting NULL in 'description' field from GwyGraphCurveModel """ self.mock_lib.gwyfile_object_graphcurvemodel_get.side_effect = ( self._getting_null_description_of_curve) meta = GwyGraphCurve._get_meta(self.gwycurve) self.assertEqual(meta['description'], '')
def test_getting_number_of_curves_in_graphcurvemodel(self): """ Test getting 'ndata' field from GwyGraphCurveModel object """ self.mock_lib.gwyfile_object_graphcurvemodel_get.side_effect = ( self._getting_number_of_points_side_effect) meta = GwyGraphCurve._get_meta(self.gwycurve) self.assertEqual(meta['ndata'], self.ndata)
def test_returned_value(self): """ Test the value returned by _get_data method" """ self.mock_lib.gwyfile_object_graphcurvemodel_get.side_effect = ( self._returned_value_side_effect) data = GwyGraphCurve._get_data(self.gwycurve, self.npoints) np.testing.assert_almost_equal(self.xdata, data[0]) np.testing.assert_almost_equal(self.ydata, data[1])
def test_getting_color_of_graphcurvemodel(self): """ Test getting 'color.red', 'color.green', 'color.blue' fields """ self.mock_lib.gwyfile_object_graphcurvemodel_get.side_effect = ( self._getting_color_side_effect) meta = GwyGraphCurve._get_meta(self.gwycurve) self.assertEqual(meta['color.red'], self.color_red) self.assertEqual(meta['color.green'], self.color_green) self.assertEqual(meta['color.blue'], self.color_blue)
def test_GwyGraphCurve_init(self, mock_get_meta, mock_get_data, mock_GwyGraphCurve): cgwycurve = Mock() test_meta = {'ndata': 256, 'description': "Curve label", 'type': 1} test_data = np.random.rand(2, 256) mock_get_meta.return_value = test_meta mock_get_data.return_value = test_data gwycurve = GwyGraphCurve.from_gwy(cgwycurve) mock_get_meta.assert_has_calls([call(cgwycurve)]) mock_get_data.assert_has_calls([call(cgwycurve, test_meta['ndata'])]) self.assertEqual(gwycurve, mock_GwyGraphCurve(data=test_data, meta=test_meta))
class GwyGraphCurve_to_gwy(unittest.TestCase): """ Tests for to_gwy method of GwyGraphCurve class""" def setUp(self): self.ndata = 10 self.data = np.random.rand(2, self.ndata) self.curve = GwyGraphCurve(data=self.data) self.description = "Curve" self.curve.meta['description'] = self.description patcher_lib = patch('pygwyfile.gwygraphcurve.lib', autospec=True) self.addCleanup(patcher_lib.stop) self.mock_lib = patcher_lib.start() self.gwycurve = Mock() def test_arguments_of_libgwyfile_func(self): """ Test arguments of gwyfile_object_new_graphcurvemodel call""" self.mock_lib.gwyfile_object_new_graphcurvemodel.side_effect = ( self._side_effect) gwycurve = self.curve.to_gwy() self.assertEqual(gwycurve, self.gwycurve) def _side_effect(self, *args): self.assertEqual(int(args[0]), self.ndata) self.assertEqual(ffi.string(args[1]), b"xdata") self.assertEqual(args[2], ffi.cast("double*", self.curve.data[0].ctypes.data)) self.assertEqual(ffi.string(args[3]), b"ydata") self.assertEqual(args[4], ffi.cast("double*", self.curve.data[1].ctypes.data)) self.assertEqual(ffi.string(args[5]), b"description") self.assertEqual(ffi.string(args[6]), self.description.encode('utf-8')) self.assertEqual(ffi.string(args[7]), b"type") self.assertEqual(int(args[8]), self.curve.meta['type']) self.assertEqual(ffi.string(args[9]), b"point_type") self.assertEqual(int(args[10]), self.curve.meta['point_type']) self.assertEqual(ffi.string(args[11]), b"line_style") self.assertEqual(int(args[12]), self.curve.meta['line_style']) self.assertEqual(ffi.string(args[13]), b"point_size") self.assertEqual(int(args[14]), self.curve.meta['point_size']) self.assertEqual(ffi.string(args[15]), b"line_size") self.assertEqual(int(args[16]), self.curve.meta['line_size']) self.assertEqual(ffi.string(args[17]), b"color.red") self.assertAlmostEqual(float(args[18]), self.curve.meta['color.red']) self.assertEqual(ffi.string(args[19]), b"color.green") self.assertAlmostEqual(float(args[20]), self.curve.meta['color.green']) self.assertEqual(ffi.string(args[21]), b"color.blue") self.assertAlmostEqual(float(args[22]), self.curve.meta['color.blue']) self.assertEqual(args[-1], ffi.NULL) return self.gwycurve
def test_init_with_empty_meta(self): """Test __init__ with empty meta arg """ gwycurve = GwyGraphCurve(data=self.test_data) np.testing.assert_almost_equal(gwycurve.data, self.test_data) self.assertDictEqual( gwycurve.meta, { 'ndata': 256, 'description': '', 'type': 1, 'point_type': 2, 'line_style': 0, 'point_size': 1, 'line_size': 1, 'color.red': 0., 'color.green': 0., 'color.blue': 0. })
def test_init_with_test_data(self): """Test __init__ with data and meta args """ gwycurve = GwyGraphCurve(data=self.test_data, meta=self.test_meta) np.testing.assert_almost_equal(gwycurve.data, self.test_data) self.assertDictEqual(gwycurve.meta, self.test_meta)