Skip to content

Commit 4745027

Browse files
committed
Now with all the tests.
1 parent 72a490a commit 4745027

File tree

12 files changed

+197
-0
lines changed

12 files changed

+197
-0
lines changed

app/ch14_testing/final/.idea/dictionaries/screencaster.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/ch14_testing/final/.idea/flask-testing.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/ch14_testing/final/pypi_org/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ def register_blueprints():
2929
from pypi_org.views import package_views
3030
from pypi_org.views import cms_views
3131
from pypi_org.views import account_views
32+
from pypi_org.views import seo_view
3233

3334
app.register_blueprint(package_views.blueprint)
3435
app.register_blueprint(home_views.blueprint)
3536
app.register_blueprint(account_views.blueprint)
37+
app.register_blueprint(seo_view.blueprint)
3638
app.register_blueprint(cms_views.blueprint)
3739

3840

app/ch14_testing/final/pypi_org/services/package_service.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import List, Optional
22
import sqlalchemy.orm
3+
from sqlalchemy.orm import Session
34

45
import pypi_org.data.db_session as db_session
56
from pypi_org.data.package import Package
@@ -56,3 +57,11 @@ def get_package_by_id(package_id: str) -> Optional[Package]:
5657
session.close()
5758

5859
return package
60+
61+
62+
def all_packages(limit: int) -> List[Package]:
63+
session: Session = db_session.create_session()
64+
try:
65+
return list(session.query(Package).limit(limit))
66+
finally:
67+
session.close()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
User-agent: *
2+
Disallow:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
3+
<!-- STATIC CONSTANT URLS -->
4+
<url>
5+
<loc>{{ site }}</loc>
6+
<lastmod>{{ last_updated_text }}</lastmod>
7+
<changefreq>daily</changefreq>
8+
<priority>1.0</priority>
9+
</url>
10+
<url>
11+
<loc>{{site}}/account/login</loc>
12+
<lastmod>{{ last_updated_text }}</lastmod>
13+
<changefreq>yearly</changefreq>
14+
<priority>0.8</priority>
15+
</url>
16+
<url>
17+
<loc>{{site}}/account/register</loc>
18+
<lastmod>${last_updated_text}</lastmod>
19+
<changefreq>yearly</changefreq>
20+
<priority>0.8</priority>
21+
</url>
22+
23+
24+
<!-- DYNAMIC PAGES (basically packages) -->
25+
{% for p in packages %}
26+
<url>
27+
<loc>{{site}}/project/{{p.id}}</loc>
28+
<lastmod>{{p.created_date.isoformat()}}</lastmod>
29+
<changefreq>weekly</changefreq>
30+
<priority>1.0</priority>
31+
</url>
32+
{% endfor %}
33+
</urlset>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import flask
2+
3+
from pypi_org.services import package_service
4+
from pypi_org.viewmodels.shared.viewmodelbase import ViewModelBase
5+
6+
7+
class SiteMapViewModel(ViewModelBase):
8+
def __init__(self, limit: int):
9+
super().__init__()
10+
self.packages = package_service.all_packages(limit)
11+
self.last_updated_text = "2018-07-15"
12+
self.site = "{}://{}".format(flask.request.scheme, flask.request.host)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import flask
2+
3+
from pypi_org.infrastructure.view_modifiers import response
4+
from pypi_org.viewmodels.seo.sitemap_viewmodel import SiteMapViewModel
5+
6+
blueprint = flask.Blueprint('seo', __name__, template_folder='templates')
7+
8+
9+
# ################### Sitemap #################################
10+
11+
12+
@blueprint.route('/sitemap.xml')
13+
@response(mimetype='application/xml', template_file='seo/sitemap.html')
14+
def sitemap():
15+
vm = SiteMapViewModel(1000)
16+
return vm.to_dict()
17+
18+
19+
# ################### Robots #################################
20+
21+
@blueprint.route('/robots.txt')
22+
@response(mimetype='text/plain', template_file='seo/robots.txt')
23+
def robots():
24+
return {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# noinspection PyUnresolvedReferences
2+
from account_tests import *
3+
# noinspection PyUnresolvedReferences
4+
from package_tests import *
5+
# noinspection PyUnresolvedReferences
6+
from sitemap_tests import *
7+
# noinspection PyUnresolvedReferences
8+
from home_tests import *
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from flask import Response
2+
3+
from tests.test_client import client, flask_app
4+
from pypi_org.views import home_views
5+
6+
7+
def test_int_homepage(client):
8+
r: Response = client.get('/')
9+
assert r.status_code == 200
10+
assert b'Find, install and publish Python packages' in r.data
11+
12+
13+
def test_v_homepage_directly():
14+
with flask_app.test_request_context(path='/'):
15+
r: Response = home_views.index()
16+
17+
assert r.status_code == 200
18+
# noinspection PyUnresolvedReferences
19+
assert len(r.model.get('releases')) > 0

0 commit comments

Comments
 (0)