
    i5i2                     v    d Z ddlZddlmc mZ ddlZddlm	Z	m
Z
mZ ddlmZmZ  G d d      Z G d d      Zy)	zTests for KPI tree.    N)KPINodeConfigNodeTypeKPICategory)KPITreeKPINodec                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	TestKPINodezTests for KPINode.c                    t        ddt        j                        }d|_        d|_        |j
                  }d}||k(  }|st        j                  d|fd||f      d	t        j                         v st        j                  |      rt        j                  |      nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}y)z"Test achievement rate calculation.testTestidname	node_typeP   d   g      T@==)z8%(py2)s
{%(py2)s = %(py0)s.achievement_rate
} == %(py5)snodepy0py2py5assert %(py7)spy7Nr   r   KPIvaluetargetachievement_rate
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanationselfr   @py_assert1@py_assert4@py_assert3@py_format6@py_format8s          d/var/www/tkim.planitai.co.jp/gemegg/20251207-make-pdf-report/project/planitai-kpi/tests/test_tree.pytest_achievement_ratez!TestKPINode.test_achievement_rate   s    &vF
$$,,$,,,,$,,,,,,t,,,t,,,$,,,,,,,,,,    c                    t        ddt        j                        }d|_        |j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx}x}}y)z%Test achievement rate when no target.r   r   r   r   Nisz8%(py2)s
{%(py2)s = %(py0)s.achievement_rate
} is %(py5)sr   r   r   r   )r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   s          r0   test_achievement_rate_no_targetz+TestKPINode.test_achievement_rate_no_target   s    &vF
$$,,$,,,,$,,,,,,t,,,t,,,$,,,,,,,,,,r2   c                    t        ddt        j                        }d|_        d|_        |j
                  }d}||u }|st        j                  d|fd||f      d	t        j                         v st        j                  |      rt        j                  |      nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}}y)z'Test achievement rate with zero target.r   r   r   r   r   Nr4   r6   r   r   r   r   r   r)   s          r0   !test_achievement_rate_zero_targetz-TestKPINode.test_achievement_rate_zero_target   s    &vF
$$,,$,,,,$,,,,,,t,,,t,,,$,,,,,,,,,,r2   c                    t        ddt        j                        }d|_        d|_        |j
                  }d}| }||k(  }|st        j                  d|fd||f      d	t        j                         v st        j                  |      rt        j                  |      nd	t        j                  |      t        j                  |      d
z  }dd|iz  }t        t        j                  |            dx}x}x}}y)zTest gap calculation.r   r   r   r   r      r   )z,%(py2)s
{%(py2)s = %(py0)s.gap
} == -%(py5)sr   r   assert %(py8)spy8N)r   r   r   r   r   gapr!   r"   r#   r$   r%   r&   r'   r(   )r*   r   r+   r,   @py_assert6r-   @py_format7@py_format9s           r0   test_gapzTestKPINode.test_gap#   s    &vF
xxBB3x3x3ttxBr2   c                    t        ddt        j                        }t        ddt        j                        }|j	                  |       |j
                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx}x}}|j
                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            dx}x}}y)zTest leaf node detection.parentParentr   childChildFr4   )z/%(py2)s
{%(py2)s = %(py0)s.is_leaf
} is %(py5)sr   r   r   NT)r   r   r   INPUT	add_childis_leafr!   r"   r#   r$   r%   r&   r'   r(   )r*   rD   rF   r+   r,   r-   r.   r/   s           r0   test_is_leafzTestKPINode.test_is_leaf+   s   H8x||L7HNNK~~&&~&&&&~&&&&&&v&&&v&&&~&&&&&&&&&&}}$$}$$$$}$$$$$$u$$$u$$$}$$$$$$$$$$r2   c                    t        ddt        j                        }t        ddt        j                        }t        ddt        j                        }|j                  |       |j                  |       |j                  }d}||k(  }|st        j                  d	|fd
||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}|j                  }d}||k(  }|st        j                  d	|fd
||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}|j                  }d}||k(  }|st        j                  d	|fd
||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}y)zTest depth calculation.rootRootr   l1L1l2L2r   r   )z-%(py2)s
{%(py2)s = %(py0)s.depth
} == %(py5)sr   r   r   N   level1   level2)r   r   KGIr   rH   rI   depthr!   r"   r#   r$   r%   r&   r'   r(   )	r*   rM   rT   rV   r+   r,   r-   r.   r/   s	            r0   
test_depthzTestKPINode.test_depth5   s   &vFDtx||DDtx~~Fv zzQzQzQttzQ|| q |q    |q      v   v   |   q       || q |q    |q      v   v   |   q       r2   N)
__name__
__module____qualname____doc__r1   r7   r9   rB   rK   rY    r2   r0   r	   r	   	   s#    ---%!r2   r	   c                   v    e Zd ZdZej
                  d        Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Zy)TestKPITreezTests for KPITree.c           	      B   t        ddt        j                  dddg      t        ddt        j                  dd	d
g      t        ddt        j                        t        d	dt        j                  t
        j                        t        d
dt        j                        gS )zSample tree configuration.revenueRevenuez{contracts} * {avg_price}	contracts	avg_price)r   r   typeformulachildren	Contractsz{leads} * {conversion}leads
conversionz	Avg Pricer   r   rf   Leads)r   r   rf   category
Conversion)r   r   rW   r   rH   r   	MARKETING)r*   s    r0   tree_configzTestKPITree.tree_configF   s     \\3%{3  \\0!<0  ^^
 ^^$..	 !^^5
 	
r2   c                 h   t        j                  |      }|j                  }|j                  }d}||k(  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}x}x}}|j                  }	t        |	      }
d	}|
|k(  }|s
t	        j
                  d|fd
|
|f      dt        j                         v st	        j                  t              rt	        j                  t              nddt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |	      t	        j                  |
      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            dx}	x}
x}}y)z#Test tree construction from config.rb   r   )zD%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.root
}.id
} == %(py7)streer   r   py4r   assert %(py9)spy9N   )zK%(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.nodes
})
} == %(py8)slen)r   py1py3r   r=   assert %(py10)spy10)r   from_configrM   r   r!   r"   r#   r$   r%   r&   r'   r(   nodesry   )r*   rq   rs   r+   r-   r?   @py_assert5r/   @py_format10@py_assert2r,   @py_assert7rA   @py_format11s                 r0   test_from_configzTestKPITree.test_from_configj   s3   "";/yy(y||(y(|y((((|y((((((t(((t(((y(((|(((y(((((((::#s:#!#!####!######s###s######4###4###:######!#######r2   c           	      T   t        j                  |      }|j                  }|j                  }t        j
                  }||k(  }|s
t        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}x}}|j                  }	|	j                  }
t        |
      }d	}||k(  }|st        j                  d|fd
||f      dt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |	      t        j                  |
      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}	x}
x}x}}|j                  j                  D cg c]  }|j                    }}d}||v }	|	st        j                  d|	fd||f      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            dx}}	d}||v }	|	st        j                  d|	fd||f      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            dx}}	yc c}w )zTest tree structure is correct.r   )zd%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.root
}.node_type
} == %(py8)s
{%(py8)s = %(py6)s.KGI
}rs   r   )r   r   ru   py6r=   r|   r}   NrU   )zi%(py7)s
{%(py7)s = %(py0)s(%(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py1)s.root
}.children
})
} == %(py10)sry   )r   rz   r{   r   r   r}   zassert %(py12)spy12rd   inz%(py1)s in %(py3)s	child_idsrz   r{   assert %(py5)sr   re   )r   r~   rM   r   r   rW   r!   r"   r#   r$   r%   r&   r'   r(   rh   ry   r   )r*   rq   rs   r+   r-   r   r   rA   r   r   r,   r?   @py_assert9@py_assert8@py_format13cr   @py_assert0@py_format4r.   s                       r0   test_tree_structurezTestKPITree.test_tree_structureq   sS   "";/ yy2y""2hll2"l2222"l222222t222t222y222"222222h222h222l222222299+9%%+s%&+!+&!++++&!++++++s+++s++++++4+++4+++9+++%+++&+++!+++++++ $(99#5#56aQTT6	6'{i''''{i'''{''''''i'''i''''''''{i''''{i'''{''''''i'''i''''''' 7s   P%c                     t        ddt        j                        g}t        j                  t
        d      5  t        j                  |       ddd       y# 1 sw Y   yxY w)z#Test that missing KGI raises error.r   r   rl   zNo KGI node foundmatchN)r   r   r   pytestraises
ValueErrorr   r~   r*   configs     r0   test_no_kgi_raiseszTestKPITree.test_no_kgi_raises~   sO     V&x||D
 ]]:-@A 	('	( 	( 	(s   AA!c                     t        ddt        j                        t        ddt        j                        g}t        j                  t
        d      5  t        j                  |       ddd       y# 1 sw Y   yxY w)	z$Test that multiple KGIs raise error.kgi1KGI1rl   kgi2KGI2zMultiple KGI nodes foundr   Nr   r   rW   r   r   r   r   r~   r   s     r0   test_multiple_kgi_raisesz$TestKPITree.test_multiple_kgi_raises   s_     V&x||DV&x||D

 ]]:-GH 	('	( 	( 	(s   A33A<c                     t        ddt        j                  dg      g}t        j                  t
        d      5  t        j                  |       ddd       y# 1 sw Y   yxY w)z/Test that invalid child reference raises error.rb   rc   nonexistentr   r   rf   rh   z	not foundr   Nr   r   s     r0   test_invalid_child_raisesz%TestKPITree.test_invalid_child_raises   sX     \\'	
 ]]:[9 	('	( 	( 	(s   AA#c                    t        j                  |      }|j                  ddd       |j                  d   }|j                  }d}||k(  }|st        j                  d|fd||f      t        j                  |      t        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d
x}x}x}}|j                  d   }|j                  }d}||k(  }|st        j                  d|fd||f      t        j                  |      t        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d
x}x}x}}y
)zTest setting node values.r   g      ?)rj   rk   rj   r   )z-%(py3)s
{%(py3)s = %(py1)s.value
} == %(py6)srz   r{   r   r<   r=   Nrk   )
r   r~   
set_valuesr   r   r!   r"   r&   r'   r(   	r*   rq   rs   r   r   r   r,   r@   rA   s	            r0   test_set_valueszTestKPITree.test_set_values   s    "";/#S9:zz'"/"((/C/(C////(C///"///(///C///////zz,'4'--44-4444-444'444-4444444444r2   c                    t        j                  |      }|j                  ddd       |j                  d   }|j                  }d}||k(  }|st        j                  d|fd||f      t        j                  |      t        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d
x}x}x}}|j                  d   }|j                  }d}||k(  }|st        j                  d|fd||f      t        j                  |      t        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d
x}x}x}}y
)zTest setting node targets.   i@B )rj   rb   rj   r   )z.%(py3)s
{%(py3)s = %(py1)s.target
} == %(py6)sr   r<   r=   Nrb   )
r   r~   set_targetsr   r   r!   r"   r&   r'   r(   r   s	            r0   test_set_targetszTestKPITree.test_set_targets   s   "";/37;<zz'"0"))0S0)S0000)S000"000)000S0000000zz)$6$++6w6+w6666+w666$666+666w6666666r2   c                    t        j                  |      }|j                         }|D cg c]  }|j                   }}d}||v }|st	        j
                  d|fd||f      t	        j                  |      dt        j                         v st	        j                  |      rt	        j                  |      nddz  }dd|iz  }	t        t	        j                  |	            dx}}d	}||v }|st	        j
                  d|fd||f      t	        j                  |      dt        j                         v st	        j                  |      rt	        j                  |      nddz  }dd|iz  }	t        t	        j                  |	            dx}}d
}||v }|st	        j
                  d|fd||f      t	        j                  |      dt        j                         v st	        j                  |      rt	        j                  |      nddz  }dd|iz  }	t        t	        j                  |	            dx}}d}||v}|st	        j
                  d|fd||f      t	        j                  |      dt        j                         v st	        j                  |      rt	        j                  |      nddz  }dd|iz  }	t        t	        j                  |	            dx}}yc c}w )zTest getting leaf nodes.re   r   r   leaf_idsr   r   r   Nrj   rk   rb   )not in)z%(py1)s not in %(py3)s)r   r~   
get_leavesr   r!   r"   r&   r#   r$   r%   r'   r(   )
r*   rq   rs   leavesnr   r   r   r   r.   s
             r0   test_get_leaveszTestKPITree.test_get_leaves   s   "";/""()QADD))&{h&&&&{h&&&{&&&&&&h&&&h&&&&&&&"w(""""w("""w""""""("""("""""""'|x''''|x'''|''''''x'''x'''''''(y((((y(((y((((((((((((((((	 *s   Kc                    t        j                  |      }|j                  ddi       |j                         }|d   }d}||k(  }|slt	        j
                  d|fd||f      t	        j                  |      t	        j                  |      dz  }dd	|iz  }t        t	        j                  |            d
x}x}}d}||v }|st	        j
                  d|fd||f      t	        j                  |      dt        j                         v st	        j                  |      rt	        j                  |      nddz  }	dd|	iz  }
t        t	        j                  |
            d
x}}|d   }t        |      }d}||k(  }|st	        j
                  d|fd||f      dt        j                         v st	        j                  t              rt	        j                  t              ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            d
x}x}x}}y
)zTest conversion to dictionary.rj   r   r   rb   r   )z%(py1)s == %(py4)s)rz   ru   zassert %(py6)sr   Nrh   r   r   resultr   r   r   rU   )z0%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} == %(py7)sry   rt   rv   rw   )r   r~   r   to_dictr!   r"   r&   r'   r(   r#   r$   r%   ry   )r*   rq   rs   r   r   r-   r   @py_format5r@   r   r.   r+   r?   r   r/   r   s                   r0   test_to_dictzTestKPITree.test_to_dict   s\   "";/#'d|(y(|y((((|y(((|(((y(((((((#zV####zV###z######V###V#######*%+s%&+!+&!++++&!++++++s+++s+++%+++&+++!+++++++r2   c                 ,   t        j                  |      }|j                         }d}||v }|st        j                  d|fd||f      t        j
                  |      dt        j                         v st        j                  |      rt        j
                  |      nddz  }dd|iz  }t        t        j                  |            dx}}d	}||v }|st        j                  d|fd||f      t        j
                  |      dt        j                         v st        j                  |      rt        j
                  |      nddz  }dd|iz  }t        t        j                  |            dx}}d
}||v }|st        j                  d|fd||f      t        j
                  |      dt        j                         v st        j                  |      rt        j
                  |      nddz  }dd|iz  }t        t        j                  |            dx}}y)zTest conversion to text.rc   r   r   textr   r   r   Nri   zFormula:)r   r~   to_textr!   r"   r&   r#   r$   r%   r'   r(   )r*   rq   rs   r   r   r   r   r.   s           r0   test_to_textzTestKPITree.test_to_text   s(   "";/||~ yD    yD   y      D   D       "{d""""{d"""{""""""d"""d"""""""!zT!!!!zT!!!z!!!!!!T!!!T!!!!!!!r2   c                    t        ddt        j                  dg      t        ddt        j                        t        ddt        j                        g}t	        j
                  |      }|j                         }t        |      }d	}||k(  }|st        j                  d
|fd||f      dt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}d}	|d   }
|
j                   } |       }|	|v }|st        j                  d|fd|	|f      t        j                  |	      t        j                  |
      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}	x}x}
x}}y)zTest orphan node detection.rb   rc   rd   r   ri   rl   orphanOrphanrS   r   )z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sry   errors)r   rz   r{   r   r<   r=   Nr   r   )zD%(py1)s in %(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s.lower
}()
})rz   ru   r   r=   r|   r}   )r   r   rW   rH   r   r~   validatery   r!   r"   r#   r$   r%   r&   r'   r(   lower)r*   r   rs   r   r   r   r,   r@   rA   r   r-   r   r   s                r0   test_validate_orphan_detectionz*TestKPITree.test_validate_orphan_detection   sk    \\%	 [{PXH8>>J	
 ""6*6{a{a{ass66{a,6!9,9??,?,,x,,,,,x,,,,x,,,9,,,?,,,,,,,,,,,,r2   N)rZ   r[   r\   r]   r   fixturerq   r   r   r   r   r   r   r   r   r   r   r   r^   r2   r0   r`   r`   C   sS    ^^!
 !
F$((((57	)	,"-r2   r`   )r]   builtinsr#   _pytest.assertion.rewrite	assertionrewriter!   r   
src.configr   r   r   src.kpi.treer   r   r	   r`   r^   r2   r0   <module>r      s/        ; ; )7! 7!t]- ]-r2   