def test_descriptor_table_tabular_remote(patch_get): descriptor = { 'name': 'name', 'profile': 'tabular-data-resource', 'path': ['http://example.com/resource_data.csv'], 'schema': 'resource_schema.json', } # Mocks patch_get('http://example.com/resource_data.csv', body="id,name\n1,english\n2,中国人") # Tests resource = Resource(descriptor, base_path='data') assert resource.table.read(keyed=True) == [ { 'id': 1, 'name': 'english' }, { 'id': 2, 'name': '中国人' }, ]
def test_descriptor_table_tabular_inline(): descriptor = { 'name': 'name', 'profile': 'tabular-data-resource', 'data': [ ['id', 'name'], ['1', 'english'], ['2', '中国人'], ], 'schema': 'resource_schema.json', } resource = Resource(descriptor, base_path='data') assert resource.table.read(keyed=True) == [ { 'id': 1, 'name': 'english' }, { 'id': 2, 'name': '中国人' }, ]
def test_preserve_resource_format_from_descriptor_on_infer_issue_188(): resource = Resource({'path': 'data/data.csvformat', 'format': 'csv'}) assert resource.infer() == { 'encoding': 'utf-8', 'format': 'csv', 'mediatype': 'text/csv', 'name': 'data', 'path': 'data/data.csvformat', 'profile': 'tabular-data-resource', 'schema': { 'fields': [{ 'format': 'default', 'name': 'city', 'type': 'string' }, { 'format': 'default', 'name': 'population', 'type': 'integer' }], 'missingValues': [''] } }
def test_data(): resource = Resource({'path': 'data/cities.tsv'}) assert resource.data[0:3] == [ { 'Area': '1807.92', 'Name': 'Acrelândia', 'Population': '12538', 'State': 'AC' }, { 'Area': '186.53', 'Name': 'Boca da Mata', 'Population': '25776', 'State': 'AL' }, { 'Area': '242.62', 'Name': 'Capela', 'Population': '17077', 'State': 'AL' }, ]
def test_descriptor_expand_tabular_dialect(): descriptor = { 'name': 'name', 'data': 'data', 'profile': 'tabular-data-resource', 'dialect': { 'delimiter': 'custom', } } resource = Resource(descriptor) assert resource.descriptor == { 'name': 'name', 'data': 'data', 'profile': 'tabular-data-resource', 'dialect': { 'delimiter': 'custom', 'doubleQuote': True, 'lineTerminator': '\r\n', 'quoteChar': '"', 'skipInitialSpace': True, 'header': True, 'caseSensitiveHeader': False, } }
def test_source_multipart_local_infer(): descriptor = {'path': ['data/chunk1.csv', 'data/chunk2.csv']} resource = Resource(descriptor) resource.infer() assert resource.descriptor == { 'name': 'chunk1', 'profile': 'tabular-data-resource', 'path': ['data/chunk1.csv', 'data/chunk2.csv'], 'format': 'csv', 'mediatype': 'text/csv', 'encoding': 'utf-8', 'schema': { 'fields': [{ 'name': 'id', 'type': 'integer', 'format': 'default' }, { 'name': 'name', 'type': 'string', 'format': 'default' }], 'missingValues': [''] } }
def test_check_integrity(): descriptor = deepcopy(DESCRIPTOR) resource = Resource(descriptor) assert resource.check_integrity()
def test_read_integrity_hash(): descriptor = deepcopy(DESCRIPTOR) descriptor['bytes'] = None resource = Resource(descriptor) resource.read(integrity=True) assert True
def test_read_integrity(): descriptor = deepcopy(DESCRIPTOR) resource = Resource(descriptor) resource.read(integrity=True) assert True
def test_raw_read(): resource = Resource({'path': 'data/foo.txt'}) assert resource.raw_read() == b'foo\n'
def test_raw_iter(): resource = Resource({'path': 'data/foo.txt'}) with resource.raw_iter() as filelike: assert list(filelike) == [b'foo\n']
def test_descriptor_retrieve_path_bad(): descriptor = 'data/bad-path.json' with pytest.raises(exceptions.DataPackageException): Resource(descriptor).descriptor
def test_descriptor_retrieve_path(): descriptor = 'data/data-resource.json' actual = Resource(descriptor).descriptor expect = expand(json.load(io.open(descriptor, encoding='utf-8'))) assert actual == expect