def location(): drop_all_tables(test_conn, test_cursor) new_york = save(State(name='New York'), test_conn, test_cursor) pennsylvania = save(State(name='Pennsylvania'), test_conn, test_cursor) brooklyn = save(City(name='Brooklyn', state_id=new_york.id), test_conn, test_cursor) manhattan = save(City(name='Manhattan', state_id=new_york.id), test_conn, test_cursor) philadelphia = save(City(name='Philadelphia', state_id=pennsylvania.id), test_conn, test_cursor) south_philly_zip = save(Zipcode(code=19019, city_id=philadelphia.id), test_conn, test_cursor) chelsea_zip = save(Zipcode(code=10001, city_id=manhattan.id), test_conn, test_cursor) dumbo_zip = save(Zipcode(code=11210, city_id=brooklyn.id), test_conn, test_cursor) venue = save(Venue(name='Los Tacos Al Pastor', price=1), test_conn, test_cursor) location = save( Location(longitude=40.7024, latitude=-73.9875, address='141 Front Street', zipcode_id=dumbo_zip.id, venue_id=venue.id), test_conn, test_cursor) yield location drop_all_tables(test_conn, test_cursor)
def run(self, hotel_details, conn, cursor): amadeus_id = hotel_details['hotel']['hotelId'] hotel_obj = db.find_by_amadeus_id(models.Hotel, amadeus_id, cursor) if hotel_obj: hotel_obj.exists = True offer = OfferBuilder().run(hotel_details, hotel_obj, conn, cursor) offer.hotel_id = hotel_obj.id saved_offer = db.save(offer, conn, cursor) print(saved_offer.__dict__) return { 'hotel': hotel_obj, 'location': hotel_obj.location_id, 'offer': saved_offer } # LOCATION METHOD - RELATIONSHIP QUERY METHOD else: # run location, save location location = LocationBuilder().run(hotel_details, conn, cursor) saved_location = db.save(location, conn, cursor) # run hotel, load in location, save hotel hotel = HotelBuilder().run(hotel_details, saved_location, conn, cursor) hotel.location_id = saved_location.id saved_hotel = db.save(hotel, conn, cursor) # run offer, load in hotel, save offer offer = OfferBuilder().run(hotel_details, saved_hotel, conn, cursor) offer.hotel_id = saved_hotel.id saved_offer = db.save(offer, conn, cursor) return { 'hotel': saved_hotel, 'location': saved_location, 'offer': saved_offer }
def build_category(): db.drop_all_tables(test_conn, test_cursor) category = models.Category() category.name = 'Taco Places' db.save(category, test_conn, test_cursor) yield db.drop_all_tables(test_conn, test_cursor)
def test_find_or_create_by_finds_when_existing_category(clean_tables): category = models.Category() category.name = 'Taco Places' db.save(category, test_conn, test_cursor) test_cursor.execute('SELECT COUNT(*) FROM categories;') begin_cat_num = test_cursor.fetchone() models.Category.find_or_create_by_name('Taco Places', test_conn, test_cursor) test_cursor.execute('SELECT COUNT(*) FROM categories;') end_cat_num = test_cursor.fetchone() assert end_cat_num == begin_cat_num
def run(self, TS_details, game, conn, cursor): earnings_attributes = self.select_attributes(TS_details) earnings = models.Earnings(**earnings_attributes) earnings.game_id = game.id # let'ss move all saves to the database to the builder class earnings = db.save(earnings, conn, cursor) return earnings
def run(self, venue_details, venue, conn, cursor): location_attributes = self.select_attributes(venue_details) location = self.build_location_city_state_zip(location_attributes, conn, cursor) location.venue_id = venue.id location = db.save(location, conn, cursor) return location
def run(self, TS_details, game, search_date, rank_type, conn, cursor): selected = self.select_attributes(TS_details, search_date, rank_type) selected['game_id'] = game.id rating = models.Rating.find_by(selected['game_id'], selected['rank_type'], selected['ranking'], selected['date_created'], cursor) if not rating: rating = db.save(models.Rating(**selected), conn, cursor) return rating
def find_or_create_by_city_state_zip(self, city_name='N/A', state_name='N/A', code=None, conn=None, cursor=None): if not city_name or not state_name: raise KeyError('must provide conn or cursor') state = db.find_or_create_by_name(models.State, state_name, conn, cursor) city = db.find_by_name(models.City, city_name, cursor) zipcode = models.Zipcode.find_by_code(code, cursor) if not city: city = models.City(name=city_name, state_id=state.id) city = db.save(city, conn, cursor) if not zipcode: zipcode = models.Zipcode(code=code, city_id=city.id) zipcode = db.save(zipcode, conn, cursor) return city, state, zipcode
def run(self, venue_details, conn, cursor): selected = self.select_attributes(venue_details) foursquare_id = selected['foursquare_id'] venue = models.Venue.find_by_foursquare_id(foursquare_id, cursor) if venue: venue.exists = True return venue else: venue = db.save(models.Venue(**selected), conn, cursor) venue.exists = False return venue
def run(self, incident_details, conn, cursor): selected = self.select_attributes(incident_details) open_data_id = selected['unique_key'] incident = models.Incident.find_by_open_data_id(open_data_id, cursor) if incident: incident_details.exists = True return incident_details else: incident_details = db.save(models.Incident(**selected), conn, cursor) incident.exists = False return incident
def run(self, TS_details, conn, cursor): selected = self.select_attributes(TS_details) game_name, game_platform = selected['name'], selected['platform'] game = models.Game.find_by_game_name_platform(game_name, game_platform, cursor) if game: game.exists = True else: selected['game_engine'] = self.IGDB_Client.find_game_engine( game_name) # limited query allowance, tap when DNE game = db.save(models.Game(**selected), conn, cursor) game.exists = False # I know this is based on my pattern, but I prefer moving to the Builder class, ask me about this. game.try_sibling_params_if_None(conn, cursor) return game
def tourist_spot(): db.drop_all_tables(test_conn, test_cursor) new_york = db.save(models.State(name='New York'), test_conn, test_cursor) pennsylvania = db.save(models.State(name='Pennsylvania'), test_conn, test_cursor) brooklyn = db.save(models.City(name='Brooklyn', state_id=new_york.id), test_conn, test_cursor) manhattan = db.save(models.City(name='Manhattan', state_id=new_york.id), test_conn, test_cursor) philadelphia = db.save( models.City(name='Philadelphia', state_id=pennsylvania.id), test_conn, test_cursor) south_philly_zip = db.save( models.Zipcode(code=19019, city_id=philadelphia.id), test_conn, test_cursor) chelsea_zip = db.save(models.Zipcode(code=10001, city_id=manhattan.id), test_conn, test_cursor) dumbo_zip = db.save(models.Zipcode(code=11210, city_id=brooklyn.id), test_conn, test_cursor) venue = db.save( models.Venue(name='Los Tacos Al Pastor', price=1, foursquare_id='4bf58dd8d48988d151941735'), test_conn, test_cursor) grimaldis = db.save(models.Venue(name='Grimaldis', price=2), test_conn, test_cursor) pizza = db.save(models.Category(name='Pizza'), test_conn, test_cursor) tourist_spot = db.save(models.Category(name='Tourist Spot'), test_conn, test_cursor) db.save(models.VenueCategory(venue_id=grimaldis.id, category_id=pizza.id), test_conn, test_cursor) db.save( models.VenueCategory(venue_id=grimaldis.id, category_id=tourist_spot.id), test_conn, test_cursor) db.save( models.VenueCategory(venue_id=venue.id, category_id=tourist_spot.id), test_conn, test_cursor) grimaldi_location = db.save( models.Location(longitude=40.7024, latitude=-73.9875, address='1 Front Street', zipcode_id=dumbo_zip.id, venue_id=grimaldis.id), test_conn, test_cursor) taco_location = db.save( models.Location(longitude=40.7024, latitude=-73.9875, address='141 Front Street', zipcode_id=dumbo_zip.id, venue_id=venue.id), test_conn, test_cursor) yield tourist_spot db.drop_all_tables(test_conn, test_cursor)
def run(self, incident_details, conn, cursor): location_attributes = self.select_attributes(incident_details) location = models.Location(**location_attributes) location = db.save(location, conn, cursor) return location
fixed drop_all_tables() -> drop_records() using TRUNCATE to delete table with foreign key added reset_all_primarykey to change primary keys back to 1 every time seed.py is run """ import psycopg2 import api.src.db as db import api.src.models as models # delete/truncate all tables and reset primary keys db.drop_all_tables(db.conn, db.cursor) db.reset_all_primarykey(db.conn, db.cursor) # seeding games amongus = db.save(models.Game(name = 'Among Us', platform = 'android', publisher = 'Innersloth LLC', release_date = '2018-07-25', genre = 'action', game_engine = 'Unity'), db.conn, db.cursor) roblox = db.save(models.Game(name = 'Roblox', platform = 'android', publisher = 'Roblox Corporation', release_date = '2019-12-31', genre = 'building', game_engine = 'Something Else'), db.conn, db.cursor) candy = db.save(models.Game(name = 'Candy Crush Saga', platform = 'android', publisher = 'King', release_date = '2015-1-1', genre = 'casual', game_engine = 'Candy Factory'), db.conn, db.cursor) amongus_ios = db.save(models.Game(name = 'Among Us', platform = 'iOS', publisher = 'Innersloth LLC', release_date = '2018-11-2', genre = 'action', game_engine = 'Unity'), db.conn, db.cursor) # breakpoint() # seeding ratings no1 = db.save(models.Rating(game_id = amongus.id, metacritic = 84, TS_rating = 4.465, rank_type = 'top free', ranking = 1, date_created = '2020-12-01'), db.conn, db.cursor) no2 = db.save(models.Rating(game_id = roblox.id, metacritic = 90, TS_rating = 4.48,
def create_venue_categories(self, venue, categories, conn, cursor): categories = [ models.VenueCategory(venue_id=venue.id, category_id=category.id) for category in categories ] return [db.save(category, conn, cursor) for category in categories]