def test_create_address(db: Session) -> None:
    created = insert_address(db)
    address_created = crud.address.get_by_id(db, created.id)
    assert created.id == address_created.id
    assert created.name == address_created.name

    delete_address(db, created)
def test_DELETE_existing_address(db: Session) -> None:
    created = insert_address(db)

    response = client.delete(f'/api/v1/address/{created.id}')
    address_from_api = response.json()
    assert response.status_code == 200
    assert created.name == address_from_api['name']

    delete_address(db, created)
def test_list_all_addresses(db: Session) -> None:
    address_count = crud.address.count(db)
    addresses = crud.address.filter(db)
    assert len(addresses) == address_count

    created = insert_address(db)
    addresses = crud.address.filter(db)
    assert len(addresses) == address_count + 1

    delete_address(db, created)
def test_PUT_existing_address(db: Session) -> None:
    created = insert_address(db)

    address_data = {'name': 'Changed'}

    response = client.put(f'/api/v1/address/{created.id}', json=address_data)
    address_from_api = response.json()
    assert response.status_code == 200
    assert address_from_api['name'] == 'Changed'

    delete_address(db, created)
def test_update_address(db: Session) -> None:
    created = insert_address(db)
    address_from_db = crud.address.get_by_id(db, created.id)
    address_update = AddressUpdate(name="Updated")
    updated_address = crud.address.update(
        db, db_object=address_from_db, object_to_update=address_update)
    address_from_db = crud.address.get_by_id(db, created.id)
    assert address_from_db.id == updated_address.id
    assert address_from_db.name == "Updated"

    delete_address(db, created)
def test_GET_address(db: Session) -> None:
    address_count = crud.address.count(db)
    response = client.get('/api/v1/address/')
    assert response.status_code == 200
    assert len(response.json()) == address_count
    created = insert_address(db)

    response = client.get('/api/v1/address/')
    assert response.status_code == 200
    assert len(response.json()) == address_count + 1

    delete_address(db, created)
def test_POST_new_address(db: Session) -> None:
    address_data = create_random_address_data()
    response = client.post('/api/v1/address/', json=address_data)

    assert response.status_code == 200

    created_address = response.json()
    address_id = created_address.get("id")

    address_from_db = crud.address.get_by_id(db, address_id)

    assert address_from_db
    assert address_from_db.name == address_data['name']

    delete_address(db, address_from_db)