Skip to content

Commit

Permalink
Remove extra function 🔥, Update unit test ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
alivx committed Dec 24, 2020
1 parent c344e46 commit 03f7479
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
26 changes: 3 additions & 23 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,8 @@ def read_root():
print("Backend Home page access.")
return {"message": "Welcome to url shortening app"}


@app.get("/get")
def get_all_urls():
"""Get short code URL from DB
Returns:
str: URL
"""
data = []
print("Getting all keys from redis.")
for key in rds.keys():
data.append({key.decode("utf8"): rds.get(key).decode("utf8")})
return data


@app.get("/{short}")
def redirect_urless(short: str):
async def redirect_urless(short: str):
"""Redirect fetch URL
Args:
Expand All @@ -78,10 +63,8 @@ def redirect_urless(short: str):
print("Redirect request [{0}] to ({1})".format(short, key))
return RedirectResponse(url=key)
except Exception:
print("not Found")

print("Could not find the key.")
return {"message": "URL not defined"}
print(f"Could not find the key {short}.")
return {"message": f"URL ({short}) not found"}


@app.post("/")
Expand All @@ -108,9 +91,6 @@ def urless(item: Item):
return {"url": url, "short": new_name}
else:
return {"message": "failed"}
# TO DO: allow deplicate values
print("URL already exists..")
return {"message": "URL already exists", "short": rds.get(url)}


if __name__ == "__main__":
Expand Down
22 changes: 21 additions & 1 deletion api/test_main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
from fastapi.testclient import TestClient

import uuid
from main import app
import json

client = TestClient(app)
rarendom = uuid.uuid4()
testURL = f"http://localhost/{rarendom}"
data = {"url": testURL, "ttl": -1}
dataJson = json.dumps(data)
short = 0


def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Welcome to url shortening app"}


def test_create_new_short():
global short
response = client.post("/", dataJson)
assert response.status_code == 200
returnData = response.json()
short = str(returnData["short"])
assert len(returnData["short"]) == 6


def test_get_url():
response = client.get(f"/{short}")
assert response.status_code == 200

0 comments on commit 03f7479

Please sign in to comment.