Skip to content

Commit 6df2392

Browse files
authored
Merge pull request #97 from TTWShell/refactor
enhance doc: add usage
2 parents 7c820ed + 261d918 commit 6df2392

File tree

4 files changed

+123
-80
lines changed

4 files changed

+123
-80
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,78 @@
1010
A flask project generator. Based on Flask + SQLAlchemy + marshmallow + webargs.
1111

1212
[https://hobbit-core.readthedocs.io/zh/latest/](https://hobbit-core.readthedocs.io/zh/latest/)
13+
14+
# Installation
15+
16+
Install and update using pip(**Still using Python 2? It is time to upgrade.**):
17+
18+
pip install -U "hobbit-core[hobbit]" # just install hobbit cmd
19+
pip install -U "hobbit-core[hobbit,hobbit_core]" # recommended when use virtualenv
20+
21+
# A Simple Example
22+
23+
## Init project:
24+
25+
hobbit --echo new -n demo -d /tmp/demo -p 5000 -t rivendell
26+
cd /tmp/demo
27+
pipenv install -r requirements.txt --pre && pipenv install --dev pytest pytest-cov pytest-env ipython flake8 ipdb
28+
pipenv shell
29+
30+
## Run server:
31+
32+
(demo) ➜ demo FLASK_APP=app/run.py flask run
33+
* Serving Flask app "app/run.py"
34+
* Environment: production
35+
WARNING: This is a development server. Do not use it in a production deployment.
36+
Use a production WSGI server instead.
37+
* Debug mode: off
38+
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
39+
40+
## Run test:
41+
42+
(demo) ➜ demo py.test
43+
===================================================== test session starts ======================================================
44+
platform darwin -- Python 3.7.0, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- /Users/Legolas/.virtualenvs/demo-OzheZQoG/bin/python3.7
45+
cachedir: .pytest_cache
46+
rootdir: /private/tmp/demo, inifile: pytest.ini
47+
plugins: env-0.6.2, cov-2.7.1
48+
collected 2 items
49+
50+
tests/test_option.py::TestOption::test_options PASSED
51+
tests/test_ping.py::TestAPIExample::test_ping_api PASSED
52+
53+
---------- coverage: platform darwin, python 3.7.0-final-0 -----------
54+
Name Stmts Miss Cover Missing
55+
----------------------------------------------------------
56+
app/__init__.py 0 0 100%
57+
app/configs/__init__.py 0 0 100%
58+
app/configs/default.py 6 0 100%
59+
app/configs/development.py 1 1 0% 1
60+
app/configs/production.py 2 2 0% 1-3
61+
app/configs/testing.py 8 0 100%
62+
app/core/__init__.py 0 0 100%
63+
app/exts.py 8 0 100%
64+
app/models/__init__.py 2 0 100%
65+
app/models/consts.py 1 0 100%
66+
app/run.py 35 1 97% 49
67+
app/schemas/__init__.py 2 0 100%
68+
app/services/__init__.py 2 0 100%
69+
app/services/option.py 6 0 100%
70+
app/tasks/__init__.py 1 1 0% 1
71+
app/utils/__init__.py 0 0 100%
72+
app/views/__init__.py 2 0 100%
73+
app/views/option.py 5 0 100%
74+
app/views/ping.py 7 0 100%
75+
tests/__init__.py 17 1 94% 29
76+
tests/conftest.py 11 0 100%
77+
tests/test_option.py 5 0 100%
78+
tests/test_ping.py 5 0 100%
79+
----------------------------------------------------------
80+
TOTAL 126 6 95%
81+
82+
83+
=================================================== 2 passed in 0.24 seconds ===================================================
84+
85+
# Others
86+
87+
hobbit --help
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
hobbit-core[hobbit,hobbit_core]=={{ version }}
2-
{%- if celery -%}
2+
{% if celery -%}
33
celery
44
{% endif %}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
hobbit-core[hobbit,hobbit_core]=={{ version }}
2-
{%- if celery -%}
2+
{% if celery -%}
33
celery
44
{% endif %}

tests/test_hobbit.py

Lines changed: 46 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
2-
from subprocess import call
2+
import subprocess
3+
import itertools
34

45
import pytest
56
from click.testing import CliRunner
67

78
from hobbit import main as hobbit
9+
from hobbit.bootstrap import templates
810

911
from . import BaseTest, rmdir, chdir
1012

@@ -23,90 +25,56 @@ def teardown_method(self, method):
2325
def runner(self):
2426
yield CliRunner()
2527

26-
def test_hobbit_cmd(self, runner):
28+
def test_not_exist_cmd(self, runner):
2729
result = runner.invoke(hobbit)
2830
assert result.exit_code == 0
2931

3032
result = runner.invoke(hobbit, ['doesnotexistcmd'], obj={})
3133
assert 'Error: cmd not exist: doesnotexistcmd' in result.output
3234

35+
@pytest.mark.parametrize(
36+
'name,template,celery_,dist',
37+
itertools.product(
38+
['haha'], templates, ['--celery', '--no-celery'],
39+
[None, '.', wkdir]))
3340
@chdir(wkdir)
34-
def test_new_cmd_nodist(self, runner):
35-
assert os.getcwd() == self.wkdir
36-
37-
result = runner.invoke(hobbit, ['--echo', 'new'], obj={})
38-
assert result.exit_code == 2
39-
assert 'Error: Missing option "-n" / "--name".' in result.output
40-
41-
result = runner.invoke(
42-
hobbit, [
43-
'--echo', 'new', '-n', 'haha', '-f', '-p', '1024',
44-
], obj={})
45-
assert result.exit_code == 0
41+
def test_new_cmd(self, runner, name, template, celery_, dist):
42+
options = [
43+
'--echo', 'new', '-p 1024', '-n', name, '-t', template, celery_]
44+
if dist:
45+
assert os.getcwd() == os.path.abspath(dist)
46+
options.extend(['-d', dist])
47+
48+
result = runner.invoke(hobbit, options, obj={})
49+
assert result.exit_code == 0, result.output
4650
assert 'mkdir\t{}'.format(self.wkdir) in result.output
4751
assert 'render\t{}'.format(self.wkdir) in result.output
48-
assert os.path.exists(os.path.join(
49-
os.getcwd(), 'app', 'models', '__init__.py'))
50-
assert 'example.py' not in result.output
51-
52-
def test_new_cmd_dist(self, runner):
53-
result = runner.invoke(
54-
hobbit, [
55-
'--echo', 'new', '-n', 'haha', '-f', '-d', self.wkdir,
56-
'-p', '1024',
57-
], obj={})
58-
assert result.exit_code == 0
59-
assert 'mkdir\t{}'.format(self.wkdir) in result.output
60-
assert 'render\t{}'.format(self.wkdir) in result.output
61-
assert os.path.exists(os.path.join(
62-
self.wkdir, 'app', 'models', '__init__.py'))
63-
assert 'example.py' not in result.output
64-
65-
@chdir(wkdir)
66-
def test_new_cmd_curdir(self, runner):
67-
assert os.getcwd() == self.wkdir
68-
69-
result = runner.invoke(
70-
hobbit, [
71-
'--echo', 'new', '-n', 'haha', '-f', '-d', '.',
72-
'-p', '1024',
73-
], obj={})
74-
assert result.exit_code == 0
75-
assert os.path.exists(os.path.join(
76-
os.getcwd(), 'app', 'models', '__init__.py'))
77-
assert 'example.py' not in result.output
78-
79-
@chdir(wkdir)
80-
def test_new_cmd(self, runner):
81-
assert os.getcwd() == self.wkdir
8252

83-
cmd = [
84-
'--echo', 'new', '-n', 'haha', '-p', '1024']
85-
result = runner.invoke(hobbit, cmd, obj={})
86-
# start + 29 files + 11 dir + 1 end + empty
87-
# in this test case. main dir exists, so mkdir - 1
88-
assert len(result.output.split('\n')) == 1 + 29 + 11 + 1 + 1 - 1
89-
assert result.exit_code == 0
90-
91-
# test no -f worked
92-
result = runner.invoke(hobbit, cmd, obj={})
93-
assert result.exit_code == 0
94-
assert ', ignore' in result.output
95-
assert call(['flake8', '.']) == 0
96-
97-
@chdir(wkdir)
98-
def test_new_cmd_celery(self, runner):
99-
assert os.getcwd() == self.wkdir
100-
101-
cmd = [
102-
'--echo', 'new', '-n', 'haha', '-p', '1024',
103-
'--celery']
104-
result = runner.invoke(hobbit, cmd, obj={})
105-
# start + files + mkdir + tail
106-
assert len(result.output.split('\n')) == 1 + 30 + 12 + 1
107-
assert result.exit_code == 0
108-
assert '/tasks' in result.output
109-
assert call(['flake8', '.']) == 0
53+
file_nums = {
54+
# tart + 29 files + 11 dir + 1 end + empty
55+
'shire | --no-celery': 1 + 29 + 11 + 1 + 1 - 1,
56+
# start + files + mkdir + tail
57+
'shire | --celery': 1 + 30 + 12 + 1,
58+
'rivendell | --no-celery': 1 + 31 + 11 + 1,
59+
'rivendell | --celery': 1 + 32 + 12 + 1,
60+
}
61+
assert len(result.output.split('\n')) == file_nums[
62+
f'{template} | {celery_}']
63+
64+
assert subprocess.call(['flake8', '.']) == 0
65+
assert subprocess.call(
66+
'pip install -r requirements.txt '
67+
'--upgrade-strategy=only-if-needed',
68+
shell=True, stdout=subprocess.DEVNULL,
69+
stderr=subprocess.DEVNULL) == 0
70+
assert subprocess.call(['pytest'], stdout=subprocess.DEVNULL) == 0
71+
72+
# test --force option
73+
result = runner.invoke(hobbit, options, obj={})
74+
assert all([i in result.output for i in ['exists ', 'ignore ...']])
75+
options.extend(['-f'])
76+
result = runner.invoke(hobbit, options, obj={})
77+
assert any([i in result.output for i in ['exists ', 'ignore ...']])
11078

11179
@pytest.fixture
11280
def csv_file(self, runner):
@@ -130,14 +98,14 @@ def test_new_rivendell_tpl_and_gen_cmd(self, runner, gen_cmd, csv_file):
13098
result = runner.invoke(hobbit, cmd, obj={})
13199
# start + files + mkdir + tail
132100
assert result.exit_code == 0
133-
assert len(result.output.split('\n')) == 1 + 31 + 11 + 1
134101

135102
# gen new module
136103
result = runner.invoke(hobbit, gen_cmd, obj={})
137104
assert result.exit_code == 0
138105
assert len(result.output.split('\n')) == 5 + 1, result.output # files
139106

140107
# flake8 check
141-
assert call(['flake8', '.']) == 0
108+
assert subprocess.call(['flake8', '.']) == 0
142109
# pytest
143-
assert call(['pytest']) == 0
110+
assert subprocess.call(
111+
['pytest', '--no-cov'], stdout=subprocess.DEVNULL) == 0

0 commit comments

Comments
 (0)