<commit_before>import urllib.parse

from django.urls import reverse

from utilities.testing import TestCase
from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine


class ClusterGroupTestCase(TestCase):
    user_permissions = (
        'virtualization.view_clustergroup',
    )

    @classmethod
    def setUpTestData(cls):

        ClusterGroup.objects.bulk_create([
            ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'),
            ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'),
            ClusterGroup(name='Cluster Group 3', slug='cluster-group-3'),
        ])

    def test_clustergroup_list(self):

        url = reverse('virtualization:clustergroup_list')

        response = self.client.get(url)
        self.assertHttpStatus(response, 200)

    def test_clustergroup_import(self):
        self.add_permissions('virtualization.add_clustergroup')

        csv_data = (
            "name,slug",
            "Cluster Group 4,cluster-group-4",
            "Cluster Group 5,cluster-group-5",
            "Cluster Group 6,cluster-group-6",
        )

        response = self.client.post(reverse('virtualization:clustergroup_import'), {'csv': '\n'.join(csv_data)})

        self.assertHttpStatus(response, 200)
        self.assertEqual(ClusterGroup.objects.count(), 6)


class ClusterTypeTestCase(TestCase):
    user_permissions = (
        'virtualization.view_clustertype',
    )

    @classmethod
    def setUpTestData(cls):

        ClusterType.objects.bulk_create([
            ClusterType(name='Cluster Type 1', slug='cluster-type-1'),
            ClusterType(name='Cluster Type 2', slug='cluster-type-2'),
            ClusterType(name='Cluster Type 3', slug='cluster-type-3'),
        ])

    def test_clustertype_list(self):

        url = reverse('virtualization:clustertype_list')

        response = self.client.get(url)
        self.assertHttpStatus(response, 200)

    def test_clustertype_import(self):
        self.add_permissions('virtualization.add_clustertype')

        csv_data = (
            "name,slug",
            "Cluster Type 4,cluster-type-4",
            "Cluster Type 5,cluster-type-5",
            "Cluster Type 6,cluster-type-6",
        )

        response = self.client.post(reverse('virtualization:clustertype_import'), {'csv': '\n'.join(csv_data)})

        self.assertHttpStatus(response, 200)
        self.assertEqual(ClusterType.objects.count(), 6)


class ClusterTestCase(TestCase):
    user_permissions = (
        'virtualization.view_cluster',
    )

    @classmethod
    def setUpTestData(cls):

        clustergroup = ClusterGroup(name='Cluster Group 1', slug='cluster-group-1')
        clustergroup.save()

        clustertype = ClusterType(name='Cluster Type 1', slug='cluster-type-1')
        clustertype.save()

        Cluster.objects.bulk_create([
            Cluster(name='Cluster 1', group=clustergroup, type=clustertype),
            Cluster(name='Cluster 2', group=clustergroup, type=clustertype),
            Cluster(name='Cluster 3', group=clustergroup, type=clustertype),
        ])

    def test_cluster_list(self):

        url = reverse('virtualization:cluster_list')
        params = {
            "group": ClusterGroup.objects.first().slug,
            "type": ClusterType.objects.first().slug,
        }

        response = self.client.get('{}?{}'.format(url, urllib.parse.urlencode(params)))
        self.assertHttpStatus(response, 200)

    def test_cluster(self):

        cluster = Cluster.objects.first()
        response = self.client.get(cluster.get_absolute_url())
        self.assertHttpStatus(response, 200)

    def test_cluster_import(self):
        self.add_permissions('virtualization.add_cluster')

        csv_data = (
            "name,type",
            "Cluster 4,Cluster Type 1",
            "Cluster 5,Cluster Type 1",
            "Cluster 6,Cluster Type 1",
        )

        response = self.client.post(reverse('virtualization:cluster_import'), {'csv': '\n'.join(csv_data)})

        self.assertHttpStatus(response, 200)
        self.assertEqual(Cluster.objects.count(), 6)


class VirtualMachineTestCase(TestCase):
    user_permissions = (
        'virtualization.view_virtualmachine',
    )

    @classmethod
    def setUpTestData(cls):

        clustertype = ClusterType(name='Cluster Type 1', slug='cluster-type-1')
        clustertype.save()

        cluster = Cluster(name='Cluster 1', type=clustertype)
        cluster.save()

        VirtualMachine.objects.bulk_create([
            VirtualMachine(name='Virtual Machine 1', cluster=cluster),
            VirtualMachine(name='Virtual Machine 2', cluster=cluster),
            VirtualMachine(name='Virtual Machine 3', cluster=cluster),
        ])

    def test_virtualmachine_list(self):

        url = reverse('virtualization:virtualmachine_list')
        params = {
            "cluster_id": Cluster.objects.first().pk,
        }

        response = self.client.get('{}?{}'.format(url, urllib.parse.urlencode(params)))
        self.assertHttpStatus(response, 200)

    def test_virtualmachine(self):

        virtualmachine = VirtualMachine.objects.first()
        response = self.client.get(virtualmachine.get_absolute_url())
        self.assertHttpStatus(response, 200)

    def test_virtualmachine_import(self):
        self.add_permissions('virtualization.add_virtualmachine')

        csv_data = (
            "name,cluster",
            "Virtual Machine 4,Cluster 1",
            "Virtual Machine 5,Cluster 1",
            "Virtual Machine 6,Cluster 1",
        )

        response = self.client.post(reverse('virtualization:virtualmachine_import'), {'csv': '\n'.join(csv_data)})

        self.assertHttpStatus(response, 200)
        self.assertEqual(VirtualMachine.objects.count(), 6)
<commit_msg>Convert virtualization view tests to use StandardTestCases<commit_after>from dcim.models import DeviceRole, Platform, Site
from utilities.testing import StandardTestCases
from virtualization.choices import *
from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine


class ClusterGroupTestCase(StandardTestCases.Views):
    model = ClusterGroup

    # Disable inapplicable tests
    test_get_object = None
    test_delete_object = None

    @classmethod
    def setUpTestData(cls):

        ClusterGroup.objects.bulk_create([
            ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'),
            ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'),
            ClusterGroup(name='Cluster Group 3', slug='cluster-group-3'),
        ])

        cls.form_data = {
            'name': 'Cluster Group X',
            'slug': 'cluster-group-x',
        }

        cls.csv_data = (
            "name,slug",
            "Cluster Group 4,cluster-group-4",
            "Cluster Group 5,cluster-group-5",
            "Cluster Group 6,cluster-group-6",
        )


class ClusterTypeTestCase(StandardTestCases.Views):
    model = ClusterType

    # Disable inapplicable tests
    test_get_object = None
    test_delete_object = None

    @classmethod
    def setUpTestData(cls):

        ClusterType.objects.bulk_create([
            ClusterType(name='Cluster Type 1', slug='cluster-type-1'),
            ClusterType(name='Cluster Type 2', slug='cluster-type-2'),
            ClusterType(name='Cluster Type 3', slug='cluster-type-3'),
        ])

        cls.form_data = {
            'name': 'Cluster Type X',
            'slug': 'cluster-type-x',
        }

        cls.csv_data = (
            "name,slug",
            "Cluster Type 4,cluster-type-4",
            "Cluster Type 5,cluster-type-5",
            "Cluster Type 6,cluster-type-6",
        )


class ClusterTestCase(StandardTestCases.Views):
    model = Cluster

    @classmethod
    def setUpTestData(cls):

        site = Site.objects.create(name='Site 1', slug='site-1')
        clustergroup = ClusterGroup.objects.create(name='Cluster Group 1', slug='cluster-group-1')
        clustertype = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')

        Cluster.objects.bulk_create([
            Cluster(name='Cluster 1', group=clustergroup, type=clustertype),
            Cluster(name='Cluster 2', group=clustergroup, type=clustertype),
            Cluster(name='Cluster 3', group=clustergroup, type=clustertype),
        ])

        cls.form_data = {
            'name': 'Cluster X',
            'group': clustergroup.pk,
            'type': clustertype.pk,
            'tenant': None,
            'site': site.pk,
            'comments': 'Some comments',
            'tags': 'Alpha,Bravo,Charlie',
        }

        cls.csv_data = (
            "name,type",
            "Cluster 4,Cluster Type 1",
            "Cluster 5,Cluster Type 1",
            "Cluster 6,Cluster Type 1",
        )


class VirtualMachineTestCase(StandardTestCases.Views):
    model = VirtualMachine

    @classmethod
    def setUpTestData(cls):

        devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1')
        platform = Platform.objects.create(name='Platform 1', slug='platform-1')
        clustertype = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
        cluster = Cluster.objects.create(name='Cluster 1', type=clustertype)

        VirtualMachine.objects.bulk_create([
            VirtualMachine(name='Virtual Machine 1', cluster=cluster),
            VirtualMachine(name='Virtual Machine 2', cluster=cluster),
            VirtualMachine(name='Virtual Machine 3', cluster=cluster),
        ])

        cls.form_data = {
            'cluster': cluster.pk,
            'tenant': None,
            'platform': None,
            'name': 'Virtual Machine X',
            'status': VirtualMachineStatusChoices.STATUS_STAGED,
            'role': devicerole.pk,
            'primary_ip4': None,
            'primary_ip6': None,
            'vcpus': 4,
            'memory': 32768,
            'disk': 4000,
            'comments': 'Some comments',
            'tags': 'Alpha,Bravo,Charlie',
            'local_context_data': None,
        }

        cls.csv_data = (
            "name,cluster",
            "Virtual Machine 4,Cluster 1",
            "Virtual Machine 5,Cluster 1",
            "Virtual Machine 6,Cluster 1",
        )

{'fraction_non_alphanumeric': 0.12081838456317269, 'fraction_numerical': 0.01454474934548628, 'mean_word_length': 2.567623659633345, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6189748', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2783, 'n_words': 537}