Update quotes in flask tutorial and example

This commit is contained in:
Roman Mogylatov 2021-09-30 15:53:37 -04:00
parent 7e794c41dd
commit c92a941fe5
9 changed files with 139 additions and 139 deletions

View File

@ -129,7 +129,7 @@ Put next into the ``views.py``:
def index():
return 'Hello, World!'
return "Hello, World!"
Ok, we have the view.
@ -170,7 +170,7 @@ Put next into the ``application.py``:
app = Flask(__name__)
app.container = container
app.add_url_rule('/', 'index', views.index)
app.add_url_rule("/", "index", views.index)
return app
@ -246,7 +246,7 @@ Edit ``application.py``:
app = Flask(__name__)
app.container = container
app.add_url_rule('/', 'index', views.index)
app.add_url_rule("/", "index", views.index)
bootstrap = Bootstrap()
bootstrap.init_app(app)
@ -398,13 +398,13 @@ Edit ``views.py``:
def index():
query = request.args.get('query', 'Dependency Injector')
limit = request.args.get('limit', 10, int)
query = request.args.get("query", "Dependency Injector")
limit = request.args.get("limit", 10, int)
repositories = []
return render_template(
'index.html',
"index.html",
query=query,
limit=limit,
repositories=repositories,
@ -553,12 +553,12 @@ Edit ``application.py``:
def create_app() -> Flask:
container = Container()
container.config.from_yaml('config.yml')
container.config.github.auth_token.from_env('GITHUB_TOKEN')
container.config.from_yaml("config.yml")
container.config.github.auth_token.from_env("GITHUB_TOKEN")
app = Flask(__name__)
app.container = container
app.add_url_rule('/', 'index', views.index)
app.add_url_rule("/", "index", views.index)
bootstrap = Bootstrap()
bootstrap.init_app(app)
@ -639,7 +639,7 @@ and put next into it:
"""Search for repositories and return formatted data."""
repositories = self._github_client.search_repositories(
query=query,
**{'in': 'name'},
**{"in": "name"},
)
return [
self._format_repo(repository)
@ -649,22 +649,22 @@ and put next into it:
def _format_repo(self, repository: Repository):
commits = repository.get_commits()
return {
'url': repository.html_url,
'name': repository.name,
'owner': {
'login': repository.owner.login,
'url': repository.owner.html_url,
'avatar_url': repository.owner.avatar_url,
"url": repository.html_url,
"name": repository.name,
"owner": {
"login": repository.owner.login,
"url": repository.owner.html_url,
"avatar_url": repository.owner.avatar_url,
},
'latest_commit': self._format_commit(commits[0]) if commits else {},
"latest_commit": self._format_commit(commits[0]) if commits else {},
}
def _format_commit(self, commit: Commit):
return {
'sha': commit.sha,
'url': commit.html_url,
'message': commit.commit.message,
'author_name': commit.commit.author.name,
"sha": commit.sha,
"url": commit.html_url,
"message": commit.commit.message,
"author_name": commit.commit.author.name,
}
Now let's add ``SearchService`` to the container.
@ -720,13 +720,13 @@ Edit ``views.py``:
@inject
def index(search_service: SearchService = Provide[Container.search_service]):
query = request.args.get('query', 'Dependency Injector')
limit = request.args.get('limit', 10, int)
query = request.args.get("query", "Dependency Injector")
limit = request.args.get("limit", 10, int)
repositories = search_service.search_repositories(query, limit)
return render_template(
'index.html',
"index.html",
query=query,
limit=limit,
repositories=repositories,
@ -752,13 +752,13 @@ Edit ``application.py``:
def create_app() -> Flask:
container = Container()
container.config.from_yaml('config.yml')
container.config.github.auth_token.from_env('GITHUB_TOKEN')
container.config.from_yaml("config.yml")
container.config.github.auth_token.from_env("GITHUB_TOKEN")
container.wire(modules=[views])
app = Flask(__name__)
app.container = container
app.add_url_rule('/', 'index', views.index)
app.add_url_rule("/", "index", views.index)
bootstrap = Bootstrap()
bootstrap.init_app(app)
@ -801,13 +801,13 @@ Edit ``views.py``:
default_query: str = Provide[Container.config.default.query],
default_limit: int = Provide[Container.config.default.limit.as_int()],
):
query = request.args.get('query', default_query)
limit = request.args.get('limit', default_limit, int)
query = request.args.get("query", default_query)
limit = request.args.get("limit", default_limit, int)
repositories = search_service.search_repositories(query, limit)
return render_template(
'index.html',
"index.html",
query=query,
limit=limit,
repositories=repositories,
@ -900,44 +900,44 @@ and put next into it:
github_client_mock = mock.Mock(spec=Github)
github_client_mock.search_repositories.return_value = [
mock.Mock(
html_url='repo1-url',
name='repo1-name',
html_url="repo1-url",
name="repo1-name",
owner=mock.Mock(
login='owner1-login',
html_url='owner1-url',
avatar_url='owner1-avatar-url',
login="owner1-login",
html_url="owner1-url",
avatar_url="owner1-avatar-url",
),
get_commits=mock.Mock(return_value=[mock.Mock()]),
),
mock.Mock(
html_url='repo2-url',
name='repo2-name',
html_url="repo2-url",
name="repo2-name",
owner=mock.Mock(
login='owner2-login',
html_url='owner2-url',
avatar_url='owner2-avatar-url',
login="owner2-login",
html_url="owner2-url",
avatar_url="owner2-avatar-url",
),
get_commits=mock.Mock(return_value=[mock.Mock()]),
),
]
with app.container.github_client.override(github_client_mock):
response = client.get(url_for('index'))
response = client.get(url_for("index"))
assert response.status_code == 200
assert b'Results found: 2' in response.data
assert b"Results found: 2" in response.data
assert b'repo1-url' in response.data
assert b'repo1-name' in response.data
assert b'owner1-login' in response.data
assert b'owner1-url' in response.data
assert b'owner1-avatar-url' in response.data
assert b"repo1-url" in response.data
assert b"repo1-name" in response.data
assert b"owner1-login" in response.data
assert b"owner1-url" in response.data
assert b"owner1-avatar-url" in response.data
assert b'repo2-url' in response.data
assert b'repo2-name' in response.data
assert b'owner2-login' in response.data
assert b'owner2-url' in response.data
assert b'owner2-avatar-url' in response.data
assert b"repo2-url" in response.data
assert b"repo2-name" in response.data
assert b"owner2-login" in response.data
assert b"owner2-url" in response.data
assert b"owner2-avatar-url" in response.data
def test_index_no_results(client, app):
@ -945,10 +945,10 @@ and put next into it:
github_client_mock.search_repositories.return_value = []
with app.container.github_client.override(github_client_mock):
response = client.get(url_for('index'))
response = client.get(url_for("index"))
assert response.status_code == 200
assert b'Results found: 0' in response.data
assert b"Results found: 0" in response.data
Now let's run it and check the coverage:

View File

@ -9,8 +9,8 @@ from .blueprints import example
def create_app() -> Flask:
container = Container()
container.config.from_yaml('config.yml')
container.config.github.auth_token.from_env('GITHUB_TOKEN')
container.config.from_yaml("config.yml")
container.config.github.auth_token.from_env("GITHUB_TOKEN")
container.wire(modules=[example])
app = Flask(__name__)

View File

@ -7,23 +7,23 @@ from githubnavigator.services import SearchService
from githubnavigator.containers import Container
blueprint = Blueprint('example', __name__, template_folder='templates/')
blueprint = Blueprint("example", __name__, template_folder="templates/")
@blueprint.route('/')
@blueprint.route("/")
@inject
def index(
search_service: SearchService = Provide[Container.search_service],
default_query: str = Provide[Container.config.default.query],
default_limit: int = Provide[Container.config.default.limit.as_int()],
):
query = request.args.get('query', default_query)
limit = request.args.get('limit', default_limit, int)
query = request.args.get("query", default_query)
limit = request.args.get("limit", default_limit, int)
repositories = search_service.search_repositories(query, limit)
return render_template(
'index.html',
"index.html",
query=query,
limit=limit,
repositories=repositories,

View File

@ -15,7 +15,7 @@ class SearchService:
"""Search for repositories and return formatted data."""
repositories = self._github_client.search_repositories(
query=query,
**{'in': 'name'},
**{"in": "name"},
)
return [
self._format_repo(repository)
@ -25,20 +25,20 @@ class SearchService:
def _format_repo(self, repository: Repository):
commits = repository.get_commits()
return {
'url': repository.html_url,
'name': repository.name,
'owner': {
'login': repository.owner.login,
'url': repository.owner.html_url,
'avatar_url': repository.owner.avatar_url,
"url": repository.html_url,
"name": repository.name,
"owner": {
"login": repository.owner.login,
"url": repository.owner.html_url,
"avatar_url": repository.owner.avatar_url,
},
'latest_commit': self._format_commit(commits[0]) if commits else {},
"latest_commit": self._format_commit(commits[0]) if commits else {},
}
def _format_commit(self, commit: Commit):
return {
'sha': commit.sha,
'url': commit.html_url,
'message': commit.commit.message,
'author_name': commit.commit.author.name,
"sha": commit.sha,
"url": commit.html_url,
"message": commit.commit.message,
"author_name": commit.commit.author.name,
}

View File

@ -20,44 +20,44 @@ def test_index(client, app):
github_client_mock = mock.Mock(spec=Github)
github_client_mock.search_repositories.return_value = [
mock.Mock(
html_url='repo1-url',
name='repo1-name',
html_url="repo1-url",
name="repo1-name",
owner=mock.Mock(
login='owner1-login',
html_url='owner1-url',
avatar_url='owner1-avatar-url',
login="owner1-login",
html_url="owner1-url",
avatar_url="owner1-avatar-url",
),
get_commits=mock.Mock(return_value=[mock.Mock()]),
),
mock.Mock(
html_url='repo2-url',
name='repo2-name',
html_url="repo2-url",
name="repo2-name",
owner=mock.Mock(
login='owner2-login',
html_url='owner2-url',
avatar_url='owner2-avatar-url',
login="owner2-login",
html_url="owner2-url",
avatar_url="owner2-avatar-url",
),
get_commits=mock.Mock(return_value=[mock.Mock()]),
),
]
with app.container.github_client.override(github_client_mock):
response = client.get(url_for('example.index'))
response = client.get(url_for("example.index"))
assert response.status_code == 200
assert b'Results found: 2' in response.data
assert b"Results found: 2" in response.data
assert b'repo1-url' in response.data
assert b'repo1-name' in response.data
assert b'owner1-login' in response.data
assert b'owner1-url' in response.data
assert b'owner1-avatar-url' in response.data
assert b"repo1-url" in response.data
assert b"repo1-name" in response.data
assert b"owner1-login" in response.data
assert b"owner1-url" in response.data
assert b"owner1-avatar-url" in response.data
assert b'repo2-url' in response.data
assert b'repo2-name' in response.data
assert b'owner2-login' in response.data
assert b'owner2-url' in response.data
assert b'owner2-avatar-url' in response.data
assert b"repo2-url" in response.data
assert b"repo2-name" in response.data
assert b"owner2-login" in response.data
assert b"owner2-url" in response.data
assert b"owner2-avatar-url" in response.data
def test_index_no_results(client, app):
@ -65,7 +65,7 @@ def test_index_no_results(client, app):
github_client_mock.search_repositories.return_value = []
with app.container.github_client.override(github_client_mock):
response = client.get(url_for('example.index'))
response = client.get(url_for("example.index"))
assert response.status_code == 200
assert b'Results found: 0' in response.data
assert b"Results found: 0" in response.data

View File

@ -9,13 +9,13 @@ from . import views
def create_app() -> Flask:
container = Container()
container.config.from_yaml('config.yml')
container.config.github.auth_token.from_env('GITHUB_TOKEN')
container.config.from_yaml("config.yml")
container.config.github.auth_token.from_env("GITHUB_TOKEN")
container.wire(modules=[views])
app = Flask(__name__)
app.container = container
app.add_url_rule('/', 'index', views.index)
app.add_url_rule("/", "index", views.index)
bootstrap = Bootstrap()
bootstrap.init_app(app)

View File

@ -15,7 +15,7 @@ class SearchService:
"""Search for repositories and return formatted data."""
repositories = self._github_client.search_repositories(
query=query,
**{'in': 'name'},
**{"in": "name"},
)
return [
self._format_repo(repository)
@ -25,20 +25,20 @@ class SearchService:
def _format_repo(self, repository: Repository):
commits = repository.get_commits()
return {
'url': repository.html_url,
'name': repository.name,
'owner': {
'login': repository.owner.login,
'url': repository.owner.html_url,
'avatar_url': repository.owner.avatar_url,
"url": repository.html_url,
"name": repository.name,
"owner": {
"login": repository.owner.login,
"url": repository.owner.html_url,
"avatar_url": repository.owner.avatar_url,
},
'latest_commit': self._format_commit(commits[0]) if commits else {},
"latest_commit": self._format_commit(commits[0]) if commits else {},
}
def _format_commit(self, commit: Commit):
return {
'sha': commit.sha,
'url': commit.html_url,
'message': commit.commit.message,
'author_name': commit.commit.author.name,
"sha": commit.sha,
"url": commit.html_url,
"message": commit.commit.message,
"author_name": commit.commit.author.name,
}

View File

@ -20,44 +20,44 @@ def test_index(client, app):
github_client_mock = mock.Mock(spec=Github)
github_client_mock.search_repositories.return_value = [
mock.Mock(
html_url='repo1-url',
name='repo1-name',
html_url="repo1-url",
name="repo1-name",
owner=mock.Mock(
login='owner1-login',
html_url='owner1-url',
avatar_url='owner1-avatar-url',
login="owner1-login",
html_url="owner1-url",
avatar_url="owner1-avatar-url",
),
get_commits=mock.Mock(return_value=[mock.Mock()]),
),
mock.Mock(
html_url='repo2-url',
name='repo2-name',
html_url="repo2-url",
name="repo2-name",
owner=mock.Mock(
login='owner2-login',
html_url='owner2-url',
avatar_url='owner2-avatar-url',
login="owner2-login",
html_url="owner2-url",
avatar_url="owner2-avatar-url",
),
get_commits=mock.Mock(return_value=[mock.Mock()]),
),
]
with app.container.github_client.override(github_client_mock):
response = client.get(url_for('index'))
response = client.get(url_for("index"))
assert response.status_code == 200
assert b'Results found: 2' in response.data
assert b"Results found: 2" in response.data
assert b'repo1-url' in response.data
assert b'repo1-name' in response.data
assert b'owner1-login' in response.data
assert b'owner1-url' in response.data
assert b'owner1-avatar-url' in response.data
assert b"repo1-url" in response.data
assert b"repo1-name" in response.data
assert b"owner1-login" in response.data
assert b"owner1-url" in response.data
assert b"owner1-avatar-url" in response.data
assert b'repo2-url' in response.data
assert b'repo2-name' in response.data
assert b'owner2-login' in response.data
assert b'owner2-url' in response.data
assert b'owner2-avatar-url' in response.data
assert b"repo2-url" in response.data
assert b"repo2-name" in response.data
assert b"owner2-login" in response.data
assert b"owner2-url" in response.data
assert b"owner2-avatar-url" in response.data
def test_index_no_results(client, app):
@ -65,7 +65,7 @@ def test_index_no_results(client, app):
github_client_mock.search_repositories.return_value = []
with app.container.github_client.override(github_client_mock):
response = client.get(url_for('index'))
response = client.get(url_for("index"))
assert response.status_code == 200
assert b'Results found: 0' in response.data
assert b"Results found: 0" in response.data

View File

@ -13,13 +13,13 @@ def index(
default_query: str = Provide[Container.config.default.query],
default_limit: int = Provide[Container.config.default.limit.as_int()],
):
query = request.args.get('query', default_query)
limit = request.args.get('limit', default_limit, int)
query = request.args.get("query", default_query)
limit = request.args.get("limit", default_limit, int)
repositories = search_service.search_repositories(query, limit)
return render_template(
'index.html',
"index.html",
query=query,
limit=limit,
repositories=repositories,