|
1 | 1 | # -*- coding: utf-8 -*- |
| 2 | +from django.db import models |
2 | 3 | from django.urls import reverse |
3 | 4 |
|
4 | | -from nav.models.manage import Interface |
| 5 | +from nav.models.manage import Interface, Location, NetType, Room, Vlan, NetboxGroup |
| 6 | +from nav.web.info.forms import SearchForm |
| 7 | +from nav.web.info.views import process_form |
5 | 8 |
|
6 | 9 |
|
7 | 10 | def test_search_for_ip_devices_should_not_crash(client): |
@@ -40,6 +43,67 @@ def test_search_for_device_groups_should_not_crash(client): |
40 | 43 | assert response.status_code == 200 |
41 | 44 |
|
42 | 45 |
|
| 46 | +class TestProcessFormDescriptionSearch: |
| 47 | + """Tests for process_form view searching by description.""" |
| 48 | + |
| 49 | + def test_location_search_by_description_should_return_results(self, db): |
| 50 | + location = Location.objects.create( |
| 51 | + id="testlocation", description="test location description" |
| 52 | + ) |
| 53 | + |
| 54 | + self.assert_search_provider_result( |
| 55 | + instance=location, |
| 56 | + provider_name='Locations', |
| 57 | + ) |
| 58 | + |
| 59 | + def test_room_search_by_description_should_return_results(self, db): |
| 60 | + room = Room.objects.create( |
| 61 | + id="testroom", description="test room description", location_id="mylocation" |
| 62 | + ) |
| 63 | + |
| 64 | + self.assert_search_provider_result( |
| 65 | + instance=room, |
| 66 | + provider_name='Rooms', |
| 67 | + ) |
| 68 | + |
| 69 | + def test_vlan_search_by_description_should_return_results(self, db): |
| 70 | + nettype = NetType.objects.create(description="testdescription") |
| 71 | + vlan = Vlan.objects.create( |
| 72 | + vlan="20", description="test vlan description", net_type=nettype |
| 73 | + ) |
| 74 | + self.assert_search_provider_result( |
| 75 | + instance=vlan, |
| 76 | + provider_name='Vlans', |
| 77 | + ) |
| 78 | + |
| 79 | + def test_devicegroup_search_by_description_should_return_results( |
| 80 | + self, db, localhost |
| 81 | + ): |
| 82 | + device_group = NetboxGroup.objects.create( |
| 83 | + id='test-group-001', description='Core network switches group' |
| 84 | + ) |
| 85 | + self.assert_search_provider_result( |
| 86 | + instance=device_group, |
| 87 | + provider_name='Device groups', |
| 88 | + ) |
| 89 | + |
| 90 | + @staticmethod |
| 91 | + def assert_search_provider_result(instance: models.Model, provider_name: str): |
| 92 | + """ |
| 93 | + Helper method to assert that a search by description returns |
| 94 | + the expected instance. |
| 95 | + """ |
| 96 | + |
| 97 | + form = SearchForm({'query': instance.description}, auto_id=False) |
| 98 | + form.is_valid() |
| 99 | + providers, _ = process_form(form) |
| 100 | + |
| 101 | + provider = next((p for p in providers if p.name == provider_name), None) |
| 102 | + assert provider is not None |
| 103 | + assert len(provider.results) == 1 |
| 104 | + assert provider.results[0].inst.id == instance.id |
| 105 | + |
| 106 | + |
43 | 107 | class TestIndexSearchPreviewView: |
44 | 108 | """Tests for the search preview feature.""" |
45 | 109 |
|
|
0 commit comments