def test_package_add_resource(): package = Package({}) resource = package.add_resource({'name': 'name', 'data': []}) assert len(package.resources) == 1 assert package.resources[0].name == 'name' assert resource.name == 'name'
def test_local_with_relative_resources_paths_is_safe(): package = Package('data/datapackage_with_foo.txt_resource.json', {}) assert package.safe()
def test_base_path_defaults_to_none(): assert Package().base_path is None
def test_it_breaks_if_theres_no_datapackage_json(tmpfile): with zipfile.ZipFile(tmpfile.name, 'w') as z: z.writestr('data.txt', 'foobar') with pytest.raises(exceptions.DataPackageException): Package(tmpfile.name, {})
def test_schema_gets_from_registry_if_available(registry_class_mock): schema = {'foo': 'bar'} registry_mock = mock.MagicMock() registry_mock.get.return_value = schema registry_class_mock.return_value = registry_mock assert Package().schema.to_dict() == schema
def test_should_raise_if_zipfile_raised_LargeZipFile(zipfile_mock, tmpfile): zipfile_mock.side_effect = zipfile.LargeZipFile() package = Package({}, {}) with pytest.raises(exceptions.DataPackageException): package.save(tmpfile)
def test_it_works_with_file_objects(datapackage_zip): package = Package(datapackage_zip) assert package.descriptor['name'] == 'proverbs' assert len(package.resources) == 1 assert package.resources[0].data == b'foo\n'
def test_cant_assign_to_resources(): descriptor = {} package = Package(descriptor) with pytest.raises(AttributeError): package.resources = ()
def test_local_resource_with_relative_path_is_loaded(): package = Package('data/datapackage_with_foo.txt_resource.json') assert len(package.resources) == 1 assert package.resources[0].source.endswith('foo.txt')
def test_base_path_is_datapackages_base_path_when_it_is_a_file(): path = 'data/empty_datapackage.json' base_path = os.path.dirname(path) package = Package(path) assert package.base_path == base_path
def test_resources_are_empty_list_by_default(): descriptor = {} package = Package(descriptor) assert package.resources == []
def test_init_raises_if_file_path_doesnt_exist(): path = 'this-file-doesnt-exist.json' with pytest.raises(exceptions.DataPackageException): Package(path)
def test_base_path_cant_be_set_directly(): package = Package() with pytest.raises(AttributeError): package.base_path = 'foo'
def test_package_remove_resource(): package = Package({'resources': [{'name': 'name', 'data': []}]}) resource = package.remove_resource('name') assert len(package.resources) == 0 assert resource.name == 'name'
def test_should_raise_if_path_doesnt_exist(): package = Package({}, {}) with pytest.raises(exceptions.DataPackageException): package.save('/non/existent/file/path')
def test_init_raises_if_path_isnt_a_json(): with pytest.raises(exceptions.DataPackageException): Package('data/not_a_json')
def test_init_raises_if_filelike_object_isnt_a_json(): invalid_json = six.StringIO('{"foo"}') with pytest.raises(exceptions.DataPackageException): Package(invalid_json)
def test_changing_resources_in_descriptor_changes_datapackage(): descriptor = {'resources': [{'data': '万事开头难'}]} package = Package(descriptor) package.descriptor['resources'][0]['name'] = 'saying' package.commit() assert package.descriptor['resources'][0]['name'] == 'saying'
def test_it_works_with_local_paths(datapackage_zip): package = Package(datapackage_zip.name) assert package.descriptor['name'] == 'proverbs' assert len(package.resources) == 1 assert package.resources[0].data == b'foo\n'
def test_init_raises_if_path_is_a_bad_json(): with pytest.raises(exceptions.DataPackageException) as excinfo: Package('data/bad_json.json') message = str(excinfo.value) assert 'Unable to parse JSON' in message assert 'line 2 column 5 (char 6)' in message
def test_local_data_path(datapackage_zip): package = Package(datapackage_zip) assert package.resources[0].local_data_path is not None with open('data/foo.txt') as data_file: with open(package.resources[0].local_data_path) as local_data_file: assert local_data_file.read() == data_file.read()
def test_saves_as_zip(tmpfile): package = Package(schema={}) package.save(tmpfile) assert zipfile.is_zipfile(tmpfile)
def test_init_uses_base_schema_by_default(): package = Package() assert package.schema.title == 'Data Package'
def test_accepts_file_paths(tmpfile): package = Package(schema={}) package.save(tmpfile.name) assert zipfile.is_zipfile(tmpfile.name)
def test_without_resources_is_safe(): descriptor = {} package = Package(descriptor, {}) assert package.safe()
def test_init_raises_if_path_json_isnt_a_dict(): with pytest.raises(exceptions.DataPackageException): Package('data/empty_array.json')
def test_zip_with_relative_resources_paths_is_safe(datapackage_zip): package = Package(datapackage_zip.name, {}) assert package.safe()
def test_works_with_resources_with_relative_paths(tmpfile): package = Package('data/datapackage_with_foo.txt_resource.json') package.save(tmpfile) with zipfile.ZipFile(tmpfile, 'r') as z: assert len(z.filelist) == 2
def test_schema(): descriptor = {} schema = {'foo': 'bar'} package = Package(descriptor, schema=schema) assert package.schema.to_dict() == schema
def test_init_accepts_file_paths(): pakcage = Package('data/empty_datapackage.json') assert pakcage.descriptor == { 'profile': 'data-package', }