예제 #1
0
def file_open(mocker: pytest_mock.MockerFixture, should_pass: bool):
    """ open """
    open_name = 'builtins.open'
    mock_file_open = mocker.patch(open_name,
                                  mocker.mock_open(read_data=b'\x00'))
    if not should_pass:
        mock_file_open.side_effect = FileNotFoundError
    return mock_file_open
예제 #2
0
 def test_write_existing_file(self, mocker: MockerFixture, fake_files):
     mock_open = mocker.mock_open()
     mocker.patch("builtins.open", mock_open)
     mocker.patch.object(Path, "exists", return_value=True)
     open_next("/path/to/file", mode="w")
     mock_open.assert_called_once_with(Path("/path/to/file-5"),
                                       mode="w",
                                       encoding="utf-8")
예제 #3
0
 def test_write_existing_file(self, mocker: MockerFixture, fake_files):
     mock_open = mocker.mock_open()
     mocker.patch("builtins.open", mock_open)
     mocker.patch.object(Path, "exists", side_effect=[True])
     open_latest("/path/to/file", mode="w")
     mock_open.assert_called_once_with(Path("/path/to/file-4"),
                                       mode="w",
                                       encoding="utf-8")
예제 #4
0
 def test_should_close_all_files_automatically(self, mocker: MockerFixture,
                                               fake_files):
     mocker.patch("builtins.open", mocker.mock_open(read_data="fake data"))
     files = []
     for file in open_all("/path/to/foo.txt", mode="r"):
         assert file.read() == "fake data"
         files.append(file)
     for file in files:
         file.close.assert_called()
예제 #5
0
def test_defaults(mocker: MockerFixture, tmp_path, basic_regular_grid):
    # Arrange
    file_name = f'{tmp_path}/test'
    open_mock = mocker.mock_open()
    mocker.patch("builtins.open", open_mock)
    expected_calls = get_expected_calls(mocker, file_name)

    # Act
    write_nexus_corp(basic_regular_grid, file_name)

    # Assert
    open_mock.assert_has_calls(expected_calls)
예제 #6
0
def test_local_coords_true(mocker: MockerFixture, tmp_path, basic_regular_grid):
    # Arrange
    file_name = f'{tmp_path}/test'
    open_mock = mocker.mock_open()
    mocker.patch("builtins.open", open_mock)
    expected_calls = get_expected_calls(mocker, file_name)
    expected_calls.insert(4, mocker.call().write('METRIC\n\n'))

    # Act
    write_nexus_corp(basic_regular_grid, file_name, write_units_keyword = True, local_coords = True)

    # Assert
    open_mock.assert_has_calls(expected_calls)
예제 #7
0
def test_write_array_to_ascii_file_append(mocker: MockerFixture):
    # Arrange
    test_extent = np.array([2, 2, 2])
    test_array = np.array([[[0, 0], [0, 0]], [[0, 0], [0, 0]]])
    open_mock = mocker.mock_open()
    mocker.patch("builtins.open", open_mock)
    expected_calls = [mocker.call('test', 'a')]

    # Act
    wd.write_array_to_ascii_file('test', test_extent, test_array, append = True)

    # Assert
    open_mock.assert_has_calls(expected_calls)
예제 #8
0
def test_write_rh_keyword_if_needed_true(mocker: MockerFixture, tmp_path, basic_regular_grid):
    # Arrange
    file_name = f'{tmp_path}/test'
    open_mock = mocker.mock_open()
    mocker.patch("builtins.open", open_mock)
    expected_calls = get_expected_calls(mocker, file_name)
    expected_calls.insert(4, mocker.call().write('RIGHTHANDED\n\n'))

    # Act
    write_nexus_corp(basic_regular_grid, file_name, write_rh_keyword_if_needed = True)

    # Assert
    open_mock.assert_has_calls(expected_calls)
예제 #9
0
def test_write_nx_ny_nz_true(mocker: MockerFixture, tmp_path, basic_regular_grid):
    # Arrange
    file_name = f'{tmp_path}/test'
    open_mock = mocker.mock_open()
    mocker.patch("builtins.open", open_mock)
    expected_calls = get_expected_calls(mocker, file_name)
    expected_calls[4:4] = [
        mocker.call().write('NX      NY      NZ\n'),
        mocker.call().write('2       2       2      \n\n')
    ]

    # Act
    write_nexus_corp(basic_regular_grid, file_name, write_nx_ny_nz = True)

    # Assert
    open_mock.assert_has_calls(expected_calls)
예제 #10
0
def test_init(mocker: MockerFixture):
    server = Server()
    open_mock = mocker.patch('builtins.open',
                             mocker.mock_open(read_data='010203'))
    mocker.patch("engine.server.FRAME_PER_SECONDS", 10)
    map2d = Map2d([Wall(1, 2, 10)])
    map2d_create_with_pattern_mock = mocker.patch.object(
        Map2d, "create_with_pattern")
    map2d_create_with_pattern_mock.return_value = map2d
    map2d_update = mocker.patch.object(map2d, "update")
    map2d_update.side_effect = lambda: server.stop_game()
    server.load_map_file("teste")
    server.start_game()

    open_mock.assert_called_once_with("teste", "r")
    map2d_update.assert_called_once()
예제 #11
0
    def test_should_open_ascending_siblings(self, mocker: MockerFixture,
                                            fake_files):

        mock_open = mocker.mock_open()
        mocker.patch("builtins.open", mock_open)

        expected_calls = [
            call(Path("/path/to/foo.txt"), mode="r", encoding="utf-8"),
            call(Path("/path/to/foo-1.txt"), mode="r", encoding="utf-8"),
            call(Path("/path/to/foo-2.txt"), mode="r", encoding="utf-8"),
            call(Path("/path/to/foo-3.txt"), mode="r", encoding="utf-8"),
            call(Path("/path/to/foo-4.txt"), mode="r", encoding="utf-8"),
        ]

        for i, file in enumerate(open_all("/path/to/foo.txt", mode="r")):
            assert expected_calls[i] == mock_open.call_args
예제 #12
0
def test_write_pure_binary_data(mocker: MockerFixture, caplog):
    # Arrange
    test_array = np.array([[[0, 0], [0, 0]], [[0, 0], [0, 0]]])
    open_mock = mocker.mock_open()
    fileno_mock = mocker.Mock(return_value = 1, name = 'fileno_mock')
    open_mock.return_value.fileno = fileno_mock

    mocker.patch("builtins.open", open_mock)
    expected_calls = [mocker.call('test', 'wb'), mocker.call().__enter__()]

    # Act
    wd.write_pure_binary_data('test', test_array)

    # Assert
    open_mock.assert_has_calls(expected_calls)
    assert 'Binary data file test created' in caplog.text
예제 #13
0
def test_write_array_to_ascii_file_no_headers(mocker: MockerFixture):
    # Arrange
    test_extent = np.array([2, 2, 2])
    test_array = np.array([[[0, 0], [0, 0]], [[0, 0], [0, 0]]])
    open_mock = mocker.mock_open()
    mocker.patch("builtins.open", open_mock)
    unexpected_calls = [
        mocker.call().write('! Data written by write_array_to_ascii_file() python function\n'),
        mocker.call().write('! Extent of array is: [2, 2, 2]\n'),
        mocker.call().write('! Maximum 20 data items per line\n')
    ]

    # Act
    wd.write_array_to_ascii_file('test', test_extent, test_array, headers = False)

    # Assert
    assert unexpected_calls not in open_mock.mock_calls
예제 #14
0
def test_write_array_to_ascii_file_binary(mocker: MockerFixture, caplog):
    # Arrange
    test_extent = np.array([2, 2, 2])
    test_array = np.array([[[0, 0], [0, 0]], [[0, 0], [0, 0]]])
    open_mock = mocker.mock_open()
    fileno_mock = mocker.Mock(return_value = 1, name = 'fileno_mock')
    open_mock.return_value.fileno = fileno_mock

    mocker.patch("builtins.open", open_mock)
    expected_calls = [mocker.call('test.db', 'wb'), mocker.call().__enter__()]

    # Act
    wd.write_array_to_ascii_file('test', test_extent, test_array, use_binary = True, binary_only = True)

    # Assert
    open_mock.assert_has_calls(expected_calls)
    assert 'Failed to write data to binary file test' not in caplog.text
예제 #15
0
def test_log_config_json(
    mocked_logging_config_module: MagicMock,
    logging_config: dict,
    json_logging_config: str,
    mocker: MockerFixture,
) -> None:
    """
    Test that one can load a json config from disk.
    """
    mocked_open = mocker.patch("uvicorn.config.open",
                               mocker.mock_open(read_data=json_logging_config))

    config = Config(app=asgi_app, log_config="log_config.json")
    config.load()

    mocked_open.assert_called_once_with("log_config.json")
    mocked_logging_config_module.dictConfig.assert_called_once_with(
        logging_config)
예제 #16
0
def test_log_config_yaml(
    mocked_logging_config_module: MagicMock,
    logging_config: dict,
    yaml_logging_config: str,
    mocker: MockerFixture,
    config_filename: str,
) -> None:
    """
    Test that one can load a yaml config from disk.
    """
    mocked_open = mocker.patch("uvicorn.config.open",
                               mocker.mock_open(read_data=yaml_logging_config))

    config = Config(app=asgi_app, log_config=config_filename)
    config.load()

    mocked_open.assert_called_once_with(config_filename)
    mocked_logging_config_module.dictConfig.assert_called_once_with(
        logging_config)
예제 #17
0
def test_write_array_to_ascii_file_blank_line_after_j_block(mocker: MockerFixture):
    # Arrange
    test_extent = np.array([2, 2, 2])
    test_array = np.array([[[0, 0], [0, 0]], [[0, 0], [0, 0]]])
    open_mock = mocker.mock_open()
    mocker.patch("builtins.open", open_mock)
    expected_calls = [
        mocker.call('test', 'w'),
        mocker.call().__enter__(),
        mocker.call().write('! Data written by write_array_to_ascii_file() python function\n'),
        mocker.call().write('! Extent of array is: [2, 2, 2]\n'),
        mocker.call().write('! Maximum 20 data items per line\n'),
        mocker.call().write('\n'),
        mocker.call().write('0.000'),
        mocker.call().write('\t'),
        mocker.call().write('0.000'),
        mocker.call().write('\n'),
        mocker.call().write('\n'),
        mocker.call().write('0.000'),
        mocker.call().write('\t'),
        mocker.call().write('0.000'),
        mocker.call().write('\n'),
        mocker.call().write('\n'),
        mocker.call().write('\n'),
        mocker.call().write('0.000'),
        mocker.call().write('\t'),
        mocker.call().write('0.000'),
        mocker.call().write('\n'),
        mocker.call().write('\n'),
        mocker.call().write('0.000'),
        mocker.call().write('\t'),
        mocker.call().write('0.000'),
        mocker.call().write('\n'),
        mocker.call().write('\n'),
        mocker.call().__exit__(None, None, None)
    ]

    # Act
    wd.write_array_to_ascii_file('test', test_extent, test_array, blank_line_after_j_block = True)

    # Assert
    open_mock.assert_has_calls(expected_calls)
예제 #18
0
def test_write_array_to_ascii_file_nan_substitute(mocker: MockerFixture):
    # Arrange
    test_extent = np.array([2, 2, 2])
    test_array = np.array([[[1, np.nan], [3, 2]], [[1, 5], [np.nan, np.nan]]])
    open_mock = mocker.mock_open()
    mocker.patch("builtins.open", open_mock)
    expected_calls = [
        mocker.call('test', 'w'),
        mocker.call().__enter__(),
        mocker.call().write('! Data written by write_array_to_ascii_file() python function\n'),
        mocker.call().write('! Extent of array is: [2, 2, 2]\n'),
        mocker.call().write('! Maximum 20 data items per line\n'),
        mocker.call().write('\n'),
        mocker.call().write('1.000'),
        mocker.call().write('\t'),
        mocker.call().write('0.000'),
        mocker.call().write('\n'),
        mocker.call().write('\n'),
        mocker.call().write('3.000'),
        mocker.call().write('\t'),
        mocker.call().write('2.000'),
        mocker.call().write('\n'),
        mocker.call().write('\n'),
        mocker.call().write('1.000'),
        mocker.call().write('\t'),
        mocker.call().write('5.000'),
        mocker.call().write('\n'),
        mocker.call().write('\n'),
        mocker.call().write('0.000'),
        mocker.call().write('\t'),
        mocker.call().write('0.000'),
        mocker.call().write('\n'),
        mocker.call().__exit__(None, None, None)
    ]

    # Act
    wd.write_array_to_ascii_file('test', test_extent, test_array, nan_substitute_value = 0)

    # Assert
    open_mock.assert_has_calls(expected_calls)
예제 #19
0
 def test_write_no_such_file(self, mocker: MockerFixture, fake_files):
     mocker.patch("builtins.open", mocker.mock_open())
     mocker.patch.object(Path, "exists", side_effect=[False])
     open_latest("/path/to/no-such-file", mode="w")
예제 #20
0
 def test_should_open_in_write_mode_by_default(self, mocker: MockerFixture,
                                               fake_files):
     mock_open = mocker.mock_open()
     mocker.patch("builtins.open", mock_open)
     for _ in enumerate(open_next("/path/to/foo.txt")):
         assert mock_open.call_args.kwargs["mode"] == "w"
예제 #21
0
def test_mock_file_open(mocker: MockerFixture):
    m = mocker.patch('builtins.open', mocker.mock_open(read_data='bible'))
    with open('foo') as h:
        result = h.read()
    m.assert_called_once_with('foo')
    assert result == 'bible'
예제 #22
0
def test__read_file(mocker: pytest_mock.MockerFixture):
    mocker.patch("builtins.open", mocker.mock_open(read_data="test input"))
    content = _read_file("testfile.txt")
    assert "test input" == content