6
6
from unittest .mock import patch
7
7
8
8
import pytest
9
+ from pydantic import ValidationError
9
10
10
11
from understack_workflows .main .netapp_configure_net import VIRTUAL_MACHINES_QUERY
11
12
from understack_workflows .main .netapp_configure_net import argument_parser
@@ -663,22 +664,22 @@ def test_vlan_validation_valid_range(self):
663
664
664
665
for vlan in valid_vlans :
665
666
interface = InterfaceInfo (
666
- name = "test-interface" ,
667
- address = "192.168.1.10/24" ,
668
- vlan = vlan
667
+ name = "test-interface" , address = "192.168.1.10/24" , vlan = vlan
669
668
)
670
669
assert interface .vlan == vlan
671
670
672
671
def test_vlan_validation_invalid_range (self ):
673
672
"""Test VLAN validation with invalid VLAN IDs (outside 1-4094 range)."""
673
+ from pydantic import ValidationError
674
+
674
675
invalid_vlans = [0 , - 1 , 4095 , 5000 , 65536 ]
675
676
676
677
for vlan in invalid_vlans :
677
- with pytest .raises (ValueError , match = "VLAN ID must be between 1 and 4094" ):
678
+ with pytest .raises (
679
+ ValidationError , match = "VLAN ID must be between 1 and 4094"
680
+ ):
678
681
InterfaceInfo (
679
- name = "test-interface" ,
680
- address = "192.168.1.10/24" ,
681
- vlan = vlan
682
+ name = "test-interface" , address = "192.168.1.10/24" , vlan = vlan
682
683
)
683
684
684
685
def test_vlan_validation_in_from_graphql_interface (self ):
@@ -700,53 +701,9 @@ def test_vlan_validation_in_from_graphql_interface(self):
700
701
"tagged_vlans" : [{"vid" : 4095 }], # Invalid VLAN
701
702
}
702
703
703
- with pytest .raises (ValueError , match = "VLAN ID must be between 1 and 4094" ):
704
+ with pytest .raises (ValidationError , match = "VLAN ID must be between 1 and 4094" ):
704
705
InterfaceInfo .from_graphql_interface (interface_data_invalid )
705
706
706
- def test_pydantic_serialization (self ):
707
- """Test Pydantic serialization features for InterfaceInfo."""
708
- interface = InterfaceInfo (
709
- name = "test-interface" ,
710
- address = "192.168.1.10/24" ,
711
- vlan = 2002
712
- )
713
-
714
- # Test model_dump (dict serialization)
715
- data = interface .model_dump ()
716
- expected = {
717
- "name" : "test-interface" ,
718
- "address" : "192.168.1.10/24" ,
719
- "vlan" : 2002
720
- }
721
- assert data == expected
722
-
723
- # Test model_dump_json (JSON serialization)
724
- json_str = interface .model_dump_json ()
725
- assert '"name":"test-interface"' in json_str
726
- assert '"address":"192.168.1.10/24"' in json_str
727
- assert '"vlan":2002' in json_str
728
-
729
- # Test model_validate (creation from dict)
730
- new_interface = InterfaceInfo .model_validate (data )
731
- assert new_interface .name == interface .name
732
- assert new_interface .address == interface .address
733
- assert new_interface .vlan == interface .vlan
734
-
735
- def test_pydantic_immutability (self ):
736
- """Test that Pydantic models are immutable (frozen)."""
737
- interface = InterfaceInfo (
738
- name = "test-interface" ,
739
- address = "192.168.1.10/24" ,
740
- vlan = 2002
741
- )
742
-
743
- # Should not be able to modify fields
744
- with pytest .raises (ValueError , match = "Instance is frozen" ):
745
- interface .name = "modified-name"
746
-
747
- with pytest .raises (ValueError , match = "Instance is frozen" ):
748
- interface .vlan = 3000
749
-
750
707
751
708
class TestVirtualMachineNetworkInfo :
752
709
"""Test cases for VirtualMachineNetworkInfo data class and validation."""
@@ -912,44 +869,6 @@ def test_from_graphql_vm_error_handling_preserves_interface_context(self):
912
869
assert "problematic-interface" in error_message
913
870
assert "no tagged VLANs" in error_message
914
871
915
- def test_pydantic_serialization (self ):
916
- """Test Pydantic serialization features for VirtualMachineNetworkInfo."""
917
- interfaces = [
918
- InterfaceInfo (name = "eth0" , address = "192.168.1.10/24" , vlan = 100 ),
919
- InterfaceInfo (name = "eth1" , address = "10.0.0.1/8" , vlan = 200 ),
920
- ]
921
- vm_info = VirtualMachineNetworkInfo (interfaces = interfaces )
922
-
923
- # Test model_dump (dict serialization)
924
- data = vm_info .model_dump ()
925
- assert "interfaces" in data
926
- assert len (data ["interfaces" ]) == 2
927
- assert data ["interfaces" ][0 ]["name" ] == "eth0"
928
- assert data ["interfaces" ][1 ]["name" ] == "eth1"
929
-
930
- # Test model_dump_json (JSON serialization)
931
- json_str = vm_info .model_dump_json ()
932
- assert '"interfaces":[' in json_str
933
- assert '"name":"eth0"' in json_str
934
- assert '"name":"eth1"' in json_str
935
-
936
- # Test model_validate (creation from dict)
937
- new_vm_info = VirtualMachineNetworkInfo .model_validate (data )
938
- assert len (new_vm_info .interfaces ) == 2
939
- assert new_vm_info .interfaces [0 ].name == "eth0"
940
- assert new_vm_info .interfaces [1 ].name == "eth1"
941
-
942
- def test_pydantic_immutability (self ):
943
- """Test that VirtualMachineNetworkInfo is immutable (frozen)."""
944
- interfaces = [
945
- InterfaceInfo (name = "eth0" , address = "192.168.1.10/24" , vlan = 100 )
946
- ]
947
- vm_info = VirtualMachineNetworkInfo (interfaces = interfaces )
948
-
949
- # Should not be able to modify the interfaces list
950
- with pytest .raises (ValueError , match = "Instance is frozen" ):
951
- vm_info .interfaces = []
952
-
953
872
954
873
class TestGraphQLQueryFunctionality :
955
874
"""Test cases for GraphQL query construction, execution, and response handling."""
@@ -1270,7 +1189,7 @@ def test_handling_of_empty_query_results(self, mock_logger):
1270
1189
1271
1190
assert result ["data" ]["virtual_machines" ] == []
1272
1191
mock_logger .info .assert_called_with (
1273
- "GraphQL query successful. Found %s virtual machine(s) " " for device: %s" ,
1192
+ "GraphQL query successful. Found %s virtual machine(s) for device: %s" ,
1274
1193
0 ,
1275
1194
"os-empty-project" ,
1276
1195
)
@@ -1303,7 +1222,7 @@ def test_handling_of_empty_query_results(self, mock_logger):
1303
1222
assert len (result ["data" ]["virtual_machines" ]) == 1
1304
1223
assert result ["data" ]["virtual_machines" ][0 ]["interfaces" ] == []
1305
1224
mock_logger .info .assert_called_with (
1306
- "GraphQL query successful. Found %s virtual machine(s) " " for device: %s" ,
1225
+ "GraphQL query successful. Found %s virtual machine(s) for device: %s" ,
1307
1226
1 ,
1308
1227
"os-empty-interfaces-project" ,
1309
1228
)
@@ -1332,7 +1251,7 @@ def test_graphql_query_logging_behavior(self, mock_logger):
1332
1251
1333
1252
# Verify info logging
1334
1253
mock_logger .info .assert_called_with (
1335
- "GraphQL query successful. Found %s virtual machine(s) " " for device: %s" ,
1254
+ "GraphQL query successful. Found %s virtual machine(s) for device: %s" ,
1336
1255
1 ,
1337
1256
"os-logging-test-project" ,
1338
1257
)
0 commit comments