Skip to content

Commit

Permalink
Merge pull request #364 from digitalocean/threading-dups
Browse files Browse the repository at this point in the history
Resolve Duplicate entries when Threading is Enabled
  • Loading branch information
Zach Moody committed Apr 8, 2021
2 parents 30b813c + 5d2bb34 commit d62e5e0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
35 changes: 18 additions & 17 deletions pynetbox/core/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,25 +327,26 @@ def get(self, add_params=None):
self.concurrent_get(ret, page_size, page_offsets)
for i in ret:
yield i
first_run = True
for i in req["results"]:
yield i
while req["next"]:
# Not worrying about making sure add_params kwargs is
# passed in here because results from detail routes aren't
# paginated, thus far.
if first_run:
req = self._make_call(
add_params={
"limit": self.limit or req["count"],
"offset": len(req["results"]),
}
)
else:
req = self._make_call(url_override=req["next"])
first_run = False
else:
first_run = True
for i in req["results"]:
yield i
while req["next"]:
# Not worrying about making sure add_params kwargs is
# passed in here because results from detail routes aren't
# paginated, thus far.
if first_run:
req = self._make_call(
add_params={
"limit": self.limit or req["count"],
"offset": len(req["results"]),
}
)
else:
req = self._make_call(url_override=req["next"])
first_run = False
for i in req["results"]:
yield i
elif isinstance(req, list):
self.count = len(req)
for i in req:
Expand Down
22 changes: 11 additions & 11 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def id_netbox_service(fixture_value):


@pytest.fixture(scope="session")
def netbox_service(
def docker_netbox_service(
pytestconfig, docker_ip, docker_services, request,
):
"""Get the netbox service to test against.
Expand Down Expand Up @@ -411,23 +411,23 @@ def netbox_service(
docker_services.wait_until_responsive(
timeout=300.0, pause=1, check=lambda: netbox_is_responsive(url)
)
nb_api = pynetbox.api(url, token="0123456789abcdef0123456789abcdef01234567")

return {
"url": url,
"netbox_version": netbox_integration_version,
"api": nb_api,
}


@pytest.fixture(scope="session", autouse=True)
def api(netbox_service):
return netbox_service["api"]
@pytest.fixture(scope="session")
def api(docker_netbox_service):
return pynetbox.api(
docker_netbox_service["url"], token="0123456789abcdef0123456789abcdef01234567"
)


@pytest.fixture(scope="session")
def nb_version(netbox_service):
return netbox_service["netbox_version"]
def nb_version(docker_netbox_service):
return docker_netbox_service["netbox_version"]


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -469,12 +469,12 @@ def device_role(api):

def pytest_generate_tests(metafunc):
"""Dynamically parametrize some functions based on args from the cli parser."""
if "netbox_service" in metafunc.fixturenames:
# parametrize the requested versions of netbox to the netbox_services fixture
if "docker_netbox_service" in metafunc.fixturenames:
# parametrize the requested versions of netbox to the docker_netbox_services fixture
# so that it will return a fixture for each of the versions requested
# individually rather than one fixture with multiple versions within it
metafunc.parametrize(
"netbox_service",
"docker_netbox_service",
metafunc.config.getoption("netbox_versions"),
ids=id_netbox_service,
indirect=True,
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/test_dcim.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from packaging import version

import pynetbox


@pytest.fixture(scope="module")
def rack(api, site):
Expand Down Expand Up @@ -86,6 +88,27 @@ def init(self, request, site):
endpoint="sites",
)

@pytest.fixture(scope="class")
def add_sites(self, api):
sites = [
api.dcim.sites.create(name="test{}".format(i), slug="test{}".format(i))
for i in range(2, 20)
]
yield
for i in sites:
i.delete()

def test_threading_duplicates(self, docker_netbox_service, add_sites):
api = pynetbox.api(
docker_netbox_service["url"],
token="0123456789abcdef0123456789abcdef01234567",
threading=True,
)
test = api.dcim.sites.all(limit=5)
test_list = list(test)
test_set = set(test_list)
assert len(test_list) == len(test_set)


class TestRack(BaseTest):
@pytest.fixture(scope="class")
Expand Down

0 comments on commit d62e5e0

Please sign in to comment.