o
    i2                     @   s  d dl mZ d dlZd dlZd dlmZ d dlmZmZ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 d d	lmZ d d
lmZmZmZ d dlmZmZmZ G dd deZG dd deZ G dd deZ!dej" d Z#G dd deZ$G dd deZ%G dd deZ&dS )    )absolute_importN)ImproperlyConfigured)TestCaseTransactionTestCaseClient)DjangoTestClient)Fault)ComplexModelBase)DjangoComplexModelDjangoComplexModelMetaemail_re)add_metaclass)FieldContainerRelatedFieldContainerUserProfile)apphello_world_service	Containerc                   @   s   e Zd Zdd Zdd ZdS )SpyneTestCasec                 C   s   t dtj| _d S )Nz/hello_world/)r   r   r   clientself r   ^/var/www/html/LicenciasMIG/venv/lib/python3.10/site-packages/spyne/test/interop/test_django.pysetUp*   s   zSpyneTestCase.setUpc                 C   s>   | j jdd}t|}| t|d | |dgd  d S )NJoe   
Hello, Joe)r   service	say_hellolistassertEquallen)r   resp	list_respr   r   r   _test_say_hello-   s   zSpyneTestCase._test_say_helloN)__name__
__module____qualname__r   r%   r   r   r   r   r   )   s    r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )DjangoViewTestCasec                 C   sF   t dt}|jdd}t|}| t|d | |dgd  d S )N/say_hello/r   r   r   )r   r   r   r   r    r!   r"   )r   r   r#   r$   r   r   r   test_say_hello5   s
   
z!DjangoViewTestCase.test_say_helloc                 C   s@   t dt}|jjdd}| d|v  | |d d d S )Nr*   r   r   Content-Typetext/xml)r   r   r   r   get_django_response
assertTrue
startswithr   r   responser   r   r   test_response_encoding<   s   
z)DjangoViewTestCase.test_response_encodingc                 C   s&   t  }|di }| j|ddd d S )Nr*   faultstringi  )status_code)r   postassertContainsr1   r   r   r   
test_errorB   s   zDjangoViewTestCase.test_errorc                 C   s:   t  }|d}| |d |jddd}| |d dS )zTest if wsdl is cached.r*   z'location="http://testserver/say_hello/"newtestserver	HTTP_HOSTz*location="http://newtestserver/say_hello/"N)r   getr7   assertNotContainsr1   r   r   r   test_cached_wsdlG   s   
z#DjangoViewTestCase.test_cached_wsdlc                 C   s:   t  }|d}| |d |jddd}| |d dS )zTest if wsdl is not cached.z/say_hello_not_cached/z2location="http://testserver/say_hello_not_cached/"r9   r:   z5location="http://newtestserver/say_hello_not_cached/"N)r   r<   r7   r1   r   r   r   test_not_cached_wsdlQ   s   
z'DjangoViewTestCase.test_not_cached_wsdlN)r&   r'   r(   r+   r3   r8   r>   r?   r   r   r   r   r)   4   s    
r)   c                   @   sx   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )ModelTestCasez-Test mapping between django and spyne models.c                 C   s   t dt| _d S )N/api/)r   r   r   r   r   r   r   r   b   s   zModelTestCase.setUpc                 C   s&   t t }| d| | d| dS )z%Test if excluded field is not mapped.idexcluded_fieldN)r   get_flat_type_infoassertInassertNotIn)r   	type_infor   r   r   test_excludee   s   
zModelTestCase.test_excludec                 C   s4   t t }|d }| |jjd | |jj dS )z;Test if primary key is mapped as optional but not nillable.rB   r   N)r   rD   r!   
Attributes
min_occursassertFalsenullable)r   rG   pk_fieldr   r   r   test_pk_mappingk   s   
zModelTestCase.test_pk_mappingc                 C   sP   t t }|d }| |jd | |jj | |jjd | |jj	 dS )z2Test if regex pattern is mapped from django model.email_fieldUnicode   N)
r   rD   r!   r&   assertIsNotNonerI   patternrJ   rK   rL   )r   rG   rO   r   r   r   test_regex_pattern_mappingr   s   
z(ModelTestCase.test_regex_pattern_mappingc                 C   sB   t t }|d }| |jd | |jjd | |jj dS )z/Test if blank fields are optional but not null.blank_fieldNormalizedStringr   N)r   rD   r!   r&   rI   rJ   rK   rL   )r   rG   rU   r   r   r   test_blank_field{   s
   
zModelTestCase.test_blank_fieldc                 C   s   t  }| }| d| dS )z9Test if blank field is omitted in as_dict representation.rU   N)r   as_dictrF   )r   	containercontainer_dictr   r   r   test_blank_as_dict   s   z ModelTestCase.test_blank_as_dictc                 C   sb   t t }|d }| |jd | |jjd | |jj | |jjd | |jj	d dS )z/Test if length validators are correctly mapped.length_validators_fieldrV   rQ      
   N)
r   rD   r!   r&   rI   rJ   r/   rL   min_lenmax_len)r   rG   r\   r   r   r   test_length_validators_field   s   
z*ModelTestCase.test_length_validators_fieldc                    sR    fdd}  t| tjjdd}tjjd||ddd | } |t d	S )
z.Test mapping from Django model to spyne model.c                      s    j jdS )N   )r   r   get_containerr   r   r   r   <lambda>   s    z2ModelTestCase.test_get_container.<locals>.<lambda>rY   )
slug_field
container2email@example.comyo)re   foreign_keyone_to_one_fieldrO   
char_fieldN)assertRaisesr   r   objectscreateassertIsInstancer   )r   rc   rY   cr   r   r   test_get_container   s   z ModelTestCase.test_get_containerc              	      s|   t dd}tdtj tj dt ||d  fdd}| }|t 	|j
d 	|jd t| dS )	z*Test complex input to create Django model.related)rB   rY   rg   )re   
date_fielddatetime_fieldrO   
time_fieldcustom_foreign_keycustom_one_to_one_fieldc                         j j S Nr   r   create_containerr   new_containerr   r   r   rd          z5ModelTestCase.test_create_container.<locals>.<lambda>N)r   r   datetimedatetodaynowtimero   r   r!   custom_one_to_one_field_idcustom_foreign_key_idrl   r   )r   related_containerr{   rp   r   r|   r   test_create_container   s   
z#ModelTestCase.test_create_containerc              	      sV   t ddddtj tj t d  fdd}| }|t t	| dS )z2Test complex unicode input to create Django model.u
   спайнspynerg   )rk   
text_fieldre   rO   rs   rt   ru   c                      rx   ry   rz   r   r|   r   r   rd      r~   z=ModelTestCase.test_create_container_unicode.<locals>.<lambda>N)
r   r   r   r   r   r   ro   r   rl   r   )r   r{   rp   r   r|   r   test_create_container_unicode   s   	z+ModelTestCase.test_create_container_unicodec                 C   sN   G dd dt }| |jd jj G dd dt }| |jd jjd dS )z6Test if optional_relations flag makes fields optional.c                   @      e Zd ZG dd dejZdS )@ModelTestCase.test_optional_relation_fields.<locals>.UserProfilec                   @      e Zd ZeZdS )KModelTestCase.test_optional_relation_fields.<locals>.UserProfile.AttributesN)r&   r'   r(   DjUserProfiledjango_modelr   r   r   r   rI          rI   Nr&   r'   r(   r
   rI   r   r   r   r   r          r   user_idc                   @   r   )r   c                   @   s   e Zd ZeZdZdS )r   TN)r&   r'   r(   r   r   django_optional_relationsr   r   r   r   rI      s    rI   Nr   r   r   r   r   r      r   r   N)r
   rK   
_type_inforI   rL   r!   rJ   )r   r   r   r   r   test_optional_relation_fields   s   z+ModelTestCase.test_optional_relation_fieldsc                    sh   t tG dd dt G  fddd }| }|j }dd tjjD }|d | 	|| dS )	z-Test if can create custom DjangoComplexModel.c                   @   s"   e Zd ZdZG dd dejZdS )zQModelTestCase.test_abstract_custom_djangomodel.<locals>.OrderedDjangoComplexModelTc                   @      e Zd ZdZdS )z\ModelTestCase.test_abstract_custom_djangomodel.<locals>.OrderedDjangoComplexModel.AttributesdeclaredNr&   r'   r(   declare_orderr   r   r   r   rI      r   rI   N)r&   r'   r(   __abstract__r	   rI   r   r   r   r   OrderedDjangoComplexModel   s    r   c                       s   e Zd ZG dd d jZdS )zMModelTestCase.test_abstract_custom_djangomodel.<locals>.OrderedFieldContainerc                   @   r   )zXModelTestCase.test_abstract_custom_djangomodel.<locals>.OrderedFieldContainer.AttributesN)r&   r'   r(   r   r   r   r   r   r   rI      r   rI   N)r&   r'   r(   rI   r   r   r   r   OrderedFieldContainer   r   r   c                 S   s   g | ]}|  qS r   )get_attname).0fieldr   r   r   
<listcomp>   s    zBModelTestCase.test_abstract_custom_djangomodel.<locals>.<listcomp>
file_fieldN)
r   r   r	   r   keysr   _metafieldsremover!   )r   r   field_containertype_info_fieldsdjango_field_namesr   r   r    test_abstract_custom_djangomodel   s   

z.ModelTestCase.test_abstract_custom_djangomodelc                 C   sL   | j tdd ttG dd dt}W d   dS 1 sw   Y  dS )z/Test if can't create non abstract custom model.z$Can create non abstract custom model)msgc                   @   r   )z^ModelTestCase.test_nonabstract_custom_djangomodel.<locals>.CustomNotAbstractDjangoComplexModelc                   @   r   )ziModelTestCase.test_nonabstract_custom_djangomodel.<locals>.CustomNotAbstractDjangoComplexModel.Attributesr   Nr   r   r   r   r   rI      r   rI   N)r&   r'   r(   r	   rI   r   r   r   r   #CustomNotAbstractDjangoComplexModel   s    r   N)rl   r   r   r   r	   )r   r   r   r   r   #test_nonabstract_custom_djangomodel   s   "z1ModelTestCase.test_nonabstract_custom_djangomodelN)r&   r'   r(   __doc__r   rH   rN   rT   rW   r[   ra   rq   r   r   r   r   r   r   r   r   r   r@   ^   s    	
r@   ^$c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )EmailRegexTestCasezTests for email_re.c                 C      |  ttd dS )zEmpty string is invalid email. NassertIsNonerematchpython_email_rer   r   r   r   
test_empty      zEmailRegexTestCase.test_emptyc                 C   r   )Test valid email.zvalid.email@example.comNrR   r   r   r   r   r   r   r   
test_valid  s   
zEmailRegexTestCase.test_validc                 C   r   )r   zvalid.email@e.x.comNr   r   r   r   r   test_valid_single_letter_domain  r   z2EmailRegexTestCase.test_valid_single_letter_domainc                 C   r   )zTest invalid email.z@example.comNr   r   r   r   r   test_invalid  r   zEmailRegexTestCase.test_invalidc                 C   s(   |  ttd |  ttd dS )z/Test if email from Top Level Domain is invalid.zbabushka@emailzbabushka@domain.email-Nr   r   r   r   r   test_invalid_tld  s   z#EmailRegexTestCase.test_invalid_tldN)	r&   r'   r(   r   r   r   r   r   r   r   r   r   r   r      s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )DjangoServiceTestCasez"Tests for Django specific service.c                 C   F   t dt}| td |j  W d   dS 1 sw   Y  dS )z?Test if Django service handles `ObjectDoesNotExist` exceptions.rA   zClient.FieldContainerNotFoundN)r   r   assertRaisesRegexpr   r   raise_does_not_existr   r   r   r   r   test_handle_does_not_exist     
"z0DjangoServiceTestCase.test_handle_does_not_existc                 C   r   )z<Test if Django service handles `ValidationError` exceptions.rA   zClient.ValidationErrorN)r   r   r   r   r   raise_validation_errorr   r   r   r   test_handle_validation_error  r   z2DjangoServiceTestCase.test_handle_validation_errorN)r&   r'   r(   r   r   r   r   r   r   r   r     s    r   c                   @   s   e Zd Zdd ZdS )FromUnicodeAssertionTestCasec                 C   s4   t  }d}d}ddd}|j||ddfi | d S )Nz/synchro/1/s  <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/
            soap/envelope/"xmlns:ns1="tns" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="
            http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header/><ns0:Body><ns1:sync><ns1:model_id>TestModel
            </ns1:model_id><ns1:timestamp>2015-09-23T13:54:51.796366+00:00</ns1:timestamp><ns1:payload>
            </ns1:payload><ns1:partial/></ns1:sync></ns0:Body></SOAP-ENV:Envelope>s   "sync"ztext/xml; charset=utf-8)
SOAPActionr,   r-   T)r   r6   )r   r   urlr   hdrsr   r   r   !test_from_unicode_does_not_assert(  s
   
z>FromUnicodeAssertionTestCase.test_from_unicode_does_not_assertN)r&   r'   r(   r   r   r   r   r   r   &  s    r   )'
__future__r   r   r   django.core.exceptionsr   django.testr   r   r   spyne.client.djangor   spyne.model.faultr   spyne.model.complexr	   spyne.util.djangor
   r   r   spyne.util.sixr   rpctest.core.modelsr   r   r   r   rpctest.core.viewsr   r   r   r   r)   r@   rS   r   r   r   r   r   r   r   r   <module>   s(   * 