o
    QDie                     @   s   d dl Z d dlmZ dZdZdZdZ	 efddZefd	d
ZefddZ	efddZ
efddZefddZefddZefddZdd Zdd Zdd Zdd ZdS )    N)REGISTRYz2
%s%s = %s, expected %s.
The values for %s are:
%szC
%s%s changed by %f, expected %f.
Value before: %s
Value after: %s
zZ
The change in value of %s%s didn't match the predicate.
Value before: %s
Value after: %s
z7
%s%s was None after.
Value before: %s
Value after: %s
c              	   K   sH   t |fd|i|}t|t||| |tt|f }| |ks"J |dS z5Asserts that metric_name{**labels} == expected_value.registryN
get_metricMETRIC_EQUALS_ERR_EXPLANATIONformat_labelsformat_vectorget_metrics_vectorexpected_valuemetric_namer   labelsvalue
assert_err r   T/var/www/Datamplify/venv/lib/python3.10/site-packages/django_prometheus/testutils.pyassert_metric_equal"      
r   c           	      K   s~   t || fi |}t|fd|i|}|dus#J t|t|||f ||p'd }t|t|||||f }||ks=J |dS )zAsserts that metric_name{**labels} changed by expected_diff between
    the frozen registry and now. A frozen registry can be obtained
    by calling save_registry, typically at the beginning of a test
    case.
    r   N        get_metric_from_frozen_registryr    METRIC_DIFF_ERR_NONE_EXPLANATIONr   METRIC_DIFF_ERR_EXPLANATION	frozen_registryexpected_diffr   r   r   saved_valuecurrent_valuediffr   r   r   r   assert_metric_diff0   $   r    c           	      K   s~   t || fi |}t|fd|i|}|dus#J t|t|||f ||p'd }t|t|||||f }||ks=J |dS )zAsserts that metric_name{**labels} isn't changed by expected_diff between
    the frozen registry and now. A frozen registry can be obtained
    by calling save_registry, typically at the beginning of a test
    case.
    r   Nr   r   r   r   r   r   assert_metric_no_diffJ   r!   r"   c              	   K   sH   t |fd|i|}t|t||| |tt|f }| |ks"J |dS r   r   r   r   r   r   assert_metric_not_equald   r   r#   c                 K   sp   t || fi |}t|fd|i|}|dus#J t|t|||f |||du s6J t|t|||f dS )zAsserts that metric_name{**labels} changed according to a provided
    predicate function between the frozen registry and now. A
    frozen registry can be obtained by calling save_registry,
    typically at the beginning of a test case.
    r   NT)r   r   r   r   METRIC_COMPARE_ERR_EXPLANATION)r   	predicater   r   r   r   r   r   r   r   assert_metric_comparer   s   r&   c                 C   s   t t|  S )zFreezes a registry. This lets a user test changes to a metric
    instead of testing the absolute value. A typical use case looks like:

        registry = save_registry()
        doStuff()
        assert_metric_diff(registry, 1, 'stuff_done_total')
    )copydeepcopylistcollect)r   r   r   r   save_registry   s   r+   c                 K   s   t | | fi |S )zGets a single metric.)r   r*   )r   r   r   r   r   r   r      s   r   c                 C   s   t | | S )aC  Returns the values for all labels of a given metric.

    The result is returned as a list of (labels, value) tuples,
    where `labels` is a dict.

    This is quite a hack since it relies on the internal
    representation of the prometheus_client, and it should
    probably be provided as a function there instead.
    )&get_metric_vector_from_frozen_registryr*   )r   r   r   r   r   r
      s   
r
   c                 C   s@   g }|D ]}|j D ]}|d | kr||d |d f q	q|S )z4Like get_metrics_vector, but from a frozen registry.r         )samplesappend)r   r   outputmetricsampler   r   r   r,      s   
r,   c                 K   sB   |D ]}|j D ]}|d | kr|d |kr|d     S qqdS )z,Gets a single metric from a frozen registry.r   r-   r.   N)r/   )r   r   r   r2   r3   r   r   r   r      s   
r   c                 C   s   d ddd |  D S )zFormat a set of labels to Prometheus representation.

    In:
      {'method': 'GET', 'port': '80'}

    Out:
      '{method="GET",port="80"}'
    z{{{}}},c                 S   s    g | ]\}}| d | dqS )z=""r   ).0kvr   r   r   
<listcomp>   s     z!format_labels.<locals>.<listcomp>)formatjoinitems)r   r   r   r   r      s   	r   c                 C   s   d dd | D S )zgFormats a list of (labels, value) where labels is a dict into a
    human-readable representation.
    
c                 S   s"   g | ]\}}t | d | qS )z = )r   )r6   r   r   r   r   r   r9      s   " z!format_vector.<locals>.<listcomp>)r;   )vectorr   r   r   r	      s   r	   )r'   prometheus_clientr   r   r   r$   r   r   r    r"   r#   r&   r+   r   r
   r,   r   r   r	   r   r   r   r   <module>   s&    
