
    ZThG                       d dl mZ d dlmZmZ d dlmZ d dlZd dlZd dlm	Z	 d dl
mZ d dl
mZ d dl
mZ d dlZd d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZm Z  d dl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d dl(m)Z) d dl*mZ d dl+m,Z,m-Z-  eej\                  j^                  j`                        ZddZ1ddZ2ddZ3ddZ4ddZ5ddZ6ddZ7ddZ8y)    )annotations)datetimetimezone)	MagicMockN)MockFixture)
FlowConfig)IngestionConfig) ListIngestionConfigFlowsResponse)MockChannel)
_mock_path)SiftAPIDeprecationWarning)IngestionValidationError)_IngestionServiceImpl)create_flow_configscreate_ingestion_config"get_ingestion_config_by_client_keyget_ingestion_config_flows)ChannelConfigChannelDataTypeChannelValuedouble_valueint32_valuestring_value)TelemetryConfig)$RuleActionCreateDataReviewAnnotation
RuleConfigc                   d}t        dt        dt        j                        g      }t        dt        dt        j                        g      }|j	                  t
              g}t        dd	||g
      }| j                  t        t                    }||_
        | j                  t        t                    }d|_
        t               }t        j                  |||       |j                  |||g       y)z
    Tests to ensure that if a user adds a new flow to the telemetry config,
    `update_flow_configs` will ensure that it gets created.
    ingestion-config-idflow_a	channel_aname	data_typer"   channelsflow_b	channel_bz	my-configzmy-key
asset_nameingestion_client_keyflowsN)r   r   r   DOUBLEas_pbFlowConfigPbr   patchr   r   return_valuer   r   r   _update_flow_configsassert_called_once_with)	mockeringestion_config_idr   r&   flows_from_apitelemetry_config$mock_get_ingestion_config_flow_namesmock_create_flow_configsmock_channels	            l/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_py/ingestion/_internal/ingest_test.py8test_ingestion_service_update_flow_configs_updates_flowsr;   0   s    
 0 )00
F  )00
F ll<01N&%v ,2<<
C]8^+_(8F(5%||J7J,KL,0)=L..|=PRbc44\CVY_X`a    c                j   t        ddd      }t        d|j                        }| j                  t	        t
                    }||_        | j                  t	        t                    }t               }t        j                  ||      }|j                          |j                  |j                  k(  sJ y)z`
    Ensure that if an ingestion config is queried by client key, a new one is not created.
    r   asset-id
client-keyr4   asset_id
client_keyr)   )r)   r*   N)IngestionConfigPbr   rB   r/   r   r   r0   r   r   r   _get_or_create_ingestion_configassert_not_calledr4   r3   mock_ingestion_configmock_telemetry_config'mock_get_ingestion_config_by_client_keymock_create_ingestion_configr9   ingestion_configs          r:   Htest_ingestion_service_get_or_create_ingestion_config_retrieves_existingrL   ^   s    
 .1 ,2==
 /5ll56/+ <Q+8#)<<
;R0S#T =L,LL
 !224//3H3\3\\\\r<   c                   t        ddd      }t        d|j                  d      }| j                  t	        t
                    }d|_        | j                  t	        t                    }||_        t               }t        j                  ||      }|j                  ||j                  g |j                  |j                         |j                  |j                  k(  sJ y)	z
    Ensure that if an ingestion config does not exist for a given client key then a new
    ingestion config is created based on the telemetry config.
    r   r>   r?   r@   r)   z	my-org-id)r)   r*   organization_idN)rC   r   rB   r/   r   r   r0   r   r   r   rD   r2   r)   rN   r4   rF   s          r:   Itest_ingestion_service_get_or_create_ingestion_config_create_if_not_existrO      s     .1 ,2==# /5ll56/+ <@+8#)<<
;R0S#T 0E -=L,LL
 !88((
((-- //3H3\3\\\\r<   c                8   t        j                  t        d      5  t        ddt        j
                        }ddd       t        dt        j                        }t        d	t        j                        }t        d
dt        d|g      t        d|g      t        d|g      g      }t        ddd      }| j                  j                  t        d      }||_        | j                  d      }|j                  D cg c]  }|j!                  t"               c}|_        | j                  j                  t        d      }	d|	_        t%               }
| j                  d      5  t        |
|      }ddd       t        j&                  t(        d      5  j+                  dt-        j.                  t0        j2                        t5        d	t7        d            g       ddd       t        j&                  t(        d      5  j+                  dt-        j.                  t0        j2                        t5        d	t7        d            t5        d	t7        d            g       ddd       t        j&                  t(        d      5  j+                  dt-        j.                  t0        j2                        t5        d	t9        d            g       ddd       t        j&                  t(        d       5  j+                  dt-        j.                  t0        j2                        t5        dt;        d            g       ddd       y# 1 sw Y   xY wc c}w # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)!ze
    Tests all the different validations that happen when trying to create an ingestion request.
    	componentmatchvoltagemotorr"   rQ   r#   Npressurer!   logsmy-assetmy-client-keyreadingr$   logr(   r   my-asset-idr@   rD   =sift_py.ingestion._internal.ingest.get_ingestion_config_flowsr1   .sift_py.ingestion._internal.ingest.RuleServicechannelconfigzcould not be foundlergfoobar)channel_namevalue	flow_name	timestampchannel_valueszmultiple valuesExpected value    zUnexpected channel)pytestwarnsr   r   r   r,   INT_64STRINGr   r   rC   r/   objectr   r0   r+   r-   r.   r   raisesr   try_create_ingestion_requestr   nowr   utcr   r   r   r   )r3   voltage_channelpressure_channellogs_channelr6   rG   #mock_get_or_create_ingestion_configmock_ingestion_config_flowsfmock_update_flow_configstransport_channelsvcs               r:   ?test_ingestion_service_try_create_ingestion_request_validationsr      s9   
 
/{	C 
'%,,

 %!(( !!((L
 ',)+;< *+ &
& .1" +1,,*=*=@+' 8M'4"(,,G# (8'='=0"#0,  &||223HJ`a,0)#	F	G 
#%#

 
/7K	L 
((ll8<<0&X8NO 	) 	

 
/7H	I 
((ll8<<0&X8NO&X8NO 	) 	

 
/7G	H 
((ll8<<0&BH 	) 	

 
/7K	L 
((ll8<<0)<;KL 	) 	

 

 
`0
 

 

 

 

 
sQ   MM'MAM*A M7	AN7ANMM'*M47NNNc           
        t        ddt        j                        }t        dt        j                        }t        dt        j                        }t        ddd	d
|j                         dgt        ddg      d      }t        ddd	d
|j                         dgt        ddg      d      }t        ddd      }| j                  j                  t        d      }||_        | j                  j                  t        d      }d|_        t        d|j                  t        d||g      t        d|g      t        d|g      g||g      }	| j                  d      }
|	j                   D cg c]  }|j#                  t$               c}|
_        t'               }| j                  d       5  t        ||	!      }|j(                  D ]  }|j*                  dgk(  rJ  	 ddd       yc c}w # 1 sw Y   yxY w)"z
    Ensures that rules are created and updated to include the asset from the
    telemetry config when the ingestion service is initialized.
    rT   rU   rV   rW   r!   rX   voltage_rule z$1 > 10z$1)channel_referencechannel_identifierzbob@example.com)assigneetagszvoltage-rule-key)r"   description
expressionchannel_referencesactionrule_client_keypressure_rule	barometerzpressure-rule-keyzmy-ingestion-configr]   rZ   r@   rD   r1   NrY   r[   r$   r\   )r)   r*   r+   rulesr^   r_   r`   )r   r   r,   ro   rp   r   fqnr   rC   r/   rq   r   r0   r   rB   r   r+   r-   r.   r   r   asset_names)r3   rv   rw   rx   rule_on_voltagerule_on_pressurerG   ry   r|   r6   rz   r{   r9   r~   rules                  r:   &test_ingestion_service_init_with_rulesr     sC   
 $!((O
 %!(( !!((L
 !"&o>Q>Q>ST
 4&
 +O ""&>N>R>R>TU
 4&
 , .1" +1,,*=*=@+' 8M'4%||223HJ`a,0)&2==)+;< *+ &
  01#( #),,G# (8'='=0"#0, =L	F	G 4# #
 II 	4D##
|333	44 404 4s   /G).-G.G..G7c           
        t        dt        j                        }t        dt        j                        }t        dt        j                        }t        ddt        d|||g      g	      }| j                  d
      }|j                  D cg c]  }|j                  t               c}|_        t        ddd      }| j                  j                  t        d      }||_        | j                  j                  t        d      }	d|	_        t               }
| j                  d      5  t        |
|      }ddd       t!        j"                  t$        j&                        }j)                  d|ddiddiddig      }|j*                  dk(  sJ t-        |j.                        dk(  sJ |j.                  d   j0                  dk(  sJ |j.                  d   j2                  dk(  sJ |j.                  d   j4                  dk(  sJ t7        j8                  t:        d      5  |j)                  d|dd iddiddig       ddd       t7        j8                  t:        d      5  |j)                  d|ddiddidd!ig       ddd       t7        j8                  t:        d"      5  |j)                  d|ddiddig       ddd       yc c}w # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   fxY w# 1 sw Y   yxY w)#z
    Tests that try_create_ingestion_request correctly handles ordered channel values
    with different data types when channel_values_by_fqn is empty (using index-based validation).
    rT   r!   countstatusrY   rZ   mixed_typesr$   r(   r^   r   r]   r@   rD   r1   Nr_   r`   doubleg      ^@int64*   stringactiverg      r         rk   rR   znot a numberg      ?zExpected 3 channel values)r   r   r,   ro   rp   r   r   r/   r+   r-   r.   r0   rC   rq   r   r   r   rt   r   ru   rs   flowlenrj   r   r   r   rm   rr   r   )r3   rv   count_channelstatus_channelr6   rz   r{   rG   ry   r|   r}   r~   ri   requests                 r:   Btest_ingestion_service_try_create_ingestion_request_ordered_valuesr   ~  s?   
 $!((O "!((M #!((N
 ',")=.I
	 #),,G# (8'='=0"#0, .1" +1,,*=*=@+' 8M'4%||223HJ`a,0)#	F	G 
#%#

 X\\*I.. ubMx 
 / G <<=(((w%%&!+++!!!$++u444!!!$**b000!!!$++x777 
/7G	H 

((# >*"8$	 	) 		


 
/7G	H 

((#5!"3	 	) 		


 
/7R	S 	
((#5!" 	) 	
	
 	
S0(
 
8

 



 

	
 	
s6   K)K
K K#!K/
KK #K,/K8c                   d}t               }t        t        d      t        d      gd      }t               }||j                  _        | j                  d      }||_        t        ||      }|j                  j                          t        |      dk(  sJ |d	   j                  dk(  sJ |d
   j                  dk(  sJ y)zW
    Tests that get_ingestion_config_flows correctly retrieves flows from the API.
    test-ingestion-config-idr   r"   r&   r   r+   next_page_tokenGsift_py.ingestion._internal.ingestion_config.IngestionConfigServiceStubr   r   r   N)r   r
   r.   r   ListIngestionConfigFlowsr0   r/   r   assert_called_oncer   r"   )r3   r4   r9   mock_responseservice_mock	stub_mockr+   s          r:   -test_get_ingestion_config_flows_returns_flowsr     s     5=L4*Lh,GHM
 ;L9FL))6QI *I&|5HIE))<<>u:??8==H$$$8==H$$$r<   c                `   d}t               }t        t        d      t        d      gd      }t        j                         }d |_        t               }||g|j                  _        | j                  d      }||_
        t        ||      }|j                  j                  d	   j                  d	   j                  d
k(  sJ |j                  j                  d   j                  d	   j                  dk(  sJ t        |      dk(  sJ |d	   j                   dk(  sJ |d   j                   dk(  sJ y)zU
    Tests that get_ingestion_config_flows falls back to using a page_size of 1.
    r   r   r   r&   r   r   c                 6    t         j                  j                  S )N)grpc
StatusCodeRESOURCE_EXHAUSTED r<   r:   <lambda>zCtest_get_ingestion_config_flows_updates_page_size.<locals>.<lambda>  s    T__?? r<   r   r   i  r   r   N)r   r
   r.   r   RpcErrorcoder   r   side_effectr/   r0   r   
mock_callsargs	page_sizer   r"   )r3   r4   r9   r   	rpc_errorr   r   r+   s           r:   1test_get_ingestion_config_flows_updates_page_sizer     s$    5=L4*Lh,GHM
 I?IN;L9BM8RL))5QI *I&|5HIE00;;A>CCAFPPTYYYY00;;A>CCAFPPTUUUUu:??8==H$$$8==H$$$r<   )r3   r   )9
__future__r   r   r   unittest.mockr   r   rm   pytest_mockr   /sift.ingestion_configs.v2.ingestion_configs_pb2r   r.   r	   rC   r
   "sift_py.ingestion._internal.ingestsift_py#sift_py._internal.test_util.channelr   sift_py._internal.test_util.fnr   _mock_path_impsift_py.errorr   !sift_py.ingestion._internal.errorr   r   ,sift_py.ingestion._internal.ingestion_configr   r   r   r   sift_py.ingestion.channelr   r   r   r   r   r   "sift_py.ingestion.config.telemetryr   sift_py.ingestion.flowsift_py.ingestion.rule.configr   r   	ingestion	_internalingestr;   rL   rO   r   r   r   r   r   r   r<   r:   <module>r      s    " ' #   # * ; G 3 F   ? - ZG--77>>?
+b\ ]F)]Xk
\`4Fr
j%8%r<   