<s> from rest_framework . test import APITestCase <EOL> from rest_framework import status <EOL> from django . contrib . auth . models import User <EOL> from tests . patterns_app . models import LocatedModel <EOL> from wq . db . patterns . models import Location <EOL> import json <EOL> class LocateRestTestCase ( APITestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = User . objects . create ( username = '<STR_LIT>' , is_superuser = True ) <EOL> self . instance = LocatedModel . objects . create ( name = "<STR_LIT>" ) <EOL> self . instance . locations . create ( geometry = '<STR_LIT>' ) <EOL> self . instance . locations . create ( geometry = '<STR_LIT>' ) <EOL> self . client . force_authenticate ( user = self . user ) <EOL> def test_locate_post ( self ) : <EOL> """<STR_LIT>""" <EOL> form = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : json . dumps ( { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ - <NUM_LIT> , <NUM_LIT> ] <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ - <NUM_LIT> , <NUM_LIT> ] <EOL> } <EOL> } <EOL> ] <EOL> } ) <EOL> } <EOL> response = self . client . post ( '<STR_LIT>' , form ) <EOL> self . assertEqual ( <EOL> response . status_code , status . HTTP_201_CREATED , response . data <EOL> ) <EOL> self . assertEqual ( response . data [ '<STR_LIT:name>' ] , "<STR_LIT>" ) <EOL> self . assertIn ( "<STR_LIT>" , response . data ) <EOL> self . assertEqual ( len ( response . data [ "<STR_LIT>" ] ) , <NUM_LIT:2> ) <EOL> locs = Location . objects . filter ( object_id = response . data [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( len ( locs ) , <NUM_LIT:2> ) <EOL> geom = locs [ <NUM_LIT:0> ] . geometry <EOL> self . assertEqual ( geom . srid , <NUM_LIT> ) <EOL> self . assertEqual ( geom . x , - <NUM_LIT> ) <EOL> self . assertEqual ( geom . y , <NUM_LIT> ) <EOL> def test_locate_put ( self ) : <EOL> """<STR_LIT>""" <EOL> existing = self . instance . locations . all ( ) <EOL> eid1 = existing [ <NUM_LIT:0> ] . id <EOL> eid2 = existing [ <NUM_LIT:1> ] . id <EOL> form = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : json . dumps ( { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : eid1 , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ - <NUM_LIT> , <NUM_LIT> ] <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : eid2 , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ - <NUM_LIT> , <NUM_LIT> ] <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ - <NUM_LIT> , <NUM_LIT> ] <EOL> } <EOL> } <EOL> ] <EOL> } ) <EOL> } <EOL> response = self . client . put ( <EOL> '<STR_LIT>' % self . instance . pk , <EOL> form <EOL> ) <EOL> self . assertEqual ( <EOL> response . status_code , status . HTTP_200_OK , response . data <EOL> ) <EOL> self . instance = LocatedModel . objects . get ( pk = self . instance . pk ) <EOL> self . assertEqual ( self . instance . name , "<STR_LIT>" ) <EOL> self . assertIn ( "<STR_LIT>" , response . data ) <EOL> self . assertEqual ( len ( response . data [ "<STR_LIT>" ] ) , <NUM_LIT:3> ) <EOL> locs = self . instance . locations . all ( ) <EOL> geom = locs . get ( id = eid1 ) . geometry <EOL> self . assertEqual ( geom . srid , <NUM_LIT> ) <EOL> self . assertEqual ( geom . x , - <NUM_LIT> ) <EOL> self . assertEqual ( geom . y , <NUM_LIT> ) <EOL> newlocs = locs . exclude ( id__in = [ eid1 , eid2 ] ) . all ( ) <EOL> self . assertEqual ( len ( newlocs ) , <NUM_LIT:1> ) <EOL> def test_locate_list_geojson ( self ) : <EOL> pk = self . instance . pk <EOL> loc1 = self . instance . locations . all ( ) [ <NUM_LIT:0> ] <EOL> loc2 = self . instance . locations . all ( ) [ <NUM_LIT:1> ] <EOL> expected = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:id>' : pk , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:label>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ loc1 . geometry . x , loc1 . geometry . y ] , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ loc2 . geometry . x , loc2 . geometry . y ] , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> } <EOL> ] <EOL> } <EOL> } ] , <EOL> '<STR_LIT:count>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:50> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( expected , data ) <EOL> def test_locate_detail_geojson ( self ) : <EOL> pk = self . instance . pk <EOL> loc1 = self . instance . locations . all ( ) [ <NUM_LIT:0> ] <EOL> loc2 = self . instance . locations . all ( ) [ <NUM_LIT:1> ] <EOL> expected = { <EOL> '<STR_LIT:id>' : pk , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:label>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ loc1 . geometry . x , loc1 . geometry . y ] , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ loc2 . geometry . x , loc2 . geometry . y ] , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> } <EOL> ] <EOL> } <EOL> } <EOL> response = self . client . get ( '<STR_LIT>' % pk ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( expected , data ) <EOL> def test_locate_edit_geojson ( self ) : <EOL> pk = self . instance . pk <EOL> loc1 = self . instance . locations . all ( ) [ <NUM_LIT:0> ] <EOL> loc2 = self . instance . locations . all ( ) [ <NUM_LIT:1> ] <EOL> expected = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { <EOL> "<STR_LIT:id>" : loc1 . pk , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT:label>" : "<STR_LIT>" % loc1 . pk , <EOL> "<STR_LIT:name>" : None , <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ loc1 . geometry . x , loc1 . geometry . y ] , <EOL> } , <EOL> } , { <EOL> "<STR_LIT:id>" : loc2 . pk , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT:label>" : "<STR_LIT>" % loc2 . pk , <EOL> "<STR_LIT:name>" : None , <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ loc2 . geometry . x , loc2 . geometry . y ] , <EOL> } <EOL> } ] , <EOL> '<STR_LIT:id>' : pk , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:label>' : '<STR_LIT>' , <EOL> } <EOL> } <EOL> response = self . client . get ( '<STR_LIT>' % pk ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( expected , data ) </s>
