
:Oc           @@  s   d  d l  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 m Z m Z d  d l m Z d  d l m Z m Z m Z m Z m Z d  d l m Z d d	 l m Z m Z m Z m Z m Z m Z m Z d
 e f d     YZ  d S(   i    (   t   absolute_import(   t   date(   t   GEOSGeometryt   Pointt
   MultiPoint(   t   Collectt   Countt   Extentt   Ft   Union(   t   Geometry(   t   mysqlt   oraclet   no_mysqlt	   no_oraclet   no_spatialite(   t   TestCasei   (   t   Cityt   Locationt   DirectoryEntryt   Parcelt   Bookt   Authort   Articlet   RelatedGeoModelTestc           B@  s   e  Z d    Z e d    Z e e d     Z e d    Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z e d    Z d   Z d   Z e d    Z e e e d      Z d   Z d   Z RS(   c         C@  s   t  j j   } t  j j   } t  j j d  } d d d f } x | | | f D]} } xt t | |  D]c \ } } | \ } }	 }
 } |  j | | j  |  j |	 | j  |  j t |
 |  | j	 j
  qe WqO Wd S(   s:   Testing `select_related` on geographic models (see #7126).t   locationt   Aurorat   TXgƆnaXgtw@@t   Roswellt   NMg9d!ZgW%}@@t	   Kecksburgt   PAgu|Sg67D@N(   R   R   gƆnaXgtw@@(   R   R   g9d!ZgW%}@@(   R   R   gu|Sg67D@(   R   t   objectst   allt   select_relatedt   zipt   assertEqualt   namet   stateR   R   t   point(   t   selft   qs1t   qs2t   qs3t   citiest   qst   reft   ct   nmt   stt   lont   lat(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test02_select_related   s    	c         @  s   d     f d   } d d d f } xa | D]Y \ } } } t  t j j d |  j | d d  } | t | |  | d j j  q. Wd S(   sH   Testing the `transform` GeoQuerySet method on related geographic models.i    c         @  sL     j  |  j | j     j  |  j | j     j |  j | j  d  S(   N(   t   assertAlmostEqualt   xt   yR$   t   srid(   R.   t   pnt(   R(   t   tol(    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt	   check_pnt+   s    R   i  s(   POINT(1490553.98959621 314792.131023984)R   i  s(   POINT(481902.189077221 868477.766629735)R   i  s'   POINT(2269923.2484839 7069381.28722222)R%   t
   field_namet   location__pointN(   s	   Kecksburgi  s(   POINT(1490553.98959621 314792.131023984)(   s   Roswelli  s(   POINT(481902.189077221 868477.766629735)(   s   Aurorai  s'   POINT(2269923.2484839 7069381.28722222)(   t   listR   R    t   filtert	   transformR   R   R'   (   R(   R;   t   transformedR%   R8   t   wktR-   (    (   R(   R:   sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test03_transform_related$   s    	*c         C@  s   t  j j t d   } d } d } t  j j d d  } t  j j d d	  j d d  } | d
 } d } xb | | f | | f | | f g D]? \ } }	 x0 t | |	  D] \ }
 } |  j |
 | |  q Wq Wd S(   sI   Testing the `extent` GeoQuerySet aggregates on related geographic models.R=   g9d!Zg|zl=@gu|Sg67D@gƆnaXR<   R&   R   t   location__point__extenti   N(   g9d!Zg|zl=@gu|Sg67D@(   gƆnaXg|zl=@gu|Sg67D@(   R   R    t	   aggregateR   t   extentt   excludeR#   R5   (   R(   t   aggst
   all_extentt   txpa_extentt   e1t   e2t   e3R:   R.   t   et   ref_valt   e_val(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt    test04a_related_extent_aggregate<   s    !
. c         C@  sF  t  j j t d   } t d d  } t d d  } t d d  } t d d	  } t d
 d  } t r t | | | | | d d } t | | d d } n3 t | | | | | d d } t | | d d } t  j j d d  }	 t  j j d d  j d d  }
 | d } |  j	 | |	  |  j	 | |
  |  j	 | |  d S(   sK   Testing the `unionagg` GeoQuerySet aggregates on related geographic models.R=   g9d!ZgW%}@@gƆnaXgtw@@gu|Sg67D@gIG9M3XgMqd@@g =Wg|zl=@R8   i  R<   t   name__inR   t   Houstont   Dallass
   Fort Wortht   location__point__unionN(   s   RoswellRS   RT   s
   Fort Worth(
   R   R    RE   R	   R   R   R   t   unionaggRG   R$   (   R(   RH   t   p1t   p2t   p3t   p4t   p5t   ref_u1t   ref_u2t   u1t   u2t   u3(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test04b_related_union_aggregateP   s"    !
c         C@  s   t  t j j   j    } d S(   s`   Testing that calling select_related on a query over a model with an FK to a model subclass worksN(   R>   R   R    R!   R"   (   R(   t   l(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt$   test05_select_related_fk_to_subclasss   s    c   	      C@  s  t  d d d } t j j d d  } | j j } | j d d t } | j d  } t	 j j
 d d	 d
 | d | d | d | d |  } | j } | j d d t } t	 j j
 d d d
 | d | d | d | d |  } t	 j j d t d   } |  j d t |   |  j d | d j  t stt	 j j d t d   } |  j d t |   |  j d | d j  n  t	 j j d t d   } |  j d t |   |  j d	 | d j  t st	 j j d t d   } |  j d t |   |  j d	 | d j  n  d S(   s*   Testing F() expressions on GeometryFields.ss   POLYGON((-97.501205 33.052520,-97.501205 33.052576,-97.501150 33.052576,-97.501150 33.052520,-97.501205 33.052520))R8   i  R%   R   i  t   cloneid   t   P1t   cityt   center1t   center2t   border1t   border2t   P2t   center1__withini   i    t   center2__withint   city__location__pointt   border2__containsN(   R   R   R    t   getR   R'   R@   t   Truet   bufferR   t   createt   centroidR?   R   R$   t   lenR%   R   (	   R(   t   b1t   pcityt   c1t   c2t   b2RW   RX   R-   (    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test06_f_expressionsx   s.    3	3c         C@  s   t  j j   } t  j j   } t  j j   } x t | | |  D]q \ } } } |  j t | d t   |  j t | d t   |  j	 | j
 | d  |  j	 | j
 | d  q@ Wd S(   s4   Testing values() and values_list() and GeoQuerySets.R'   i   N(   R   R    R!   t   valuest   values_listR#   t
   assertTruet
   isinstanceR
   R$   R'   (   R(   t   gqst   gvqst   gvlqst   mt   dt   t(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test07_values   s    "c         C@  s[   t  j j   } t  j j d  } x3 t | |  D]" \ } } |  j | j | j  q1 Wd S(   s0   Testing defer() and only() on Geographic models.R'   N(   R   R    R!   t   deferR#   R$   R'   (   R(   R-   t   def_qst   loct   def_loc(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test08_defer_only   s    c         C@  s|   d	 } d
 } t  j j d  j d d  } xK t | | |  D]7 \ } } } |  j | d |  |  j | d |  q= Wd S(   sM   Ensuring correct primary key column is selected across relations. See #10757.i   i   i   i   i   t   idt   location__idN(   i   i   i   i   i   (   i   i   i   i   i   (   R   R    t   order_byR|   R#   R$   (   R(   t   city_idst   loc_idst   ids_qst   val_dictt   c_idt   l_id(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test09_pk_relations   s    "c         C@  s   t  j j d d  j j j d  } t  j j d d  j j j d  } t  j j d |  } t  j j d |  } | | B} g  | D] } | j ^ q } |  j d t	 |   |  j
 d | k  |  j
 d | k  d S(   s9   Testing the combination of two GeoQuerySets.  See #10807.R%   R   g?R   t   location__point__withini   N(   R   R    Rp   R   R'   Rr   R?   R%   R$   Ru   R~   (   R(   t   buf1t   buf2R)   R*   t   combinedR/   t   names(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test10_combine   s    $$
c         C@  sc   d d l  } d d l m } t j j   } | j | j  } | j |  } |  j	 | | j
  d S(   s?   Ensuring GeoQuery objects are unpickled correctly.  See #10839.i    N(   t   GeoQuery(   t   picklet    django.contrib.gis.db.models.sqlR   R   R    R!   t   dumpst   queryt   loadsR$   t	   __class__(   R(   R   R   R-   t   q_strt   q(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test11_geoquery_pickle   s    c         C@  sY   t  j j d d  } t j j d t d   j d | j j  } |  j d | j	  d S(   sB   Testing `Count` aggregate use with the `GeoManager` on geo-fields.R%   RT   t
   num_citiesRf   R   i   N(
   R   R    Rp   R   t   annotateR   R   R   R$   R   (   R(   t   dallasR   (    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test12a_count   s    -c         C@  s   t  j j d t d   j d d  } t  j j d  j d t d   j d d  } |  j d t |   |  j d | d j  |  j d t |   |  j d | d d  d S(	   sR   Testing `Count` aggregate use with the `GeoManager` on non geo-fields. See #11087.t	   num_bookst   bookst   num_books__gti   R%   i   i    N(	   R   R    R   R   R?   R|   R$   Ru   R   (   R(   R-   t   vqs(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test12b_count   s    '0c         C@  s   t  j j d d  j d t d   j d d d  } |  j d t |   |  j d | d d  |  j t	 | d d t
   d	 S(
   s8   Testing `Count` aggregate with `.values()`.  See #15305.R   i   R   Rf   R'   i   i   i    N(   R   R    R?   R   R   R|   R$   Ru   R~   R   R   (   R(   R-   (    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test13c_count   s    6c         C@  sJ   t  j j d d  } t  j j d  j d d  } |  j d | j  d S(   sO   Testing `select_related` on a nullable ForeignKey via `GeoManager`. See #11381.t   titles   Without Authort   authorN(   R   R    Rs   R"   Rp   R$   t   NoneR   (   R(   t	   no_authort   b(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test13_select_related_null_fk  s    c         C@  s   t  d  } t j j d d  j d d  } t j j d d  j t d   d } x: | | f D], } |  j d t |   |  j | |  qb Wd S(	   sA   Testing the `collect` GeoQuerySet method and `Collect` aggregate.s_   MULTIPOINT(-97.516111 33.058333,-96.801611 32.782057,-95.363151 29.763374,-96.801611 32.782057)R&   R   R<   R=   t   location__point__collecti   N(	   R   R   R    R?   t   collectRE   R   R$   Ru   (   R(   t   ref_geomRx   Ry   t   coll(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test14_collect  s    	!(c         C@  s%   t  j j d  } t | j  } d S(   sT   Testing doing select_related on the related name manager of a unique FK. See #13934.t   author__articleN(   R   R    R"   t   strR   (   R(   R-   t   sql(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test15_invalid_select_related  s    c         C@  sg   g  t  t j j d t d   j d d   D] } | j ^ q. } | j   |  j d d g |  d S(   sM   Ensure annotated date querysets work if spatial backend is used.  See #14648.R   R   t   dobt   yeari  i  N(	   R>   R   R    R   R   t   datesR   t   sortR$   (   R(   t   dtt   birth_years(    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   test16_annotated_date_queryset'  s    @
(   t   __name__t
   __module__R4   R   RC   R   RQ   Ra   Rc   R{   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyR      s*   	#		0					
			N(!   t
   __future__R    t   datetimeR   t   django.contrib.gis.geosR   R   R   t   django.contrib.gis.db.modelsR   R   R   R   R	   t#   django.contrib.gis.geometry.backendR
   t   django.contrib.gis.tests.utilsR   R   R   R   R   t   django.testR   t   modelsR   R   R   R   R   R   R   R   (    (    (    sB   ../Django//lib/python/django/contrib/gis/tests/relatedapp/tests.pyt   <module>   s   ((4