Exemple #1
0
    def test_annotation_same_as_get_for_object(self):
        """
        This test incorperates features from all of the above tests cases to ensure
        the annotate_config_context_data() and get_for_object() queryset methods are the same.
        """
        context1 = ConfigContext(name="context 1",
                                 weight=101,
                                 data={
                                     "a": 123,
                                     "b": 456,
                                     "c": 777
                                 })
        context2 = ConfigContext(name="context 2",
                                 weight=100,
                                 data={
                                     "a": 123,
                                     "b": 456,
                                     "c": 789
                                 })
        context3 = ConfigContext(name="context 3", weight=99, data={"d": 1})
        context4 = ConfigContext(name="context 4", weight=99, data={"d": 2})
        ConfigContext.objects.bulk_create(
            [context1, context2, context3, context4])

        annotated_queryset = Device.objects.filter(
            name=self.device.name).annotate_config_context_data()
        self.assertEqual(self.device.get_config_context(),
                         annotated_queryset[0].get_config_context())
Exemple #2
0
    def setUpTestData(cls):

        site = Site.objects.create(name='Site 1', slug='site-1')

        # Create three ConfigContexts
        for i in range(1, 4):
            configcontext = ConfigContext(
                name='Config Context {}'.format(i),
                data={'foo': i}
            )
            configcontext.save()
            configcontext.sites.add(site)

        cls.form_data = {
            'name': 'Config Context X',
            'weight': 200,
            'description': 'A new config context',
            'is_active': True,
            'regions': [],
            'sites': [site.pk],
            'roles': [],
            'platforms': [],
            'tenant_groups': [],
            'tenants': [],
            'tags': [],
            'data': '{"foo": 123}',
        }

        cls.bulk_edit_data = {
            'weight': 300,
            'is_active': False,
            'description': 'New description',
        }
    def test_higher_weight_wins(self):

        context1 = ConfigContext(
            name="context 1",
            weight=101,
            data={
                "a": 123,
                "b": 456,
                "c": 777
            }
        )
        context2 = ConfigContext(
            name="context 2",
            weight=100,
            data={
                "a": 123,
                "b": 456,
                "c": 789
            }
        )
        ConfigContext.objects.bulk_create([context1, context2])

        expected_data = {
            "a": 123,
            "b": 456,
            "c": 777
        }
        self.assertEqual(self.device.get_config_context(), expected_data)
Exemple #4
0
    def setUpTestData(cls):

        config_contexts = (
            ConfigContext(name='Config Context 1', weight=100, data={'foo': 123}),
            ConfigContext(name='Config Context 2', weight=200, data={'bar': 456}),
            ConfigContext(name='Config Context 3', weight=300, data={'baz': 789}),
        )
        ConfigContext.objects.bulk_create(config_contexts)
Exemple #5
0
    def setUp(self):

        self.client = Client()

        site = Site(name='Site 1', slug='site-1')
        site.save()

        # Create three ConfigContexts
        for i in range(1, 4):
            configcontext = ConfigContext(name='Config Context {}'.format(i),
                                          data='{{"foo": {}}}'.format(i))
            configcontext.save()
            configcontext.sites.add(site)
Exemple #6
0
    def setUp(self):
        user = create_test_user(permissions=['extras.view_configcontext'])
        self.client = Client()
        self.client.force_login(user)

        site = Site(name='Site 1', slug='site-1')
        site.save()

        # Create three ConfigContexts
        for i in range(1, 4):
            configcontext = ConfigContext(name='Config Context {}'.format(i),
                                          data='{{"foo": {}}}'.format(i))
            configcontext.save()
            configcontext.sites.add(site)
Exemple #7
0
    def setUp(self):

        self.client = Client()

        site = Site(name='Site 1', slug='site-1')
        site.save()

        # Create three ConfigContexts
        for i in range(1, 4):
            configcontext = ConfigContext(
                name='Config Context {}'.format(i),
                data='{{"foo": {}}}'.format(i)
            )
            configcontext.save()
            configcontext.sites.add(site)
Exemple #8
0
    def test_render_configcontext_for_object(self):

        # Create a Device for which we'll render a config context
        manufacturer = Manufacturer.objects.create(name='Test Manufacturer',
                                                   slug='test-manufacturer')
        device_type = DeviceType.objects.create(manufacturer=manufacturer,
                                                model='Test Device Type')
        device_role = DeviceRole.objects.create(name='Test Role',
                                                slug='test-role')
        site = Site.objects.create(name='Test Site', slug='test-site')
        device = Device.objects.create(name='Test Device',
                                       device_type=device_type,
                                       device_role=device_role,
                                       site=site)

        # Test default config contexts (created at test setup)
        rendered_context = device.get_config_context()
        self.assertEqual(rendered_context['foo'], 123)
        self.assertEqual(rendered_context['bar'], 456)
        self.assertEqual(rendered_context['baz'], 789)

        # Add another context specific to the site
        configcontext4 = ConfigContext(name='Test Config Context 4',
                                       data={'site_data': 'ABC'})
        configcontext4.save()
        configcontext4.sites.add(site)
        rendered_context = device.get_config_context()
        self.assertEqual(rendered_context['site_data'], 'ABC')

        # Override one of the default contexts
        configcontext5 = ConfigContext(name='Test Config Context 5',
                                       weight=2000,
                                       data={'foo': 999})
        configcontext5.save()
        configcontext5.sites.add(site)
        rendered_context = device.get_config_context()
        self.assertEqual(rendered_context['foo'], 999)

        # Add a context which does NOT match our device and ensure it does not apply
        site2 = Site.objects.create(name='Test Site 2', slug='test-site-2')
        configcontext6 = ConfigContext(name='Test Config Context 6',
                                       weight=2000,
                                       data={'bar': 999})
        configcontext6.save()
        configcontext6.sites.add(site2)
        rendered_context = device.get_config_context()
        self.assertEqual(rendered_context['bar'], 456)