Exemplo n.º 1
0
    def test_tags(self):
        q = Question.create(
            author_id=self.user._id,
            title='How to patch KDE on FreeBSD?',
            body="subjxxxxxx",
            tags=["kde", "freebsd", "anime"],
        )
        q.save()
        self.run_tasks()

        cnt = 0
        for tag in Tag.find():
            cnt += 1
            self.assertEqual(tag.questions_count, 1)
        self.assertEqual(cnt, len(q.tags))

        q2 = Question.create(
            author_id=self.user._id,
            title='TCPDump on FreeBSD',
            body="Does FreeBSD has a linux-like tcpdump or it's been as usual",
            tags=["freebsd", "tcpdump"],
        )
        q2.save()
        self.run_tasks()

        t = Tag.get("freebsd")
        self.assertEqual(t.questions_count, 2)

        q2.destroy()
        q.destroy()
        self.run_tasks()
        self.assertEqual(Tag.find().count(),
                         4)  # tags are not deleted automatically anymore
Exemplo n.º 2
0
    def test_create_question(self):
        attrs = {
            "title": "a real question",
            "body": "what is the meaning of life and everything",
            "tags": ["guide"]
        }
        resp = self.post(f"/api/v1/questions/", json=attrs)
        self.assertEqual(resp.status_code, 401)

        resp = self.post(f"/api/v1/questions/", json=attrs, user=self.user1)
        self.assertEqual(resp.status_code, 200)
        self.run_tasks()

        data = resp.json
        q = Question.get(data["data"]["_id"])
        self.assertEqual(q.body, attrs["body"])
        self.assertEqual(q.title, attrs["title"])
        self.assertCountEqual(q.tags, attrs["tags"])

        t: Tag = Tag.get("guide")
        self.assertIsNotNone(t)
        self.assertEqual(t.questions_count, 1)
Exemplo n.º 3
0
def unsubscribe(tagname):
    Tag.get(tagname, "tag not found")
    user: User = get_user_from_app_context()
    user.unsubscribe_from_tag(tagname)
    return json_response({"data": user.tag_subscription.to_dict()})
Exemplo n.º 4
0
def show(tagname):
    tag = Tag.get(tagname, "tag not found")
    return json_response({"data": tag.to_dict()})