
    'ZIh<                         d dl mZ d dlmZmZ d dlmZmZ d dlZd dl	m	Z	 d dl
Z
 e
j                  e      Z G d d      Zy)    )MongoClient)ConnectionFailureServerSelectionTimeoutError)MONGODB_URI
MONGODB_DBN)datetimec                   .    e Zd Zd ZddZd Zd ZddZy)	MongoHandlerc                 |   	 t        t        d      | _        | j                  j                          | j                  t           | _        | j
                  j                  | _        t        j                  d       y # t        t        f$ r1}t        j                  dt        |              t        d      d }~ww xY w)Ni  )serverSelectionTimeoutMSz!Successfully connected to MongoDBzFailed to connect to MongoDB: zDCould not connect to MongoDB. Please check your connection settings.)r   r   clientserver_infor   db
test_cases
collectionloggerinfor   r   errorstr	Exception)selfes     7D:\Projects\AI-TestCaseGenerator\utils\mongo_handler.py__init__zMongoHandler.__init__   s    		d%kDQDKKK##%kk*-DG"gg00DOKK;<!#>? 	dLL9#a&BCbcc	ds   A8A; ;B;
,B66B;Nc                 X   	 t        t        j                               }||t        j                         ||i d}| j
                  j                  |       t        j                  d|        |S # t        $ r1}t        j                  dt        |              t        d      d}~ww xY w)z+Save test case data and generate unique URL)_id	test_data
created_aturl_keyitem_idstatusz&Successfully saved test case with ID: zError saving test case: z$Failed to save test case to databaseN)r   uuiduuid4r   utcnowr   
insert_oner   r   r   r   )r   r   r    	unique_iddocumentr   s         r   save_test_casezMongoHandler.save_test_case   s    	DDJJL)I &&oo/$"H OO&&x0KK@LM 	DLL3CF8<=BCC	Ds   A,A/ /	B)8,B$$B)c                 
   	 | j                   j                  d|i      }|st        j                  d|        yt        j	                  d| d|        d}|rGd|vrCd|vr?| j                   j                  d|idd	| |ii       d
}t        j	                  d|        d}d|v r-t        |d   t              rd
}t        j	                  d|        |r|d   }d}t        |      D ]  \  }	}
|
j                  dd      }||k(  st        j	                  d|        | j                   j                  d|idd|	 d|ii      }|s%| j                   j                  d|idd	| |ii       d
} n |st        j                  d| d|        yy
d|v rnd|d   v rf|d   d   }d }d|v rJ|j                  d      }t        |      dk\  r+|d    d|d    d|d    }t        j	                  d|        t        |      D ]  \  }	}
|
j                  d|
j                  dd            }|
j                  d|
j                  dd            }|r||v rt        j	                  d|        | j                   j                  d|idd |	 d!|ii      }|s%| j                   j                  d|idd	| |ii       |j                  dkD  rt        j	                  d"|         y
|r|r||v rt        j	                  d#| d$|        | j                   j                  d|idd |	 d!|ii      }|s%| j                   j                  d|idd	| |ii       |j                  dkD  rt        j	                  d%|         y
|s|||v st        j	                  d&       | j                   j                  d|idd |	 d!|ii      }|r'|s%| j                   j                  d|idd	| |ii       |j                  dkD  st        j	                  d'|         y
 t        |      D ]  \  }	}
|
j                  d(      |k(  s|
j                  d)      |k(  s/t        j	                  d*|	        |
j                  d|
j                  dd            }| j                   j                  d|idd |	 d!|ii      }|r'|s%| j                   j                  d|idd	| |ii       |j                  dkD  c S  t        j                  d+| d|        yt        j                  d,| d-       y# t        $ r+}t        j                  d.t        |              Y d }~yd }~ww xY w)/Nr   z No document found with url_key: Fz/Updating status for test case with identifier 'z' in document ./$setzstatus.Tz-Updated central status dictionary for title: r   z Shared view update detected for Title z"Found shared view match by title: z
test_data.z.StatuszNo test case found with title 'z' in shared view document r   _   r         zExtracted UI identifier: titleContentcontentzFound match in title: ztest_data.test_cases.z.statusz/Successfully updated status by title match for zFound match for UI identifier z in title: z7Successfully updated status by UI identifier match for zFound match in contentz1Successfully updated status by content match for test_case_idzTest Case IDzFound direct ID match at index zNo test case found matching 'z	Document z has no test casesz!Error updating test case status: )r   find_oner   r   r   
update_one
isinstancelist	enumerategetwarningsplitlenmodified_countr   r   )r   r   r6   r!   doctitle_foundis_shared_viewr   foundidxtcr3   resultui_identifierpartsr5   r   s                    r   update_test_case_statusz$MongoHandler.update_test_case_status*   s   e	//**Iw+?@C?yIJ KKI,Wefmenop  K < 7C|<S**(~6?@ #KL>Z[ #Nc!j[1A4&H!%>wiHI -
 (4 GCFF7B/E ,&H$PQ!%!;!;&0#
3%w&?%HI"  + OO66!*G 4!'GE7*;V)D E
 !%'* NN%D\NRlmtlu#vw #K8H(H -l;
 !%,&(..s3E5zQ+08*AeAhZqq
(K&?$OP  )4 ;(GCFF7BFF7B,?@E ffYy"0EFG !6&<UG$DE!%!;!;&0#(=cU'&JF%ST"  + OO66!*G 4!'GE7*;V)D E
 "0014"KK*YZfYg(hi#' %=E3I&D]OS^_d^e$fg!%!;!;&0#(=cU'&JF%ST"  + OO66!*G 4!'GE7*;V)D E
 "0014"KK*aboap(qr#' <7#:&<>!%!;!;&0#(=cU'&JF%ST" ! OO66!*G 4!'GE7*;V)D E
 "0014"KK*[\h[i(jk#'w;(|  )4 9GCvvn-=AW[gAg&EcU$KL "ww0C D!%!;!;&0#(=cU'&JF%ST" ! OO66!*G 4!'GE7*;V)D E
  &44q88#9( !>|nN[bZcde7)3EFG 	LL<SVHEF	sd   7T C
T BT ET BT 6T :T  A3T 5T :T 
BT $T T 	U$!U

Uc                     	 | j                   j                  d|i      }|st        j                  d|        |S # t        $ r1}t        j                  dt        |              t	        d      d}~ww xY w)z"Retrieve test case data by URL keyr    No test case found for URL key: zError retrieving test case: z*Failed to retrieve test case from databaseN)r   r7   r   r=   r   r   r   )r   r   rG   r   s       r   get_test_casezMongoHandler.get_test_case   ss    	J__--y'.BCF!A'KLM 	JLL7Ax@AHII	Js   8; 	A5,A00A5c           
      T   	 | j                   j                  d|i      }|st        j                  d|        yd|v rt        j	                  d|d           nt        j	                  d       d|v rpt        |d   t              r]t        |d         D ]K  \  }}|j                  dd	      }|j                  d
d	      }|s-t        j	                  d| d| d| d       M nd|v rd|d   v rt        |d   d         D ]k  \  }}|j                  d|j                  dd	            }|j                  d
|j                  dd	            }|sMt        j	                  d| d| d| d       m d|v r1|d   r,|s*t        j	                  dt        |d          d       |d   S i }d|v rdt        |d   t              rQt        j	                  d       |d   D ]3  }d|v s|j                  dd	      }|j                  d
d	      }|s/|||<   5 nyd|v rud|d   v rnt        j	                  d       |d   d   D ]N  }|j                  d|j                  dd	            }|j                  d
|j                  dd	            }|sJ|||<   P |rFt        j	                  dt        |       d|        | j                   j                  d|idd|ii       t        j	                  dt        |       d|        |S # t        $ r+}	t        j                  dt        |	              Y d}	~	yd}	~	ww xY w)zRetrieve all status values for test cases in a document
        
        Args:
            url_key: The unique URL key for the document
            force_refresh: If True, forces a direct database query to get fresh data
        r   rL   Nr!   zSTATUS DICT in MongoDB: z"NO STATUS DICT in MongoDB documentr   r-   r.   StatuszSHARED VIEW TC[z
]: Title='z', Status=''r   r3   zMAIN VIEW TC[zFound z# status values in status dictionaryz.Building status values from shared view formatz,Building status values from main view formatz%UPDATING status dict in MongoDB with z	 values: r,   z
Returning z status values for z*Error retrieving test case status values: )r   r7   r   r=   r   r9   r:   r;   r<   r?   r8   r   r   r   )
r   r   force_refreshrG   irF   r3   r!   status_valuesr   s
             r   get_test_case_status_valuesz(MongoHandler.get_test_case_status_values   sW   K	
 __--y'.BCF!A'KL 6!6vh7G6HIJ@A f$F;4G)N&vk':; `EArFF7B/EVVHb1FoaS
5'U[T\\]$^_	` &<6+;N+N&vk':<'HI ^EArFF7BFF7B,?@EVVHbffXr.BCFmA3j{SYRZZ[$\]	^ 6!fX&6}fS)9%:$;;^_`h'' M f$F;4G)NLM - :B"} "w 3!#"!5 39M%0: &<6+;N+NJK -l; 6BFF7BFF7B,?@EVVHbffXr.BCF/5e,	6 CCDVCWW`an`opq**(h67
 KK*S%7$88KG9UV   	LLEc!fXNO	sE   7K3 BK3 B
K3 AK3 ":K3 &K3 A9K3 >A4K3 3	L'<!L""L')N)F)__name__
__module____qualname__r   r(   rJ   rM   rT        r   r
   r
   
   s!    
dD&fP	JRrY   r
   )pymongor   pymongo.errorsr   r   config.settingsr   r   r"   r   logging	getLoggerrU   r   r
   rX   rY   r   <module>r_      s5     I 3   			8	$e erY   