File: test_multiformat_endpoint.py

package info (click to toggle)
python-pynetbox 7.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,296 kB
  • sloc: python: 4,343; makefile: 3
file content (139 lines) | stat: -rw-r--r-- 5,191 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Tests for ROMultiFormatDetailEndpoint."""

import unittest
from unittest.mock import patch

import pynetbox


class ROMultiFormatDetailEndpointTestCase(unittest.TestCase):
    """Test cases for ROMultiFormatDetailEndpoint class."""

    def setUp(self):
        """Set up test fixtures."""
        self.nb = pynetbox.api("http://localhost:8000", token="test-token")

    def test_list_returns_json_by_default(self):
        """list() without render parameter returns JSON data."""
        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value={"id": 123, "name": "Test Rack"},
        ):
            rack = self.nb.dcim.racks.get(123)

        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value=[
                {"id": 1, "name": "U1"},
                {"id": 2, "name": "U2"},
            ],
        ):
            result = rack.elevation.list()
            # Should return generator that yields dict items
            result_list = list(result)
            self.assertEqual(len(result_list), 2)
            # Verify custom_return was applied (RUs objects)
            self.assertTrue(hasattr(result_list[0], "id"))

    def test_list_with_render_svg_returns_raw_string(self):
        """list(render='svg') returns raw SVG string."""
        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value={"id": 123, "name": "Test Rack"},
        ):
            rack = self.nb.dcim.racks.get(123)

        svg_content = '<svg xmlns="http://www.w3.org/2000/svg"><rect/></svg>'
        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value=svg_content,
        ):
            result = rack.elevation.list(render="svg")
            # Should return raw string, not wrapped in list
            self.assertIsInstance(result, str)
            self.assertEqual(result, svg_content)
            self.assertIn("<svg", result)

    def test_list_with_render_json_returns_json_data(self):
        """list(render='json') explicitly returns JSON data."""
        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value={"id": 123, "name": "Test Rack"},
        ):
            rack = self.nb.dcim.racks.get(123)

        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value=[
                {"id": 1, "name": "U1"},
                {"id": 2, "name": "U2"},
            ],
        ):
            result = rack.elevation.list(render="json")
            # Should return JSON like default behavior
            result_list = list(result)
            self.assertEqual(len(result_list), 2)
            self.assertTrue(hasattr(result_list[0], "id"))

    def test_svg_response_not_processed_by_custom_return(self):
        """SVG response bypasses custom_return transformation."""
        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value={"id": 123, "name": "Test Rack"},
        ):
            rack = self.nb.dcim.racks.get(123)

        svg_content = "<svg><text>Test</text></svg>"
        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value=svg_content,
        ):
            result = rack.elevation.list(render="svg")
            # Result should be raw string, not Record object
            self.assertIsInstance(result, str)
            self.assertFalse(hasattr(result, "id"))
            self.assertFalse(hasattr(result, "serialize"))

    def test_empty_response_with_svg(self):
        """Empty SVG response is handled correctly."""
        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value={"id": 123, "name": "Test Rack"},
        ):
            rack = self.nb.dcim.racks.get(123)

        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value="",
        ):
            result = rack.elevation.list(render="svg")
            # Should return empty string
            self.assertIsInstance(result, str)
            self.assertEqual(result, "")

    def test_create_raises_not_implemented(self):
        """create() raises NotImplementedError (read-only endpoint)."""
        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value={"id": 123, "name": "Test Rack"},
        ):
            rack = self.nb.dcim.racks.get(123)

        # ROMultiFormatDetailEndpoint should be read-only
        with self.assertRaises(NotImplementedError):
            rack.elevation.create({"data": "test"})

    def test_unsupported_render_format_raises_error(self):
        """Unsupported render format raises ValueError."""
        with patch(
            "pynetbox.core.query.Request._make_call",
            return_value={"id": 123, "name": "Test Rack"},
        ):
            rack = self.nb.dcim.racks.get(123)

        # Unsupported render formats should raise ValueError
        with self.assertRaises(ValueError) as context:
            rack.elevation.list(render="png")

        self.assertIn("Unsupported render format", str(context.exception))
        self.assertIn("png", str(context.exception))