def test_api_update_course(api_client, course_factory):

    # arrange
    courses1 = course_factory(name='Pytest for dummies 1')
    courses2 = course_factory(name='C# for advanced levels prof')
    courses3 = course_factory(name='C++ for dummies 3')

    url = reverse("courses-detail", args=(courses2.id, ))
    product_payload = {
        "name": "C# only for very advanced levels profi!",
    }

    # act
    # Передаем POST-запрос
    resp = api_client.put(url, product_payload)

    #assert
    assert resp.status_code == HTTP_200_OK
    assert resp.status_text == 'OK'

    resp_json = resp.json()
    # если данные есть
    if resp_json != []:
        st_t = resp_json["name"]
        assert st_t == "C# only for very advanced levels profi!"
def test_api_delete_course(api_client, course_factory):

    # arrange
    courses1 = course_factory(name='Pytest for dummies 1')
    courses2 = course_factory(name='C# for advanced levels prof')
    courses3 = course_factory(name='C++ for dummies 3')

    url = reverse("courses-detail", args=(courses2.id, ))

    # act
    # Передаем POST-запрос
    resp = api_client.delete(url)

    #assert
    assert resp.status_code == HTTP_204_NO_CONTENT
    assert resp.status_text == 'No Content'
def test_api_get_course_filter_name(api_client, course_factory):

    # arrange
    # класс для создания запросов, имитирующих работу API приложения
    #client = APIClient()
    #GET - retrieve
    #DELETE - destroy
    #создаем три сущности
    course_factory(name='Pytest for dummies 1')
    course_factory(name='C# for advanced levels prof')
    course_factory(name='C++ for dummies 3')

    url = reverse("courses-list")

    # act
    # Передаем GET-запрос
    resp = api_client.get(url, {"name": ['C# for advanced levels prof']})

    #assert
    assert resp.status_code == HTTP_200_OK
    resp_json = resp.json()
    #если данные есть
    if resp_json:
        #проверяем список:
        for check_name in resp_json:
            assert check_name["name"] == "C# for advanced levels prof"
def test_api_get_course_filter_id(api_client, course_factory):

    # arrange
    # класс для создания запросов, имитирующих работу API приложения
    #client = APIClient()
    #GET - retrieve
    #DELETE - destroy
    #создаем три сущности
    course1 = course_factory(name='Pytest for dummies 1')
    course2 = course_factory(name='Pytest for dummies 2')
    course3 = course_factory(name='Pytest for dummies 3')

    url = reverse("courses-list")

    # act
    # Передаем GET-запрос
    resp = api_client.get(url, {"id": [course1.id, course3.id]})

    #assert
    assert resp.status_code == HTTP_200_OK
    resp_json = resp.json()
    #если данные есть
    if resp_json != []:
        assert len(resp_json) == 2
def test_api_get_course_num(api_client, course_factory):

    # arrange
    #GET - retrieve
    #DELETE - destroy
    #создаем сущности
    courses = course_factory(name='Pytest for dummies')
    #формируем url вида: '/api/v1/courses/1/'
    url = reverse("courses-detail", args=(courses.id, ))

    # act
    resp = api_client.get(url)

    #assert
    assert resp.status_code == HTTP_200_OK
    resp_json = resp.json()
    #если данные есть
    if resp_json != []:
        st_t = resp_json["name"]
        assert st_t == "Pytest for dummies"
def test_api_get_courses(api_client, course_factory):

    # arrange
    # класс для создания запросов, имитирующих работу API приложения
    #client = APIClient()
    #GET - list
    #POST - create
    course_factory(name='Pytest for dummies')
    course_factory(name='Pytest for dummies 2')
    course_factory(name='Pytest for dummies 3')

    url = reverse("courses-list")

    # act
    resp = api_client.get(url)

    #assert
    assert resp.status_code == HTTP_200_OK
    resp_json = resp.json()
    #если данные есть
    if resp_json:  #так небезопасно if resp_json != []:
        num_json = len(resp_json)
        assert num_json == 3