Spaces:
Running
Running
| #!/usr/bin/env python3 | |
| """ | |
| Test script to verify customer notes creation and update functionality | |
| updates the customer's FollowupDate field. | |
| This script tests both: | |
| 1. Creating a customer note updates the customer's FollowupDate | |
| 2. Updating a customer note updates the customer's FollowupDate | |
| Usage: | |
| python test_customer_notes_followup.py | |
| """ | |
| import requests | |
| import json | |
| from datetime import datetime, timedelta | |
| BASE_URL = "http://localhost:8001" # Adjust port as needed | |
| TEST_CUSTOMER_ID = 16425 # Use an existing customer ID | |
| def test_customer_followup_date_update(): | |
| """Test that creating and updating customer notes updates the FollowupDate""" | |
| print("π§ͺ Testing Customer Notes FollowupDate Update Functionality") | |
| print("=" * 60) | |
| # Step 1: Get current customer data | |
| print(f"\nπ Step 1: Getting current customer data for ID {TEST_CUSTOMER_ID}") | |
| response = requests.get(f"{BASE_URL}/api/v1/customers/{TEST_CUSTOMER_ID}") | |
| if response.status_code != 200: | |
| print(f"β Failed to get customer data: {response.status_code} - {response.text}") | |
| return False | |
| customer_before = response.json() | |
| original_followup_date = customer_before.get('FollowupDate') | |
| print(f" Original FollowupDate: {original_followup_date}") | |
| # Step 2: Create a new customer note | |
| print(f"\nπ Step 2: Creating a new customer note") | |
| note_data = { | |
| "notes": "Test note to verify FollowupDate update functionality", | |
| "employee_id": "TEST01", | |
| "customer_type_id": 1, | |
| "date": datetime.now().isoformat() | |
| } | |
| response = requests.post( | |
| f"{BASE_URL}/api/v1/notes/customers/{TEST_CUSTOMER_ID}", | |
| json=note_data, | |
| headers={"Content-Type": "application/json"} | |
| ) | |
| if response.status_code != 201: | |
| print(f"β Failed to create note: {response.status_code} - {response.text}") | |
| return False | |
| created_note = response.json() | |
| note_id = created_note.get('id') | |
| print(f"β Note created successfully with ID: {note_id}") | |
| # Step 3: Verify customer FollowupDate was updated | |
| print(f"\nπ Step 3: Checking if customer FollowupDate was updated") | |
| response = requests.get(f"{BASE_URL}/api/v1/customers/{TEST_CUSTOMER_ID}") | |
| if response.status_code != 200: | |
| print(f"β Failed to get updated customer data: {response.status_code}") | |
| return False | |
| customer_after_create = response.json() | |
| new_followup_date = customer_after_create.get('FollowupDate') | |
| print(f" Original FollowupDate: {original_followup_date}") | |
| print(f" Updated FollowupDate: {new_followup_date}") | |
| if new_followup_date != original_followup_date: | |
| print("β FollowupDate was updated after creating note!") | |
| else: | |
| print("β FollowupDate was NOT updated after creating note") | |
| return False | |
| # Step 4: Get the created note's ID for updating | |
| print(f"\nπ Step 4: Getting customer notes to find note ID for updating") | |
| response = requests.get(f"{BASE_URL}/api/v1/notes/customers/{TEST_CUSTOMER_ID}") | |
| if response.status_code != 200: | |
| print(f"β Failed to get customer notes: {response.status_code}") | |
| return False | |
| notes_list = response.json() | |
| # Find our test note | |
| test_note = None | |
| for note in notes_list: | |
| if "Test note to verify FollowupDate update" in note.get('notes', ''): | |
| test_note = note | |
| break | |
| if not test_note: | |
| print("β Could not find the created test note") | |
| return False | |
| note_id2 = test_note.get('note_id2') | |
| print(f"β Found test note with NoteID2: {note_id2}") | |
| # Step 5: Update the customer note | |
| print(f"\nβοΈ Step 5: Updating the customer note with specific date") | |
| # Use a specific future date to clearly show the UI-provided date is used | |
| future_date = (datetime.now() + timedelta(days=7)).isoformat() | |
| update_data = { | |
| "notes": "Updated test note to verify FollowupDate update on modification", | |
| "employee_id": "TEST02", | |
| "date": future_date | |
| } | |
| print(f" Setting note date to: {future_date}") | |
| # Wait a moment to ensure different timestamps | |
| import time | |
| time.sleep(1) | |
| response = requests.put( | |
| f"{BASE_URL}/api/v1/notes/customers/{TEST_CUSTOMER_ID}/{note_id2}", | |
| json=update_data, | |
| headers={"Content-Type": "application/json"} | |
| ) | |
| if response.status_code != 200: | |
| print(f"β Failed to update note: {response.status_code} - {response.text}") | |
| return False | |
| print("β Note updated successfully") | |
| # Step 6: Verify customer FollowupDate was updated again | |
| print(f"\nπ Step 6: Checking if customer FollowupDate was updated after modification") | |
| response = requests.get(f"{BASE_URL}/api/v1/customers/{TEST_CUSTOMER_ID}") | |
| if response.status_code != 200: | |
| print(f"β Failed to get customer data after update: {response.status_code}") | |
| return False | |
| customer_after_update = response.json() | |
| final_followup_date = customer_after_update.get('FollowupDate') | |
| print(f" After create: {new_followup_date}") | |
| print(f" After update: {final_followup_date}") | |
| print(f" Expected date: {future_date}") | |
| if final_followup_date != new_followup_date: | |
| print("β FollowupDate was updated after modifying note!") | |
| # Check if the FollowupDate approximately matches the UI-provided date | |
| from datetime import datetime | |
| try: | |
| final_dt = datetime.fromisoformat(final_followup_date.replace('Z', '+00:00')) if 'Z' in final_followup_date else datetime.fromisoformat(final_followup_date) | |
| expected_dt = datetime.fromisoformat(future_date) | |
| time_diff = abs((final_dt - expected_dt).total_seconds()) | |
| if time_diff < 60: # Within 1 minute tolerance | |
| print("β FollowupDate matches UI-provided date!") | |
| else: | |
| print(f"β οΈ FollowupDate differs from UI-provided date by {time_diff} seconds") | |
| except Exception as e: | |
| print(f"β οΈ Could not compare dates: {e}") | |
| else: | |
| print("β FollowupDate was NOT updated after modifying note") | |
| return False | |
| # Step 7: Cleanup - Delete the test note | |
| print(f"\nπ§Ή Step 7: Cleaning up test note") | |
| response = requests.delete(f"{BASE_URL}/api/v1/notes/customers/{TEST_CUSTOMER_ID}/{note_id2}") | |
| if response.status_code == 204: | |
| print("β Test note deleted successfully") | |
| else: | |
| print(f"β οΈ Failed to delete test note: {response.status_code}") | |
| print(f"\nπ All tests completed successfully!") | |
| print("β Customer FollowupDate is properly updated when:") | |
| print(" - Creating new customer notes") | |
| print(" - Updating existing customer notes") | |
| return True | |
| if __name__ == "__main__": | |
| try: | |
| success = test_customer_followup_date_update() | |
| if success: | |
| print(f"\nβ All tests passed!") | |
| else: | |
| print(f"\nβ Some tests failed!") | |
| except Exception as e: | |
| print(f"\nπ₯ Test script error: {e}") |