Skip to content

Commit

Permalink
URl_checks реализация тестов и фикс
Browse files Browse the repository at this point in the history
  • Loading branch information
KAnanev committed Jul 13, 2023
1 parent 90d1635 commit 1cb5e21
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
32 changes: 25 additions & 7 deletions page_analyzer/services/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@
from page_analyzer.services.db import PostgresDB

GET_ITEMS = """SELECT
json_build_object(
'id', id,
'name', name,
'created_at', created_at
) AS result
FROM urls;"""
json_build_object(
'id', urls.id,
'name', urls.name,
'created_at', urls.created_at,
'url_checks', COALESCE(json_agg(json_build_object(
'id', latest_url_checks.id,
'url_id', latest_url_checks.url_id,
'status_code', latest_url_checks.status_code,
'h1', latest_url_checks.h1,
'title', latest_url_checks.title,
'description', latest_url_checks.description,
'created_at', latest_url_checks.created_at
)) FILTER (WHERE latest_url_checks.id IS NOT NULL), '[]'::json)
) AS result
FROM urls
LEFT JOIN LATERAL (
SELECT *
FROM url_checks
WHERE url_checks.url_id = urls.id
ORDER BY url_checks.created_at DESC
LIMIT 1
) AS latest_url_checks ON true
GROUP BY urls.id;"""

GET_JSON_BY_ID = """SELECT
json_build_object(
Expand Down Expand Up @@ -63,7 +80,8 @@ def get_all_urls(self) -> List[URLModel] | None:
items = self.db.execute_query(GET_ITEMS, many=True)
if items:
sorted_items = sorted(items, key=lambda item: -item['result']['id'])
items = [URLModel(**item['result']) for item in sorted_items]
print(sorted_items)
items = [URLSModel(**item['result']) for item in sorted_items]
return items

def get_url_id_by_url_name(self, item: URLModel) -> Optional[URLModel]:
Expand Down
7 changes: 7 additions & 0 deletions page_analyzer/templates/urls.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@
{{ item.name }}
</a>
</td>
{% if item.url_checks %}
{% for url_checks in item.url_checks %}
<td>{{ url_checks.created_at }}</td>
<td>{{ url_checks.status_code }}</td>
{% endfor %}
{% else %}
<td></td>
<td></td>
{% endif %}
</tr>
{% endfor %}
{% endif %}
Expand Down
2 changes: 2 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def test_post_url_check(client):

response = client.post('/urls/1/checks', follow_redirects=True)
assert '6' in response.text
assert '200' in response.text

response = client.post('/urls/2/checks', follow_redirects=True)
assert '7' in response.text
assert '200' in response.text

0 comments on commit 1cb5e21

Please sign in to comment.