def __init__(self, conn): Element.__init__(self, 'profiles') self.conn = conn self.stmt = StatementCursor(self.conn) self.env = ProfileEnvironment(self.conn) self.profiletraits = ProfileTrait(self.conn) self._profiles = {} self._profile = Profile(self.conn) for row in self.stmt.select(table='profiles', order='profile'): self._append_profile(row.profile, row.suite)
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')
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')
class PaellaProfiles(Element): def __init__(self, conn): Element.__init__(self, 'profiles') self.conn = conn self.stmt = StatementCursor(self.conn) self.env = ProfileEnvironment(self.conn) self.profiletraits = ProfileTrait(self.conn) self._profiles = {} self._profile = Profile(self.conn) for row in self.stmt.select(table='profiles', order='profile'): self._append_profile(row.profile, row.suite) def _append_profile(self, profile, suite): element = self.export_profile(profile, suite) self._profiles[profile] = element self.appendChild(self._profiles[profile]) def export_profile(self, profile, suite=None): if suite is None: row = self.stmt.select_row(table='profiles',clause=Eq('profile', profile)) suite = row['suite'] suite = str(suite) profile = str(profile) self.env.set_profile(profile) element = ProfileElement(profile, suite) element.append_traits(self.profiletraits.trait_rows(profile)) element.append_families(self._profile.family_rows(profile)) element.append_variables(self.env.get_rows()) return element def insert_profile(self, profile): idata = {'profile' : profile.name, 'suite' : profile.suite} self.stmt.insert(table='profiles', data=idata) idata = {'profile' : profile.name, 'trait' : None, 'ord' : 0} for trait, ord in profile.traits: print trait, ord idata['trait'] = trait idata['ord'] = ord #str(ord) self.stmt.insert(table='profile_trait', data=idata) idata = {'profile' : profile.name, 'trait' : None, 'name' : None, 'value': None} idata = dict(profile=profile.name) for family in profile.families: idata['family'] = family self.stmt.insert(table='profile_family', data=idata) idata = dict(profile=profile.name) for trait, name, value in profile.vars: idata['trait'] = trait idata['name'] = name idata['value'] = value self.stmt.insert(table='profile_variables', data=idata) def export_profiles(self, path): rows = self.stmt.select(fields='profile', table='profiles', clause=None) for row in rows: self.write_profile(row.profile, path) def write_profile(self, profile, path): xmlfile = file(join(path, '%s.xml' % profile), 'w') data = self.export_profile(profile) data.writexml(xmlfile, indent='\t', newl='\n', addindent='\t') xmlfile.close()
class PaellaProfiles(Element): def __init__(self, conn): Element.__init__(self, 'profiles') self.conn = conn self.stmt = StatementCursor(self.conn) self.env = ProfileEnvironment(self.conn) self.profiletraits = ProfileTrait(self.conn) self._profiles = {} self._profile = Profile(self.conn) for row in self.stmt.select(table='profiles', order='profile'): self._append_profile(row.profile, row.suite) def _append_profile(self, profile, suite): element = self.export_profile(profile, suite) self._profiles[profile] = element self.appendChild(self._profiles[profile]) def export_profile(self, profile, suite=None): if suite is None: row = self.stmt.select_row(table='profiles', clause=Eq('profile', profile)) suite = row['suite'] suite = str(suite) profile = str(profile) self.env.set_profile(profile) element = ProfileElement(profile, suite) element.append_traits(self.profiletraits.trait_rows(profile)) element.append_families(self._profile.family_rows(profile)) element.append_variables(self.env.get_rows()) return element def insert_profile(self, profile): idata = {'profile': profile.name, 'suite': profile.suite} self.stmt.insert(table='profiles', data=idata) idata = {'profile': profile.name, 'trait': None, 'ord': 0} for trait, ord in profile.traits: print trait, ord idata['trait'] = trait idata['ord'] = ord #str(ord) self.stmt.insert(table='profile_trait', data=idata) idata = { 'profile': profile.name, 'trait': None, 'name': None, 'value': None } idata = dict(profile=profile.name) for family in profile.families: idata['family'] = family self.stmt.insert(table='profile_family', data=idata) idata = dict(profile=profile.name) for trait, name, value in profile.vars: idata['trait'] = trait idata['name'] = name idata['value'] = value self.stmt.insert(table='profile_variables', data=idata) def export_profiles(self, path): rows = self.stmt.select(fields='profile', table='profiles', clause=None) for row in rows: self.write_profile(row.profile, path) def write_profile(self, profile, path): xmlfile = file(join(path, '%s.xml' % profile), 'w') data = self.export_profile(profile) data.writexml(xmlfile, indent='\t', newl='\n', addindent='\t') xmlfile.close()