def test_read_json_data(tmp_path):
    zip_filename = Path(tmp_path, 'test_zip.zip')
    expected = {'key': 'value'}
    target_filename = 'file.txt'

    with zipfile.ZipFile(str(zip_filename), 'w') as zip_file:
        zip_file.writestr(target_filename, json.dumps(expected))

    result = zip_file_reader.read_json_data(zip_filename,
                                            Path(target_filename))

    assert result == expected
def test_read_json_data_bad_zip_file_name(tmp_path, caplog, capfd):
    zip_filename = Path(tmp_path, 'test_zip.zip')
    expected = {'key': 'value'}
    target_filename = 'file.txt'

    with zipfile.ZipFile(str(zip_filename), 'w') as zip_file:
        zip_file.writestr(target_filename, json.dumps(expected))

    with pytest.raises(SystemExit):
        _ = zip_file_reader.read_json_data('bad_file_name', target_filename)

    assert f'Error - unable to read zip file "bad_file_name"' in caplog.messages

    out, err = capfd.readouterr()
    assert 'Error - unable to read zip file ' in out
    assert err == ''
def test_read_json_data_bad_file_name(tmp_path, caplog, capfd):
    zip_filename = Path(tmp_path, 'test_zip.zip')
    expected = {'key': 'value'}
    target_filename = 'file.txt'

    with zipfile.ZipFile(str(zip_filename), 'w') as zip_file:
        zip_file.writestr(target_filename, json.dumps(expected))

    _ = zip_file_reader.read_json_data(zip_filename, Path('bad_file_name'),
                                       'note title')

    assert f'Warning - For the note "note title" - unable to find the file "bad_file_name" in the zip file "{zip_filename}"' in caplog.messages

    out, err = capfd.readouterr()
    assert 'Warning - For the note "note title" - unable to find the file "bad_file_name" in the zip file ' in out
    assert err == ''
    def fetch_notebook_json(self, notebook_id):
        if notebook_id == 'recycle-bin':
            return {'title': 'recycle-bin'}

        self.logger.info(
            f"Fetching json data file {notebook_id} from {self.nsx_file.nsx_file_name}"
        )
        note_book_json = zip_file_reader.read_json_data(
            self.nsx_file.nsx_file_name, Path(notebook_id))

        if note_book_json is None:
            self.logger.warning(
                "Unable to read notebook json data from nsx file. using 'title': 'Unknown Notebook'"
            )
            return {'title': 'Unknown Notebook'}

        return note_book_json
Example #5
0
 def fetch_json_data(self, data_id):
     self.logger.info(
         f"Fetching json data file {data_id} from {self._nsx_file_name}")
     return zip_file_reader.read_json_data(self._nsx_file_name,
                                           Path(data_id))