
    D6i                     t    d Z ddlmZ ddlZddlmZ ddlmZm	Z	 d Z
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zy)a  
The functions in this module can be used for testing that the constraints of
your models. Each assert function runs SQL UPDATEs that check for the existence
of given constraint. Consider the following model::


    class User(Base):
        __tablename__ = 'user'
        id = sa.Column(sa.Integer, primary_key=True)
        name = sa.Column(sa.String(200), nullable=True)
        email = sa.Column(sa.String(255), nullable=False)


    user = User(name='John Doe', email='john@example.com')
    session.add(user)
    session.commit()


We can easily test the constraints by assert_* functions::


    from sqlalchemy_utils import (
        assert_nullable,
        assert_non_nullable,
        assert_max_length
    )

    assert_nullable(user, 'name')
    assert_non_nullable(user, 'email')
    assert_max_length(user, 'name', 200)

    # raises AssertionError because the max length of email is 255
    assert_max_length(user, 'email', 300)
    )DecimalN)ARRAY)	DataErrorIntegrityErrorc                 H   t         j                  j                  |       }t        j                  | j                        j
                  |   } |j                  j                         j                  di |j                  |i}|j                  |       |j                          y )N )saormobject_sessioninspect	__class__columnstableupdatevalueskeyexecuteflush)objfieldvaluesessioncolumnquerys         [/home/azureuser/techstart-app/venv/lib/python3.12/site-packages/sqlalchemy_utils/asserts.py_update_fieldr   +   ss    ff##C(GZZ&..u5F(FLL!((?FJJ+>?EOOEMMO    c                     	 t        | ||       y # |$ rD}t        j                  j                  |       }|j	                          J t        |             d }~ww xY wN)r   r	   r
   r   rollbackstr)r   r   r   reraise_excer   s         r   _expect_successful_updater$   3   sQ    c5%( &&'',c!fus    A?AAc                    	 t        | ||       t        d|z        # |$ r Y nw xY w	 t        j                  j	                  |       }|j                          y # t        j                  j	                  |       }|j                          w xY w)NzExpected update to raise %s)r   AssertionErrorr	   r
   r   r    )r   r   r   expected_excr   s        r   _expect_failing_updater(   <   s    c5%( :\IJJ   &&'', &&'',s!    A %A %A 1B
c                 6   t        | t              rt        | j                  t        j                        rdgS t        | j                  t        j
                        rdgS t        | j                  t        j                        rt        d      gS t        d      y)Nr   a0zUnknown array item type)	
isinstancer   	item_typer	   IntegerStringNumericr   	TypeErrortype_s    r   _repeated_valuer4   H   sk    %eoorzz23J35L4CL>!566r   c                 :    t        | t              rt        S t        S r   )r,   r   r   r   r2   s    r   _expected_exceptionr6   V   s    %r   c                 (    t        | |dt               y)z
    Assert that given column is nullable. This is checked by running an SQL
    update that assigns given column as None.

    :param obj: SQLAlchemy declarative model object
    :param column: Name of the column
    N)r$   r   r   r   s     r   assert_nullabler9   ]   s     c64@r   c                 (    t        | |dt               y)z
    Assert that given column is not nullable. This is checked by running an SQL
    update that assigns given column as None.

    :param obj: SQLAlchemy declarative model object
    :param column: Name of the column
    N)r(   r   r8   s     r   assert_non_nullabler;   h   s     3n=r   c                    t        j                  | j                        j                  |   j                  }t        | |t        |      |z  t        |             t        | |t        |      |dz   z  t        |             y)a  
    Assert that the given column is of given max length. This function supports
    string typed columns as well as PostgreSQL array typed columns.

    In the following example we add a check constraint that user can have a
    maximum of 5 favorite colors and then test this.::


        class User(Base):
            __tablename__ = 'user'
            id = sa.Column(sa.Integer, primary_key=True)
            favorite_colors = sa.Column(ARRAY(sa.String), nullable=False)
            __table_args__ = (
                sa.CheckConstraint(
                    sa.func.array_length(favorite_colors, 1) <= 5
                )
            )


        user = User(name='John Doe', favorite_colors=['red', 'blue'])
        session.add(user)
        session.commit()


        assert_max_length(user, 'favorite_colors', 5)


    :param obj: SQLAlchemy declarative model object
    :param column: Name of the column
    :param max_length: Maximum length of given column
       N)	r	   r   r   r   typer$   r4   r6   r(   )r   r   
max_lengthr3   s       r   assert_max_lengthr@   s   sr    @ JJs}}%--f5::EV_U+j8:Me:T *q.1E"	r   c                 R    t        | ||t               t        | ||dz
  t               y)z
    Assert that the given column must have a minimum value of `min_value`.

    :param obj: SQLAlchemy declarative model object
    :param column: Name of the column
    :param min_value: The minimum allowed value for given column
    r=   Nr$   r   r(   r   r   	min_values      r   assert_min_valuerE      $     c69nE3	A~Fr   c                 R    t        | ||t               t        | ||dz   t               y)z
    Assert that the given column must have a minimum value of `max_value`.

    :param obj: SQLAlchemy declarative model object
    :param column: Name of the column
    :param max_value: The maximum allowed value for given column
    r=   NrB   rC   s      r   assert_max_valuerH      rF   r   )__doc__decimalr   
sqlalchemyr	   sqlalchemy.dialects.postgresqlr   sqlalchemy.excr   r   r   r$   r(   r4   r6   r9   r;   r@   rE   rH   r   r   r   <module>rN      sO   !F   0 4	A>)X	G	Gr   