Ejemplo n.º 1
0
    def test_all_invalid_pagination_args(self):

        api = Mock(base_url="http://localhost:8000/api")
        app = Mock(name="test")
        test_obj = Endpoint(api, app, "test")
        with self.assertRaises(ValueError) as _:
            test_obj.all(offset=1)
Ejemplo n.º 2
0
 def test_choices(self):
     with patch("pynetbox.core.query.Request.options",
                return_value=Mock()) as mock:
         api = Mock(base_url="http://localhost:8000/api")
         app = Mock(name="test")
         mock.return_value = {
             "actions": {
                 "POST": {
                     "letter": {
                         "choices": [
                             {
                                 "display_name": "A",
                                 "value": 1
                             },
                             {
                                 "display_name": "B",
                                 "value": 2
                             },
                             {
                                 "display_name": "C",
                                 "value": 3
                             },
                         ]
                     }
                 }
             }
         }
         test_obj = Endpoint(api, app, "test")
         choices = test_obj.choices()
         self.assertEqual(choices["letter"][1]["display_name"], "B")
         self.assertEqual(choices["letter"][1]["value"], 2)
Ejemplo n.º 3
0
    def test_bulk_update_records(self):
        with patch("pynetbox.core.query.Request._make_call",
                   return_value=Mock()) as mock:
            from pynetbox.core.response import Record

            ids = [1, 3, 5]
            mock.return_value = True
            api = Mock(base_url="http://localhost:8000/api")
            app = Mock(name="test")
            test_obj = Endpoint(api, app, "test")
            objects = [
                Record(
                    {
                        "id": i,
                        "name": "dummy" + str(i),
                        "unchanged": "yes"
                    },
                    api,
                    test_obj,
                ) for i in ids
            ]
            for o in objects:
                o.name = "fluffy" + str(o.id)
            mock.return_value = [o.serialize() for o in objects]
            test = test_obj.update(objects)
            mock.assert_called_with(verb="patch",
                                    data=[{
                                        "id": i,
                                        "name": "fluffy" + str(i)
                                    } for i in ids])
            self.assertTrue(test)
Ejemplo n.º 4
0
    def test_delete_with_recordset(self):
        with patch("pynetbox.core.query.Request._make_call",
                   return_value=Mock()) as mock:
            from pynetbox.core.response import RecordSet

            ids = [1, 3, 5]

            class FakeRequest:
                def get(self):
                    return iter([{
                        "id": i,
                        "name": "dummy" + str(i)
                    } for i in ids])

            mock.return_value = True
            api = Mock(base_url="http://localhost:8000/api")
            app = Mock(name="test")
            test_obj = Endpoint(api, app, "test")
            recordset = RecordSet(test_obj, FakeRequest())
            test = test_obj.delete(recordset)
            mock.assert_called_with(verb="delete",
                                    data=[{
                                        "id": i
                                    } for i in ids])
            self.assertTrue(test)
Ejemplo n.º 5
0
    def test_filter_reserved_kwargs(self):

        api = Mock(base_url="http://localhost:8000/api")
        app = Mock(name="test")
        test_obj = Endpoint(api, app, "test")
        with self.assertRaises(ValueError) as _:
            test_obj.filter(id=1)
Ejemplo n.º 6
0
 def test_get_with_filter(self):
     with patch("pynetbox.core.query.Request._make_call",
                return_value=Mock()) as mock:
         mock.return_value = [{"id": 123}]
         api = Mock(base_url="http://localhost:8000/api")
         app = Mock(name="test")
         test_obj = Endpoint(api, app, "test")
         test = test_obj.get(name="test")
         self.assertEqual(test.id, 123)
Ejemplo n.º 7
0
 def test_get_no_results(self):
     with patch("pynetbox.core.query.Request._make_call",
                return_value=Mock()) as mock:
         mock.return_value = []
         api = Mock(base_url="http://localhost:8000/api")
         app = Mock(name="test")
         test_obj = Endpoint(api, app, "test")
         test = test_obj.get(name="test")
         self.assertIsNone(test)
Ejemplo n.º 8
0
 def test_get_greater_than_one(self):
     with patch("pynetbox.core.query.Request._make_call",
                return_value=Mock()) as mock:
         mock.return_value = [{"id": 123}, {"id": 321}]
         api = Mock(base_url="http://localhost:8000/api")
         app = Mock(name="test")
         test_obj = Endpoint(api, app, "test")
         with self.assertRaises(ValueError) as _:
             test_obj.get(name="test")
Ejemplo n.º 9
0
 def test_filter(self):
     with patch("pynetbox.core.query.Request._make_call",
                return_value=Mock()) as mock:
         api = Mock(base_url="http://localhost:8000/api")
         app = Mock(name="test")
         mock.return_value = [{"id": 123}, {"id": 321}]
         test_obj = Endpoint(api, app, "test")
         test = test_obj.filter(test="test")
         self.assertEqual(len(test), 2)
Ejemplo n.º 10
0
 def test_bulk_update_json(self):
     with patch("pynetbox.core.query.Request._make_call",
                return_value=Mock()) as mock:
         ids = [1, 3, 5]
         changes = [{"id": i, "name": "puffy" + str(i)} for i in ids]
         mock.return_value = True
         api = Mock(base_url="http://localhost:8000/api")
         app = Mock(name="test")
         mock.return_value = changes
         test_obj = Endpoint(api, app, "test")
         test = test_obj.update(changes)
         mock.assert_called_with(verb="patch", data=changes)
         self.assertTrue(test)
Ejemplo n.º 11
0
 def test_delete_with_ids(self):
     with patch("pynetbox.core.query.Request._make_call",
                return_value=Mock()) as mock:
         ids = [1, 3, 5]
         mock.return_value = True
         api = Mock(base_url="http://localhost:8000/api")
         app = Mock(name="test")
         test_obj = Endpoint(api, app, "test")
         test = test_obj.delete(ids)
         mock.assert_called_with(verb="delete",
                                 data=[{
                                     "id": i
                                 } for i in ids])
         self.assertTrue(test)
Ejemplo n.º 12
0
    def test_delete_with_objects(self):
        with patch("pynetbox.core.query.Request._make_call",
                   return_value=Mock()) as mock:
            from pynetbox.core.response import Record

            ids = [1, 3, 5]
            mock.return_value = True
            api = Mock(base_url="http://localhost:8000/api")
            app = Mock(name="test")
            test_obj = Endpoint(api, app, "test")
            objects = [
                Record({
                    "id": i,
                    "name": "dummy" + str(i)
                }, api, test_obj) for i in ids
            ]
            test = test_obj.delete(objects)
            mock.assert_called_with(verb="delete",
                                    data=[{
                                        "id": i
                                    } for i in ids])
            self.assertTrue(test)
Ejemplo n.º 13
0
    def init_recordset(cls):
        data = [{
            "id": i,
            "name": "dummy" + str(i),
            "status": "active"
        } for i in cls.ids]
        api = Mock(base_url="http://localhost:8000/api")
        app = Mock(name="test")

        class FakeRequest:
            def get(self):
                return iter(data)

            def patch(self):
                return iter(data)

        return RecordSet(Endpoint(api, app, "test"), FakeRequest())
Ejemplo n.º 14
0
 def __getattr__(self, name):
     if name == "secrets":
         self._set_session_key()
     return Endpoint(self.api, self, name, model=self.model)
Ejemplo n.º 15
0
 def __getattr__(self, name):
     return Endpoint(self.api, self, name, model=self.model)