Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Money data type added. #324

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Empty file.
23 changes: 23 additions & 0 deletions spinta/backends/postgresql/types/money/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sqlalchemy as sa

from spinta import commands
from spinta.backends.postgresql.components import PostgreSQL
from spinta.backends.postgresql.helpers import get_column_name
from spinta.components import Context
from spinta.types.money.components import Money


@commands.prepare.register(Context, PostgreSQL, Money)
def prepare(
context: Context,
backend: PostgreSQL,
dtype: Money,
):
prop = dtype.prop
name = get_column_name(prop)

columns = [
sa.Column(f'{name}.amount', sa.Integer),
Gediminas-msc marked this conversation as resolved.
Show resolved Hide resolved
sa.Column(f'{name}.currency', sa.String),
Gediminas-msc marked this conversation as resolved.
Show resolved Hide resolved
]
return columns
1 change: 1 addition & 0 deletions spinta/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
'image': 'spinta.types.datatype:Image',
'geometry': 'spinta.types.geometry.components:Geometry',
'spatial': 'spinta.types.geometry.components:Spatial',
'money': 'spinta.types.money.components:Money',
'ref': 'spinta.types.datatype:Ref',
'backref': 'spinta.types.datatype:BackRef',
'generic': 'spinta.types.datatype:Generic',
Expand Down
4 changes: 4 additions & 0 deletions spinta/types/datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ class URI(String):
pass


class Money(DataType):
pass


class Ref(DataType):
# Referenced model
model: Model
Expand Down
Empty file added spinta/types/money/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions spinta/types/money/components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Optional

from spinta.types.datatype import DataType


class Money(DataType):
amount: int = None # Money amount
Gediminas-msc marked this conversation as resolved.
Show resolved Hide resolved
currency: Optional[str] = None # Currency (tree letter code).
43 changes: 43 additions & 0 deletions tests/dtypes/test_money.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from pytest import FixtureRequest
from spinta.core.config import RawConfig
from spinta.testing.client import create_test_client
from spinta.testing.data import listdata
from spinta.testing.manifest import bootstrap_manifest


def test_money(
rc: RawConfig,
postgresql: str,
request: FixtureRequest,
):
context = bootstrap_manifest(rc, '''
d | r | b | m | property | type | ref
backends/postgres/dtypes/money | |
| | | City | |
| | | | name | string |
| | | | amount | integer |
Gediminas-msc marked this conversation as resolved.
Show resolved Hide resolved
''', backend=postgresql, request=request)

model: str = 'backends/postgres/dtypes/money/City'

app = create_test_client(context)
app.authmodel(model, [
'insert',
'getall',
])

# Write data
resp = app.post(f'/{model}', json={
'name': "Vilnius",
'amount': 100
Gediminas-msc marked this conversation as resolved.
Show resolved Hide resolved
})
assert resp.status_code == 201

# Read data
resp = app.get(f'/{model}')
assert listdata(resp, full=True) == [
{
'name': "Vilnius",
'amount': 100
Gediminas-msc marked this conversation as resolved.
Show resolved Hide resolved
}
]
Gediminas-msc marked this conversation as resolved.
Show resolved Hide resolved