From f8b3e9b286006189905f2a0e752ee38d4530502b Mon Sep 17 00:00:00 2001 From: Areeb Ahmed <135235925+areebahmeddd@users.noreply.github.com> Date: Sun, 9 Mar 2025 04:15:47 +0530 Subject: [PATCH 1/2] fix: update website after changing tag value --- folksonomy/api.py | 10 +++++++++- tests/test_main.py | 24 ++++++++++++++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/folksonomy/api.py b/folksonomy/api.py index 843fe7f..df008d8 100644 --- a/folksonomy/api.py +++ b/folksonomy/api.py @@ -469,7 +469,15 @@ async def product_tag_update(response: Response, detail=re.sub(r'.*@@ (.*) @@\n.*$', r'\1', e.pgerror)[:-1], ) if cur.rowcount == 1: - return "ok" + # Fetch the updated tag value + cur, timing = await db.db_exec( + """ + SELECT * FROM folksonomy WHERE product = %s AND owner = %s AND k = %s + """, + (product_tag.product, product_tag.owner, product_tag.k) + ) + updated_tag = await cur.fetchone() + return JSONResponse(status_code=200, content=dict(zip([desc[0] for desc in cur.description], updated_tag)), headers={"x-pg-timing": timing}) elif cur.rowcount == 0: # non existing key raise HTTPException( status_code=404, diff --git a/tests/test_main.py b/tests/test_main.py index 3a2962b..893fc5e 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -578,16 +578,20 @@ def test_put_invalid(with_sample): @pytest.mark.asyncio async def test_put(with_sample): - headers = {"Authorization": "Bearer foo__Utest-token"} - response = client.put("/product", headers=headers, json={ - "product": BARCODE_1, "k": "color", "v": "purple", "version": 2}) - assert response.status_code == 200, f'valid new version should return 200, got {response.status_code} {response.text}' - await check_tag(BARCODE_1, "color", v="purple", version=2) - # and again - response = client.put("/product", headers=headers, json={ - "product": BARCODE_1, "k": "color", "v": "brown", "version": 3}) - assert response.status_code == 200, f'valid new version should return 200, got {response.status_code} {response.text}' - await check_tag(BARCODE_1, "color", v="brown", version=3) + headers = {"Authorization": "Bearer foo__Utest-token"} + response = client.put("/product", headers=headers, json={ + "product": BARCODE_1, "k": "color", "v": "purple", "version": 2}) + assert response.status_code == 200, f'valid new version should return 200, got {response.status_code} {response.text}' + updated_tag = response.json() + assert updated_tag["v"] == "purple", f'updated value should be "purple", got {updated_tag["v"]}' + await check_tag(BARCODE_1, "color", v="purple", version=2) + # and again + response = client.put("/product", headers=headers, json={ + "product": BARCODE_1, "k": "color", "v": "brown", "version": 3}) + assert response.status_code == 200, f'valid new version should return 200, got {response.status_code} {response.text}' + updated_tag = response.json() + assert updated_tag["v"] == "brown", f'updated value should be "brown", got {updated_tag["v"]}' + await check_tag(BARCODE_1, "color", v="brown", version=3) def test_delete_invalid(with_sample): From d9ed24cd60cb698730ac7c4f1d3efcb2daf4f781 Mon Sep 17 00:00:00 2001 From: Areeb Ahmed <135235925+areebahmeddd@users.noreply.github.com> Date: Sun, 9 Mar 2025 18:46:48 +0530 Subject: [PATCH 2/2] fix failing checks --- folksonomy/api.py | 15 ++++----------- tests/test_main.py | 29 +++++++++++++++-------------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/folksonomy/api.py b/folksonomy/api.py index df008d8..d6b874f 100644 --- a/folksonomy/api.py +++ b/folksonomy/api.py @@ -469,15 +469,8 @@ async def product_tag_update(response: Response, detail=re.sub(r'.*@@ (.*) @@\n.*$', r'\1', e.pgerror)[:-1], ) if cur.rowcount == 1: - # Fetch the updated tag value - cur, timing = await db.db_exec( - """ - SELECT * FROM folksonomy WHERE product = %s AND owner = %s AND k = %s - """, - (product_tag.product, product_tag.owner, product_tag.k) - ) - updated_tag = await cur.fetchone() - return JSONResponse(status_code=200, content=dict(zip([desc[0] for desc in cur.description], updated_tag)), headers={"x-pg-timing": timing}) + # Return the updated tag instead of just "ok" + return product_tag elif cur.rowcount == 0: # non existing key raise HTTPException( status_code=404, @@ -500,7 +493,7 @@ async def product_tag_delete(response: Response, check_owner_user(user, owner, allow_anonymous=False) k, v = sanitize_data(k, None) try: - # Setting version to 0, this is seen as a reset, + # Setting version to 0, this is seen as a reset, # while maintaining history in folksonomy_versions cur, timing = await db.db_exec( """ @@ -637,4 +630,4 @@ async def pong(response: Response): """ cur, timing = await db.db_exec("SELECT current_timestamp AT TIME ZONE 'GMT'",()) pong = await cur.fetchone() - return {"ping": "pong @ %s" % pong[0]} \ No newline at end of file + return {"ping": "pong @ %s" % pong[0]} diff --git a/tests/test_main.py b/tests/test_main.py index 893fc5e..e3b62d7 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -578,20 +578,21 @@ def test_put_invalid(with_sample): @pytest.mark.asyncio async def test_put(with_sample): - headers = {"Authorization": "Bearer foo__Utest-token"} - response = client.put("/product", headers=headers, json={ - "product": BARCODE_1, "k": "color", "v": "purple", "version": 2}) - assert response.status_code == 200, f'valid new version should return 200, got {response.status_code} {response.text}' - updated_tag = response.json() - assert updated_tag["v"] == "purple", f'updated value should be "purple", got {updated_tag["v"]}' - await check_tag(BARCODE_1, "color", v="purple", version=2) - # and again - response = client.put("/product", headers=headers, json={ - "product": BARCODE_1, "k": "color", "v": "brown", "version": 3}) - assert response.status_code == 200, f'valid new version should return 200, got {response.status_code} {response.text}' - updated_tag = response.json() - assert updated_tag["v"] == "brown", f'updated value should be "brown", got {updated_tag["v"]}' - await check_tag(BARCODE_1, "color", v="brown", version=3) + with TestClient(app) as client: + headers = {"Authorization": "Bearer foo__Utest-token"} + response = client.put("/product", headers=headers, json={ + "product": BARCODE_1, "k": "color", "v": "purple", "version": 2}) + assert response.status_code == 200, f'valid new version should return 200, got {response.status_code} {response.text}' + updated_tag = response.json() + assert updated_tag["v"] == "purple", f'updated value should be "purple", got {updated_tag["v"]}' + await check_tag(BARCODE_1, "color", v="purple", version=2) + # and again + response = client.put("/product", headers=headers, json={ + "product": BARCODE_1, "k": "color", "v": "brown", "version": 3}) + assert response.status_code == 200, f'valid new version should return 200, got {response.status_code} {response.text}' + updated_tag = response.json() + assert updated_tag["v"] == "brown", f'updated value should be "brown", got {updated_tag["v"]}' + await check_tag(BARCODE_1, "color", v="brown", version=3) def test_delete_invalid(with_sample):