Skip to content

Commit

Permalink
tests modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
karina-klinkeviciute committed May 3, 2024
1 parent 8efd84b commit 80100db
Showing 1 changed file with 134 additions and 131 deletions.
265 changes: 134 additions & 131 deletions tests/datasets/dataframe/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fsspec.implementations.memory import MemoryFileSystem

from spinta import commands
from spinta.commands import getall
from spinta.commands import getall, get_model
from spinta.components import Mode, Model, Property, PropertyGiven, UrlParams, Version
from spinta.core.config import RawConfig
from spinta.datasets.backends.dataframe.components import Xml
Expand All @@ -25,127 +25,59 @@ def fs():
MemoryFileSystem.store.clear()


def test_csv_tabular_seperator_default_error(rc: RawConfig, fs: AbstractFileSystem):
fs.pipe('cities.csv', (
'šalis;miestas\n'
'lt;Vilnius\n'
'lv;Ryga\n'
'ee;Talin'
).encode('utf-8'))

context, manifest = prepare_manifest(rc, '''
d | r | b | m | property | type | ref | source | prepare | access
example | | | | |
| csv | csv | | memory://cities.csv | |
| | | City | | name | | |
| | | | name | string | | miestas | | open
| | | | country | string | | šalis | | open
''', mode=Mode.external)
context.loaded = True
app = create_test_client(context)
app.authmodel('example/City', ['getall'])

resp = app.get('/example/City')
assert resp.status_code == 404


def test_csv_tabular_seperator(rc: RawConfig, fs: AbstractFileSystem):
fs.pipe('cities.csv', (
'šalis;miestas\n'
'lt;Vilnius\n'
'lv;Ryga\n'
'ee;Talin'
).encode('utf-8'))

context, manifest = prepare_manifest(rc, '''
d | r | b | m | property | type | ref | source | prepare | access
example | | | | |
| csv | csv | | memory://cities.csv | tabular(sep:";") |
| | | City | | name | | |
| | | | name | string | | miestas | | open
| | | | country | string | | šalis | | open
''', mode=Mode.external)
context.loaded = True
app = create_test_client(context)
app.authmodel('example/City', ['getall'])

resp = app.get('/example/City')
assert listdata(resp, sort=False) == [
('lt', 'Vilnius'),
('lv', 'Ryga'),
('ee', 'Talin'),
]


def test_xsd_base64(rc: RawConfig, tmp_path: Path):
xml = """<CITY>
<NAME>Kaunas</NAME>
<COUNTRY>Lietuva</COUNTRY>
<FILE>VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIDEzIGxhenkgZG9ncy4=</FILE>
</CITY>"""

path = tmp_path / 'cities.xml'
path.write_text(xml)

context, manifest = prepare_manifest(rc, f'''
d | r | b | m | property | type | ref | source | prepare | access
example | | | | |
| res1 | xml | | {tmp_path}/cities.xml | |
| | | City | | | /CITY | |
| | | | name | string | | NAME | | open
| | | | country | string | | COUNTRY | | open
| | | | file | binary | | FILE | base64() | open
''', mode=Mode.external)
#
# /example/City?select(name, country, base64(file))
context.loaded = True
app = create_test_client(context)
app.authmodel('example/City', ['getall'])

resp = app.get('/example/City')
assert listdata(resp, sort=False) == [
('Lietuva', "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIDEzIGxhenkgZG9ncy4=", 'Kaunas', )
]


# def test_base64_getall(rc: RawConfig, tmp_path: Path):
# backend = Xml()
# backend.config = {'dsn': '/tmp/pytest-of-karina/pytest-2/test_xsd_base640/cities.xml', 'name': 'example/res1', 'type': 'xml'}
# backend.name = "example/res1"
# # model = Model()
# # model.backend = backend
# def test_csv_tabular_seperator_default_error(rc: RawConfig, fs: AbstractFileSystem):
# fs.pipe('cities.csv', (
# 'šalis;miestas\n'
# 'lt;Vilnius\n'
# 'lv;Ryga\n'
# 'ee;Talin'
# ).encode('utf-8'))
#
# context, manifest = prepare_manifest(rc, '''
# d | r | b | m | property | type | ref | source | prepare | access
# example | | | | |
# | csv | csv | | memory://cities.csv | |
# | | | City | | name | | |
# | | | | name | string | | miestas | | open
# | | | | country | string | | šalis | | open
# ''', mode=Mode.external)
# context.loaded = True
# app = create_test_client(context)
# app.authmodel('example/City', ['getall'])
#
# # external = Attribute()
# # external.name = "FILE"
# # given = PropertyGiven()
# # given.prepare = {'appended': False, 'prepare': 'base64()', 'source': ''}
# #
# # file_property = Property()
# # file_property.place = 'file'
# # file_property.dtype = Binary()
# # file_property.external = external
# # file_property.given = given
# # file_property.prepare_given = [{'appended': False, 'prepare': 'base64()', 'source': ''}]
# #
# # properties = {
# # 'file': file_property
# # }
# # external = Entity()
# # resource = Resource()
# # resource.name = 'res1'
# # resource.external = '/tmp/pytest-of-karina/pytest-14/test_xsd_base640/cities.xml'
# # external.resource = resource
# # dataset = Dataset()
# # dataset.name = "example"
# # external.dataset = Dataset()
# # keymap = SqlAlchemyKeyMap()
# # keymap.name = 'default'
# # model.flatprops = properties
# # model.leafprops = properties
# # model.external = external
# # model.keymap = keymap
# resp = app.get('/example/City')
# assert resp.status_code == 404
#
#
# def test_csv_tabular_seperator(rc: RawConfig, fs: AbstractFileSystem):
# fs.pipe('cities.csv', (
# 'šalis;miestas\n'
# 'lt;Vilnius\n'
# 'lv;Ryga\n'
# 'ee;Talin'
# ).encode('utf-8'))
#
# context, manifest = prepare_manifest(rc, '''
# d | r | b | m | property | type | ref | source | prepare | access
# example | | | | |
# | csv | csv | | memory://cities.csv | tabular(sep:";") |
# | | | City | | name | | |
# | | | | name | string | | miestas | | open
# | | | | country | string | | šalis | | open
# ''', mode=Mode.external)
# context.loaded = True
# app = create_test_client(context)
# app.authmodel('example/City', ['getall'])
#
# resp = app.get('/example/City')
# assert listdata(resp, sort=False) == [
# ('lt', 'Vilnius'),
# ('lv', 'Ryga'),
# ('ee', 'Talin'),
# ]
#
#
# def test_xsd_base64(rc: RawConfig, tmp_path: Path):
# xml = """<CITY>
# <NAME>Kaunas</NAME>
# <COUNTRY>Lietuva</COUNTRY>
Expand All @@ -154,6 +86,7 @@ def test_xsd_base64(rc: RawConfig, tmp_path: Path):
#
# path = tmp_path / 'cities.xml'
# path.write_text(xml)
#
# context, manifest = prepare_manifest(rc, f'''
# d | r | b | m | property | type | ref | source | prepare | access
# example | | | | |
Expand All @@ -165,17 +98,87 @@ def test_xsd_base64(rc: RawConfig, tmp_path: Path):
# ''', mode=Mode.external)
# #
# # /example/City?select(name, country, base64(file))
#
# request = make_get_request('/example/City', 'select(name, country, base64(file))')
# params = commands.prepare(
# context,
# UrlParams(),
# Version(),
# request,
# )
# model = params.model
#
# context.loaded = True
# result = list(getall(context, model, backend))
# app = create_test_client(context)
# app.authmodel('example/City', ['getall'])
#
# assert result == []
# resp = app.get('/example/City')
# assert listdata(resp, sort=False) == [
# ('Lietuva', "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIDEzIGxhenkgZG9ncy4=", 'Kaunas', )
# ]


def test_base64_getall(rc: RawConfig, tmp_path: Path):
# backend = Xml()
# backend.config = {'dsn': '/tmp/pytest-of-karina/pytest-2/test_xsd_base640/cities.xml', 'name': 'example/res1', 'type': 'xml'}
# backend.name = "example/res1"
# model = Model()
# model.backend = backend


# external = Attribute()
# external.name = "FILE"
# given = PropertyGiven()
# given.prepare = {'appended': False, 'prepare': 'base64()', 'source': ''}
#
# file_property = Property()
# file_property.place = 'file'
# file_property.dtype = Binary()
# file_property.external = external
# file_property.given = given
# file_property.prepare_given = [{'appended': False, 'prepare': 'base64()', 'source': ''}]
#
# properties = {
# 'file': file_property
# }
# external = Entity()
# resource = Resource()
# resource.name = 'res1'
# resource.external = '/tmp/pytest-of-karina/pytest-14/test_xsd_base640/cities.xml'
# external.resource = resource
# dataset = Dataset()
# dataset.name = "example"
# external.dataset = Dataset()
# keymap = SqlAlchemyKeyMap()
# keymap.name = 'default'
# model.flatprops = properties
# model.leafprops = properties
# model.external = external
# model.keymap = keymap

xml = """<CITY>
<NAME>Kaunas</NAME>
<COUNTRY>Lietuva</COUNTRY>
<FILE>VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIDEzIGxhenkgZG9ncy4=</FILE>
</CITY>"""

path = tmp_path / 'cities.xml'
path.write_text(xml)
context, manifest = prepare_manifest(rc, f'''
d | r | b | m | property | type | ref | source | prepare | access
example | | | | |
| res1 | xml | | {tmp_path}/cities.xml | |
| | | City | | | /CITY | |
| | | | name | string | | NAME | | open
| | | | country | string | | COUNTRY | | open
| | | | file | binary | | FILE | base64() | open
''', mode=Mode.external)
#
# /example/City?select(name, country, base64(file))

# request = make_get_request('/example/City', 'select(name, country, base64(file))')
# params = commands.prepare(
# context,
# UrlParams(),
# Version(),
# request,
# )
# model = params.model

model = get_model(context, manifest, 'example/City')

# context.loaded = True
backend = manifest.backend
result = list(getall(context, model, backend))

assert result == []

0 comments on commit 80100db

Please sign in to comment.