예제 #1
0
def test_get_url_by_id_correct() -> None:
    with CustomTestClient() as c:
        url1 = Url(
            id="abc123",
            url="www.test1.org",
            yearmonth="201912",
            undesired_url=True,
        )
        url2 = Url(
            id="qwe987",
            url="www.test2.net",
            yearmonth="200506",
            undesired_url=False,
            payed_content=True,
        )
        db.session.add(url1)
        db.session.add(url2)
        db.session.commit()
        res = c.get('/v1/urls/abc123')
        data = res.get_json()
        assert data["url_id"] == "abc123"
        assert data["url"] == "www.test1.org"
        assert data["yearmonth"] == "201912"
        assert data["undesired_url"] is True
        assert data["payed_content"] is False
        assert res.status_code == 200

        res2 = c.get('/v1/urls/qwe987')
        data = res2.get_json()
        assert data["url_id"] == "qwe987"
        assert data["url"] == "www.test2.net"
        assert data["yearmonth"] == "200506"
        assert data["undesired_url"] is False
        assert data["payed_content"] is True
        assert res.status_code == 200
예제 #2
0
def create_url(dto: CreateUrlDTO) -> None:
    assert_valid_yearmonth(dto.yearmonth)
    url = Url(id=dto.id,
              yearmonth=dto.yearmonth,
              url=dto.url,
              undesired_url=dto.undesired_url)
    url_repo.save(url)
예제 #3
0
def test_get_articles_by_yearmonth_correct() -> None:
    with CustomTestClient() as c:
        url_1 = Url(id="qaz987",
                    url="test",
                    yearmonth="201910",
                    undesired_url=False)
        article_1 = Article(
            id="qaz987",
            headline="This is my headline",
            body="This is a body text to my article in this test.",
            created_at=datetime.utcnow())
        url_2 = Url(id="abc123",
                    url="test2",
                    yearmonth="201911",
                    undesired_url=False)
        article_2 = Article(
            id="abc123",
            headline="This is my headline for another article",
            body="This is a body text to my other article that i want to find",
            created_at=datetime.utcnow())
        url_3 = Url(id="uhn768",
                    url="test3",
                    yearmonth="201910",
                    undesired_url=False)
        article_3 = Article(id="uhn768",
                            headline="This",
                            body="This is a body text.",
                            created_at=datetime.utcnow())
        db.session.add(url_1)
        db.session.add(url_2)
        db.session.add(url_3)
        db.session.add(article_1)
        db.session.add(article_2)
        db.session.add(article_3)
        db.session.commit()
        res = c.get(f'/v1/articles/yearmonth/201910')
        assert res.status_code == 200
        data = res.get_json()
        assert data[0]["article_id"] == "qaz987"
        assert data[0]["headline"] == "This is my headline"
        assert data[0]["body"] == \
            "This is a body text to my article in this test."
        assert data[0]["created_at"] == article_1.created_at.__str__()
        assert data[1]["article_id"] == "uhn768"
        assert data[1]["headline"] == "This"
        assert data[1]["body"] == "This is a body text."
        assert data[1]["created_at"] == article_3.created_at.__str__()
예제 #4
0
def test_get_urls_by_yearmonth() -> None:
    with CustomTestClient() as c:
        url1 = Url(
            id="abc123",
            url="www.test1.org",
            yearmonth="201912",
            undesired_url=True,
            scraped_at=datetime.utcnow()
        )
        url2 = Url(
            id="qwe987",
            url="www.test2.net",
            yearmonth="201912",
            undesired_url=False,
            payed_content=False,
        )
        url3 = Url(
            id="try345",
            url="another.test3.com",
            yearmonth="200503",
            undesired_url=False,
            payed_content=False,
        )
        db.session.add(url1)
        db.session.add(url2)
        db.session.add(url3)
        db.session.commit()
        res = c.get('/v1/urls/yearmonth/201912')
        assert res.status_code == 200
        data = res.get_json()
        assert len(data) == 2
        assert data[0]["url_id"] == "abc123"
        assert data[0]["url"] == "www.test1.org"
        assert data[0]["yearmonth"] == "201912"
        assert data[1]["url_id"] == "qwe987"
        assert data[1]["url"] == "www.test2.net"
        assert data[1]["yearmonth"] == "201912"
예제 #5
0
def test_post_article_correct() -> None:
    with CustomTestClient() as c:
        url = Url(id="abc123",
                  url="test",
                  yearmonth="201910",
                  undesired_url=False)
        db.session.add(url)
        db.session.commit()
        res = c.post('/v1/articles',
                     data=json.dumps({
                         "id": "abc123",
                         "body": "This is my article body text",
                         "headline": "Breaking News!!"
                     }),
                     content_type="application/json")
        assert res.status_code == 200
예제 #6
0
def test_put_urls_unscraped_correct() -> None:
    with CustomTestClient() as c:
        url = Url(
            id="abc123",
            url="www.test1.org",
            yearmonth="201912"
        )
        db.session.add(url)
        db.session.commit()
        assert Url.query.filter_by(id="abc123").first().scraped_at is None
        time_scraped = datetime.utcnow()
        res = c.put(
            '/v1/urls/abc123/unscraped',
            data=json.dumps({
                "scraped_at": time_scraped.__str__(),
                }),
            content_type="application/json"
            )
        assert res.status_code == 200
        record = Url.query.filter_by(id="abc123").first()
        assert record.scraped_at == time_scraped
예제 #7
0
def test_get_article_by_id_correct() -> None:
    with CustomTestClient() as c:
        url = Url(id="qaz987",
                  url="test",
                  yearmonth="201910",
                  undesired_url=False)
        article = Article(
            id="qaz987",
            headline="This is my headline",
            body="This is a body text to my article in this test.",
            created_at=datetime.utcnow())
        db.session.add(url)
        db.session.add(article)
        db.session.commit()
        article_id = "qaz987"
        res = c.get(f'/v1/articles/id/{article_id}')
        assert res.status_code == 200
        data = res.get_json()
        assert data["article_id"] == "qaz987"
        assert data["headline"] == "This is my headline"
        assert data["body"] == \
            "This is a body text to my article in this test."
        assert data["created_at"] == article.created_at.__str__()
예제 #8
0
def flag_url_is_scraped(url: Url, scraped_at: datetime) -> None:
    url.scraped_at = scraped_at
    db.session.commit()