示例#1
0
def test_create_country(db: Session) -> None:
    country_create = create_random_country()
    created = crud.create(db, country_create)
    country_created = crud.get_by_id(db, created.id)
    assert created.id == country_created.id
    assert created.code == country_created.code
    assert created.language == country_created.language
    crud.remove(db, id=created.id)
示例#2
0
def test_list_all_countries(db: Session) -> None:
    country_count = crud.count(db)
    countries = crud.filter(db)
    assert len(countries) == country_count
    new_country = create_random_country()
    created = crud.create(db, new_country)
    countries = crud.filter(db)
    assert len(countries) == country_count + 1
    crud.remove(db, id=created.id)
def test_GET_existing_country(db: Session) -> None:
    country_create = create_random_country()
    created = crud.create(db, country_create)

    response = client.get(f'/api/v1/country/{created.id}')
    country_from_api = response.json()
    assert response.status_code == 200
    assert country_from_api['name'] == country_create.name
    crud.remove(db, id=created.id)
示例#4
0
def test_delete_country(db: Session) -> None:
    country_create = create_random_country()
    created = crud.create(db, country_create)

    country_from_db = crud.get_by_id(db, created.id)
    assert country_from_db
    deleted = crud.remove(db, id=created.id)
    country_from_db = crud.get_by_id(db, created.id)
    assert country_from_db is None
    assert deleted.id == created.id
def test_PUT_existing_country(db: Session) -> None:
    country_create = create_random_country()
    created = crud.create(db, country_create)

    country_data = {'name': 'Changed'}

    response = client.put(f'/api/v1/country/{created.id}', json=country_data)
    country_from_api = response.json()
    assert response.status_code == 200
    assert country_from_api['name'] == 'Changed'
    crud.remove(db, id=created.id)
def test_GET_countries(db: Session) -> None:
    country_count = crud.count(db)
    response = client.get('/api/v1/country/')
    assert response.status_code == 200
    assert len(response.json()) == country_count
    new_country = create_random_country()
    created = crud.create(db, object_to_create=new_country)

    response = client.get('/api/v1/country/')
    assert response.status_code == 200
    assert len(response.json()) == country_count + 1
    crud.remove(db, id=created.id)
示例#7
0
def test_update_country(db: Session) -> None:
    country_create = create_random_country()
    created = crud.create(db, country_create)

    country_from_db = crud.get_by_id(db, created.id)
    country_update = CountryUpdate(currency="USD")
    updated_country = crud.update(db,
                                  db_object=country_from_db,
                                  object_to_update=country_update)
    country_from_db = crud.get_by_id(db, created.id)
    assert country_from_db.id == updated_country.id
    assert country_from_db.currency == "USD"

    crud.remove(db, id=created.id)
def test_POST_existing_country_code(db: Session) -> None:
    country_create = create_random_country()
    created = crud.create(db, country_create)

    country_data = {
        'name': random_upper_string(),
        'code': country_create.code,
        'language': country_create.language,
        'currency': country_create.currency,
    }
    response = client.post('/api/v1/country/', json=country_data)

    created_country = response.json()
    assert response.status_code == 400
    assert "_id" not in created_country
    crud.remove(db, id=created.id)
async def post_country(*,
                       db: Session = Depends(get_db),
                       country: CountryCreate) -> Any:
    country_by_name = crud.get_by_name(db, name=country.name)
    if country_by_name:
        raise HTTPException(
            status_code=400,
            detail=f"The country with name '{country.name}' already exists",
        )
    country_by_code = crud.get_by_code(db, code=country.code)
    if country_by_code:
        raise HTTPException(
            status_code=400,
            detail=f"The country with code '{country.code}' already exists",
        )
    created = crud.create(db, object_to_create=country)
    return created
def insert_country(db: Session):
    country_create = create_random_country()
    return crud.create(db, country_create)