Esempio n. 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)
Esempio n. 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)
Esempio n. 3
0
def test_create_province(db: Session) -> None:
    created = insert_province(db)
    province_created = province_crud.get_by_id(db, created.id)
    assert created.id == province_created.id
    assert created.name == province_created.name
    assert created.region == province_created.region

    crud.remove(db, id=created.country_id)
    province_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)
Esempio n. 5
0
def test_delete_province(db: Session) -> None:
    created = insert_province(db)
    province_from_db = province_crud.get_by_id(db, created.id)
    assert province_from_db
    deleted = province_crud.remove(db, id=created.id)
    province_from_db = province_crud.get_by_id(db, created.id)
    assert province_from_db is None
    assert deleted.id == created.id

    crud.remove(db, id=created.country_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)
Esempio n. 7
0
def test_list_all_provinces(db: Session) -> None:
    province_count = province_crud.count(db)
    provinces = province_crud.filter(db)
    assert len(provinces) == province_count

    created = insert_province(db)
    countries = province_crud.filter(db)
    assert len(countries) == province_count + 1

    crud.remove(db, id=created.country_id)
    province_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)
Esempio n. 9
0
def test_update_province(db: Session) -> None:
    created = insert_province(db)
    province_from_db = province_crud.get_by_id(db, created.id)
    province_update = ProvinceUpdate(name="Updated")
    updated_province = province_crud.update(db,
                                            db_object=province_from_db,
                                            object_to_update=province_update)
    province_from_db = province_crud.get_by_id(db, created.id)
    assert province_from_db.id == updated_province.id
    assert province_from_db.name == "Updated"

    crud.remove(db, id=created.country_id)
    province_crud.remove(db, id=created.id)
Esempio n. 10
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_new_country(db: Session) -> None:
    country_data = create_random_country_data()
    response = client.post('/api/v1/country/', json=country_data)

    assert response.status_code == 200

    created_country = response.json()
    country_id = created_country.get("id")

    country_from_db = crud.get_by_id(db, country_id)

    assert country_from_db
    assert country_from_db.name == country_data['name']
    crud.remove(db, id=country_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)
Esempio n. 13
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
Esempio n. 14
0
def delete_country(
        *,
        db: Session = Depends(get_db),
        country_id: int,
) -> Any:
    country = crud.get_by_id(db=db, id=country_id)
    if not country:
        raise HTTPException(status_code=404, detail="Country not found")
    country = crud.remove(db=db, id=country_id)
    return country