Skip to content

Commit 6f605ce

Browse files
authored
Merge pull request piccolo-orm#26 from piccolo-orm/test_coverage
Test coverage
2 parents 9b55430 + 63a2973 commit 6f605ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+635
-93
lines changed

piccolo.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
# This is used for running Piccolo commands on the example project within
3+
# tests.
4+
export PICCOLO_CONF="tests.postgres_conf"
5+
python -m piccolo.main $@

piccolo/apps/app/commands/new.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ def new_app(app_name: str, root: str = "."):
2222
app_root = os.path.join(root, app_name)
2323

2424
if os.path.exists(app_root):
25-
print("Folder already exists - exiting.")
26-
sys.exit(1)
25+
sys.exit("Folder already exists - exiting.")
2726
os.mkdir(app_root)
2827

2928
with open(os.path.join(app_root, "__init__.py"), "w"):

piccolo/apps/migrations/commands/backwards.py

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1+
from __future__ import annotations
2+
import os
3+
import sys
4+
15
from piccolo.apps.migrations.auto import MigrationManager
6+
from piccolo.apps.migrations.commands.base import BaseMigrationManager
27
from piccolo.apps.migrations.tables import Migration
38
from piccolo.utils.sync import run_sync
4-
from .base import BaseMigrationManager
59

610

711
class BackwardsMigrationManager(BaseMigrationManager):
8-
def __init__(self, app_name: str, migration_id: str):
12+
def __init__(
13+
self,
14+
app_name: str,
15+
migration_id: str,
16+
auto_agree: bool = False,
17+
clean: bool = False,
18+
):
919
self.migration_id = migration_id
1020
self.app_name = app_name
21+
self.auto_agree = auto_agree
22+
self.clean = clean
1123
super().__init__()
1224

1325
def run(self):
26+
self.create_migration_table()
27+
1428
app_modules = self.get_app_modules()
1529

1630
migration_modules = []
@@ -27,8 +41,7 @@ def run(self):
2741
app_name=self.app_name
2842
)
2943
if len(ran_migration_ids) == 0:
30-
print("No migrations to reverse!")
31-
return
44+
sys.exit("No migrations to reverse!")
3245

3346
#######################################################################
3447

@@ -40,7 +53,7 @@ def run(self):
4053
earliest_migration_id = self.migration_id
4154

4255
if earliest_migration_id not in ran_migration_ids:
43-
print(
56+
sys.exit(
4457
"Unrecognized migration name - must be one of "
4558
f"{ran_migration_ids}"
4659
)
@@ -57,10 +70,14 @@ def run(self):
5770

5871
#######################################################################
5972

60-
_continue = input(
61-
"About to undo the following migrations:\n"
62-
f"{reversed_migration_ids}\n"
63-
"Enter y to continue.\n"
73+
_continue = (
74+
"y"
75+
if self.auto_agree
76+
else input(
77+
"About to undo the following migrations:\n"
78+
f"{reversed_migration_ids}\n"
79+
"Enter y to continue.\n"
80+
)
6481
)
6582
if _continue == "y":
6683
print("Undoing migrations")
@@ -76,11 +93,20 @@ def run(self):
7693
Migration.delete().where(
7794
Migration.name == migration_id
7895
).run_sync()
79-
else:
80-
print("Not proceeding.")
8196

97+
if self.clean:
98+
os.unlink(migration_module.__file__)
8299

83-
def backwards(app_name: str, migration_id: str = "1"):
100+
else: # pragma: no cover
101+
sys.exit("Not proceeding.")
102+
103+
104+
def backwards(
105+
app_name: str,
106+
migration_id: str = "1",
107+
auto_agree: bool = False,
108+
clean: bool = False,
109+
):
84110
"""
85111
Undo migrations up to a specific migration.
86112
@@ -91,26 +117,41 @@ def backwards(app_name: str, migration_id: str = "1"):
91117
Migrations will be reversed up to and including this migration_id.
92118
Specify a value of 'all' to undo all of the migrations. Specify a
93119
value of '1' to undo the most recent migration.
120+
:param auto_agree:
121+
Automatically agree to any input prompts.
122+
:param clean:
123+
If true, the migration files which have been run backwards are deleted
124+
from the disk after completing.
125+
94126
"""
95127
if app_name == "all":
96128
sorted_app_names = BaseMigrationManager().get_sorted_app_names()
97129
sorted_app_names.reverse()
98130

99-
_continue = input(
100-
"You're about to undo the migrations for the following apps:\n"
101-
f"{sorted_app_names}\n"
102-
"Are you sure you want to continue?\n"
103-
"Enter y to continue.\n"
131+
_continue = (
132+
"y"
133+
if auto_agree
134+
else input(
135+
"You're about to undo the migrations for the following apps:\n"
136+
f"{sorted_app_names}\n"
137+
"Are you sure you want to continue?\n"
138+
"Enter y to continue.\n"
139+
)
104140
)
105141
if _continue == "y":
106142
for _app_name in sorted_app_names:
107143
print(f"Undoing {_app_name}")
108144
manager = BackwardsMigrationManager(
109-
app_name=_app_name, migration_id="all"
145+
app_name=_app_name,
146+
migration_id="all",
147+
auto_agree=auto_agree,
110148
)
111149
manager.run()
112150
else:
113151
manager = BackwardsMigrationManager(
114-
app_name=app_name, migration_id=migration_id
152+
app_name=app_name,
153+
migration_id=migration_id,
154+
auto_agree=auto_agree,
155+
clean=clean,
115156
)
116157
manager.run()

piccolo/apps/migrations/commands/forwards.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
import sys
33
import typing as t
44

5-
from .base import BaseMigrationManager
6-
from piccolo.conf.apps import AppConfig
7-
from piccolo.apps.migrations.tables import Migration
85
from piccolo.apps.migrations.auto import MigrationManager
9-
from piccolo.conf.apps import MigrationModule
6+
from piccolo.apps.migrations.commands.base import BaseMigrationManager
7+
from piccolo.apps.migrations.tables import Migration
8+
from piccolo.conf.apps import AppConfig, MigrationModule
109
from piccolo.utils.sync import run_sync
1110

1211

@@ -40,8 +39,7 @@ def run_migrations(self, app_config: AppConfig) -> None:
4039
print(f"Haven't run = {havent_run}")
4140

4241
if len(havent_run) == 0:
43-
print("No migrations left to run!")
44-
return
42+
sys.exit("No migrations left to run!")
4543

4644
if self.migration_id == "all":
4745
subset = havent_run

piccolo/apps/migrations/commands/new.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def _create_new_migration(app_config: AppConfig, auto=False) -> None:
7575
)
7676

7777
if sum([len(i.statements) for i in alter_statements]) == 0:
78-
print("No changes detected - exiting.")
79-
sys.exit(1)
78+
sys.exit("No changes detected - exiting.")
8079

8180
file_contents = render_template(
8281
migration_id=_id,
@@ -151,8 +150,7 @@ def new(app_name: str, auto: bool = False):
151150

152151
engine = Finder().get_engine()
153152
if auto and isinstance(engine, SQLiteEngine):
154-
print("Auto migrations aren't currently supported by SQLite.")
155-
sys.exit(1)
153+
sys.exit("Auto migrations aren't currently supported by SQLite.")
156154

157155
app_config = Finder().get_app_config(app_name=app_name)
158156

piccolo/apps/playground/commands/run.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,9 @@ def run(
131131
try:
132132
import IPython # type: ignore
133133
except ImportError:
134-
print(
134+
sys.exit(
135135
"Install iPython using `pip install ipython` to use this feature."
136136
)
137-
sys.exit(1)
138137

139138
if engine.upper() == "POSTGRES":
140139
db: Engine = PostgresEngine(

piccolo/apps/project/commands/new.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def new_piccolo_conf(engine_name: str, force: bool = False, root: str = "."):
2323
file_path = os.path.join(root, "piccolo_conf.py")
2424

2525
if os.path.exists(file_path) and not force:
26-
print("The file already exists - exiting.")
27-
sys.exit(1)
26+
sys.exit("The file already exists - exiting.")
2827

2928
with open(file_path, "w") as f:
3029
template = JINJA_ENV.get_template("piccolo_conf.py.jinja")

piccolo/apps/shell/commands/run.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,28 @@
44
from piccolo.conf.apps import AppRegistry, AppConfig, Finder
55
from piccolo.table import Table
66

7+
try:
8+
import IPython # type: ignore
9+
from IPython.core.interactiveshell import _asyncio_runner # type: ignore
10+
11+
IPYTHON = True
12+
except ImportError:
13+
IPYTHON = False
14+
715

8-
def start_ipython_shell(**tables: t.Dict[str, t.Type[Table]]):
9-
try:
10-
import IPython # type: ignore
11-
except ImportError:
12-
print(
16+
def start_ipython_shell(
17+
**tables: t.Dict[str, t.Type[Table]]
18+
): # pragma: no cover
19+
if not IPYTHON:
20+
sys.exit(
1321
"Install iPython using `pip install ipython` to use this feature."
1422
)
15-
sys.exit(1)
1623

1724
existing_global_names = globals().keys()
1825
for table_class_name, table_class in tables.items():
1926
if table_class_name not in existing_global_names:
2027
globals()[table_class_name] = table_class
2128

22-
from IPython.core.interactiveshell import _asyncio_runner # type: ignore
23-
2429
IPython.embed(using=_asyncio_runner)
2530

2631

@@ -41,7 +46,9 @@ def run():
4146
app_config: AppConfig = app_config
4247
print(f"Importing {app_name} tables:")
4348
if app_config.table_classes:
44-
for table_class in app_config.table_classes:
49+
for table_class in sorted(
50+
app_config.table_classes, key=lambda x: x.__name__
51+
):
4552
table_class_name = table_class.__name__
4653
print(f"- {table_class_name}")
4754
tables[table_class_name] = table_class

piccolo/apps/user/commands/change_password.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def change_password():
1717
confirmed_password = get_confirmed_password()
1818

1919
if not password == confirmed_password:
20-
print("Passwords don't match!")
21-
sys.exit(1)
20+
sys.exit("Passwords don't match!")
2221

2322
BaseUser.update_password_sync(user=username, password=password)
2423

piccolo/apps/user/commands/create.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@ def create():
4343
confirmed_password = get_confirmed_password()
4444

4545
if not password == confirmed_password:
46-
print("Passwords don't match!")
47-
sys.exit(1)
46+
sys.exit("Passwords don't match!")
4847

4948
if len(password) < 4:
50-
print("The password is too short")
51-
sys.exit(1)
49+
sys.exit("The password is too short")
5250

5351
is_admin = get_is_admin()
5452

0 commit comments

Comments
 (0)