Skip to content

Commit

Permalink
Testing improvements (#49)
Browse files Browse the repository at this point in the history
* Incremental improvement of test_dcim.py

* Greatly reduce code duplication in tests, make test data *marginally* more realistic for Nautobot

* Remove more unneeded six.PY3 checks

* Update CHANGELOG
  • Loading branch information
glennmatthews committed Apr 27, 2022
1 parent 41d7fed commit e41b0bc
Show file tree
Hide file tree
Showing 66 changed files with 370 additions and 793 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Changelog

## v1.1.0
## v1.1.1

### Bug Fixes

(#43) Incorrect attribute (**display_name**) set for VirtualChassis in `__str__` method of record. Changed to **display**.
(#44) Incorrect method signatures for new `api_version` argument causing data to be set as `api_version`.
(#46) Incorrect attribute (**display_name**) set for VirtualChassis in `__str__` method of record. Changed to **display**.
(#46) Added assert_called_with() checks to several unit tests; various test refactoring

## v1.1.0
## v1.1.0 (YANKED)

(#36) Add api_version argument [@timizuoebideri1]

Expand Down
101 changes: 101 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import unittest
from unittest.mock import patch

import pynautobot

from .util import Response

api = pynautobot.api("http://localhost:8000", token="abc123",)

HEADERS = {
"accept": "application/json;",
"authorization": "Token abc123",
}

POST_HEADERS = {
"Content-Type": "application/json;",
"authorization": "Token abc123",
}


class Generic:
class Tests(unittest.TestCase):
ret = pynautobot.core.response.Record

app = "" # to be set by subclasses
name = "" # to be set by subclasses
name_singular = "" # to be optionally set by subclasses
uuid = "5b39ba88-e5ab-4be2-89f5-5a016473b53c"

@property
def bulk_uri(self):
return f"http://localhost:8000/api/{self.app}/{self.name.replace('_', '-')}/"

@property
def detail_uri(self):
return f"http://localhost:8000/api/{self.app}/{self.name.replace('_', '-')}/{self.uuid}/"

def setUp(self):
self.nb = getattr(api, self.app)
self.endpoint = getattr(self.nb, self.name)
if not self.name_singular:
self.name_singular = self.name[:-1]

#
# Generic test cases for the given app/name
#

def test_get_all(self):
with patch(
"requests.sessions.Session.get", return_value=Response(fixture=f"{self.app}/{self.name}.json"),
) as mock:
ret = self.endpoint.all()
self.assertIsInstance(ret, list)
self.assertIsInstance(ret[0], self.ret)
mock.assert_called_with(self.bulk_uri, params={}, json=None, headers=HEADERS)

def test_filter(self):
with patch(
"requests.sessions.Session.get", return_value=Response(fixture=f"{self.app}/{self.name}.json"),
) as mock:
ret = self.endpoint.filter(name="test")
self.assertIsInstance(ret, list)
self.assertIsInstance(ret[0], self.ret)
mock.assert_called_with(self.bulk_uri, params={"name": "test"}, json=None, headers=HEADERS)

def test_get(self):
with patch(
"requests.sessions.Session.get", return_value=Response(fixture=f"{self.app}/{self.name_singular}.json"),
) as mock:
ret = self.endpoint.get(self.uuid)
self.assertIsInstance(ret, self.ret)
self.assertEqual(ret.id, self.uuid)
self.assertIsInstance(str(ret), str)
self.assertIsInstance(dict(ret), dict)
mock.assert_called_with(self.detail_uri, params={}, json=None, headers=HEADERS)

def test_delete(self):
with patch(
"requests.sessions.Session.get", return_value=Response(fixture=f"{self.app}/{self.name_singular}.json"),
) as mock, patch("requests.sessions.Session.delete") as delete:
ret = self.endpoint.get(self.uuid)
# get() was already tested more thoroughly above, not repeated here
self.assertTrue(ret.delete())
mock.assert_called_with(self.detail_uri, params={}, json=None, headers=HEADERS)
delete.assert_called_with(self.detail_uri, params={}, json=None, headers=HEADERS)

def test_diff(self):
with patch(
"requests.sessions.Session.get", return_value=Response(fixture=f"{self.app}/{self.name_singular}.json"),
):
ret = self.endpoint.get(self.uuid)
self.assertTrue(ret)
self.assertEqual(ret._diff(), set())

def test_serialize(self):
with patch(
"requests.sessions.Session.get", return_value=Response(fixture=f"{self.app}/{self.name_singular}.json"),
):
ret = self.endpoint.get(self.uuid)
self.assertTrue(ret)
self.assertTrue(ret.serialize())
4 changes: 2 additions & 2 deletions tests/fixtures/circuits/circuit.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"cid": "123456",
"provider": {
"id": 1,
Expand All @@ -19,4 +19,4 @@
"description": "",
"comments": "",
"custom_fields": {}
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/circuits/circuit_termination.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"circuit": {
"id": 1,
"url": "http://localhost:8000/api/circuits/circuits/1/",
Expand Down Expand Up @@ -36,4 +36,4 @@
"upstream_speed": null,
"xconnect_id": "",
"pp_info": ""
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/circuits/circuit_type.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"name": "Transit",
"slug": "transit"
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/circuits/provider.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"name": "TEST",
"slug": "test",
"asn": null,
Expand All @@ -9,4 +9,4 @@
"admin_contact": "",
"comments": "",
"custom_fields": {}
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/cable.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"termination_a_type": "dcim.consoleport",
"termination_a_id": 1,
"termination_a": {
Expand Down Expand Up @@ -37,4 +37,4 @@
"color": "",
"length": null,
"length_unit": null
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/console_port.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device": {
"id": 1,
"url": "http://localhost:8000/api/dcim/devices/1/",
Expand All @@ -19,4 +19,4 @@
"connected_console": 1
},
"connection_status": true
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/console_port_template.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device_type": {
"id": 1,
"url": "http://localhost:8000/api/dcim/device-types/1/",
Expand All @@ -13,4 +13,4 @@
"slug": "mx960"
},
"name": "Console (RE0)"
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/console_server_port.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device": {
"id": 9,
"url": "http://localhost:8000/api/dcim/devices/9/",
Expand All @@ -8,4 +8,4 @@
},
"name": "Port 1",
"connected_console": 3
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/console_server_port_template.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device_type": {
"id": 3,
"url": "http://localhost:8000/api/dcim/device-types/3/",
Expand All @@ -13,4 +13,4 @@
"slug": "qfx5100-24q"
},
"name": "Console"
}
}
2 changes: 1 addition & 1 deletion tests/fixtures/dcim/device.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"name": "test1-edge1",
"display_name": "test1-edge1",
"device_type": {
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/device_bay.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device": {
"id": 13,
"url": "http://localhost:8000/api/dcim/devices/13/",
Expand All @@ -8,4 +8,4 @@
},
"name": "thing-1",
"installed_device": null
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/device_bay_template.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device_type": {
"id": 7,
"url": "http://localhost:8000/api/dcim/device-types/7/",
Expand All @@ -13,4 +13,4 @@
"slug": "mothership"
},
"name": "thing-1"
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/device_role.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"name": "Router",
"slug": "router",
"color": "purple"
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/device_type.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"manufacturer": {
"id": 1,
"url": "http://localhost:8000/api/dcim/manufacturers/1/",
Expand All @@ -22,4 +22,4 @@
"comments": "",
"custom_fields": {},
"instance_count": 2
}
}
6 changes: 3 additions & 3 deletions tests/fixtures/dcim/interface.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device": {
"id": 2,
"url": "http://localhost:8000/api/dcim/devices/2/",
Expand Down Expand Up @@ -57,11 +57,11 @@
},
"tagged_vlans": [
{
"id": 3,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"url": "http://localhost:8000/api/ipam/vlans/248/",
"vid": 1210,
"name": "v1210",
"display_name": "1210 (v1210)"
}
]
}
}
2 changes: 1 addition & 1 deletion tests/fixtures/dcim/interface_connection.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"interface_a": {
"id": 99,
"url": "http://localhost:8000/api/dcim/interfaces/99/",
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/interface_template.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device_type": {
"id": 1,
"url": "http://localhost:8000/api/dcim/device-types/1/",
Expand All @@ -18,4 +18,4 @@
"label": "1000BASE-T (1GE)"
},
"mgmt_only": true
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/manufacturer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"name": "Juniper",
"slug": "juniper"
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/platform.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"name": "Juniper Junos",
"slug": "juniper-junos",
"rpc_client": "juniper-junos"
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/power_outlet.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device": {
"id": 11,
"url": "http://localhost:8000/api/dcim/devices/11/",
Expand All @@ -8,4 +8,4 @@
},
"name": "AA1",
"connected_port": 1
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/dcim/power_outlet_template.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": 1,
"id": "5b39ba88-e5ab-4be2-89f5-5a016473b53c",
"device_type": {
"id": 6,
"url": "http://localhost:8000/api/dcim/device-types/6/",
Expand All @@ -13,4 +13,4 @@
"slug": "cwg-24vym415c9"
},
"name": "AA1"
}
}

0 comments on commit e41b0bc

Please sign in to comment.