def test_get_collection_startswith(client, mock_thing, db_session):
    # Create and save two more things with the same name as the mock.
    ThingFactory.create(name=mock_thing.name,
                        created=str(datetime.datetime.now()))
    ThingFactory.create(name=mock_thing.name,
                        created=str(datetime.datetime.now()))

    startswith_data = {"meta": {"args": {"name": "mock"}}}

    res = client.post("/thing/startswith", json={"meta": startswith_data})
    assert res.status_code == 200

    result = res.get_json()
    assert result["meta"]["count"] == 3
def test_get_collection_filter_by_name_for_partial_match_returs_no_results(
        client, mock_thing, db_session):
    # Create and save two more things with the same name as the mock.
    ThingFactory.create(name=mock_thing.name,
                        created=str(datetime.datetime.now()))
    ThingFactory.create(name=mock_thing.name,
                        created=str(datetime.datetime.now()))

    name = "mock_"
    res = client.get(f"/thing/?filter[name]={name}")
    assert res.status_code == 200

    result = res.get_json()
    assert result["meta"]["count"] == 0
def test_get_collection_filtered_by_name_exact_match(client, mock_thing,
                                                     db_session):
    # Create and save two more things with the same name as the mock.
    ThingFactory.create(name=mock_thing.name,
                        created=str(datetime.datetime.now()))
    ThingFactory.create(name=mock_thing.name,
                        created=str(datetime.datetime.now()))

    res = client.get(f"/thing/?filter[name]={mock_thing.name}")
    assert res.status_code == 200

    result = res.get_json()
    assert result["meta"]["count"] == 3
    for el in result["data"]:
        assert el["attributes"]["name"] == mock_thing.name
def test_get_collection_filter_by_id(client, mock_thing, db_session):
    # Create and save two more things with the same name as the mock but different ids.
    ThingFactory.create(name=mock_thing.name,
                        created=str(datetime.datetime.now()),
                        id="mock_id1")
    ThingFactory.create(name=mock_thing.name,
                        created=str(datetime.datetime.now()),
                        id="mock_id2")

    res = client.get(f"/thing/?filter[id]={mock_thing.id}")
    assert res.status_code == 200

    result = res.get_json()
    assert result["meta"]["count"] == 1
    # no need for uniqueness check because id is primary key.
    assert result["data"][0]["id"] == mock_thing.id
示例#5
0
def mock_thing(db_session):
    thing = ThingFactory.create(name="mock_thing", created=str(datetime.datetime.now()), description="mock_description")

    yield thing