Ejemplo n.º 1
0
    def test_load_invalidfile(self, grid):
        # Invalid filename
        file = ["invalid_file.gsp"]
        with mock.patch('builtins.open', mock.mock_open()):
            with pytest.raises(TypeError):
                GriSPy.load_grid(file=file)

            # Invalid instance of GriSPy
            file = "invalid_file.gsp"
            with mock.patch('os.path.isfile', return_value=True):
                with mock.patch('pickle.load', return_value=grid.__dict__):
                    with pytest.raises(TypeError):
                        GriSPy.load_grid(file=file)
Ejemplo n.º 2
0
    def test_load_samestate_ag(self, grid):
        file = "test_load_grid.gsp"
        with mock.patch('builtins.open', mock.mock_open()):
            with mock.patch('pickle.dump') as pd:
                # Save a first time
                grid.save_grid(file=file)
                args_pd, kwargs_pd = pd.call_args_list[0]

                # Load again to check the state is the same
                with mock.patch('os.path.isfile', return_value=True):
                    with mock.patch('pickle.load', return_value=args_pd[0]):
                        gsp_tmp = GriSPy.load_grid(file)
                        assert_(isinstance(gsp_tmp["dim"], int))
                        assert_(isinstance(gsp_tmp["data"], np.ndarray))
                        assert_(isinstance(gsp_tmp["k_bins"], np.ndarray))
                        assert_(isinstance(gsp_tmp["metric"], str))
                        assert_(isinstance(gsp_tmp["N_cells"], int))
                        assert_(isinstance(gsp_tmp["grid"], dict))
                        assert_(isinstance(gsp_tmp["periodic"], dict))
                        assert_(isinstance(gsp_tmp["periodic_flag"], bool))
                        assert_(isinstance(gsp_tmp["time"], dict))
                        assert_equal(grid["data"], gsp_tmp["data"])
                        assert_equal(grid["dim"], gsp_tmp["dim"])
                        assert_equal(grid["N_cells"], gsp_tmp["N_cells"])
                        assert_equal(grid["metric"], gsp_tmp["metric"])
                        assert_equal(grid["periodic"], gsp_tmp["periodic"])
                        assert_equal(grid["grid"], gsp_tmp["grid"])
                        assert_equal(grid["k_bins"], gsp_tmp["k_bins"])
                        assert_equal(grid["time"], gsp_tmp["time"])
Ejemplo n.º 3
0
 def test_load_nofile(self):
     with mock.patch('os.path.isfile', return_value=False):
         with pytest.raises(FileNotFoundError,
                            match=r"There is no file named.*"):
             GriSPy.load_grid("this_file_should_not_exist.gsp")