def test_convert_visible_to_bool_gwyitem(self, mock_new_gwyitem_bool, mock_add_gwyitem): """ Create bool gwyitem from visible attribute""" GwyContainer._add_graph_visibility_to_gwycontainer(self.graph, self.gwycontainer, self.key) mock_new_gwyitem_bool.assert_has_calls( [call("/0/graph/graph/1/visible", self.graph.visible)])
def test_add_gwyitems_to_gwycontainer(self, mock_new_gwyitem_bool, mock_add_gwyitem): """ Add bool gwyitem to gwycontainer""" GwyContainer._add_graph_visibility_to_gwycontainer(self.graph, self.gwycontainer, self.key) mock_add_gwyitem.assert_has_calls( [call(mock_new_gwyitem_bool.return_value, self.gwycontainer)])
def test_no_graphs_in_container(self, mock_get_graph_ids): """Return empty list if graph_ids list is empty """ mock_get_graph_ids.return_value = [] gwyfile = Mock(spec=Gwyfile) graphs = GwyContainer._dump_graphs(gwyfile) self.assertEqual(graphs, [])
def test_if_graphs_exist(self): """graphs is a list of GwyGraphModel, channels is None """ graphs = [Mock(GwyGraphModel), Mock(GwyGraphModel)] container = GwyContainer(graphs=graphs) self.assertEqual(container.graphs, graphs) self.assertEqual(container.channels, [])
def test_no_channels_in_container(self, mock_get_channel_ids): """Return empty list if channel_ids list is empty """ mock_get_channel_ids.return_value = [] gwyfile = Mock(spec=Gwyfile) channels = GwyContainer._dump_channels(gwyfile) self.assertEqual(channels, [])
def test_if_channels_exist(self): """channels is a list of GwyChannel, graphs is None """ channels = [Mock(GwyChannel), Mock(GwyChannel)] container = GwyContainer(channels=channels) self.assertEqual(container.channels, channels) self.assertEqual(container.graphs, [])
def test_return_basename_of_the_file_if_filename_is_set(self): """Return basename "/filename" field is set in GwyContainer""" gwyfile = Mock(spec=Gwyfile) pathname = "/home/user/data/sample.gwy" basename = "sample.gwy" gwyfile.get_gwyitem_string.return_value = pathname actual_return = GwyContainer._get_filename(gwyfile) self.assertEqual(actual_return, basename)
def test_return_None_if_filename_is_unset(self): """Return None if "/filename" field is unset in GwyContainer""" gwyfile = Mock(spec=Gwyfile) gwyfile.get_gwyitem_string.return_value = None actual_return = GwyContainer._get_filename(gwyfile) gwyfile.get_gwyitem_string.assert_has_calls( [call("/filename")]) self.assertIsNone(actual_return)
def test_channels_and_graphs_are_not_None(self): """channels is a list of GwyChannel, graphs is a list of GwyGraphModel """ channels = [Mock(GwyChannel), Mock(GwyChannel)] graphs = [Mock(GwyGraphModel), Mock(GwyGraphModel)] container = GwyContainer(channels=channels, graphs=graphs) self.assertEqual(container.channels, channels) self.assertEqual(container.graphs, graphs)
def test_libgwyfile_function_returns_null(self): """ Returns empty list if libgwyfile function returns NULL """ enum_graphs = self.mock_lib.gwyfile_object_container_enumerate_graphs enum_graphs.return_value = ffi.NULL ids = GwyContainer._get_graph_ids(self.gwyfile) self.assertEqual(ids, [])
def test_libgwyfile_function_returns_non_zero_channels(self): """ Returns list of graph ids if their number is not zero """ self.mock_lib.gwyfile_object_container_enumerate_graphs.side_effect = ( self._side_effect_non_zero_graphs) ids = GwyContainer._get_graph_ids(self.gwyfile) self.assertEqual(ids, [1, 2])
def test_getting_gwygraphmodel_objects(self, mock_get_graph_ids, mock_GwyGraphModel): """Get <GwyGraphModel*> objects and their visibility flags from Gwyfile object """ graph_ids = [1, 2, 3] graph_keys = ["/0/graph/graph/{:d}".format(graph_id) for graph_id in graph_ids] graph_vis_keys = ["/0/graph/graph/{:d}/visible".format(graph_id) for graph_id in graph_ids] mock_get_graph_ids.return_value = graph_ids gwyfile = Mock(spec=Gwyfile) GwyContainer._dump_graphs(gwyfile) gwyfile.get_gwyitem_object.assert_has_calls( [call(graph_key) for graph_key in graph_keys]) gwyfile.get_gwyitem_bool.assert_has_calls( [call(graph_vis_key) for graph_vis_key in graph_vis_keys])
def test_libgwyfile_function_returns_non_zero_channels(self): """ Returns list of channel ids if their number is not zero """ enum_chs = self.mock_lib.gwyfile_object_container_enumerate_channels enum_chs.side_effect = self._side_effect_non_zero_channels ids = GwyContainer._get_channel_ids(self.gwyfile) self.assertEqual(ids, [0, 1, 2])
def test_returned_value(self, mock_get_graph_ids, mock_GwyGraphModel): """Convert <GwyGraphModel*> object to GwyGraphModel objects and return the latter """ graph_ids = [1, 2, 3] mock_get_graph_ids.return_value = graph_ids gwygraphmodels = [Mock() for graph_id in graph_ids] gwyfile = Mock(spec=Gwyfile) graphs = GwyContainer._dump_graphs(gwyfile) self.assertListEqual(graphs, [mock_GwyGraphModel(gwygraphmodel) for gwygraphmodel in gwygraphmodels])
def test_convert_channel_ids_to_GwyChannel_list(self, mock_get_channel_ids, mock_GwyChannel): """Convert list of channel_ids to list of GwyChannel objects and return the latter """ channel_ids = [0, 1, 2] mock_get_channel_ids.return_value = channel_ids gwyfile = Mock(spec=Gwyfile) channels = GwyContainer._dump_channels(gwyfile) self.assertListEqual(channels, [mock_GwyChannel.from_gwy(gwyfile, channel_id) for channel_id in channel_ids])
def test_from_gwy_method_of_GwyContainer(self, mock_dump_channels, mock_dump_graphs, mock_get_filename, mock_GwyContainer): gwyfile = Mock(spec=Gwyfile) channels = [Mock(spec=GwyChannel), Mock(spec=GwyChannel)] graphs = [Mock(spec=GwyGraphModel)] filename = 'sample.gwy' mock_get_filename.return_value = filename mock_dump_channels.return_value = channels mock_dump_graphs.return_value = graphs mock_GwyContainer.return_value = Mock(spec=GwyContainer) container = GwyContainer.from_gwy(gwyfile) mock_get_filename.assert_has_calls( [call(gwyfile)]) mock_dump_channels.assert_has_calls( [call(gwyfile)]) mock_dump_graphs.assert_has_calls( [call(gwyfile)]) mock_GwyContainer.assert_has_calls( [call(filename=filename, channels=channels, graphs=graphs)]) self.assertEqual(container, mock_GwyContainer.return_value)
def test_channels_and_graphs_are_None(self): """both channels and graphs are None """ container = GwyContainer() self.assertEqual(container.channels, []) self.assertEqual(container.graphs, [])