Exemplo n.º 1
0
def test_search_by_tag(client, api_urls):
    pet1 = Pet.objects.create(name='smolboye', tag='pupper')
    pet2 = Pet.objects.create(name='longboye', tag='doggo')
    assert len(get_data_from_response(client.get('/api/pets'))) == 2
    assert len(get_data_from_response(client.get('/api/pets', {'tags': 'pupper'}))) == 1
    assert len(get_data_from_response(client.get('/api/pets', {'tags': 'daggo'}))) == 0
    assert len(get_data_from_response(client.get('/api/pets', {'tags': 'doggo'}))) == 1
    assert len(get_data_from_response(client.get('/api/pets', {'tags': 'pupper,doggo'}))) == 2
Exemplo n.º 2
0
def test_post_pet(client, api_urls, with_tag):
    payload = {
        'name': get_random_string(15),
    }
    if with_tag:
        payload['tag'] = get_random_string(15)

    pet = get_data_from_response(
        client.post('/api/pets',
                    json.dumps(payload),
                    content_type='application/json'))
    assert pet['name'] == payload['name']
    assert pet['id']
    if with_tag:
        assert pet['tag'] == payload['tag']

    # Test we can get the pet from the API now
    assert get_data_from_response(client.get('/api/pets')) == [pet]
    assert get_data_from_response(client.get(f"/api/pets/{pet['id']}")) == pet
Exemplo n.º 3
0
def test_update_pet(client, api_urls):
    pet1 = Pet.objects.create(name='henlo')
    payload = {'name': 'worl', 'tag': 'bunner'}
    resp = client.patch(f'/api/pets/{pet1.id}',
                        json.dumps(payload),
                        content_type='application/json')
    assert resp.status_code == 200

    pet_data = get_data_from_response(client.get('/api/pets'))[0]
    assert pet_data['name'] == 'worl'
    assert pet_data['tag'] == 'bunner'
Exemplo n.º 4
0
def test_optional_trailing_slash(client, api_urls):
    assert get_data_from_response(client.get('/api/pets/')) == []
Exemplo n.º 5
0
def test_get_empty_list(client, api_urls):
    assert get_data_from_response(client.get('/api/pets')) == []
Exemplo n.º 6
0
def test_delete_pet(client, api_urls):
    pet1 = Pet.objects.create(name='henlo')
    pet2 = Pet.objects.create(name='worl')
    assert len(get_data_from_response(client.get('/api/pets'))) == 2
    client.delete(f'/api/pets/{pet1.id}')
    assert len(get_data_from_response(client.get('/api/pets'))) == 1