Пример #1
0
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'
Пример #2
0
def test_local_with_relative_resources_paths_is_safe():
    package = Package('data/datapackage_with_foo.txt_resource.json', {})
    assert package.safe()
Пример #3
0
def test_base_path_defaults_to_none():
    assert Package().base_path is None
Пример #4
0
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, {})
Пример #5
0
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
Пример #6
0
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)
Пример #7
0
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'
Пример #8
0
def test_cant_assign_to_resources():
    descriptor = {}
    package = Package(descriptor)
    with pytest.raises(AttributeError):
        package.resources = ()
Пример #9
0
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')
Пример #10
0
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
Пример #11
0
def test_resources_are_empty_list_by_default():
    descriptor = {}
    package = Package(descriptor)
    assert package.resources == []
Пример #12
0
def test_init_raises_if_file_path_doesnt_exist():
    path = 'this-file-doesnt-exist.json'
    with pytest.raises(exceptions.DataPackageException):
        Package(path)
Пример #13
0
def test_base_path_cant_be_set_directly():
    package = Package()
    with pytest.raises(AttributeError):
        package.base_path = 'foo'
Пример #14
0
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'
Пример #15
0
def test_should_raise_if_path_doesnt_exist():
    package = Package({}, {})
    with pytest.raises(exceptions.DataPackageException):
        package.save('/non/existent/file/path')
Пример #16
0
def test_init_raises_if_path_isnt_a_json():
    with pytest.raises(exceptions.DataPackageException):
        Package('data/not_a_json')
Пример #17
0
def test_init_raises_if_filelike_object_isnt_a_json():
    invalid_json = six.StringIO('{"foo"}')
    with pytest.raises(exceptions.DataPackageException):
        Package(invalid_json)
Пример #18
0
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'
Пример #19
0
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'
Пример #20
0
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
Пример #21
0
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()
Пример #22
0
def test_saves_as_zip(tmpfile):
    package = Package(schema={})
    package.save(tmpfile)
    assert zipfile.is_zipfile(tmpfile)
Пример #23
0
def test_init_uses_base_schema_by_default():
    package = Package()
    assert package.schema.title == 'Data Package'
Пример #24
0
def test_accepts_file_paths(tmpfile):
    package = Package(schema={})
    package.save(tmpfile.name)
    assert zipfile.is_zipfile(tmpfile.name)
Пример #25
0
def test_without_resources_is_safe():
    descriptor = {}
    package = Package(descriptor, {})
    assert package.safe()
Пример #26
0
def test_init_raises_if_path_json_isnt_a_dict():
    with pytest.raises(exceptions.DataPackageException):
        Package('data/empty_array.json')
Пример #27
0
def test_zip_with_relative_resources_paths_is_safe(datapackage_zip):
    package = Package(datapackage_zip.name, {})
    assert package.safe()
Пример #28
0
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
Пример #29
0
def test_schema():
    descriptor = {}
    schema = {'foo': 'bar'}
    package = Package(descriptor, schema=schema)
    assert package.schema.to_dict() == schema
Пример #30
0
def test_init_accepts_file_paths():
    pakcage = Package('data/empty_datapackage.json')
    assert pakcage.descriptor == {
        'profile': 'data-package',
    }