Thanks to visit codestin.com
Credit goes to github.com

Skip to content

test(functional): replace len() calls with list membership checks #2118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions tests/functional/api/test_clusters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def test_project_clusters(project):
project.clusters.create(
cluster = project.clusters.create(
{
"name": "cluster1",
"platform_kubernetes_attributes": {
Expand All @@ -9,21 +9,20 @@ def test_project_clusters(project):
}
)
clusters = project.clusters.list()
assert len(clusters) == 1
assert cluster in clusters

cluster = clusters[0]
cluster.platform_kubernetes_attributes = {"api_url": "http://newurl"}
cluster.save()

cluster = project.clusters.list()[0]
assert cluster.platform_kubernetes["api_url"] == "http://newurl"

cluster.delete()
assert len(project.clusters.list()) == 0
assert cluster not in project.clusters.list()


def test_group_clusters(group):
group.clusters.create(
cluster = group.clusters.create(
{
"name": "cluster1",
"platform_kubernetes_attributes": {
Expand All @@ -33,14 +32,13 @@ def test_group_clusters(group):
}
)
clusters = group.clusters.list()
assert len(clusters) == 1
assert cluster in clusters

cluster = clusters[0]
cluster.platform_kubernetes_attributes = {"api_url": "http://newurl"}
cluster.save()

cluster = group.clusters.list()[0]
assert cluster.platform_kubernetes["api_url"] == "http://newurl"

cluster.delete()
assert len(group.clusters.list()) == 0
assert cluster not in group.clusters.list()
12 changes: 6 additions & 6 deletions tests/functional/api/test_current_user.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
def test_current_user_email(gl):
gl.auth()
mail = gl.user.emails.create({"email": "[email protected]"})
assert len(gl.user.emails.list()) == 2
assert mail in gl.user.emails.list()

mail.delete()
assert len(gl.user.emails.list()) == 1
assert mail not in gl.user.emails.list()


def test_current_user_gpg_keys(gl, GPG_KEY):
gl.auth()
gkey = gl.user.gpgkeys.create({"key": GPG_KEY})
assert len(gl.user.gpgkeys.list()) == 1
assert gkey in gl.user.gpgkeys.list()

# Seems broken on the gitlab side
gkey = gl.user.gpgkeys.get(gkey.id)
gkey.delete()
assert len(gl.user.gpgkeys.list()) == 0
assert gkey not in gl.user.gpgkeys.list()


def test_current_user_ssh_keys(gl, SSH_KEY):
gl.auth()
key = gl.user.keys.create({"title": "testkey", "key": SSH_KEY})
assert len(gl.user.keys.list()) == 1
assert key in gl.user.keys.list()

key.delete()
assert len(gl.user.keys.list()) == 0
assert key not in gl.user.keys.list()


def test_current_user_status(gl):
Expand Down
7 changes: 3 additions & 4 deletions tests/functional/api/test_deploy_keys.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
def test_project_deploy_keys(gl, project, DEPLOY_KEY):
deploy_key = project.keys.create({"title": "foo@bar", "key": DEPLOY_KEY})
project_keys = list(project.keys.list())
assert len(project_keys) == 1
assert deploy_key in project.keys.list()

project2 = gl.projects.create({"name": "deploy-key-project"})
project2.keys.enable(deploy_key.id)
assert len(project2.keys.list()) == 1
assert deploy_key in project2.keys.list()

project2.keys.delete(deploy_key.id)
assert len(project2.keys.list()) == 0
assert deploy_key not in project2.keys.list()
project2.delete()
16 changes: 8 additions & 8 deletions tests/functional/api/test_deploy_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def test_project_deploy_tokens(gl, project):
"scopes": ["read_registry"],
}
)
assert len(project.deploytokens.list()) == 1
assert gl.deploytokens.list() == project.deploytokens.list()
assert deploy_token in project.deploytokens.list()
assert set(project.deploytokens.list()) <= set(gl.deploytokens.list())

deploy_token = project.deploytokens.get(deploy_token.id)
assert deploy_token.name == "foo"
Expand All @@ -17,8 +17,8 @@ def test_project_deploy_tokens(gl, project):
assert deploy_token.username == "bar"

deploy_token.delete()
assert len(project.deploytokens.list()) == 0
assert len(gl.deploytokens.list()) == 0
assert deploy_token not in project.deploytokens.list()
assert deploy_token not in gl.deploytokens.list()


def test_group_deploy_tokens(gl, group):
Expand All @@ -29,13 +29,13 @@ def test_group_deploy_tokens(gl, group):
}
)

assert len(group.deploytokens.list()) == 1
assert gl.deploytokens.list() == group.deploytokens.list()
assert deploy_token in group.deploytokens.list()
assert set(group.deploytokens.list()) <= set(gl.deploytokens.list())

deploy_token = group.deploytokens.get(deploy_token.id)
assert deploy_token.name == "foo"
assert deploy_token.scopes == ["read_registry"]

deploy_token.delete()
assert len(group.deploytokens.list()) == 0
assert len(gl.deploytokens.list()) == 0
assert deploy_token not in group.deploytokens.list()
assert deploy_token not in gl.deploytokens.list()
56 changes: 23 additions & 33 deletions tests/functional/api/test_gitlab.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import warnings

import pytest

import gitlab
Expand Down Expand Up @@ -27,7 +25,7 @@ def test_broadcast_messages(gl):
assert msg.color == "#444444"

msg.delete()
assert len(gl.broadcastmessages.list()) == 0
assert msg not in gl.broadcastmessages.list()


def test_markdown(gl):
Expand Down Expand Up @@ -109,10 +107,10 @@ def test_template_license(gl):

def test_hooks(gl):
hook = gl.hooks.create({"url": "http://whatever.com"})
assert len(gl.hooks.list()) == 1
assert hook in gl.hooks.list()

hook.delete()
assert len(gl.hooks.list()) == 0
assert hook not in gl.hooks.list()


def test_namespaces(gl):
Expand Down Expand Up @@ -151,10 +149,10 @@ def test_events(gl):
def test_features(gl):
feat = gl.features.set("foo", 30)
assert feat.name == "foo"
assert len(gl.features.list()) == 1
assert feat in gl.features.list()

feat.delete()
assert len(gl.features.list()) == 0
assert feat not in gl.features.list()


def test_pagination(gl, project):
Expand Down Expand Up @@ -198,54 +196,46 @@ def test_rate_limits(gl):
def test_list_default_warning(gl):
"""When there are more than 20 items and use default `list()` then warning is
generated"""
with warnings.catch_warnings(record=True) as caught_warnings:
with pytest.warns(UserWarning, match="python-gitlab.readthedocs.io") as record:
gl.gitlabciymls.list()
assert len(caught_warnings) == 1
warning = caught_warnings[0]
assert isinstance(warning.message, UserWarning)
message = str(warning.message)
assert "python-gitlab.readthedocs.io" in message

assert len(record) == 1
warning = record[0]
assert __file__ == warning.filename


def test_list_page_nowarning(gl):
def test_list_page_nowarning(gl, recwarn):
"""Using `page=X` will disable the warning"""
with warnings.catch_warnings(record=True) as caught_warnings:
gl.gitlabciymls.list(page=1)
assert len(caught_warnings) == 0
gl.gitlabciymls.list(page=1)
assert not recwarn


def test_list_all_false_nowarning(gl):
def test_list_all_false_nowarning(gl, recwarn):
"""Using `all=False` will disable the warning"""
with warnings.catch_warnings(record=True) as caught_warnings:
gl.gitlabciymls.list(all=False)
assert len(caught_warnings) == 0
gl.gitlabciymls.list(all=False)
assert not recwarn


def test_list_all_true_nowarning(gl):
def test_list_all_true_nowarning(gl, recwarn):
"""Using `all=True` will disable the warning"""
with warnings.catch_warnings(record=True) as caught_warnings:
items = gl.gitlabciymls.list(all=True)
assert len(caught_warnings) == 0
items = gl.gitlabciymls.list(all=True)
assert not recwarn
assert len(items) > 20


def test_list_iterator_true_nowarning(gl):
def test_list_iterator_true_nowarning(gl, recwarn):
"""Using `iterator=True` will disable the warning"""
with warnings.catch_warnings(record=True) as caught_warnings:
items = gl.gitlabciymls.list(iterator=True)
assert len(caught_warnings) == 0
items = gl.gitlabciymls.list(iterator=True)
assert not recwarn
assert len(list(items)) > 20


def test_list_as_list_false_warnings(gl):
"""Using `as_list=False` will disable the UserWarning but cause a
DeprecationWarning"""
with warnings.catch_warnings(record=True) as caught_warnings:
with pytest.warns(DeprecationWarning) as record:
items = gl.gitlabciymls.list(as_list=False)
assert len(caught_warnings) == 1
for warning in caught_warnings:
assert isinstance(warning.message, DeprecationWarning)
assert len(record) == 1
assert len(list(items)) > 20


Expand Down
Loading