Skip to content

Commit

Permalink
adding test for 404
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Oct 7, 2023
1 parent 28dff1a commit 2981a33
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 8 deletions.
22 changes: 14 additions & 8 deletions rogue_scholar_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
load_dotenv()
supabase_url: str = os.environ.get("SUPABASE_URL")
supabase_key: str = os.environ.get("SUPABASE_ANON_KEY")
supabase: SupabaseClient = create_client(supabase_url=supabase_url, supabase_key=supabase_key)
supabase: SupabaseClient = create_client(
supabase_url=supabase_url, supabase_key=supabase_key
)
blogsSelect = "slug, title, description, language, favicon, feed_url, feed_format, home_page_url, generator, category"
blogWithPostsSelect = "slug, title, description, language, favicon, feed_url, current_feed_url, archive_prefix, feed_format, home_page_url, use_mastodon, created_at, modified_at, license, generator, category, backlog, prefix, status, plan, funding, items: posts (id, doi, url, archive_url, title, summary, published_at, updated_at, indexed_at, authors, image, tags, language, reference)"
postsSelect = "id, doi, url, archive_url, title, summary, published_at, updated_at, indexed_at, authors, image, tags, language, reference, relationships, blog_name, blog_slug"
Expand Down Expand Up @@ -130,13 +132,6 @@ async def post(slug, suffix=None):
return jsonify(response.data)
elif slug in prefixes and suffix:
doi = f"https://doi.org/{slug}/{suffix}"
response = (
supabase.table("posts")
.select(postsWithContentSelect)
.eq("doi", doi)
.single()
.execute()
)
if format_ in formats:
content_types = {
"bibtex": "application/x-bibtex",
Expand Down Expand Up @@ -167,6 +162,17 @@ async def post(slug, suffix=None):
},
)
else:
try:
response = (
supabase.table("posts")
.select(postsWithContentSelect)
.eq("doi", doi)
.single()
.execute()
)
except Exception as e:
print(e)
return {"error": "Post not found"}, 404
return jsonify(response.data)
else:
# Check if slug is a valid UUID
Expand Down
64 changes: 64 additions & 0 deletions tests/cassettes/test-routes/test_post_not_found.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
interactions:
- request:
body: '{}'
headers:
accept:
- application/vnd.pgrst.object+json
accept-encoding:
- gzip, deflate
accept-profile:
- public
connection:
- keep-alive
content-length:
- '2'
content-profile:
- public
content-type:
- application/json
host:
- db.rogue-scholar.org
user-agent:
- python-httpx/0.24.1
x-client-info:
- supabase-py/1.2.0
method: GET
uri: https://db.rogue-scholar.org/rest/v1/posts?select=id%2C%20doi%2C%20url%2C%20archive_url%2C%20title%2C%20summary%2C%20content_html%2C%20published_at%2C%20updated_at%2C%20indexed_at%2C%20authors%2C%20image%2C%20tags%2C%20language%2C%20reference%2C%20relationships%2C%20blog_name%2C%20blog_slug%2C%20blog%3A%20blogs%21inner%28%2A%29&doi=eq.https%3A//doi.org/10.59350/sfzv4-xdb69
response:
content: '{"code":"PGRST116","details":"Results contain 0 rows, application/vnd.pgrst.object+json
requires 1 row","hint":null,"message":"JSON object requested, multiple (or no)
rows returned"}'
headers:
Access-Control-Allow-Origin:
- '*'
CF-Cache-Status:
- DYNAMIC
CF-Ray:
- 8129904aaa472181-DUS
Connection:
- keep-alive
Content-Type:
- application/vnd.pgrst.object+json; charset=utf-8
Date:
- Sat, 07 Oct 2023 22:26:04 GMT
Server:
- cloudflare
Strict-Transport-Security:
- max-age=2592000; includeSubDomains
Transfer-Encoding:
- chunked
Vary:
- Accept-Encoding
Via:
- kong/2.8.1
X-Kong-Proxy-Latency:
- '0'
X-Kong-Upstream-Latency:
- '5'
alt-svc:
- h3=":443"; ma=86400
sb-gateway-version:
- '1'
http_version: HTTP/1.1
status_code: 406
version: 1
10 changes: 10 additions & 0 deletions tests/test-routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ async def test_post_as_citation():
== "Fernández, N. (2023). ¿Qué libros científicos publicamos? https://doi.org/10.59350/sfzv4-xdb68"
)


@pytest.mark.vcr
async def test_post_as_citation_with_style():
test_client = app.test_client()
Expand All @@ -166,3 +167,12 @@ async def test_post_as_citation_with_locale():
result
== "1. Fernández N. ¿Qué libros científicos publicamos? 6 de octubre de 2023; Disponible en: http://dx.doi.org/10.59350/sfzv4-xdb68"
)


@pytest.mark.vcr
async def test_post_not_found():
test_client = app.test_client()
response = await test_client.get("/posts/10.59350/sfzv4-xdb69")
assert response.status_code == 404
result = await response.get_json()
assert result == {"error": "Post not found"}

0 comments on commit 2981a33

Please sign in to comment.