Пример #1
0
 def testZipAPI(self):
   # Check to make sure that the Geocoder API is not used if not needed
   # Only checks that geocode IS NOT called the second time (zipcode MUST come from somwhere, ensuring that Google API is called the first time)
   prof_1 = self.Profiles.find_one({'user_id':'1'})
   self.assertEquals(prof_1['_id'],  '1')
   #zip creation phase, should make API call and obtain zipcode
   with mock.patch('main.Profile.detect_home', return_value=(-122.259769,37.871758)) as func1:
       with mock.patch('main.Profile.detect_home_from_db', return_value=(-122.259769,37.871758)) as func2:
           Profile.update_profiles(True)
           self.assertEquals(func1.called, True)
           self.assertEquals(func2.called, True)
           func1.reset_mock()
           func2.reset_mock()
           prof_1 = self.Profiles.find_one({'user_id':'1'})
           self.assertEquals(prof_1['zip'],  '94709')
           #zip should be stored, no API call
           with mock.patch('main.Profile.Geocoder.reverse_geocode', return_value=None) as func3:
               Profile.update_profiles(True)
               self.assertEquals(func3.called, False)
               prof_1 = self.Profiles.find_one({'user_id':'1'})
               self.assertEquals(prof_1['zip'],  '94709')
   with mock.patch('main.Profile.detect_home', return_value=(-122.2584,37.8697)) as func1:
           #new zip retrieval phase, should make API call and obtain zipcode
               Profile.update_profiles(True)
               prof_1 = self.Profiles.find_one({'user_id':'1'})
               self.assertEquals(prof_1['zip'],  '94720')
               with mock.patch('main.Profile.Geocoder.reverse_geocode', return_value=None) as func3:
                   #zip should be stored, no API call
                   Profile.update_profiles(True)
                   self.assertEquals(func3.called, False)
                   func3.reset_mock()
                   prof_1 = self.Profiles.find_one({'user_id':'1'})
                   self.assertEquals(prof_1['zip'],  '94720')
Пример #2
0
 def testZipCreation(self):
   # Make sure that zipcodes are updated in the database, correctly and when needed
   prof_1 = self.Profiles.find_one({'user_id':'1'})
   self.assertEquals(prof_1['_id'],  '1')
   #zip creation phase, should make API call and obtain zipcode
   with mock.patch('main.Profile.detect_home', return_value=(-122.259769,37.871758)) as func1:
       with mock.patch('main.Profile.detect_home_from_db', return_value=(-122.259769,37.871758)) as func2:
           Profile.update_profiles(True)
           self.assertEquals(func1.called, True)
           self.assertEquals(func2.called, True)
           func1.reset_mock()
           func2.reset_mock()
           prof_1 = self.Profiles.find_one({'user_id':'1'})
           self.assertEquals(prof_1['zip'],  '94709')
   #test new address/zipcode: database value does not equal home value
   with mock.patch('main.Profile.detect_home', return_value=(-122.2584,37.8697)) as func1:
           #new zip retrieval phase, should make API call and obtain zipcode
           Profile.update_profiles(True)
           self.assertEquals(func1.called, True)
           func1.reset_mock()
           prof_1 = self.Profiles.find_one({'user_id':'1'})
           self.assertEquals(prof_1['zip'],  '94720')