def test_it_returns_conflict_error_if_new_technology_name_exist(client):
    python_data = {
        'name': 'Python',
        'description': 'Object oriented programming language',
    }
    elixir_data = {
        'name': 'Elixir',
        'description': 'Functional programming language',
    }
    create_resp = client.post(
        '/technologies',
        json=python_data,
        headers=auth_header_with_all_permissions
    )
    create_resp_data = create_resp.get_json()
    client.post(
        '/technologies',
        json=elixir_data,
        headers=auth_header_with_all_permissions
    )
    edited_data = {
        'name': 'Elixir',
    }
    edited_resp = client.patch(
        create_resp_data['href'],
        json=edited_data,
        headers=auth_header_with_all_permissions
    )
    edited_resp_data = edited_resp.get_json()
    assert '409' in edited_resp.status
    assert edited_resp_data['error'] == 409
    assert 'Entity already exists' in edited_resp_data['message']
def test_it_edits_given_authors_data(client):
    data = {
        'name': 'Paweł',
        'surname': 'Niesiołowski',
        'email': '*****@*****.**',
    }
    author_href = client.post(
        '/authors',
        json=data,
        headers=auth_header_with_all_permissions
    ).get_json()['href']
    edited_data = {
        'email': '*****@*****.**',
    }
    resp = client.patch(
        author_href,
        json=edited_data,
        headers=auth_header_with_all_permissions
    )
    authors = Author.query.filter(Author.email == '*****@*****.**').all()
    assert '204' in resp.status
    assert len(authors) == 1
    author = authors[0]
    assert author.name == 'Paweł'
    assert author.surname == 'Niesiołowski'
def test_it_edits_reviewer(client):
    python_id = create_technology_and_return_id(client, 'Python')
    elixir_id = create_technology_and_return_id(client, 'Elixir')
    new_reviewer_data = {
        'name': 'Paweł',
        'surname': 'Niesiołowski',
        'email': '*****@*****.**',
        'description': 'Experienced programmer.',
        'technologies': [python_id],
    }
    reviewer_href = client.post(
        '/reviewers',
        json=new_reviewer_data,
        headers=auth_header_with_all_permissions
    ).get_json()['href']

    edited_reviewer_data = {
        'description': 'Very experienced programmer.',
        'technologies': [python_id, elixir_id],
    }

    resp = client.patch(
        reviewer_href,
        json=edited_reviewer_data,
        headers=auth_header_with_all_permissions
    )
    reviewers = Reviewer.query.filter(
        Reviewer.description == 'Very experienced programmer.'
    ).all()

    assert '204' in resp.status
    assert len(reviewers) == 1
    assert len(reviewers[0].technologies) == 2
def test_it_returns_404_if_edited_author_does_not_exist(client):
    resp = client.patch(
        '/authors/1',
        json=[],
        headers=auth_header_with_all_permissions
    )
    resp_data = resp.get_json()
    assert '404' in resp.status
    assert resp_data['error'] == 404
    assert 'Not found' in resp_data['message']
def test_it_returns_404_if_edited_data_does_not_exist(client):
    data = {
        'name': 'Python',
    }
    resp = client.patch(
        '/technologies/1',
        json=data,
        headers=auth_header_with_all_permissions
    )
    resp_data = resp.get_json()
    assert '404' in resp.status
    assert resp_data['error'] == 404
    assert resp_data['message'] == 'Not found'
def test_it_changes_technologies_for_author(client):
    python_data = {
        'name': 'Python',
        'description': 'Programming language',
    }
    elixir_data = {
        'name': 'Elixir',
        'description': 'Programming language',
    }
    python_resp = client.post(
        '/technologies',
        json=python_data,
        headers=auth_header_with_all_permissions
    ).get_json()
    elixir_resp = client.post(
        '/technologies',
        json=elixir_data,
        headers=auth_header_with_all_permissions
    ).get_json()
    python_technology = client.get(python_resp['href']).get_json()['data']
    elixir_technology = client.get(elixir_resp['href']).get_json()['data']
    data = {
        'name': 'Paweł',
        'surname': 'Niesiołowski',
        'email': '*****@*****.**',
        'technologies': [python_technology['id']],
    }
    author_href = client.post(
        '/authors',
        json=data,
        headers=auth_header_with_all_permissions
    ).get_json()['href']

    edited_data = {
        'technologies': [elixir_technology['id']],
    }
    resp = client.patch(
        author_href,
        json=edited_data,
        headers=auth_header_with_all_permissions
    )
    authors = Author.query.filter(Author.surname == 'Niesiołowski').all()
    assert '204' in resp.status
    assert len(authors) == 1
    assert len(authors[0].technologies) == 1
    assert authors[0].technologies[0].name == 'Elixir'
def test_it_returns_404_if_technology_name_is_empty_string(client):
    data = {
        'name': 'Python',
        'description': 'Object oriented programming language',
    }
    href = client.post(
        '/technologies',
        json=data,
        headers=auth_header_with_all_permissions
    ).get_json()['href']
    resp = client.patch(
        href,
        json={'name': ''},
        headers=auth_header_with_all_permissions
    )
    resp_data = resp.get_json()
    assert '422' in resp.status
    assert resp_data['error'] == 422
    assert 'Validation error' in resp_data['message']
def test_it_edits_technology(client):
    data = {
        'name': 'Python 3',
        'description': 'Programming language',
    }
    create_resp = client.post(
        '/technologies',
        json=data,
        headers=auth_header_with_all_permissions
    )
    create_resp_data = create_resp.get_json()
    get_resp = client.get(create_resp_data['href'])
    get_resp_data = get_resp.get_json()
    edited_data = {
        'name': 'Python',
    }
    edit_resp = client.patch(
        create_resp_data['href'],
        json=edited_data,
        headers=auth_header_with_all_permissions
    )
    assert '204' in edit_resp.status
    technologies = Technology.query.filter(Technology.name == 'Python').all()
    assert len(technologies) == 1