コード例 #1
0
def test_list_all_subcategories(db: Session) -> None:
    subcategory_count = crud.subcategory.count(db)
    subcategories = crud.subcategory.filter(db)
    assert len(subcategories) == subcategory_count
    created = insert_subcategory(db)
    subcategories = crud.subcategory.filter(db)
    assert len(subcategories) == subcategory_count + 1
    delete_subcategory(db, created)
コード例 #2
0
def test_GET_existing_subcategory(db: Session) -> None:
    created = insert_subcategory(db)

    response = client.get(f'/api/v1/subcategory/{created.id}')
    subcategory_from_api = response.json()
    assert response.status_code == 200
    assert subcategory_from_api['name'] == created.name

    delete_subcategory(db, created)
コード例 #3
0
def test_update_subcategory(db: Session) -> None:
    created = insert_subcategory(db)
    subcategory_from_db = crud.subcategory.get_by_id(db, created.id)
    subcategory_update = SubcategoryUpdate(name="Changed")
    updated_subcategory = crud.subcategory.update(
        db, db_object=subcategory_from_db, object_to_update=subcategory_update)
    subcategory_from_db = crud.subcategory.get_by_id(db, created.id)
    assert subcategory_from_db.id == updated_subcategory.id
    assert subcategory_from_db.name == "Changed"
    delete_subcategory(db, created)
コード例 #4
0
def test_PUT_existing_subcategory(db: Session) -> None:
    created = insert_subcategory(db)

    subcategory_data = {'name': 'Changed'}

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

    delete_subcategory(db, created)
コード例 #5
0
def test_GET_subcategory(db: Session) -> None:
    subcategory_count = crud.subcategory.count(db)
    response = client.get('/api/v1/subcategory/')
    assert response.status_code == 200
    assert len(response.json()) == subcategory_count
    created = insert_subcategory(db)

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

    delete_subcategory(db, created)
コード例 #6
0
def test_POST_existing_subcategory_name(db: Session) -> None:
    created = insert_subcategory(db)

    subcategory_data = {
        'name': created.name,
        'description': random_upper_string(),
        'category_id': 1
    }
    response = client.post('/api/v1/subcategory/', json=subcategory_data)

    created_subcategory = response.json()
    assert response.status_code == 400
    assert "_id" not in created_subcategory

    delete_subcategory(db, created)
コード例 #7
0
def test_POST_new_subcategory(db: Session) -> None:
    subcategory_data = create_random_subcategory_data()
    response = client.post('/api/v1/subcategory/', json=subcategory_data)

    assert response.status_code == 200

    created_subcategory = response.json()
    subcategory_id = created_subcategory.get("id")

    subcategory_from_db = crud.subcategory.get_by_id(db, subcategory_id)

    assert subcategory_from_db
    assert subcategory_from_db.name == subcategory_data['name']

    delete_subcategory(db, subcategory_from_db)
コード例 #8
0
def test_create_subcategory(db: Session) -> None:
    created = insert_subcategory(db)
    subcategory_created = crud.subcategory.get_by_id(db, created.id)
    assert created.id == subcategory_created.id
    assert created.description == subcategory_created.description
    delete_subcategory(db, created)