def test_serialize_tag(post_factory, tag_factory, tag_category_factory): tag = tag_factory(names=['tag1', 'tag2'], category=tag_category_factory(name='cat')) tag.tag_id = 1 tag.description = 'description' tag.suggestions = [ tag_factory(names=['sug1']), tag_factory(names=['sug2']) ] tag.implications = [ tag_factory(names=['impl1']), tag_factory(names=['impl2']) ] tag.last_edit_time = datetime(1998, 1, 1) post1 = post_factory() post2 = post_factory() post1.tags = [tag] post2.tags = [tag] db.session.add_all([tag, post1, post2]) db.session.flush() result = tags.serialize_tag(tag) result['suggestions'].sort() result['implications'].sort() assert result == { 'names': ['tag1', 'tag2'], 'version': 1, 'category': 'cat', 'creationTime': datetime(1996, 1, 1, 0, 0), 'lastEditTime': datetime(1998, 1, 1, 0, 0), 'description': 'description', 'suggestions': ['sug1', 'sug2'], 'implications': ['impl1', 'impl2'], 'usages': 2, }
def test_serialize_tag(post_factory, tag_factory, tag_category_factory): tag = tag_factory( names=['tag1', 'tag2'], category=tag_category_factory(name='cat')) tag.tag_id = 1 tag.description = 'description' tag.suggestions = [ tag_factory(names=['sug1']), tag_factory(names=['sug2'])] tag.implications = [ tag_factory(names=['impl1']), tag_factory(names=['impl2'])] tag.last_edit_time = datetime(1998, 1, 1) post1 = post_factory() post2 = post_factory() post1.tags = [tag] post2.tags = [tag] db.session.add_all([tag, post1, post2]) db.session.flush() result = tags.serialize_tag(tag) result['suggestions'].sort() result['implications'].sort() assert result == { 'names': ['tag1', 'tag2'], 'version': 1, 'category': 'cat', 'creationTime': datetime(1996, 1, 1, 0, 0), 'lastEditTime': datetime(1998, 1, 1, 0, 0), 'description': 'description', 'suggestions': ['sug1', 'sug2'], 'implications': ['impl1', 'impl2'], 'usages': 2, }
def get(self, ctx, tag_name): auth.verify_privilege(ctx.user, 'tags:view') tag = tags.get_tag_by_name(tag_name) result = tags.get_tag_siblings(tag) serialized_siblings = [] for sibling, occurrences in result: serialized_siblings.append({ 'tag': tags.serialize_tag(sibling), 'occurrences': occurrences }) return {'siblings': serialized_siblings}
def _serialize(ctx, tag): return tags.serialize_tag( tag, options=util.get_serialization_options(ctx))
def test_serialize_tag_when_empty(): assert tags.serialize_tag(None, None) is None
def _serialize(ctx: rest.Context, tag: model.Tag) -> rest.Response: return tags.serialize_tag( tag, options=serialization.get_serialization_options(ctx))
def test_serialize_tag(post_factory, tag_factory, tag_category_factory): cat = tag_category_factory(name="cat") tag = tag_factory(names=["tag1", "tag2"], category=cat) # tag.tag_id = 1 tag.description = "description" tag.suggestions = [ tag_factory(names=["sug1"], category=cat), tag_factory(names=["sug2"], category=cat), ] tag.implications = [ tag_factory(names=["impl1"], category=cat), tag_factory(names=["impl2"], category=cat), ] tag.last_edit_time = datetime(1998, 1, 1) post1 = post_factory() post1.tags = [tag] post2 = post_factory() post2.tags = [tag] db.session.add_all([tag, post1, post2]) db.session.flush() result = tags.serialize_tag(tag) result["suggestions"].sort(key=lambda relation: relation["names"][0]) result["implications"].sort(key=lambda relation: relation["names"][0]) assert result == { "names": ["tag1", "tag2"], "version": 1, "category": "cat", "creationTime": datetime(1996, 1, 1, 0, 0), "lastEditTime": datetime(1998, 1, 1, 0, 0), "description": "description", "suggestions": [ { "names": ["sug1"], "category": "cat", "usages": 0 }, { "names": ["sug2"], "category": "cat", "usages": 0 }, ], "implications": [ { "names": ["impl1"], "category": "cat", "usages": 0 }, { "names": ["impl2"], "category": "cat", "usages": 0 }, ], "usages": 2, }