piccolo 1.5.1__py3-none-any.whl → 1.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. piccolo/__init__.py +1 -1
  2. piccolo/apps/playground/commands/run.py +70 -18
  3. piccolo/columns/base.py +31 -40
  4. piccolo/columns/column_types.py +11 -8
  5. piccolo/columns/m2m.py +16 -6
  6. piccolo/columns/readable.py +9 -7
  7. piccolo/query/__init__.py +1 -4
  8. piccolo/query/base.py +1 -5
  9. piccolo/query/functions/__init__.py +16 -0
  10. piccolo/query/functions/aggregate.py +179 -0
  11. piccolo/query/functions/base.py +21 -0
  12. piccolo/query/functions/string.py +73 -0
  13. piccolo/query/methods/__init__.py +18 -1
  14. piccolo/query/methods/count.py +3 -3
  15. piccolo/query/methods/delete.py +1 -1
  16. piccolo/query/methods/exists.py +1 -1
  17. piccolo/query/methods/objects.py +1 -1
  18. piccolo/query/methods/select.py +17 -232
  19. piccolo/query/methods/update.py +1 -1
  20. piccolo/query/mixins.py +9 -2
  21. piccolo/querystring.py +101 -13
  22. piccolo/table.py +8 -24
  23. {piccolo-1.5.1.dist-info → piccolo-1.6.0.dist-info}/METADATA +1 -1
  24. {piccolo-1.5.1.dist-info → piccolo-1.6.0.dist-info}/RECORD +35 -30
  25. tests/apps/migrations/commands/test_forwards_backwards.py +3 -0
  26. tests/apps/shell/commands/test_run.py +1 -0
  27. tests/conf/test_apps.py +6 -0
  28. tests/example_apps/music/tables.py +10 -0
  29. tests/query/test_functions.py +102 -0
  30. tests/table/test_output.py +88 -36
  31. tests/table/test_select.py +2 -9
  32. {piccolo-1.5.1.dist-info → piccolo-1.6.0.dist-info}/LICENSE +0 -0
  33. {piccolo-1.5.1.dist-info → piccolo-1.6.0.dist-info}/WHEEL +0 -0
  34. {piccolo-1.5.1.dist-info → piccolo-1.6.0.dist-info}/entry_points.txt +0 -0
  35. {piccolo-1.5.1.dist-info → piccolo-1.6.0.dist-info}/top_level.txt +0 -0
@@ -1,8 +1,9 @@
1
1
  import json
2
2
  from unittest import TestCase
3
3
 
4
- from tests.base import DBTestCase, engine_is
5
- from tests.example_apps.music.tables import Band, RecordingStudio
4
+ from piccolo.table import create_db_tables_sync, drop_db_tables_sync
5
+ from tests.base import DBTestCase
6
+ from tests.example_apps.music.tables import Band, Instrument, RecordingStudio
6
7
 
7
8
 
8
9
  class TestOutputList(DBTestCase):
@@ -32,51 +33,102 @@ class TestOutputJSON(DBTestCase):
32
33
 
33
34
 
34
35
  class TestOutputLoadJSON(TestCase):
36
+ tables = [RecordingStudio, Instrument]
37
+ json = {"a": 123}
38
+
35
39
  def setUp(self):
36
- RecordingStudio.create_table().run_sync()
40
+ create_db_tables_sync(*self.tables)
41
+
42
+ recording_studio = RecordingStudio(
43
+ {
44
+ RecordingStudio.facilities: self.json,
45
+ RecordingStudio.facilities_b: self.json,
46
+ }
47
+ )
48
+ recording_studio.save().run_sync()
49
+
50
+ instrument = Instrument(
51
+ {
52
+ Instrument.recording_studio: recording_studio,
53
+ Instrument.name: "Piccolo",
54
+ }
55
+ )
56
+ instrument.save().run_sync()
37
57
 
38
58
  def tearDown(self):
39
- RecordingStudio.alter().drop_table().run_sync()
59
+ drop_db_tables_sync(*self.tables)
40
60
 
41
61
  def test_select(self):
42
- json = {"a": 123}
43
-
44
- RecordingStudio(facilities=json, facilities_b=json).save().run_sync()
45
-
46
- results = RecordingStudio.select().output(load_json=True).run_sync()
47
-
48
- if engine_is("cockroach"):
49
- self.assertEqual(
50
- results,
51
- [
52
- {
53
- "id": results[0]["id"],
54
- "facilities": {"a": 123},
55
- "facilities_b": {"a": 123},
56
- }
57
- ],
62
+ results = (
63
+ RecordingStudio.select(
64
+ RecordingStudio.facilities, RecordingStudio.facilities_b
58
65
  )
59
- else:
60
- self.assertEqual(
61
- results,
62
- [
63
- {
64
- "id": 1,
65
- "facilities": {"a": 123},
66
- "facilities_b": {"a": 123},
67
- }
68
- ],
66
+ .output(load_json=True)
67
+ .run_sync()
68
+ )
69
+
70
+ self.assertEqual(
71
+ results,
72
+ [
73
+ {
74
+ "facilities": self.json,
75
+ "facilities_b": self.json,
76
+ }
77
+ ],
78
+ )
79
+
80
+ def test_join(self):
81
+ """
82
+ Make sure it works correctly when the JSON column is on a joined table.
83
+
84
+ https://github.com/piccolo-orm/piccolo/issues/1001
85
+
86
+ """
87
+ results = (
88
+ Instrument.select(
89
+ Instrument.name,
90
+ Instrument.recording_studio._.facilities,
69
91
  )
92
+ .output(load_json=True)
93
+ .run_sync()
94
+ )
70
95
 
71
- def test_objects(self):
72
- json = {"a": 123}
96
+ self.assertEqual(
97
+ results,
98
+ [
99
+ {
100
+ "name": "Piccolo",
101
+ "recording_studio.facilities": self.json,
102
+ }
103
+ ],
104
+ )
73
105
 
74
- RecordingStudio(facilities=json, facilities_b=json).save().run_sync()
106
+ def test_join_with_alias(self):
107
+ results = (
108
+ Instrument.select(
109
+ Instrument.name,
110
+ Instrument.recording_studio._.facilities.as_alias(
111
+ "facilities"
112
+ ),
113
+ )
114
+ .output(load_json=True)
115
+ .run_sync()
116
+ )
75
117
 
76
- results = RecordingStudio.objects().output(load_json=True).run_sync()
118
+ self.assertEqual(
119
+ results,
120
+ [
121
+ {
122
+ "name": "Piccolo",
123
+ "facilities": self.json,
124
+ }
125
+ ],
126
+ )
77
127
 
78
- self.assertEqual(results[0].facilities, json)
79
- self.assertEqual(results[0].facilities_b, json)
128
+ def test_objects(self):
129
+ results = RecordingStudio.objects().output(load_json=True).run_sync()
130
+ self.assertEqual(results[0].facilities, self.json)
131
+ self.assertEqual(results[0].facilities_b, self.json)
80
132
 
81
133
 
82
134
  class TestOutputNested(DBTestCase):
@@ -7,7 +7,8 @@ from piccolo.apps.user.tables import BaseUser
7
7
  from piccolo.columns import Date, Varchar
8
8
  from piccolo.columns.combination import WhereRaw
9
9
  from piccolo.query import OrderByRaw
10
- from piccolo.query.methods.select import Avg, Count, Max, Min, SelectRaw, Sum
10
+ from piccolo.query.functions.aggregate import Avg, Count, Max, Min, Sum
11
+ from piccolo.query.methods.select import SelectRaw
11
12
  from piccolo.query.mixins import DistinctOnError
12
13
  from piccolo.table import Table, create_db_tables_sync, drop_db_tables_sync
13
14
  from tests.base import (
@@ -927,14 +928,6 @@ class TestSelect(DBTestCase):
927
928
  self.assertEqual(float(response["popularity_avg"]), 1003.3333333333334)
928
929
  self.assertEqual(response["popularity_sum"], 3010)
929
930
 
930
- def test_avg_validation(self):
931
- with self.assertRaises(ValueError):
932
- Band.select(Avg(Band.name)).run_sync()
933
-
934
- def test_sum_validation(self):
935
- with self.assertRaises(ValueError):
936
- Band.select(Sum(Band.name)).run_sync()
937
-
938
931
  def test_columns(self):
939
932
  """
940
933
  Make sure the colums method can be used to specify which columns to