-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfb674a
commit fb7a5ed
Showing
11 changed files
with
378 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Math functions | ||
============== | ||
|
||
.. currentmodule:: piccolo.query.functions.math | ||
|
||
Abs | ||
--- | ||
|
||
.. autoclass:: Abs | ||
:class-doc-from: class | ||
|
||
Ceil | ||
---- | ||
|
||
.. autoclass:: Ceil | ||
:class-doc-from: class | ||
|
||
Floor | ||
----- | ||
|
||
.. autoclass:: Floor | ||
:class-doc-from: class | ||
|
||
Round | ||
----- | ||
|
||
.. autoclass:: Round | ||
:class-doc-from: class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
These functions mirror their counterparts in the Postgresql docs: | ||
https://www.postgresql.org/docs/current/functions-math.html | ||
""" | ||
|
||
from .base import Function | ||
|
||
|
||
class Abs(Function): | ||
""" | ||
Absolute value. | ||
""" | ||
|
||
function_name = "ABS" | ||
|
||
|
||
class Ceil(Function): | ||
""" | ||
Nearest integer greater than or equal to argument. | ||
""" | ||
|
||
function_name = "CEIL" | ||
|
||
|
||
class Floor(Function): | ||
""" | ||
Nearest integer less than or equal to argument. | ||
""" | ||
|
||
function_name = "FLOOR" | ||
|
||
|
||
class Round(Function): | ||
""" | ||
Rounds to nearest integer. | ||
""" | ||
|
||
function_name = "ROUND" | ||
|
||
|
||
__all__ = ( | ||
"Abs", | ||
"Ceil", | ||
"Floor", | ||
"Round", | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import typing as t | ||
from unittest import TestCase | ||
|
||
from piccolo.table import Table, create_db_tables_sync, drop_db_tables_sync | ||
from tests.example_apps.music.tables import Band, Manager | ||
|
||
|
||
class FunctionTest(TestCase): | ||
tables: t.List[t.Type[Table]] | ||
|
||
def setUp(self) -> None: | ||
create_db_tables_sync(*self.tables) | ||
|
||
def tearDown(self) -> None: | ||
drop_db_tables_sync(*self.tables) | ||
|
||
|
||
class BandTest(FunctionTest): | ||
tables = [Band, Manager] | ||
|
||
def setUp(self) -> None: | ||
super().setUp() | ||
|
||
manager = Manager({Manager.name: "Guido"}) | ||
manager.save().run_sync() | ||
|
||
band = Band( | ||
{ | ||
Band.name: "Pythonistas", | ||
Band.manager: manager, | ||
Band.popularity: 1000, | ||
} | ||
) | ||
band.save().run_sync() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from piccolo.query.functions import Reverse, Upper | ||
from piccolo.querystring import QueryString | ||
from tests.base import engines_skip | ||
from tests.example_apps.music.tables import Band | ||
|
||
from .base import BandTest | ||
|
||
|
||
@engines_skip("sqlite") | ||
class TestNested(BandTest): | ||
""" | ||
Skip the the test for SQLite, as it doesn't support ``Reverse``. | ||
""" | ||
|
||
def test_nested(self): | ||
""" | ||
Make sure we can nest functions. | ||
""" | ||
response = Band.select(Upper(Reverse(Band.name))).run_sync() | ||
self.assertListEqual(response, [{"upper": "SATSINOHTYP"}]) | ||
|
||
def test_nested_with_joined_column(self): | ||
""" | ||
Make sure nested functions can be used on a column from a joined table. | ||
""" | ||
response = Band.select(Upper(Reverse(Band.manager._.name))).run_sync() | ||
self.assertListEqual(response, [{"upper": "ODIUG"}]) | ||
|
||
def test_nested_within_querystring(self): | ||
""" | ||
If we wrap a function in a custom QueryString - make sure the columns | ||
are still accessible, so joins are successful. | ||
""" | ||
response = Band.select( | ||
QueryString("CONCAT({}, '!')", Upper(Band.manager._.name)), | ||
).run_sync() | ||
|
||
self.assertListEqual(response, [{"concat": "GUIDO!"}]) | ||
|
||
|
||
class TestWhereClause(BandTest): | ||
|
||
def test_where(self): | ||
""" | ||
Make sure where clauses work with functions. | ||
""" | ||
response = ( | ||
Band.select(Band.name) | ||
.where(Upper(Band.name) == "PYTHONISTAS") | ||
.run_sync() | ||
) | ||
self.assertListEqual(response, [{"name": "Pythonistas"}]) | ||
|
||
def test_where_with_joined_column(self): | ||
""" | ||
Make sure where clauses work with functions, when a joined column is | ||
used. | ||
""" | ||
response = ( | ||
Band.select(Band.name) | ||
.where(Upper(Band.manager._.name) == "GUIDO") | ||
.run_sync() | ||
) | ||
self.assertListEqual(response, [{"name": "Pythonistas"}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import decimal | ||
|
||
from piccolo.columns import Numeric | ||
from piccolo.query.functions.math import Abs, Ceil, Floor, Round | ||
from piccolo.table import Table | ||
|
||
from .base import FunctionTest | ||
|
||
|
||
class Ticket(Table): | ||
price = Numeric(digits=(5, 2)) | ||
|
||
|
||
class TestMath(FunctionTest): | ||
|
||
tables = [Ticket] | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.ticket = Ticket({Ticket.price: decimal.Decimal("36.50")}) | ||
self.ticket.save().run_sync() | ||
|
||
def test_floor(self): | ||
response = Ticket.select(Floor(Ticket.price, alias="price")).run_sync() | ||
self.assertListEqual(response, [{"price": decimal.Decimal("36.00")}]) | ||
|
||
def test_ceil(self): | ||
response = Ticket.select(Ceil(Ticket.price, alias="price")).run_sync() | ||
self.assertListEqual(response, [{"price": decimal.Decimal("37.00")}]) | ||
|
||
def test_abs(self): | ||
self.ticket.price = decimal.Decimal("-1.50") | ||
self.ticket.save().run_sync() | ||
response = Ticket.select(Abs(Ticket.price, alias="price")).run_sync() | ||
self.assertListEqual(response, [{"price": decimal.Decimal("1.50")}]) | ||
|
||
def test_round(self): | ||
response = Ticket.select(Round(Ticket.price, alias="price")).run_sync() | ||
self.assertListEqual(response, [{"price": decimal.Decimal("37.00")}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from piccolo.query.functions.string import Upper | ||
from tests.example_apps.music.tables import Band | ||
|
||
from .base import BandTest | ||
|
||
|
||
class TestUpperFunction(BandTest): | ||
|
||
def test_column(self): | ||
""" | ||
Make sure we can uppercase a column's value. | ||
""" | ||
response = Band.select(Upper(Band.name)).run_sync() | ||
self.assertListEqual(response, [{"upper": "PYTHONISTAS"}]) | ||
|
||
def test_alias(self): | ||
response = Band.select(Upper(Band.name, alias="name")).run_sync() | ||
self.assertListEqual(response, [{"name": "PYTHONISTAS"}]) | ||
|
||
def test_joined_column(self): | ||
""" | ||
Make sure we can uppercase a column's value from a joined table. | ||
""" | ||
response = Band.select(Upper(Band.manager._.name)).run_sync() | ||
self.assertListEqual(response, [{"upper": "GUIDO"}]) |
Oops, something went wrong.