@@ -129,7 +129,7 @@ Put next into the ``views.py``:
129
129
130
130
131
131
def index ():
132
- return ' Hello, World!'
132
+ return " Hello, World!"
133
133
134
134
Ok, we have the view.
135
135
@@ -170,7 +170,7 @@ Put next into the ``application.py``:
170
170
171
171
app = Flask(__name__ )
172
172
app.container = container
173
- app.add_url_rule(' / ' , ' index' , views.index)
173
+ app.add_url_rule(" / " , " index" , views.index)
174
174
175
175
return app
176
176
@@ -246,7 +246,7 @@ Edit ``application.py``:
246
246
247
247
app = Flask(__name__ )
248
248
app.container = container
249
- app.add_url_rule(' / ' , ' index' , views.index)
249
+ app.add_url_rule(" / " , " index" , views.index)
250
250
251
251
bootstrap = Bootstrap()
252
252
bootstrap.init_app(app)
@@ -398,13 +398,13 @@ Edit ``views.py``:
398
398
399
399
400
400
def index ():
401
- query = request.args.get(' query' , ' Dependency Injector' )
402
- limit = request.args.get(' limit' , 10 , int )
401
+ query = request.args.get(" query" , " Dependency Injector" )
402
+ limit = request.args.get(" limit" , 10 , int )
403
403
404
404
repositories = []
405
405
406
406
return render_template(
407
- ' index.html' ,
407
+ " index.html" ,
408
408
query = query,
409
409
limit = limit,
410
410
repositories = repositories,
@@ -553,12 +553,12 @@ Edit ``application.py``:
553
553
554
554
def create_app () -> Flask:
555
555
container = Container()
556
- container.config.from_yaml(' config.yml' )
557
- container.config.github.auth_token.from_env(' GITHUB_TOKEN' )
556
+ container.config.from_yaml(" config.yml" )
557
+ container.config.github.auth_token.from_env(" GITHUB_TOKEN" )
558
558
559
559
app = Flask(__name__ )
560
560
app.container = container
561
- app.add_url_rule(' / ' , ' index' , views.index)
561
+ app.add_url_rule(" / " , " index" , views.index)
562
562
563
563
bootstrap = Bootstrap()
564
564
bootstrap.init_app(app)
@@ -639,7 +639,7 @@ and put next into it:
639
639
""" Search for repositories and return formatted data."""
640
640
repositories = self ._github_client.search_repositories(
641
641
query = query,
642
- ** {' in ' : ' name' },
642
+ ** {" in " : " name" },
643
643
)
644
644
return [
645
645
self ._format_repo(repository)
@@ -649,22 +649,22 @@ and put next into it:
649
649
def _format_repo (self , repository : Repository):
650
650
commits = repository.get_commits()
651
651
return {
652
- ' url' : repository.html_url,
653
- ' name' : repository.name,
654
- ' owner' : {
655
- ' login' : repository.owner.login,
656
- ' url' : repository.owner.html_url,
657
- ' avatar_url' : repository.owner.avatar_url,
652
+ " url" : repository.html_url,
653
+ " name" : repository.name,
654
+ " owner" : {
655
+ " login" : repository.owner.login,
656
+ " url" : repository.owner.html_url,
657
+ " avatar_url" : repository.owner.avatar_url,
658
658
},
659
- ' latest_commit' : self ._format_commit(commits[0 ]) if commits else {},
659
+ " latest_commit" : self ._format_commit(commits[0 ]) if commits else {},
660
660
}
661
661
662
662
def _format_commit (self , commit : Commit):
663
663
return {
664
- ' sha' : commit.sha,
665
- ' url' : commit.html_url,
666
- ' message' : commit.commit.message,
667
- ' author_name' : commit.commit.author.name,
664
+ " sha" : commit.sha,
665
+ " url" : commit.html_url,
666
+ " message" : commit.commit.message,
667
+ " author_name" : commit.commit.author.name,
668
668
}
669
669
670
670
Now let's add ``SearchService `` to the container.
@@ -720,13 +720,13 @@ Edit ``views.py``:
720
720
721
721
@inject
722
722
def index (search_service : SearchService = Provide[Container.search_service]):
723
- query = request.args.get(' query' , ' Dependency Injector' )
724
- limit = request.args.get(' limit' , 10 , int )
723
+ query = request.args.get(" query" , " Dependency Injector" )
724
+ limit = request.args.get(" limit" , 10 , int )
725
725
726
726
repositories = search_service.search_repositories(query, limit)
727
727
728
728
return render_template(
729
- ' index.html' ,
729
+ " index.html" ,
730
730
query = query,
731
731
limit = limit,
732
732
repositories = repositories,
@@ -752,13 +752,13 @@ Edit ``application.py``:
752
752
753
753
def create_app () -> Flask:
754
754
container = Container()
755
- container.config.from_yaml(' config.yml' )
756
- container.config.github.auth_token.from_env(' GITHUB_TOKEN' )
755
+ container.config.from_yaml(" config.yml" )
756
+ container.config.github.auth_token.from_env(" GITHUB_TOKEN" )
757
757
container.wire(modules = [views])
758
758
759
759
app = Flask(__name__ )
760
760
app.container = container
761
- app.add_url_rule(' / ' , ' index' , views.index)
761
+ app.add_url_rule(" / " , " index" , views.index)
762
762
763
763
bootstrap = Bootstrap()
764
764
bootstrap.init_app(app)
@@ -801,13 +801,13 @@ Edit ``views.py``:
801
801
default_query : str = Provide[Container.config.default.query],
802
802
default_limit : int = Provide[Container.config.default.limit.as_int()],
803
803
):
804
- query = request.args.get(' query' , default_query)
805
- limit = request.args.get(' limit' , default_limit, int )
804
+ query = request.args.get(" query" , default_query)
805
+ limit = request.args.get(" limit" , default_limit, int )
806
806
807
807
repositories = search_service.search_repositories(query, limit)
808
808
809
809
return render_template(
810
- ' index.html' ,
810
+ " index.html" ,
811
811
query = query,
812
812
limit = limit,
813
813
repositories = repositories,
@@ -900,55 +900,55 @@ and put next into it:
900
900
github_client_mock = mock.Mock(spec = Github)
901
901
github_client_mock.search_repositories.return_value = [
902
902
mock.Mock(
903
- html_url = ' repo1-url' ,
904
- name = ' repo1-name' ,
903
+ html_url = " repo1-url" ,
904
+ name = " repo1-name" ,
905
905
owner = mock.Mock(
906
- login = ' owner1-login' ,
907
- html_url = ' owner1-url' ,
908
- avatar_url = ' owner1-avatar-url' ,
906
+ login = " owner1-login" ,
907
+ html_url = " owner1-url" ,
908
+ avatar_url = " owner1-avatar-url" ,
909
909
),
910
910
get_commits = mock.Mock(return_value = [mock.Mock()]),
911
911
),
912
912
mock.Mock(
913
- html_url = ' repo2-url' ,
914
- name = ' repo2-name' ,
913
+ html_url = " repo2-url" ,
914
+ name = " repo2-name" ,
915
915
owner = mock.Mock(
916
- login = ' owner2-login' ,
917
- html_url = ' owner2-url' ,
918
- avatar_url = ' owner2-avatar-url' ,
916
+ login = " owner2-login" ,
917
+ html_url = " owner2-url" ,
918
+ avatar_url = " owner2-avatar-url" ,
919
919
),
920
920
get_commits = mock.Mock(return_value = [mock.Mock()]),
921
921
),
922
922
]
923
923
924
924
with app.container.github_client.override(github_client_mock):
925
- response = client.get(url_for(' index' ))
925
+ response = client.get(url_for(" index" ))
926
926
927
927
assert response.status_code == 200
928
- assert b ' Results found: 2' in response.data
928
+ assert b " Results found: 2" in response.data
929
929
930
- assert b ' repo1-url' in response.data
931
- assert b ' repo1-name' in response.data
932
- assert b ' owner1-login' in response.data
933
- assert b ' owner1-url' in response.data
934
- assert b ' owner1-avatar-url' in response.data
930
+ assert b " repo1-url" in response.data
931
+ assert b " repo1-name" in response.data
932
+ assert b " owner1-login" in response.data
933
+ assert b " owner1-url" in response.data
934
+ assert b " owner1-avatar-url" in response.data
935
935
936
- assert b ' repo2-url' in response.data
937
- assert b ' repo2-name' in response.data
938
- assert b ' owner2-login' in response.data
939
- assert b ' owner2-url' in response.data
940
- assert b ' owner2-avatar-url' in response.data
936
+ assert b " repo2-url" in response.data
937
+ assert b " repo2-name" in response.data
938
+ assert b " owner2-login" in response.data
939
+ assert b " owner2-url" in response.data
940
+ assert b " owner2-avatar-url" in response.data
941
941
942
942
943
943
def test_index_no_results (client , app ):
944
944
github_client_mock = mock.Mock(spec = Github)
945
945
github_client_mock.search_repositories.return_value = []
946
946
947
947
with app.container.github_client.override(github_client_mock):
948
- response = client.get(url_for(' index' ))
948
+ response = client.get(url_for(" index" ))
949
949
950
950
assert response.status_code == 200
951
- assert b ' Results found: 0' in response.data
951
+ assert b " Results found: 0" in response.data
952
952
953
953
Now let's run it and check the coverage:
954
954
0 commit comments