def delete_article_by_id(article_id: str) -> bool:

    collection = Article.collection()
    article_id = ensure_object_id(article_id)

    resp = collection.delete_one(dict(_id=article_id))

    return resp.deleted_count == 1
def create_article(article_input: ArticleInput) -> Article:

    collection = Article.collection()
    document = article_input.dict()

    resp = collection.insert_one(document)
    inserted_id = resp.inserted_id

    document["_id"] = inserted_id

    return Article.from_mongodb(document)
def get_article_by_id(article_id: Union[str, ObjectId]) -> Optional[Article]:

    article_id = ensure_object_id(article_id)

    collection = Article.collection()
    found = collection.find_one(article_id)

    if not found:
        return None

    return Article.from_mongodb(found)
def update_article_by_id(article_id: Union[str, ObjectId],
                         article_args: ArticleInput) -> Optional[Article]:

    collection = Article.collection()
    article_id = ensure_object_id(article_id)

    resp = collection.update_one(dict(_id=article_id),
                                 {"$set": article_args.dict()})

    if resp.modified_count == 1:
        return Article(id=str(article_id), **article_args.dict())
    else:
        return None
Esempio n. 5
0
def test_delete_article_exist(client, mock_article_gen):

    collection = Article.collection()

    article1 = mock_article_gen(title="First")
    article2 = mock_article_gen(title="Second")

    assert collection.count_documents({}) == 2

    resp = client.delete(f"/article/{article1.id}")

    print(resp.json())
    assert resp.status_code == 200
    assert collection.count_documents({}) == 1
Esempio n. 6
0
def test_delete_article_doesnt_exist(client, mock_article_gen):

    collection = Article.collection()

    non_existent_id = ObjectId()
    article1 = mock_article_gen(title="First")
    article2 = mock_article_gen(title="Second")

    assert collection.count_documents({}) == 2

    resp = client.delete(f"/article/{non_existent_id}")

    assert resp.status_code == 404
    assert collection.count_documents({}) == 2
Esempio n. 7
0
def test_get_all_article(client, mock_article_gen):

    article_quantity = 4
    collection = Article.collection()

    for i in range(article_quantity):
        mock_article_gen(title=f"Article n{i}")

    assert collection.count_documents({}) == article_quantity

    resp = client.get("/article/")

    assert resp.status_code == 200
    body = resp.json()

    assert len(body) == article_quantity
def test_put_article_doesnt_exist(client, mock_article):

    non_existent_id = str(ObjectId())
    collection = Article.collection()
    assert collection.count_documents({}) == 1

    args = {"title": "My New Title", "content": "different now"}
    resp = client.put(f"/article/{non_existent_id}", json=args)

    assert resp.status_code == 404

    from_db = get_article_by_id(mock_article.id)

    assert from_db.title == mock_article.title
    assert from_db.content == mock_article.content
    assert collection.count_documents({}) == 1
def test_create_article_default_date(client):

    collection = Article.collection()
    assert collection.count_documents({}) == 0

    args = {
        "title": "My Article",
        "content": "this is a pytest"
    }

    resp = client.post("/article/", json=args)

    assert resp.status_code == 201
    assert collection.count_documents({}) == 1
    from_db = collection.find_one()

    assert from_db["published_at"] is not None
def test_put_article_exist(client, mock_article):

    collection = Article.collection()
    assert collection.count_documents({}) == 1

    article = mock_article
    args = {"title": "My New Title", "content": "different now"}

    resp = client.put(f"/article/{article.id}", json=args)

    assert resp.status_code == 200

    from_db = get_article_by_id(article.id)

    assert from_db.title == "My New Title"
    assert from_db.content == "different now"
    assert collection.count_documents({}) == 1
def test_mock_article(mock_article_gen):

    collection = Article.collection()
    assert collection.count_documents({}) == 0

    title = "mytitle"
    content = "mycontent"
    published_at = datetime.utcnow()

    article = mock_article_gen(title=title, content=content, published_at=published_at)

    assert collection.count_documents({}) == 1
    assert article.id is not None

    from_db = collection.find_one(ObjectId(article.id))

    assert from_db is not None
    assert article.id == str(from_db["_id"])
    assert article.title == str(from_db["title"])
    assert article.content == str(from_db["content"])
def test_create_article_valid(client):

    collection = Article.collection()
    assert collection.count_documents({}) == 0

    now = datetime.utcnow().replace(microsecond=0)
    args = {
        "title": "My Article",
        "content": "this is a pytest",
        "published_at": now.isoformat()
    }

    resp = client.post("/article/", json=args)

    assert resp.status_code == 201
    assert collection.count_documents({}) == 1
    from_db = collection.find_one()

    assert from_db["_id"] is not None
    assert from_db["title"] == args["title"]
    assert from_db["content"] == args["content"]
def get_all_articles() -> List[Article]:

    collection = Article.collection()
    all_articles = collection.find({})

    return list(map(Article.from_mongodb, all_articles))