|
| 1 | +# Copyright 2025 Marcus Furlong <furlongm@gmail.com> |
| 2 | +# |
| 3 | +# This file is part of Patchman. |
| 4 | +# |
| 5 | +# Patchman is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, version 3 only. |
| 8 | +# |
| 9 | +# Patchman is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with Patchman. If not, see <http://www.gnu.org/licenses/> |
| 16 | + |
| 17 | +from django.contrib.auth.models import User |
| 18 | +from django.test import TestCase, override_settings |
| 19 | +from rest_framework import status |
| 20 | +from rest_framework.test import APITestCase |
| 21 | + |
| 22 | +from arch.models import MachineArchitecture, PackageArchitecture |
| 23 | + |
| 24 | + |
| 25 | +@override_settings( |
| 26 | + CELERY_TASK_ALWAYS_EAGER=True, |
| 27 | + CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}} |
| 28 | +) |
| 29 | +class ArchitectureAPITests(APITestCase): |
| 30 | + """Tests for the Architecture API endpoints.""" |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + """Set up test data.""" |
| 34 | + self.user = User.objects.create_user( |
| 35 | + username='testuser', password='testpass' |
| 36 | + ) |
| 37 | + self.client.force_authenticate(user=self.user) |
| 38 | + |
| 39 | + self.machine_arch = MachineArchitecture.objects.create(name='x86_64') |
| 40 | + self.pkg_arch = PackageArchitecture.objects.create(name='amd64') |
| 41 | + |
| 42 | + def test_list_machine_architectures(self): |
| 43 | + """Test listing machine architectures.""" |
| 44 | + response = self.client.get('/api/machine-architecture/') |
| 45 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 46 | + self.assertEqual(len(response.data['results']), 1) |
| 47 | + |
| 48 | + def test_retrieve_machine_architecture(self): |
| 49 | + """Test retrieving a machine architecture.""" |
| 50 | + response = self.client.get(f'/api/machine-architecture/{self.machine_arch.id}/') |
| 51 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 52 | + self.assertEqual(response.data['name'], 'x86_64') |
| 53 | + |
| 54 | + def test_list_package_architectures(self): |
| 55 | + """Test listing package architectures.""" |
| 56 | + response = self.client.get('/api/package-architecture/') |
| 57 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 58 | + self.assertEqual(len(response.data['results']), 1) |
| 59 | + |
| 60 | + def test_retrieve_package_architecture(self): |
| 61 | + """Test retrieving a package architecture.""" |
| 62 | + response = self.client.get(f'/api/package-architecture/{self.pkg_arch.id}/') |
| 63 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 64 | + self.assertEqual(response.data['name'], 'amd64') |
| 65 | + |
| 66 | + |
| 67 | +@override_settings( |
| 68 | + CELERY_TASK_ALWAYS_EAGER=True, |
| 69 | + CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}} |
| 70 | +) |
| 71 | +class MachineArchitectureModelTests(TestCase): |
| 72 | + """Tests for the MachineArchitecture model.""" |
| 73 | + |
| 74 | + def test_machine_architecture_creation(self): |
| 75 | + """Test creating a machine architecture.""" |
| 76 | + arch = MachineArchitecture.objects.create(name='aarch64') |
| 77 | + self.assertEqual(arch.name, 'aarch64') |
| 78 | + |
| 79 | + def test_machine_architecture_string_representation(self): |
| 80 | + """Test MachineArchitecture __str__ method.""" |
| 81 | + arch = MachineArchitecture.objects.create(name='i686') |
| 82 | + self.assertEqual(str(arch), 'i686') |
| 83 | + |
| 84 | + def test_machine_architecture_unique_name(self): |
| 85 | + """Test that machine architecture names must be unique.""" |
| 86 | + MachineArchitecture.objects.create(name='unique-arch') |
| 87 | + from django.db import IntegrityError |
| 88 | + with self.assertRaises(IntegrityError): |
| 89 | + MachineArchitecture.objects.create(name='unique-arch') |
| 90 | + |
| 91 | + |
| 92 | +@override_settings( |
| 93 | + CELERY_TASK_ALWAYS_EAGER=True, |
| 94 | + CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}} |
| 95 | +) |
| 96 | +class PackageArchitectureModelTests(TestCase): |
| 97 | + """Tests for the PackageArchitecture model.""" |
| 98 | + |
| 99 | + def test_package_architecture_creation(self): |
| 100 | + """Test creating a package architecture.""" |
| 101 | + arch = PackageArchitecture.objects.create(name='arm64') |
| 102 | + self.assertEqual(arch.name, 'arm64') |
| 103 | + |
| 104 | + def test_package_architecture_string_representation(self): |
| 105 | + """Test PackageArchitecture __str__ method.""" |
| 106 | + arch = PackageArchitecture.objects.create(name='noarch') |
| 107 | + self.assertEqual(str(arch), 'noarch') |
| 108 | + |
| 109 | + def test_package_architecture_unique_name(self): |
| 110 | + """Test that package architecture names must be unique.""" |
| 111 | + PackageArchitecture.objects.create(name='all') |
| 112 | + from django.db import IntegrityError |
| 113 | + with self.assertRaises(IntegrityError): |
| 114 | + PackageArchitecture.objects.create(name='all') |
| 115 | + |
| 116 | + def test_common_architectures(self): |
| 117 | + """Test creating common architecture types.""" |
| 118 | + archs = ['x86_64', 'amd64', 'i386', 'i686', 'noarch', 'all', 'arm64', 'aarch64'] |
| 119 | + for arch_name in archs: |
| 120 | + arch = PackageArchitecture.objects.create(name=arch_name) |
| 121 | + self.assertEqual(str(arch), arch_name) |
0 commit comments