askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
test_versioning.py · 6.1 KB · 198 lines
| 1 | """ |
| 2 | Test Phase 3: Versioning System |
| 3 | |
| 4 | Tests for version retrieval and rollback functionality. |
| 5 | """ |
| 6 | |
| 7 | import requests |
| 8 | import json |
| 9 | |
| 10 | BASE_URL = 'http://localhost:8001' |
| 11 | |
| 12 | |
| 13 | def test_get_version(): |
| 14 | """Test getting specific version""" |
| 15 | print('\n=== Test 1: Get Specific Version ===') |
| 16 | |
| 17 | # First, update the counter object to create versions |
| 18 | # Get current source |
| 19 | response = requests.get(f'{BASE_URL}/objects/basics_counter?source=true') |
| 20 | data = response.json() |
| 21 | original_source = data['source'] |
| 22 | print(f"Original source length: {len(original_source)} chars") |
| 23 | |
| 24 | # Modify it to create version 2 |
| 25 | new_source = original_source.replace('count = 1', 'count = 100') |
| 26 | response = requests.put( |
| 27 | f'{BASE_URL}/objects/basics_counter?source=true', |
| 28 | json={ |
| 29 | 'code': new_source, |
| 30 | 'author': 'test_user', |
| 31 | 'message': 'Change count to 100' |
| 32 | } |
| 33 | ) |
| 34 | print(f"Update status: {response.json()['status']}") |
| 35 | assert response.json()['status'] == 'ok' |
| 36 | |
| 37 | # Modify again to create version 3 |
| 38 | new_source2 = new_source.replace('count = 100', 'count = 999') |
| 39 | response = requests.put( |
| 40 | f'{BASE_URL}/objects/basics_counter?source=true', |
| 41 | json={ |
| 42 | 'code': new_source2, |
| 43 | 'author': 'test_user', |
| 44 | 'message': 'Change count to 999' |
| 45 | } |
| 46 | ) |
| 47 | print(f"Update status: {response.json()['status']}") |
| 48 | assert response.json()['status'] == 'ok' |
| 49 | |
| 50 | # Now get version 1 (original) |
| 51 | response = requests.get(f'{BASE_URL}/objects/basics_counter?version=1') |
| 52 | data = response.json() |
| 53 | |
| 54 | print(f"\nVersion 1 retrieval:") |
| 55 | print(f" Status: {response.status_code}") |
| 56 | print(f" Timestamp: {data['version'].get('timestamp', 'N/A')}") |
| 57 | print(f" Author: {data['version'].get('author', 'N/A')}") |
| 58 | print(f" Message: {data['version'].get('message', 'N/A')}") |
| 59 | print(f" Content contains 'count = 1': {'count = 1' in data['version']['content']}") |
| 60 | |
| 61 | assert response.status_code == 200 |
| 62 | assert data['status'] == 'ok' |
| 63 | assert 'version' in data |
| 64 | assert 'content' in data['version'] |
| 65 | assert 'count = 1' in data['version']['content'] # Original value |
| 66 | |
| 67 | # Get version 2 |
| 68 | response = requests.get(f'{BASE_URL}/objects/basics_counter?version=2') |
| 69 | data = response.json() |
| 70 | |
| 71 | print(f"\nVersion 2 retrieval:") |
| 72 | print(f" Content contains 'count = 100': {'count = 100' in data['version']['content']}") |
| 73 | |
| 74 | assert 'count = 100' in data['version']['content'] # Modified value |
| 75 | |
| 76 | print('\n✅ Version retrieval works') |
| 77 | |
| 78 | |
| 79 | def test_rollback(): |
| 80 | """Test rollback to previous version""" |
| 81 | print('\n=== Test 2: Rollback to Previous Version ===') |
| 82 | |
| 83 | # Rollback to version 1 |
| 84 | response = requests.post( |
| 85 | f'{BASE_URL}/objects/basics_counter', |
| 86 | json={ |
| 87 | 'action': 'rollback', |
| 88 | 'version_id': 1, |
| 89 | 'author': 'test_user', |
| 90 | 'message': 'Rollback to original version' |
| 91 | } |
| 92 | ) |
| 93 | data = response.json() |
| 94 | |
| 95 | print(f"Rollback response:") |
| 96 | print(f" Status: {response.status_code}") |
| 97 | print(f" Message: {data.get('message', 'N/A')}") |
| 98 | |
| 99 | assert response.status_code == 200 |
| 100 | assert data['status'] == 'ok' |
| 101 | assert 'Rolled back' in data['message'] |
| 102 | assert data['version_id'] == 1 |
| 103 | |
| 104 | # Verify source code is back to version 1 |
| 105 | response = requests.get(f'{BASE_URL}/objects/basics_counter?source=true') |
| 106 | data = response.json() |
| 107 | |
| 108 | print(f"\nCurrent source after rollback:") |
| 109 | print(f" Contains 'count = 1': {'count = 1' in data['source']}") |
| 110 | |
| 111 | assert 'count = 1' in data['source'] # Should be back to original |
| 112 | |
| 113 | # Verify version history shows the rollback as a new version |
| 114 | response = requests.get(f'{BASE_URL}/objects/basics_counter?versions=true') |
| 115 | data = response.json() |
| 116 | |
| 117 | print(f"\nVersion history after rollback:") |
| 118 | print(f" Total versions: {data['count']}") |
| 119 | if data['count'] >= 4: |
| 120 | latest = data['versions'][0] # Newest first |
| 121 | print(f" Latest version message: {latest.get('message', 'N/A')}") |
| 122 | assert 'Rollback' in latest.get('message', '') |
| 123 | |
| 124 | print('\n✅ Rollback works') |
| 125 | |
| 126 | |
| 127 | def test_invalid_version(): |
| 128 | """Test error handling for invalid version""" |
| 129 | print('\n=== Test 3: Invalid Version Handling ===') |
| 130 | |
| 131 | # Try to get non-existent version |
| 132 | response = requests.get(f'{BASE_URL}/objects/basics_counter?version=999') |
| 133 | data = response.json() |
| 134 | |
| 135 | print(f"Invalid version response:") |
| 136 | print(f" Status: {response.status_code}") |
| 137 | print(f" Response: {data}") |
| 138 | |
| 139 | assert response.status_code == 404 |
| 140 | # Error response might be in different format |
| 141 | if 'status' in data: |
| 142 | assert data['status'] == 'error' |
| 143 | if 'message' in data: |
| 144 | assert 'not found' in data['message'].lower() |
| 145 | |
| 146 | # Try rollback to non-existent version |
| 147 | response = requests.post( |
| 148 | f'{BASE_URL}/objects/basics_counter', |
| 149 | json={ |
| 150 | 'action': 'rollback', |
| 151 | 'version_id': 999, |
| 152 | 'author': 'test_user', |
| 153 | 'message': 'Should fail' |
| 154 | } |
| 155 | ) |
| 156 | data = response.json() |
| 157 | |
| 158 | print(f"\nInvalid rollback response:") |
| 159 | print(f" Status: {response.status_code}") |
| 160 | print(f" Response: {data}") |
| 161 | |
| 162 | # Should be an error (either 4xx or 5xx or status='error') |
| 163 | if 'status' in data: |
| 164 | assert data['status'] == 'error' |
| 165 | else: |
| 166 | # If no status field, check HTTP status code |
| 167 | assert response.status_code >= 400 |
| 168 | |
| 169 | print('\n✅ Error handling works') |
| 170 | |
| 171 | |
| 172 | if __name__ == '__main__': |
| 173 | print('Testing Phase 3: Versioning System') |
| 174 | print('=' * 50) |
| 175 | |
| 176 | try: |
| 177 | # Test version retrieval |
| 178 | test_get_version() |
| 179 | |
| 180 | # Test rollback |
| 181 | test_rollback() |
| 182 | |
| 183 | # Test error handling |
| 184 | test_invalid_version() |
| 185 | |
| 186 | print('\n' + '=' * 50) |
| 187 | print('✅ All versioning tests passed!') |
| 188 | print('=' * 50) |
| 189 | |
| 190 | except AssertionError as e: |
| 191 | print(f'\n❌ Test failed: {e}') |
| 192 | raise |
| 193 | except requests.exceptions.ConnectionError: |
| 194 | print('\n❌ Error: Server not running') |
| 195 | print('Start server with: python run_server.py') |
| 196 | except Exception as e: |
| 197 | print(f'\n❌ Unexpected error: {e}') |
| 198 | raise |