Skip to content

Commit 46d38da

Browse files
committed
fixing tests
1 parent b722b89 commit 46d38da

File tree

8 files changed

+36
-30
lines changed

8 files changed

+36
-30
lines changed

piccolo/columns/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ def get_full_name(self, just_alias=False) -> str:
8888
"$".join([i._name for i in self.call_chain])
8989
+ f"${column_name}"
9090
)
91-
9291
alias = f"{self.call_chain[-1].table_alias}.{self._name}"
9392
if just_alias:
9493
return alias

piccolo/columns/column_types.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ def __init__(self, **kwargs) -> None:
3636

3737

3838
class PrimaryKey(Column):
39-
# Was column_type = 'SERIAL' for Postgres
40-
column_type = 'INTEGER'
39+
40+
@property
41+
def column_type(self):
42+
engine_type = self._table.Meta.db.engine_type
43+
if engine_type == 'postgres':
44+
return 'SERIAL'
45+
elif engine_type == 'sqlite':
46+
return 'INTEGER'
47+
raise Exception('Unrecognized engine type')
4148

4249
def default(self, engine_type: str = 'postgres'):
4350
if engine_type == 'postgres':

piccolo/query/methods/select.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from __future__ import annotations
22
from collections import OrderedDict
3-
from itertools import groupby
43
import typing as t
54

65
from piccolo.query.base import Query
7-
from piccolo.columns import Column, ForeignKey
6+
from piccolo.columns import Column
87
from piccolo.query.mixins import (
98
ColumnsMixin,
109
CountMixin,
@@ -77,34 +76,32 @@ def check_valid_call_chain(self, keys: t.List[Column]) -> bool:
7776

7877
@property
7978
def querystring(self) -> QueryString:
80-
joins: t.List[str] = []
81-
82-
if len(self.selected_columns) == 0:
83-
self.selected_columns = self.table.Meta.columns
84-
85-
column_names: t.List[str] = [
86-
c.get_full_name() for c in self.selected_columns
87-
]
88-
columns_str = ", ".join(column_names)
89-
90-
#######################################################################
9179
# JOIN
92-
9380
self.check_valid_call_chain(self.selected_columns)
9481

9582
select_joins = self.get_joins(self.selected_columns)
9683
where_joins = self.get_joins(self.get_where_columns())
9784
order_by_joins = self.get_joins(self.get_order_by_columns())
9885

9986
# Combine all joins, and remove duplicates
100-
joins = list(
87+
joins: t.List[str] = list(
10188
OrderedDict.fromkeys(
10289
select_joins + where_joins + order_by_joins
10390
)
10491
)
10592

10693
#######################################################################
10794

95+
if len(self.selected_columns) == 0:
96+
self.selected_columns = self.table.Meta.columns
97+
98+
column_names: t.List[str] = [
99+
c.get_full_name() for c in self.selected_columns
100+
]
101+
columns_str = ", ".join(column_names)
102+
103+
#######################################################################
104+
108105
select = "SELECT DISTINCT" if self.distinct else "SELECT"
109106
query = f'{select} {columns_str} FROM {self.table.Meta.tablename}'
110107

piccolo/query/methods/update.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ def postgres_querystring(self) -> QueryString:
2828
self.validate()
2929

3030
columns_str = ', '.join([
31-
f'{column_name} = {{}}' for column_name, _ in self._values.items()
31+
f'{col._name} = {{}}' for col, _ in self._values.items()
3232
])
3333

3434
query = f'UPDATE {self.table.Meta.tablename} SET ' + columns_str
3535

3636
querystring = QueryString(
3737
query,
38-
self._values.values()
38+
*self._values.values()
3939
)
4040

4141
# TODO - need to support joins

piccolo/table.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ def __new__(cls, name, bases, namespace, **kwds):
4848
if hasattr(base, 'Meta'):
4949
_columns = getattr(base.Meta, 'columns', None)
5050
if _columns:
51-
columns = _columns + columns
51+
# Don't copy over the primary key.
52+
columns = [i for i in _columns if not
53+
isinstance(i, PrimaryKey)] + columns
5254

53-
if 'id' not in namespace.keys():
54-
namespace['id'] = PrimaryKey()
55+
primary_key = PrimaryKey()
56+
namespace['id'] = primary_key
57+
table.id = primary_key
5558

5659
for key, value in namespace.items():
5760
if issubclass(type(value), Column):
@@ -236,10 +239,10 @@ def save(self):
236239
if type(self.id) == int:
237240
# pre-existing row
238241
kwargs = {
239-
i._name: getattr(self, i._name, None) for i in cls.Meta.columns
242+
i: getattr(self, i._name, None) for i in cls.Meta.columns
240243
}
241244
_id = kwargs.pop('id')
242-
return cls.update(**kwargs).where(
245+
return cls.update.values(kwargs).where(
243246
cls.id == _id
244247
)
245248
else:

tests/table/test_instance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class TestInstance(DBTestCase):
99

1010
def test_insert(self):
1111
Pythonistas = Band(name="Pythonistas")
12-
self.assertEqual(Pythonistas.__str__(), "(DEFAULT,'Pythonistas',null,null)")
12+
self.assertEqual(Pythonistas.__str__(), "('Pythonistas',null,null,DEFAULT)")
1313

1414
def test_non_existant_column(self):
1515
with self.assertRaises(ValueError):

tests/table/test_objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_get_all(self):
1616
self.assertTrue(isinstance(instance, Band))
1717
self.assertTrue(instance.name == 'Pythonistas')
1818

19-
# No try changing the value and saving it.
19+
# Now try changing the value and saving it.
2020
instance.name = 'Rustaceans'
2121
save_query = instance.save()
2222
save_query.run_sync()

tests/table/test_update.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class TestUpdate(DBTestCase):
77
def test_update(self):
88
self.insert_rows()
99

10-
Band.update(
11-
name='Pythonistas3'
12-
).where(
10+
Band.update.values({
11+
Band.name: 'Pythonistas3'
12+
}).where(
1313
Band.name == 'Pythonistas'
1414
).run_sync()
1515

0 commit comments

Comments
 (0)