|
| 1 | +""" |
| 2 | +Test to demonstrate and verify the mutability consistency between vcon.dialog and vcon.parties properties. |
| 3 | +""" |
| 4 | +import pytest |
| 5 | +from vcon import Vcon |
| 6 | +from vcon.party import Party |
| 7 | +from vcon.dialog import Dialog |
| 8 | +from datetime import datetime, timezone |
| 9 | + |
| 10 | + |
| 11 | +def test_mutability_consistency_demonstration(): |
| 12 | + """ |
| 13 | + Demonstrate that both vcon.dialog and vcon.parties now work consistently. |
| 14 | + |
| 15 | + This test shows that: |
| 16 | + - vcon.dialog returns mutable references (in-place modifications work) |
| 17 | + - vcon.parties now also returns mutable references (in-place modifications work) |
| 18 | + """ |
| 19 | + # Create a vCon with parties and dialogs |
| 20 | + vcon = Vcon.build_new() |
| 21 | + |
| 22 | + # Add parties |
| 23 | + party1 = Party(name="Alice", tel="+1234567890") |
| 24 | + party2 = Party(name="Bob", tel="+1987654321") |
| 25 | + vcon.add_party(party1) |
| 26 | + vcon.add_party(party2) |
| 27 | + |
| 28 | + # Add dialogs |
| 29 | + dialog1 = Dialog( |
| 30 | + type="text", |
| 31 | + start=datetime.now(timezone.utc).isoformat(), |
| 32 | + parties=[0, 1], |
| 33 | + body="Hello, this is a test message" |
| 34 | + ) |
| 35 | + dialog2 = Dialog( |
| 36 | + type="text", |
| 37 | + start=datetime.now(timezone.utc).isoformat(), |
| 38 | + parties=[0], |
| 39 | + body="This is another message" |
| 40 | + ) |
| 41 | + vcon.add_dialog(dialog1) |
| 42 | + vcon.add_dialog(dialog2) |
| 43 | + |
| 44 | + # Test dialog mutability (should work - returns direct references) |
| 45 | + dialog_list = vcon.dialog |
| 46 | + original_body = dialog_list[0]["body"] |
| 47 | + dialog_list[0]["body"] = "Modified dialog body" |
| 48 | + |
| 49 | + # Verify the change is reflected in the internal data |
| 50 | + assert vcon.dialog[0]["body"] == "Modified dialog body" |
| 51 | + assert vcon.vcon_dict["dialog"][0]["body"] == "Modified dialog body" |
| 52 | + |
| 53 | + # Test parties mutability (now works - returns mutable references) |
| 54 | + parties_list = vcon.parties |
| 55 | + original_name = parties_list[0]["name"] |
| 56 | + |
| 57 | + # This modification now affects the internal data |
| 58 | + parties_list[0]["name"] = "Modified Alice" |
| 59 | + |
| 60 | + # Verify the change IS reflected in the internal data (new consistent behavior) |
| 61 | + assert vcon.parties[0]["name"] == "Modified Alice" # Modified name |
| 62 | + assert vcon.vcon_dict["parties"][0]["name"] == "Modified Alice" # Internal data changed |
| 63 | + |
| 64 | + |
| 65 | +def test_mutability_consistency_after_fix(): |
| 66 | + """ |
| 67 | + Test that after the fix, both vcon.dialog and vcon.parties behave consistently. |
| 68 | + |
| 69 | + This test verifies that both properties return mutable references. |
| 70 | + """ |
| 71 | + # Create a vCon with parties and dialogs |
| 72 | + vcon = Vcon.build_new() |
| 73 | + |
| 74 | + # Add parties |
| 75 | + party1 = Party(name="Alice", tel="+1234567890") |
| 76 | + party2 = Party(name="Bob", tel="+1987654321") |
| 77 | + vcon.add_party(party1) |
| 78 | + vcon.add_party(party2) |
| 79 | + |
| 80 | + # Add dialogs |
| 81 | + dialog1 = Dialog( |
| 82 | + type="text", |
| 83 | + start=datetime.now(timezone.utc).isoformat(), |
| 84 | + parties=[0, 1], |
| 85 | + body="Hello, this is a test message" |
| 86 | + ) |
| 87 | + vcon.add_dialog(dialog1) |
| 88 | + |
| 89 | + # Test dialog mutability (should work) |
| 90 | + dialog_list = vcon.dialog |
| 91 | + dialog_list[0]["body"] = "Modified dialog body" |
| 92 | + assert vcon.dialog[0]["body"] == "Modified dialog body" |
| 93 | + assert vcon.vcon_dict["dialog"][0]["body"] == "Modified dialog body" |
| 94 | + |
| 95 | + # Test parties mutability (should work after fix) |
| 96 | + parties_list = vcon.parties |
| 97 | + parties_list[0]["name"] = "Modified Alice" |
| 98 | + |
| 99 | + # Verify the change IS reflected in the internal data (after fix) |
| 100 | + assert vcon.parties[0]["name"] == "Modified Alice" |
| 101 | + assert vcon.vcon_dict["parties"][0]["name"] == "Modified Alice" |
| 102 | + |
| 103 | + # Test that we can still access party data as dictionaries |
| 104 | + assert isinstance(vcon.parties[0], dict) |
| 105 | + assert "name" in vcon.parties[0] |
| 106 | + assert "tel" in vcon.parties[0] |
| 107 | + |
| 108 | + |
| 109 | +def test_party_object_creation_still_works(): |
| 110 | + """ |
| 111 | + Test that we can still create Party objects from the dictionary data when needed. |
| 112 | + """ |
| 113 | + vcon = Vcon.build_new() |
| 114 | + |
| 115 | + # Add a party |
| 116 | + party = Party(name="Alice", tel="+1234567890") |
| 117 | + vcon.add_party(party) |
| 118 | + |
| 119 | + # Get the party data as dictionary |
| 120 | + party_dict = vcon.parties[0] |
| 121 | + |
| 122 | + # Create a Party object from the dictionary data |
| 123 | + party_obj = Party(**party_dict) |
| 124 | + |
| 125 | + # Verify the Party object works correctly |
| 126 | + assert party_obj.name == "Alice" |
| 127 | + assert party_obj.tel == "+1234567890" |
| 128 | + assert isinstance(party_obj, Party) |
| 129 | + |
| 130 | + |
| 131 | +def test_backward_compatibility(): |
| 132 | + """ |
| 133 | + Test that existing code that expects Party objects still works. |
| 134 | + """ |
| 135 | + vcon = Vcon.build_new() |
| 136 | + |
| 137 | + # Add a party |
| 138 | + party = Party(name="Alice", tel="+1234567890") |
| 139 | + vcon.add_party(party) |
| 140 | + |
| 141 | + # Get parties and create Party objects (common pattern) |
| 142 | + parties = [Party(**party_dict) for party_dict in vcon.parties] |
| 143 | + |
| 144 | + # Verify the Party objects work correctly |
| 145 | + assert len(parties) == 1 |
| 146 | + assert parties[0].name == "Alice" |
| 147 | + assert parties[0].tel == "+1234567890" |
| 148 | + assert isinstance(parties[0], Party) |
0 commit comments