mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 10:58:37 -06:00
Split dummy models
Instead of using a single model for testing, one is used for testing the plugin API and a dedicated one is used for testing the NetBox plugin model features.
This commit is contained in:
parent
8b0af50019
commit
2a6702e88b
@ -0,0 +1,30 @@
|
||||
import taggit.managers
|
||||
import utilities.json
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('dummy_plugin', '0001_initial'),
|
||||
('extras', '0122_charfield_null_choices'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='DummyNetBoxModel',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)),
|
||||
('created', models.DateTimeField(auto_now_add=True, null=True)),
|
||||
('last_updated', models.DateTimeField(auto_now=True, null=True)),
|
||||
(
|
||||
'custom_field_data',
|
||||
models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
|
||||
),
|
||||
('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
@ -1,34 +0,0 @@
|
||||
import taggit.managers
|
||||
import utilities.json
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('dummy_plugin', '0001_initial'),
|
||||
('extras', '0122_charfield_null_choices'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='dummymodel',
|
||||
name='created',
|
||||
field=models.DateTimeField(auto_now_add=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='dummymodel',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='dummymodel',
|
||||
name='last_updated',
|
||||
field=models.DateTimeField(auto_now=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='dummymodel',
|
||||
name='tags',
|
||||
field=taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag'),
|
||||
),
|
||||
]
|
@ -3,7 +3,7 @@ from django.db import models
|
||||
from netbox.models import NetBoxModel
|
||||
|
||||
|
||||
class DummyModel(NetBoxModel):
|
||||
class DummyModel(models.Model):
|
||||
name = models.CharField(
|
||||
max_length=20
|
||||
)
|
||||
@ -13,3 +13,7 @@ class DummyModel(NetBoxModel):
|
||||
|
||||
class Meta:
|
||||
ordering = ['name']
|
||||
|
||||
|
||||
class DummyNetBoxModel(NetBoxModel):
|
||||
pass
|
||||
|
@ -6,5 +6,6 @@ from . import views
|
||||
urlpatterns = (
|
||||
path('models/', views.DummyModelsView.as_view(), name='dummy_model_list'),
|
||||
path('models/add/', views.DummyModelAddView.as_view(), name='dummy_model_add'),
|
||||
path('models/<int:pk>/', views.DummyModelView.as_view(), name='dummymodel'),
|
||||
|
||||
path('netboxmodel/<int:pk>/', views.DummyNetBoxModelView.as_view(), name='dummynetboxmodel'),
|
||||
)
|
||||
|
@ -7,11 +7,15 @@ from django.views.generic import View
|
||||
from dcim.models import Site
|
||||
from netbox.views import generic
|
||||
from utilities.views import register_model_view
|
||||
from .models import DummyModel
|
||||
from .models import DummyModel, DummyNetBoxModel
|
||||
# Trigger registration of custom column
|
||||
from .tables import mycol # noqa: F401
|
||||
|
||||
|
||||
#
|
||||
# DummyModel
|
||||
#
|
||||
|
||||
class DummyModelsView(View):
|
||||
|
||||
def get(self, request):
|
||||
@ -19,10 +23,6 @@ class DummyModelsView(View):
|
||||
return HttpResponse(f"Instances: {instance_count}")
|
||||
|
||||
|
||||
class DummyModelView(generic.ObjectView):
|
||||
queryset = DummyModel.objects.all()
|
||||
|
||||
|
||||
class DummyModelAddView(View):
|
||||
|
||||
def get(self, request):
|
||||
@ -37,6 +37,18 @@ class DummyModelAddView(View):
|
||||
return HttpResponse("Instance created")
|
||||
|
||||
|
||||
#
|
||||
# DummyNetBoxModel
|
||||
#
|
||||
|
||||
class DummyNetBoxModelView(generic.ObjectView):
|
||||
queryset = DummyNetBoxModel.objects.all()
|
||||
|
||||
|
||||
#
|
||||
# API
|
||||
#
|
||||
|
||||
@register_model_view(Site, 'extra', path='other-stuff')
|
||||
class ExtraCoreModelView(View):
|
||||
|
||||
|
@ -1,7 +1,10 @@
|
||||
from unittest import skipIf
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from core.models import ObjectChange
|
||||
from netbox.tests.dummy_plugin.models import DummyModel
|
||||
from netbox.tests.dummy_plugin.models import DummyNetBoxModel
|
||||
|
||||
|
||||
class ModelTest(TestCase):
|
||||
@ -12,9 +15,9 @@ class ModelTest(TestCase):
|
||||
|
||||
self.assertEqual(m.get_absolute_url(), f'/core/changelog/{m.pk}/')
|
||||
|
||||
def test_absolute_url(self):
|
||||
m = DummyModel(name='Foo')
|
||||
m.full_clean()
|
||||
m.save()
|
||||
@skipIf('netbox.tests.dummy_plugin' not in settings.PLUGINS, "dummy_plugin not in settings.PLUGINS")
|
||||
def test_get_absolute_url_plugin(self):
|
||||
m = DummyNetBoxModel()
|
||||
m.pk = 123
|
||||
|
||||
self.assertEqual(m.get_absolute_url(), f"/plugins/dummy-plugin/models/{m.pk}/")
|
||||
self.assertEqual(m.get_absolute_url(), f'/plugins/dummy-plugin/netboxmodel/{m.pk}/')
|
||||
|
Loading…
Reference in New Issue
Block a user