Skip to content

Commit

Permalink
fix(subject): None value in tag name (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarvie8176 authored Dec 25, 2021
1 parent f1d247a commit 9591ea4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
11 changes: 5 additions & 6 deletions pol/curd/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,11 @@ def tags(self):
# defaults to utf-8
tags_deserialized = dict_to_list(loads(self.tags_serialized.encode()))

return list(
map(
lambda tag: {"name": tag["tag_name"], "count": tag["result"]},
tags_deserialized,
)
)
return [
{"name": tag["tag_name"], "count": tag["result"]}
for tag in tags_deserialized
if tag["tag_name"] is not None # remove tags like { "tag_name": None }
]


async def get_one(db: Database, *where) -> Subject:
Expand Down
1 change: 1 addition & 0 deletions tests/app/api_v0/fixtures/subject_2585_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a:30:{i:0;a:2:{s:8:"tag_name";s:6:"炮姐";s:6:"result";s:4:"1956";}i:1;a:2:{s:8:"tag_name";s:12:"超电磁炮";s:6:"result";s:4:"1756";}i:2;a:2:{s:8:"tag_name";s:9:"J.C.STAFF";s:6:"result";s:4:"1746";}i:3;a:2:{s:8:"tag_name";s:12:"御坂美琴";s:6:"result";s:4:"1367";}i:4;a:2:{s:8:"tag_name";s:6:"百合";s:6:"result";s:4:"1240";}i:5;a:2:{s:8:"tag_name";s:12:"2009年10月";s:6:"result";s:3:"917";}i:6;a:2:{s:8:"tag_name";s:8:"bilibili";s:6:"result";s:3:"795";}i:7;a:2:{s:8:"tag_name";s:2:"TV";s:6:"result";s:3:"709";}i:8;a:2:{s:8:"tag_name";s:6:"黑子";s:6:"result";s:3:"702";}i:9;a:2:{s:8:"tag_name";s:18:"科学超电磁炮";s:6:"result";s:3:"621";}i:10;a:2:{s:8:"tag_name";s:18:"魔法禁书目录";s:6:"result";s:3:"518";}i:11;a:2:{s:8:"tag_name";s:4:"2009";s:6:"result";s:3:"409";}i:12;a:2:{s:8:"tag_name";s:9:"漫画改";s:6:"result";s:3:"288";}i:13;a:2:{s:8:"tag_name";s:9:"傲娇娘";s:6:"result";s:3:"280";}i:14;a:2:{s:8:"tag_name";s:6:"校园";s:6:"result";s:3:"156";}i:15;a:2:{s:8:"tag_name";s:6:"战斗";s:6:"result";s:3:"144";}i:16;a:2:{s:8:"tag_name";s:12:"长井龙雪";s:6:"result";s:3:"123";}i:17;a:2:{s:8:"tag_name";s:6:"漫改";s:6:"result";s:3:"110";}i:18;a:2:{s:8:"tag_name";s:6:"姐控";s:6:"result";s:3:"107";}i:19;a:2:{s:8:"tag_name";s:12:"轻小说改";s:6:"result";s:2:"93";}i:20;a:2:{s:8:"tag_name";s:6:"科幻";s:6:"result";s:2:"82";}i:21;a:2:{s:8:"tag_name";s:9:"超能力";s:6:"result";s:2:"73";}i:22;a:2:{s:8:"tag_name";s:6:"日常";s:6:"result";s:2:"58";}i:23;a:2:{s:8:"tag_name";s:6:"奇幻";s:6:"result";s:2:"54";}i:24;a:2:{s:8:"tag_name";s:12:"豊崎愛生";s:6:"result";s:2:"53";}i:25;a:2:{s:8:"tag_name";s:12:"長井龍雪";s:6:"result";s:2:"47";}i:26;a:2:{s:8:"tag_name";s:24:"某科学的超电磁炮";s:6:"result";s:2:"47";}i:27;a:2:{s:8:"tag_name";N;s:6:"result";s:2:"42";}i:28;a:2:{s:8:"tag_name";s:12:"佐藤利奈";s:6:"result";s:2:"38";}i:29;a:2:{s:8:"tag_name";s:12:"新井里美";s:6:"result";s:2:"34";}}
58 changes: 57 additions & 1 deletion tests/app/api_v0/test_subject.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from pathlib import Path

import orjson.orjson
from redis import Redis
from sqlalchemy.orm import Session
from starlette.testclient import TestClient

from pol import config
from pol import sa, config
from pol.db.tables import ChiiSubjectField

fixtures_path = Path(__file__).parent.joinpath("fixtures")


def test_subject_not_found(client: TestClient):
Expand Down Expand Up @@ -151,3 +157,53 @@ def test_subject_tags_empty(client: TestClient, mock_subject):
mock_subject(sid)
response = client.get(f"/v0/subjects/{sid}")
assert response.json()["tags"] == []


def test_subject_tags_none(client: TestClient, mock_subject, db_session: Session):
"""
should exclude a tag if name is None.
todo: can count be None too?
"""
sid = 15234524
mock_subject(sid)
field_tags_with_none_val = (
fixtures_path.joinpath("subject_2585_tags.txt").read_bytes().strip()
)
db_session.execute(
sa.update(ChiiSubjectField)
.where(ChiiSubjectField.field_sid == sid)
.values(field_tags=field_tags_with_none_val)
)
db_session.commit()
response = client.get(f"/v0/subjects/{sid}")
assert response.json()["tags"] == [
{"name": "炮姐", "count": 1956},
{"name": "超电磁炮", "count": 1756},
{"name": "J.C.STAFF", "count": 1746},
{"name": "御坂美琴", "count": 1367},
{"name": "百合", "count": 1240},
{"name": "2009年10月", "count": 917},
{"name": "bilibili", "count": 795},
{"name": "TV", "count": 709},
{"name": "黑子", "count": 702},
{"name": "科学超电磁炮", "count": 621},
{"name": "魔法禁书目录", "count": 518},
{"name": "2009", "count": 409},
{"name": "漫画改", "count": 288},
{"name": "傲娇娘", "count": 280},
{"name": "校园", "count": 156},
{"name": "战斗", "count": 144},
{"name": "长井龙雪", "count": 123},
{"name": "漫改", "count": 110},
{"name": "姐控", "count": 107},
{"name": "轻小说改", "count": 93},
{"name": "科幻", "count": 82},
{"name": "超能力", "count": 73},
{"name": "日常", "count": 58},
{"name": "奇幻", "count": 54},
{"name": "豊崎愛生", "count": 53},
{"name": "長井龍雪", "count": 47},
{"name": "某科学的超电磁炮", "count": 47},
{"name": "佐藤利奈", "count": 38},
{"name": "新井里美", "count": 34},
]

0 comments on commit 9591ea4

Please sign in to comment.