Coverage for functions \ flipdare \ search \ result \ typesense_model_loader.py: 57%

21 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-05-08 12:22 +1000

1#!/usr/bin/env python 

2# Copyright (c) 2026 Flipdare Pty Ltd. All rights reserved. 

3# 

4# This file is part of Flipdare's proprietary software and contains 

5# confidential and copyrighted material. Unauthorised copying, 

6# modification, distribution, or use of this file is strictly 

7# prohibited without prior written permission from Flipdare Pty Ltd. 

8# 

9# This software includes third-party components licensed under MIT, 

10# BSD, and Apache 2.0 licences. See THIRD_PARTY_NOTICES for details. 

11# 

12 

13 

14from pydantic import ValidationError 

15from flipdare.app_log import LOG 

16from flipdare.app_types import TypesenseDict 

17from flipdare.error.app_error import CodePathError 

18from flipdare.search.result.typesense_models import TResultModel 

19from flipdare.util.debug_util import stringify_debug 

20 

21 

22class TypesenseModelLoader: 

23 def __init__(self, result: TypesenseDict) -> None: 

24 self.result = result 

25 

26 def load(self) -> TResultModel: 

27 try: 

28 return TResultModel.model_validate(self.result) 

29 except ValidationError as e: 

30 # Iterate through each specific error 

31 detail = [] 

32 

33 for error in e.errors(): 

34 field = " -> ".join(str(loc) for loc in error["loc"]) 

35 message = error["msg"] 

36 detail.append(f"Field: {field}, Error: {message}") 

37 

38 msg = ( 

39 "Failed to parse search result JSON into TResultModel.\n" 

40 "Validation errors:\n" + "\n".join(detail) + "\n" 

41 "Original JSON:\n" + stringify_debug(self.result) 

42 ) 

43 LOG().error(msg) 

44 raise CodePathError(msg) from e