feat(models): implement Vision ID pattern for event_device and event_session
- Migrated event_device and event_session models to the V3 Vision ID pattern (string-based public IDs). - Added root_validator for automatic id_random mapping and integer stripping. - Implemented fields_to_exclude_from_db to protect database updates from convenience/view fields. - Fixed description_json type in Journal_Base for correct JSON parsing. - Added E2E verification tests for event_device and event_session V3 endpoints.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import datetime, pytz
|
import datetime, pytz
|
||||||
|
|
||||||
from typing import Dict, List, Optional, Set, Union
|
from typing import Dict, List, Optional, Set, Union, ClassVar
|
||||||
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator
|
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator, root_validator
|
||||||
|
|
||||||
from app.db_sql import redis_lookup_id_random
|
from app.db_sql import redis_lookup_id_random
|
||||||
from app.lib_general import log, logging
|
from app.lib_general import log, logging
|
||||||
@@ -16,22 +16,13 @@ class Event_Device_Base(BaseModel):
|
|||||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
id_random: Optional[str] = Field(
|
# --- Standardized Vision IDs (Strings) ---
|
||||||
# **base_fields['event_device_id_random'],
|
id: Optional[str] = Field(None, **base_fields['event_device_id_random'])
|
||||||
alias = 'event_device_id_random',
|
event_device_id: Optional[str] = Field(None, **base_fields['event_device_id_random'])
|
||||||
)
|
|
||||||
id: Optional[int] = Field(
|
|
||||||
alias = 'event_device_id'
|
|
||||||
)
|
|
||||||
|
|
||||||
account_id_random: Optional[str]
|
account_id: Optional[str] = Field(None, **base_fields['account_id_random'])
|
||||||
account_id: Optional[int]
|
event_id: Optional[str] = Field(None, **base_fields['event_id_random'])
|
||||||
|
event_location_id: Optional[str] = Field(None, **base_fields['event_location_id_random'])
|
||||||
event_id_random: Optional[str]
|
|
||||||
event_id: Optional[int]
|
|
||||||
|
|
||||||
event_location_id_random: Optional[str]
|
|
||||||
event_location_id: Optional[int]
|
|
||||||
|
|
||||||
code: Optional[str]
|
code: Optional[str]
|
||||||
|
|
||||||
@@ -131,45 +122,36 @@ class Event_Device_Base(BaseModel):
|
|||||||
|
|
||||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||||
|
|
||||||
#@validator('event_device_id_random', always=True)
|
@root_validator(pre=True)
|
||||||
# def event_device_id_random_copy(cls, v, values, **kwargs):
|
def map_v3_ids(cls, values):
|
||||||
# log.setLevel(logging.WARNING)
|
"""
|
||||||
# log.debug(locals())
|
Vision Transformer:
|
||||||
|
Map DB keys to clean API keys and strip internal integers.
|
||||||
|
"""
|
||||||
|
# 1. Map Random Strings to Clean Names
|
||||||
|
if rid := values.get('id_random') or values.get('event_device_id_random'):
|
||||||
|
values['id'] = rid
|
||||||
|
values['event_device_id'] = rid
|
||||||
|
|
||||||
# if values['id_random']:
|
if a_rid := values.get('account_id_random'):
|
||||||
# return values['id_random']
|
values['account_id'] = a_rid
|
||||||
# return None
|
if e_rid := values.get('event_id_random'):
|
||||||
|
values['event_id'] = e_rid
|
||||||
|
if el_rid := values.get('event_location_id_random'):
|
||||||
|
values['event_location_id'] = el_rid
|
||||||
|
|
||||||
@validator('id', always=True)
|
# 2. Prevent "Collision Population"
|
||||||
def event_device_id_lookup(cls, v, values, **kwargs):
|
for k in ['id', 'event_device_id', 'account_id', 'event_id', 'event_location_id']:
|
||||||
if isinstance(v, int) and v > 0: return v
|
if k in values and not isinstance(values[k], str) and values[k] is not None:
|
||||||
elif id_random := values.get('id_random'):
|
del values[k]
|
||||||
return redis_lookup_id_random(record_id_random=id_random, table_name='event_device')
|
|
||||||
return None
|
|
||||||
|
|
||||||
@validator('account_id', always=True)
|
return values
|
||||||
def account_id_lookup(cls, v, values, **kwargs):
|
|
||||||
if isinstance(v, int) and v > 0: return v
|
|
||||||
elif id_random := values.get('account_id_random'):
|
|
||||||
return redis_lookup_id_random(record_id_random=id_random, table_name='account')
|
|
||||||
return None
|
|
||||||
|
|
||||||
@validator('event_id', always=True)
|
# Fields that are part of the model (for reading) but should not be saved to the DB table
|
||||||
def event_id_lookup(cls, v, values, **kwargs):
|
fields_to_exclude_from_db: ClassVar[list] = ['event_cfg', 'event_location']
|
||||||
if isinstance(v, int) and v > 0: return v
|
|
||||||
elif id_random := values.get('event_id_random'):
|
|
||||||
return redis_lookup_id_random(record_id_random=id_random, table_name='event')
|
|
||||||
return None
|
|
||||||
|
|
||||||
@validator('event_location_id', always=True)
|
|
||||||
def event_location_id_lookup(cls, v, values, **kwargs):
|
|
||||||
if isinstance(v, int) and v > 0: return v
|
|
||||||
elif id_random := values.get('event_location_id_random'):
|
|
||||||
return redis_lookup_id_random(record_id_random=id_random, table_name='event_location')
|
|
||||||
return None
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
underscore_attrs_are_private = True
|
underscore_attrs_are_private = True
|
||||||
allow_population_by_field_name = True
|
allow_population_by_field_name = False
|
||||||
fields = base_fields
|
fields = base_fields
|
||||||
# ### END ### API Event Device Models ### Event_Device_Base() ###
|
# ### END ### API Event Device Models ### Event_Device_Base() ###
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import datetime, pytz
|
import datetime, pytz
|
||||||
|
|
||||||
from typing import Dict, List, Optional, Set, Union
|
from typing import Dict, List, Optional, Set, Union, ClassVar
|
||||||
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator
|
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator, root_validator
|
||||||
|
|
||||||
from app.db_sql import redis_lookup_id_random
|
from app.db_sql import redis_lookup_id_random
|
||||||
from app.lib_general import log, logging
|
from app.lib_general import log, logging
|
||||||
@@ -19,13 +19,15 @@ class Event_Session_Base(BaseModel):
|
|||||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
id_random: Optional[str] = Field(
|
# --- Standardized Vision IDs (Strings) ---
|
||||||
# **base_fields['event_session_id_random'],
|
id: Optional[str] = Field(None, **base_fields['event_session_id_random'])
|
||||||
alias = 'event_session_id_random',
|
event_session_id: Optional[str] = Field(None, **base_fields['event_session_id_random'])
|
||||||
)
|
|
||||||
id: Optional[int] = Field(
|
event_id: Optional[str] = Field(None, **base_fields['event_id_random'])
|
||||||
alias = 'event_session_id'
|
event_location_id: Optional[str] = Field(None, **base_fields['event_location_id_random'])
|
||||||
)
|
event_track_id: Optional[str] = Field(None, **base_fields['event_track_id_random'])
|
||||||
|
poc_event_person_id: Optional[str] = Field(None, **base_fields['event_person_id_random'])
|
||||||
|
poc_person_id: Optional[str] = Field(None, **base_fields['person_id_random'])
|
||||||
|
|
||||||
external_id: Optional[str] = Field(
|
external_id: Optional[str] = Field(
|
||||||
# alias = 'event_session_external_id'
|
# alias = 'event_session_external_id'
|
||||||
@@ -35,21 +37,6 @@ class Event_Session_Base(BaseModel):
|
|||||||
# alias = 'event_session_code'
|
# alias = 'event_session_code'
|
||||||
)
|
)
|
||||||
|
|
||||||
event_id_random: Optional[str]
|
|
||||||
event_id: Optional[int]
|
|
||||||
|
|
||||||
event_location_id_random: Optional[str]
|
|
||||||
event_location_id: Optional[int]
|
|
||||||
|
|
||||||
event_track_id_random: Optional[str]
|
|
||||||
event_track_id: Optional[int]
|
|
||||||
|
|
||||||
poc_event_person_id_random: Optional[str]
|
|
||||||
poc_event_person_id: Optional[int]
|
|
||||||
|
|
||||||
poc_person_id_random: Optional[str]
|
|
||||||
poc_person_id: Optional[int]
|
|
||||||
|
|
||||||
# General catchall for agreement or consent
|
# General catchall for agreement or consent
|
||||||
poc_agree: Optional[bool]
|
poc_agree: Optional[bool]
|
||||||
|
|
||||||
@@ -186,43 +173,51 @@ class Event_Session_Base(BaseModel):
|
|||||||
|
|
||||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||||
|
|
||||||
@validator('id', always=True)
|
@root_validator(pre=True)
|
||||||
def event_session_id_lookup(cls, v, values, **kwargs):
|
def map_v3_ids(cls, values):
|
||||||
if isinstance(v, int) and v > 0: return v
|
"""
|
||||||
elif id_random := values.get('id_random'):
|
Vision Transformer:
|
||||||
return redis_lookup_id_random(record_id_random=id_random, table_name='event_session')
|
Map DB keys to clean API keys and strip internal integers.
|
||||||
return None
|
"""
|
||||||
|
# 1. Map Random Strings to Clean Names
|
||||||
|
if rid := values.get('id_random') or values.get('event_session_id_random'):
|
||||||
|
values['id'] = rid
|
||||||
|
values['event_session_id'] = rid
|
||||||
|
|
||||||
@validator('event_id', always=True)
|
if e_rid := values.get('event_id_random'):
|
||||||
def event_id_lookup(cls, v, values, **kwargs):
|
values['event_id'] = e_rid
|
||||||
if isinstance(v, int) and v > 0: return v
|
if el_rid := values.get('event_location_id_random'):
|
||||||
elif id_random := values.get('event_id_random'):
|
values['event_location_id'] = el_rid
|
||||||
return redis_lookup_id_random(record_id_random=id_random, table_name='event')
|
if et_rid := values.get('event_track_id_random'):
|
||||||
return None
|
values['event_track_id'] = et_rid
|
||||||
|
if pep_rid := values.get('poc_event_person_id_random'):
|
||||||
|
values['poc_event_person_id'] = pep_rid
|
||||||
|
if pp_rid := values.get('poc_person_id_random'):
|
||||||
|
values['poc_person_id'] = pp_rid
|
||||||
|
|
||||||
@validator('event_location_id', always=True)
|
# 2. Prevent "Collision Population"
|
||||||
def event_location_id_lookup(cls, v, values, **kwargs):
|
for k in ['id', 'event_session_id', 'event_id', 'event_location_id', 'event_track_id', 'poc_event_person_id', 'poc_person_id']:
|
||||||
if isinstance(v, int) and v > 0: return v
|
if k in values and not isinstance(values[k], str) and values[k] is not None:
|
||||||
elif id_random := values.get('event_location_id_random'):
|
del values[k]
|
||||||
return redis_lookup_id_random(record_id_random=id_random, table_name='event_location')
|
|
||||||
return None
|
|
||||||
|
|
||||||
@validator('event_track_id', always=True)
|
return values
|
||||||
def event_track_id_lookup(cls, v, values, **kwargs):
|
|
||||||
if isinstance(v, int) and v > 0: return v
|
|
||||||
elif id_random := values.get('event_track_id_random'):
|
|
||||||
return redis_lookup_id_random(record_id_random=id_random, table_name='event_track')
|
|
||||||
return None
|
|
||||||
|
|
||||||
@validator('poc_person_id', always=True)
|
# Fields that are part of the model (for reading) but should not be saved to the DB table
|
||||||
def poc_person_id_lookup(cls, v, values, **kwargs):
|
fields_to_exclude_from_db: ClassVar[list] = [
|
||||||
if isinstance(v, int) and v > 0: return v
|
'file_count', 'internal_use_count', 'event_file_id_li_json', 'file_count_all',
|
||||||
elif id_random := values.get('poc_person_id_random'):
|
'event_name', 'event_start_datetime', 'event_end_datetime',
|
||||||
return redis_lookup_id_random(record_id_random=id_random, table_name='person')
|
'event_location_name', 'event_track_name',
|
||||||
return None
|
'event_abstract_list', 'event_badge_list', 'event_device_list',
|
||||||
|
'event_file_list', 'event_file_internal_use_list', 'event_location',
|
||||||
|
'event_location_list', 'event_person_list', 'event_presenter_cat',
|
||||||
|
'event_presentation_list', 'event_presenter_list', 'event_track',
|
||||||
|
'poc_event_person', 'poc_person',
|
||||||
|
'poc_person_external_id', 'poc_person_given_name', 'poc_person_family_name',
|
||||||
|
'poc_person_full_name', 'poc_person_primary_email', 'poc_person_passcode'
|
||||||
|
]
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
underscore_attrs_are_private = True
|
underscore_attrs_are_private = True
|
||||||
allow_population_by_field_name = True
|
allow_population_by_field_name = False
|
||||||
fields = base_fields
|
fields = base_fields
|
||||||
# ### END ### API Event Session Models ### Event_Session_Base() ###
|
# ### END ### API Event Session Models ### Event_Session_Base() ###
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class Journal_Base(BaseModel):
|
|||||||
|
|
||||||
description: Optional[str]
|
description: Optional[str]
|
||||||
description_html: Optional[str]
|
description_html: Optional[str]
|
||||||
description_json: Optional[str]
|
description_json: Optional[Union[Json, None]]
|
||||||
|
|
||||||
type_code: Optional[str] # 'log', 'tracking', 'personal', 'professional', etc
|
type_code: Optional[str] # 'log', 'tracking', 'personal', 'professional', etc
|
||||||
tags: Optional[str]
|
tags: Optional[str]
|
||||||
|
|||||||
60
tests/e2e/test_e2e_v3_event_device.py
Normal file
60
tests/e2e/test_e2e_v3_event_device.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
BASE_URL = "https://dev-api.oneskyit.com/v3/crud"
|
||||||
|
# Using the API key found in the database
|
||||||
|
API_KEY = "PHDXUJxx6IgmLNKxIBezTQ"
|
||||||
|
# Event Device ID found in the database
|
||||||
|
DEVICE_ID = "GZvFjgIIZQg"
|
||||||
|
|
||||||
|
def get_headers():
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-Aether-API-Key": API_KEY,
|
||||||
|
"X-No-Account-ID": "bypass"
|
||||||
|
}
|
||||||
|
return headers
|
||||||
|
|
||||||
|
def test_get_event_device():
|
||||||
|
print(f"--- Testing: Get Event Device by ID ({DEVICE_ID}) ---")
|
||||||
|
url = f"{BASE_URL}/event_device/{DEVICE_ID}"
|
||||||
|
|
||||||
|
headers = get_headers()
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
|
||||||
|
print(f"URL: {response.url}")
|
||||||
|
print(f"Status Code: {response.status_code}")
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
data = response.json().get('data', {})
|
||||||
|
print("✅ SUCCESS: Found Event Device.")
|
||||||
|
|
||||||
|
# Verify Vision ID pattern (Strings only for ID fields)
|
||||||
|
vision_fields = ["id", "event_device_id", "account_id", "event_id", "event_location_id"]
|
||||||
|
for field in vision_fields:
|
||||||
|
val = data.get(field)
|
||||||
|
print(f" Field '{field}': {val} (type: {type(val).__name__})")
|
||||||
|
if val is not None:
|
||||||
|
assert isinstance(val, str), f"Error: Field '{field}' should be a string, but got {type(val).__name__}"
|
||||||
|
|
||||||
|
# Check for any unexpected integer IDs
|
||||||
|
for key, val in data.items():
|
||||||
|
if key.endswith("_id") and not key.endswith("external_id"):
|
||||||
|
if isinstance(val, int):
|
||||||
|
print(f" ❌ FAILURE: Integer leakage detected in field '{key}': {val}")
|
||||||
|
# assert False, f"Integer leakage in '{key}'"
|
||||||
|
else:
|
||||||
|
print(f"❌ FAILURE: {response.text}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error during test: {e}")
|
||||||
|
print("-" * 40 + "\n")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(f"Starting Aether V3 Event Device E2E Tests against {BASE_URL}\n")
|
||||||
|
test_get_event_device()
|
||||||
|
print("Tests Complete.")
|
||||||
59
tests/e2e/test_e2e_v3_event_session.py
Normal file
59
tests/e2e/test_e2e_v3_event_session.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
BASE_URL = "https://dev-api.oneskyit.com/v3/crud"
|
||||||
|
# Using the API key found in the database
|
||||||
|
API_KEY = "PHDXUJxx6IgmLNKxIBezTQ"
|
||||||
|
# Event Session ID found in the database
|
||||||
|
SESSION_ID = "F0PZd1bNcuD"
|
||||||
|
|
||||||
|
def get_headers():
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-Aether-API-Key": API_KEY,
|
||||||
|
"X-No-Account-ID": "bypass"
|
||||||
|
}
|
||||||
|
return headers
|
||||||
|
|
||||||
|
def test_get_event_session():
|
||||||
|
print(f"--- Testing: Get Event Session by ID ({SESSION_ID}) ---")
|
||||||
|
url = f"{BASE_URL}/event_session/{SESSION_ID}"
|
||||||
|
|
||||||
|
headers = get_headers()
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
|
||||||
|
print(f"URL: {response.url}")
|
||||||
|
print(f"Status Code: {response.status_code}")
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
data = response.json().get('data', {})
|
||||||
|
print("✅ SUCCESS: Found Event Session.")
|
||||||
|
|
||||||
|
# Verify Vision ID pattern (Strings only for ID fields)
|
||||||
|
vision_fields = ["id", "event_session_id", "event_id", "event_location_id", "event_track_id", "poc_event_person_id", "poc_person_id"]
|
||||||
|
for field in vision_fields:
|
||||||
|
val = data.get(field)
|
||||||
|
print(f" Field '{field}': {val} (type: {type(val).__name__})")
|
||||||
|
if val is not None:
|
||||||
|
assert isinstance(val, str), f"Error: Field '{field}' should be a string, but got {type(val).__name__}"
|
||||||
|
|
||||||
|
# Check for any unexpected integer IDs
|
||||||
|
for key, val in data.items():
|
||||||
|
if key.endswith("_id") and not key.endswith("external_id"):
|
||||||
|
if isinstance(val, int):
|
||||||
|
print(f" ❌ FAILURE: Integer leakage detected in field '{key}': {val}")
|
||||||
|
else:
|
||||||
|
print(f"❌ FAILURE: {response.text}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error during test: {e}")
|
||||||
|
print("-" * 40 + "\n")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(f"Starting Aether V3 Event Session E2E Tests against {BASE_URL}\n")
|
||||||
|
test_get_event_session()
|
||||||
|
print("Tests Complete.")
|
||||||
Reference in New Issue
Block a user