Beispiel #1
0
 def test_set_encompassing_city_exists(self):
     city = Location(name="Wixom").save()
     city.encompassed_by.connect(self.state)
     self.state.encompasses.connect(city)
     res = self.address.set_encompassing()
     self.assertTrue(res.encompassed_by.is_connected(city))
     city.delete()
    def test_google_places_city_without_state(self, m):
        query = 'MATCH (a:Location) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)
        url = "https://maps.googleapis.com/maps/api/" \
              "place/details/json?placeid=%s&key=%s" % (
                  wixom_without_state_data['place_id'],
                  settings.GOOGLE_MAPS_API_SERVER)
        m.get(url, json=wixom_server_response, status_code=status.HTTP_200_OK)

        location = parse_google_places(
            wixom_without_state_data['address_components'],
            wixom_without_state_data['place_id'])

        self.assertEqual(location.name, "Wixom")
        res, _ = db.cypher_query('MATCH (a:Location '
                                 '{name: "Michigan"}) RETURN a')
        state = Location.inflate(res.one)
        res, _ = db.cypher_query(
            'MATCH (a:Location '
            '{name: "United States of America"}) RETURN a')
        country = Location.inflate(res.one)
        self.assertTrue(state in location.encompassed_by)
        self.assertTrue(location in state.encompasses)

        self.assertTrue(country in state.encompassed_by)
        self.assertTrue(state in country.encompasses)
Beispiel #3
0
 def test_address_has_no_lat_long(self):
     mi = Location(name=us.states.lookup("MI").name,
                   sector="federal").save()
     address = Address(state="MI").save()
     res = connect_to_state_districts.apply_async(
         kwargs={'object_uuid': address.object_uuid})
     self.assertFalse(res.result)
     mi.delete()
     address.delete()
Beispiel #4
0
 def test_address_has_lat_long_outside_usa(self):
     mi = Location(name=us.states.lookup("MI").name,
                   sector="federal").save()
     # lat/long of Greenwich UK
     address = Address(state="MI", latitude=51.4800,
                       longitude=0.0000).save()
     res = connect_to_state_districts.apply_async(
         kwargs={'object_uuid': address.object_uuid})
     self.assertTrue(res.result)
     mi.delete()
     address.delete()
Beispiel #5
0
 def test_get_full_name_state_senator(self):
     self.position.name = "State Senator"
     self.position.level = "state_upper"
     self.position.save()
     state = Location(name="Michigan", level="federal").save()
     district = Location(name="38", level="state_upper").save()
     state.encompasses.connect(district)
     district.encompassed_by.connect(state)
     self.position.location.connect(district)
     res = Position.get_full_name(self.position.object_uuid)
     self.assertEqual(res['full_name'], "State Senator for Michigan's "
                      "38th district")
Beispiel #6
0
 def test_get_full_name_state_house_rep(self):
     self.position.name = "State House Representative"
     self.position.level = "state_lower"
     self.position.save()
     state = Location(name="Michigan", level="federal").save()
     district = Location(name="15", level="state_lower").save()
     state.encompasses.connect(district)
     district.encompassed_by.connect(state)
     self.position.location.connect(district)
     res = Position.get_full_name(self.position.object_uuid)
     self.assertEqual(
         res['full_name'], "State House Representative"
         " for Michigan's 15th district")
Beispiel #7
0
def update_address_location(object_uuid):
    try:
        address = Address.nodes.get(object_uuid=object_uuid)
    except (DoesNotExist, Address.DoesNotExist, CypherException, IOError,
            ClientError) as e:
        raise update_address_location.retry(exc=e,
                                            countdown=3,
                                            max_retries=None)
    try:
        state = us.states.lookup(address.state)
        district = address.congressional_district
        query = 'MATCH (a:Address {object_uuid:"%s"})-[r:ENCOMPASSED_BY]->' \
                '(l:Location) DELETE r' % object_uuid
        db.cypher_query(query)
        query = 'MATCH (s:Location {name:"%s"})-[:ENCOMPASSES]->' \
                '(d:Location {name:"%s", sector:"federal"}) RETURN d' % \
                (state, district)
        res, _ = db.cypher_query(query)
        if res.one is not None:
            district = Location.inflate(res.one)
            address.encompassed_by.connect(district)
        address.set_encompassing()
    except (CypherException, IOError, ClientError) as e:
        raise update_address_location.retry(exc=e,
                                            countdown=3,
                                            max_retries=None)
    return True
 def populate_sectors(self):
     skip = 0
     while True:
         query = 'MATCH (location:Location) RETURN location ' \
                 'SKIP %s LIMIT 25' % skip
         skip += 24
         res, _ = db.cypher_query(query)
         if not res.one:
             break
         for location in [Location.inflate(row[0]) for row in res]:
             if not location.sector:
                 try:
                     encompassed_by = Location.nodes.get(
                         object_uuid=location.get_encompassed_by(
                             location.object_uuid)[0]).name
                 except (DoesNotExist, Location.DoesNotExist, IndexError):
                     encompassed_by = None
                 try:
                     int(location.name)
                     location.sector = "federal"
                 except ValueError:
                     if location.name == "United States of America":
                         location.sector = "federal"
                         location.save()
                         continue
                     state = us.states.lookup(location.name)
                     if state is None:
                         location.sector = "local"
                     elif encompassed_by is not None and state is not None \
                             and encompassed_by != \
                             "United States of America":
                         location.sector = "local"
                     else:
                         location.sector = "federal"
                 location.save()
Beispiel #9
0
 def test_get_full_name_senator(self):
     self.position.name = "Senator"
     self.position.level = "federal"
     self.position.save()
     state = Location(name="Michigan", level="federal").save()
     self.position.location.connect(state)
     res = Position.get_full_name(self.position.object_uuid)
     self.assertEqual(res['full_name'], "Senator of Michigan")
Beispiel #10
0
 def get_location(self):
     from sb_locations.neo_models import Location
     query = 'MATCH (a:Mission {object_uuid: "%s"})' \
             '-[:WITHIN]->(b:Location) RETURN b' % self.object_uuid
     res, _ = db.cypher_query(query)
     if res.one:
         return Location.inflate(res.one)
     else:
         return None
 def setUp(self):
     settings.CELERY_ALWAYS_EAGER = True
     query = "MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r"
     db.cypher_query(query)
     cache.clear()
     self.unit_under_test_name = 'quest'
     self.email = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.user = User.objects.get(email=self.email)
     self.url = "http://testserver"
     self.quest = Quest(about='Test Bio',
                        owner_username=self.pleb.username).save()
     self.quest.editors.connect(self.pleb)
     self.quest.moderators.connect(self.pleb)
     self.usa = Location(name="United States of America").save()
     self.michigan = Location(name="Michigan").save()
     self.d11 = Location(name="11", sector="federal").save()
     self.usa.encompasses.connect(self.michigan)
     self.michigan.encompassed_by.connect(self.usa)
     self.michigan.encompasses.connect(self.d11)
     self.d11.encompassed_by.connect(self.michigan)
     self.address = Address(street="125 Glenwood Drive",
                            city="Walled Lake",
                            state="Michigan",
                            postal_code="48390",
                            country="USA",
                            county="Oakland",
                            congressional_district=11,
                            validated=True).save()
     self.address.encompassed_by.connect(self.d11)
     self.quest.address.connect(self.address)
     cache.clear()
     self.stripe = stripe
     self.stripe.api_key = settings.STRIPE_SECRET_KEY
     stripe.api_version = settings.STRIPE_API_VERSION
     self.mission = Mission(owner_username=self.pleb.username,
                            title=str(uuid1()),
                            focus_name="advocacy",
                            location_name="11").save()
     self.mission.location.connect(self.d11)
     self.quest.missions.connect(self.mission)
     self.email2 = "*****@*****.**"
     self.pleb2 = create_user_util_test(self.email2)
     self.user2 = User.objects.get(email=self.email2)
    def test_google_places_city(self):
        query = 'MATCH (a:Location) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)
        location = parse_google_places(wixom_data['address_components'],
                                       wixom_data['place_id'])

        self.assertEqual(location.name, "Wixom")
        res, _ = db.cypher_query('MATCH (a:Location '
                                 '{name: "Michigan"}) RETURN a')
        state = Location.inflate(res.one)
        res, _ = db.cypher_query(
            'MATCH (a:Location '
            '{name: "United States of America"}) RETURN a')
        country = Location.inflate(res.one)
        self.assertTrue(state in location.encompassed_by)
        self.assertTrue(location in state.encompasses)

        self.assertTrue(country in state.encompassed_by)
        self.assertTrue(state in country.encompasses)
    def test_google_places_country_already_exists(self):
        query = 'MATCH (a:Location) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)
        old_location = Location(name="United States of America").save()
        location = parse_google_places(us_data['address_components'],
                                       us_data['place_id'])

        self.assertEqual(location.name, "United States of America")
        self.assertIsInstance(location, Location)
        self.assertEqual(old_location.object_uuid, location.object_uuid)
Beispiel #14
0
 def setUp(self):
     self.address = Address(city="Wixom", state="MI").save()
     try:
         Location.nodes.get(name="Wixom").delete()
     except (Location.DoesNotExist, DoesNotExist):
         pass
     except MultipleNodesReturned:
         query = 'MATCH (a:Location {name:"Wixom"}) RETURN a'
         res, _ = db.cypher_query(query)
         for location in res[0]:
             Location.inflate(location).delete()
     try:
         self.state = Location.nodes.get(name="Michigan")
     except MultipleNodesReturned:
         query = 'MATCH (a:Location {name:"Michigan"}) RETURN a'
         res, _ = db.cypher_query(query)
         for location in res[0]:
             Location.inflate(location).delete()
     except (Location.DoesNotExist, DoesNotExist):
         self.state = Location(name="Michigan").save()
    def test_google_places_city_already_exists(self):
        query = 'MATCH (a:Location) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)
        old_country = Location(name="United States of America").save()
        old_state = Location(name="Michigan").save()
        old_city = Location(name="Wixom").save()
        old_country.encompasses.connect(old_state)
        old_state.encompassed_by.connect(old_country)
        old_state.encompasses.connect(old_city)
        old_city.encompassed_by.connect(old_state)

        location = parse_google_places(wixom_data['address_components'],
                                       wixom_data['place_id'])

        self.assertEqual(location.name, "Wixom")
        self.assertIsInstance(location, Location)
        self.assertEqual(old_city.object_uuid, location.object_uuid)

        self.assertTrue(old_state in location.encompassed_by)
        self.assertTrue(location in old_state.encompasses)
Beispiel #16
0
 def add_addresses_to_locations(self):
     for address in Address.nodes.all():
         if not address.encompassed_by.all():
             state = us.states.lookup(address.state)
             district = address.congressional_district
             query = 'MATCH (s:Location {name:"%s"})-[:ENCOMPASSES]->' \
                     '(d:Location {name:"%s"}) RETURN d' % \
                     (state, district)
             res, _ = db.cypher_query(query)
             district = Location.inflate(res[0][0])
             address.encompassed_by.connect(district)
Beispiel #17
0
 def set_encompassing(self):
     from .tasks import connect_to_state_districts
     try:
         encompassed_by = Location.nodes.get(name=self.city)
         if Location.get_single_encompassed_by(
                 encompassed_by.object_uuid) != \
                 us.states.lookup(self.state).name:
             # if a location node exists with an incorrect encompassing
             # state
             raise DoesNotExist("This Location does not exist")
     except (Location.DoesNotExist, DoesNotExist):
         encompassed_by = Location(name=self.city, sector="local").save()
         try:
             city_encompassed = Location.nodes.get(
                 name=us.states.lookup(self.state).name)
         except (Location.DoesNotExist, DoesNotExist):
             city_encompassed = Location(name=us.states.lookup(
                 self.state).name,
                                         sector="federal").save()
         if city_encompassed not in encompassed_by.encompassed_by:
             encompassed_by.encompassed_by.connect(city_encompassed)
         if encompassed_by not in city_encompassed.encompasses:
             city_encompassed.encompasses.connect(encompassed_by)
     except (MultipleNodesReturned, Exception):
         query = 'MATCH (l1:Location {name:"%s"})-[:ENCOMPASSED_BY]->' \
                 '(l2:Location {name:"%s"}) RETURN l1' % \
                 (self.city, self.state)
         res, _ = db.cypher_query(query)
         if res.one is not None:
             encompassed_by = Location.inflate(res.one)
         else:
             encompassed_by = None
     if encompassed_by is not None:
         if encompassed_by not in self.encompassed_by:
             self.encompassed_by.connect(encompassed_by)
     # get or create the state level districts and attach them to the
     # address
     spawn_task(task_func=connect_to_state_districts,
                task_param={'object_uuid': self.object_uuid})
     return self
Beispiel #18
0
    def test_multiple_cities_same_name(self):
        address = Address(city="Redford", state="Michigan").save()
        city = Location(name="Redford").save()
        city2 = Location(name="Redford").save()
        try:
            new_state = Location.nodes.get(name="Washington")
        except (Location.DoesNotExist, DoesNotExist):
            new_state = Location(name="Washington").save()
        self.state.encompasses.connect(city)
        city.encompassed_by.connect(self.state)
        city2.encompassed_by.connect(new_state)

        new_state.encompasses.connect(city2)
        res = address.set_encompassing()
        self.assertTrue(res.encompassed_by.is_connected(city))
        city.delete()
        city2.delete()
        address.delete()
Beispiel #19
0
def connect_to_state_districts(object_uuid):
    try:
        address = Address.nodes.get(object_uuid=object_uuid)
    except (DoesNotExist, Address.DoesNotExist, CypherException, IOError,
            ClientError) as e:
        raise connect_to_state_districts.retry(exc=e,
                                               countdown=3,
                                               max_retries=None)
    try:
        lookup_url = settings.OPENSTATES_DISTRICT_SEARCH_URL % \
            (address.latitude, address.longitude) \
            + "&apikey=53f7bd2a41df42c082bb2f07bd38e6aa"
    except TypeError:
        # in case an address doesn't have a latitude or longitude
        return False
    response = requests.get(
        lookup_url, headers={"content-type": 'application/json; charset=utf8'})
    response_json = response.json()
    try:
        for rep in response_json:
            try:
                sector = 'state_%s' % rep['chamber']
                query = 'MATCH (l:Location {name: "%s", sector:"federal"})-' \
                        '[:ENCOMPASSES]->(district:Location {name:"%s", ' \
                        'sector:"%s"}) RETURN district ' % \
                        (us.states.lookup(address.state).name,
                         rep['district'], sector)
                res, _ = db.cypher_query(query)
            except KeyError:
                return False
            try:
                res = res[0]
            except IndexError as e:
                raise connect_to_state_districts.retry(exc=e,
                                                       countdown=3,
                                                       max_retries=None)
            try:
                state_district = Location.inflate(res.district)
            except (CypherException, ClientError, IOError,
                    CouldNotCommit) as e:
                raise connect_to_state_districts.retry(exc=e,
                                                       countdown=3,
                                                       max_retries=None)
            if state_district not in address.encompassed_by:
                address.encompassed_by.connect(state_district)
        spawn_task(task_func=create_and_attach_state_level_reps,
                   task_param={"rep_data": response_json})
        return True
    except (CypherException, IOError, ClientError, CouldNotCommit) as e:
        raise connect_to_state_districts.retry(exc=e,
                                               countdown=3,
                                               max_retries=None)
Beispiel #20
0
 def test_update_with_matching_location(self):
     settings.CELERY_ALWAYS_EAGER = True
     self.client.force_authenticate(user=self.user)
     url = reverse('address-detail',
                   kwargs={'object_uuid': self.address.object_uuid})
     state = Location(name="Michigan").save()
     district = Location(name="10", sector="federal").save()
     state.encompasses.connect(district)
     district.encompassed_by.connect(state)
     data = {
         'city': "Walled Lake",
         'longitude': -83.48016,
         'state': "MI",
         'street': "300 Eagle Pond Dr.",
         'postal_code': "48390-3071",
         'congressional_district': "10",
         'latitude': 42.54083
     }
     response = self.client.put(url, data=data, format='json')
     settings.CELERY_ALWAYS_EAGER = False
     address = Address.nodes.get(object_uuid=response.data['id'])
     self.assertIn(district, address.encompassed_by)
    def test_google_places_state(self):
        query = 'MATCH (a:Location) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)
        location = parse_google_places(quebec_data['address_components'],
                                       quebec_data['place_id'])

        self.assertEqual(location.name, "Quebec")
        res, _ = db.cypher_query('MATCH (a:Location '
                                 '{name: "Canada"}) RETURN a')
        country = Location.inflate(res.one)

        self.assertTrue(country in location.encompassed_by)
        self.assertTrue(location in country.encompasses)
 def setUp(self):
     self.unit_under_test_name = 'location'
     self.email = "*****@*****.**"
     create_user_util_test(self.email)
     self.pleb = Pleb.nodes.get(email=self.email)
     self.user = User.objects.get(email=self.email)
     self.url = "http://testserver"
     query = 'MATCH (a:Position) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
     db.cypher_query(query)
     query = 'MATCH (a:Location) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
     db.cypher_query(query)
     self.location = Location(name="Michigan").save()
     self.city = Location(name="Walled Lake").save()
     self.senator = Position(name="Senator").save()
     self.house_rep = Position(name="House Rep").save()
     self.school = Position(name="School Board", level="local").save()
     self.location.encompasses.connect(self.city)
     self.city.encompassed_by.connect(self.location)
     self.senator.location.connect(self.location)
     self.house_rep.location.connect(self.location)
     self.school.location.connect(self.city)
     cache.clear()
 def setUp(self):
     settings.CELERY_ALWAYS_EAGER = True
     self.email = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.user = User.objects.get(email=self.email)
     self.headers = {"content-type": 'application/json; charset=utf8'}
     with open("sb_public_official/tests/michigan_reps.json") as json_file:
         self.json_data = json.load(json_file)
     self.mi = Location(name=us.states.lookup("MI").name,
                        sector="federal").save()
     self.lower = Location(name='38', sector='state_lower').save()
     self.upper = Location(name='15', sector='state_upper').save()
     self.mi.encompasses.connect(self.lower)
     self.lower.encompassed_by.connect(self.mi)
     self.mi.encompasses.connect(self.upper)
     self.upper.encompassed_by.connect(self.mi)
     self.lower_pos = Position().save()
     self.lower_pos.location.connect(self.lower)
     self.upper_pos = Position().save()
     self.upper_pos.location.connect(self.upper)
Beispiel #24
0
 def populate_location_data(self):
     try:
         usa = Location.nodes.get(name='United States of America')
     except (DoesNotExist, Location.DoesNotExist):
         usa = Location(name="United States of America").save()
     query = 'MATCH (a:Location {object_uuid: "%s"})-' \
             '[:POSITIONS_AVAILABLE]->' \
             '(p:Position) RETURN p.name' % usa.object_uuid
     res, _ = db.cypher_query(query)
     positions = [row[0] for row in res]
     if "President" not in positions:
         pres = Position(name="President").save()
         pres.location.connect(usa)
     for root, dirs, files in \
             os.walk('sb_locations/management/commands/states/'):
         if not dirs:
             _, state = root.split("states/")
             state_name = us.states.lookup(state).name
             with open(root + "/" + files[0]) as geo_data:
                 file_data = loads(geo_data.read())
                 try:
                     state = Location.nodes.get(name=state_name)
                 except (DoesNotExist, Location.DoesNotExist):
                     state = Location(name=state_name,
                                      geo_data=dumps(
                                          file_data['coordinates'])).save()
                 except MultipleNodesReturned:
                     continue
                 usa.encompasses.connect(state)
                 state.encompassed_by.connect(usa)
                 query = 'MATCH (a:Location {object_uuid: "%s"})-' \
                         '[:POSITIONS_AVAILABLE]->' \
                         '(p:Position) RETURN p.name' % state.object_uuid
                 res, _ = db.cypher_query(query)
                 positions = [row[0] for row in res]
                 if "Senator" not in positions:
                     senator = Position(name='Senator').save()
                     senator.location.connect(state)
     for root, dirs, files in \
             os.walk('sb_locations/management/commands/districts/'):
         try:
             if files[0] != '.DS_Store':
                 _, district_data = root.split('districts/')
                 state, district = district_data.split('-')
                 if not int(district):
                     district = 1
                 try:
                     state_node = Location.nodes.get(
                         name=us.states.lookup(state).name)
                 except MultipleNodesReturned:
                     continue
                 with open(root + "/shape.geojson") as geo_data:
                     file_data = loads(geo_data.read())
                     query = 'MATCH (l:Location {name:"%s"})-' \
                             '[:ENCOMPASSES]->(d:Location {name:"%s"}) ' \
                             'RETURN d' % \
                             (state_node.name, district)
                     res, _ = db.cypher_query(query)
                     if not res:
                         district = Location(
                             name=int(district),
                             geo_data=dumps(
                                 file_data['coordinates'])).save()
                         district.encompassed_by.connect(state_node)
                         usa.encompasses.connect(district)
                         state_node.encompasses.connect(district)
                         query = 'MATCH (a:Location {object_uuid: "%s"})-' \
                                 '[:POSITIONS_AVAILABLE]->' \
                                 '(p:Position) ' \
                                 'RETURN p.name' % district.object_uuid
                         res, _ = db.cypher_query(query)
                         positions = [row[0] for row in res]
                         if "House Representative" not in positions:
                             house_rep = Position(
                                 name="House Representative").save()
                             house_rep.location.connect(district)
         except IndexError:
             pass
     return True
Beispiel #25
0
 def test_create_state_districts_already_exist(self):
     mi = Location(name=us.states.lookup("MI").name,
                   sector="federal").save()
     address = Address(state="MI", latitude=42.532020,
                       longitude=-83.496500).save()
     upper = Location(name="15", sector="state_upper").save()
     lower = Location(name="38", sector="state_lower").save()
     address.encompassed_by.connect(lower)
     address.encompassed_by.connect(upper)
     mi.encompasses.connect(upper)
     upper.encompassed_by.connect(mi)
     mi.encompasses.connect(lower)
     lower.encompassed_by.connect(mi)
     res = connect_to_state_districts.apply_async(
         kwargs={'object_uuid': address.object_uuid})
     self.assertTrue(res.result)
     query = 'MATCH (l:Location {name:"38", sector:"state_lower"}), ' \
             '(l2:Location {name:"15", sector:"state_upper"}) RETURN l, l2'
     res, _ = db.cypher_query(query)
     lower = Location.inflate(res[0].l)
     upper = Location.inflate(res[0].l2)
     self.assertTrue(lower in address.encompassed_by)
     self.assertTrue(upper in address.encompassed_by)
     res = connect_to_state_districts.apply_async(
         kwargs={'object_uuid': address.object_uuid})
     self.assertTrue(res.result)
     query = 'MATCH (l:Location {name:"38", sector:"state_lower"}), ' \
             '(l2:Location {name:"15", sector:"state_upper"}) RETURN l, l2'
     res, _ = db.cypher_query(query)
     self.assertEqual(len(res[0]), 2)  # assert only two nodes returned
     self.assertEqual(lower, Location.inflate(res[0].l))
     # assert only one lower node
     self.assertEqual(upper, Location.inflate(res[0].l2))
     # assert only one upper node
     mi.delete()
     address.delete()
     upper.delete()
     lower.delete()
Beispiel #26
0
 def test_create_state_districts(self):
     mi = Location(name=us.states.lookup("MI").name,
                   sector="federal").save()
     address = Address(state="MI", latitude=42.532020,
                       longitude=-83.496500).save()
     lower = Location(name='38', sector='state_lower').save()
     upper = Location(name='15', sector='state_upper').save()
     mi.encompasses.connect(lower)
     lower.encompassed_by.connect(mi)
     mi.encompasses.connect(upper)
     upper.encompassed_by.connect(mi)
     res = connect_to_state_districts.apply_async(
         kwargs={'object_uuid': address.object_uuid})
     self.assertTrue(res.result)
     self.assertTrue(lower in address.encompassed_by)
     self.assertTrue(upper in address.encompassed_by)
     mi.delete()
     address.delete()
     upper.delete()
     lower.delete()
Beispiel #27
0
 def test_get_location(self):
     location = Location().save()
     self.mission.location.connect(location)
     res = self.mission.get_location()
     self.assertEqual(res.object_uuid, location.object_uuid)
class TestCreateStateDistricts(TestCase):
    def setUp(self):
        settings.CELERY_ALWAYS_EAGER = True
        self.email = "*****@*****.**"
        self.pleb = create_user_util_test(self.email)
        self.user = User.objects.get(email=self.email)
        self.headers = {"content-type": 'application/json; charset=utf8'}
        with open("sb_public_official/tests/michigan_reps.json") as json_file:
            self.json_data = json.load(json_file)
        self.mi = Location(name=us.states.lookup("MI").name,
                           sector="federal").save()
        self.lower = Location(name='38', sector='state_lower').save()
        self.upper = Location(name='15', sector='state_upper').save()
        self.mi.encompasses.connect(self.lower)
        self.lower.encompassed_by.connect(self.mi)
        self.mi.encompasses.connect(self.upper)
        self.upper.encompassed_by.connect(self.mi)
        self.lower_pos = Position().save()
        self.lower_pos.location.connect(self.lower)
        self.upper_pos = Position().save()
        self.upper_pos.location.connect(self.upper)

    def tearDown(self):
        settings.CELERY_ALWAYS_EAGER = False
        self.mi.delete()
        self.lower.delete()
        self.upper.delete()
        self.upper_pos.delete()
        self.lower_pos.delete()

    def test_success(self):
        res = create_and_attach_state_level_reps.apply_async(
            kwargs={'rep_data': self.json_data})
        while not res.ready():
            time.sleep(1)
        self.assertTrue(res.result)

    def test_rep_already_exists(self):
        try:
            official = PublicOfficial.nodes.get(bioguideid="MIL000290")
        except (PublicOfficial.DoesNotExist, DoesNotExist):
            official = PublicOfficial(bioguideid="MIL000290",
                                      first_name="Kathy",
                                      last_name="Crawford",
                                      state_district="38",
                                      state_chamber="lower",
                                      state="mi").save()
        res = create_and_attach_state_level_reps.apply_async(
            kwargs={'rep_data': self.json_data})
        while not res.ready():
            time.sleep(1)
        self.assertTrue(res.result)
        camp = official.get_quest()
        self.assertTrue(camp in official.quest)
        official.delete()
        camp.delete()

    def test_rep_already_has_campaign(self):
        try:
            official = PublicOfficial.nodes.get(bioguideid="MIL000290")
            official.delete()
        except (PublicOfficial.DoesNotExist, DoesNotExist):
            pass
        official = PublicOfficial(bioguideid="MIL000290",
                                  first_name="Kathy",
                                  last_name="Crawford",
                                  state_district="38",
                                  state_chamber="lower",
                                  state="mi").save()
        campaign = Quest(first_name=official.first_name,
                         last_name=official.last_name).save()
        official.quest.connect(campaign)
        res = create_and_attach_state_level_reps.apply_async(
            kwargs={'rep_data': self.json_data})
        while not res.ready():
            time.sleep(1)
        self.assertTrue(res.result)
        official.delete()
        campaign.delete()