def two_saved_planets(app): # Arrange earth = Planet(name="Earth", description="home sweet home") saturn = Planet(name="Saturn", description="I have rings") db.session.add_all([earth, saturn]) db.session.commit()
def two_saved_planets(app): # Arrange earth = Planet(id=1, name="earth", description="only planet with life", order=3) mars = Planet(id=2, name="mars", description="the red planet", order=4) db.session.add_all([earth, mars]) # Alternatively, we could do # db.session.add(ocean_book) # db.session.add(mountain_book) db.session.commit() # @pytest.fixture # def planet_data(app): # new_planet_instance = Planet(name="venus", description="hot hot hot", order=2) # db.session.add(new_planet_instance) # db.session.commit() # return { # "name": "venus", # "description": "hot hot hot", # "order" : 2 # }
def three_saved_planets(app): earth = Planet(name="Earth", description="nice big rock") pluto = Planet(name="Pluto", description="am I a planet tho?") mars = Planet(name="Mars", description="I have robots on me") db.session.add_all([earth, pluto, mars]) db.session.commit()
def two_saved_planets(app): swirly_planet = Planet(name="Swirly Planet", description="a swirl planet", size="Small") loopy_planet = Planet(name="Loopy Planet", description="a loop planet", size="Medium") db.session.add_all([swirly_planet, loopy_planet]) db.session.commit()
def two_saved_planets(app): # Arrange mercury_planet = Planet(name="Mercury", description="Planet Mercury") mars_planet = Planet(name="Mars", description="Planet Mars") db.session.add_all([mercury_planet, mars_planet]) db.session.commit()
def two_saved_planets(app): venus_planet = Planet(name="Venus", description="Goddess of love and beauty", founder="Heidi and Manu") earth_planet = Planet(name="Earth", description="Mother Earth", founder="Jesus") db.session.add_all([venus_planet, earth_planet]) db.session.commit()
def test_create_one_planet(client): new_planet_instance = Planet(name="venus", description="hot hot hot", order=2) response = client.post("/planets", json=new_planet_instance.to_json()) response_body = response.get_data(as_text=True) assert response.status_code == 201 assert response_body == '"Planet venus successfully created"\n'
def two_saved_planets(app): # Arrange Saturn_planet = Planet(name="Saturn", description="Round", color="Pink") Pluto_planet = Planet(name="Pluto", description="Rocky", color="Blue") db.session.add_all([Saturn_planet, Pluto_planet]) # Alternatively, we could do # db.session.add(ocean_book) # db.session.add(mountain_book) db.session.commit()
def two_saved_planets(app): # Arrange planet_foozie = Planet(name="Foozie", description="The fuzziest planet") planet_the_bomb = Planet(name="The Bomb", description="Explosive") db.session.add_all([planet_foozie, planet_the_bomb]) db.session.commit() # Alternatively, we could do # db.session.add(ocean_book) # db.session.add(mountain_book)
def two_saved_planets(app): #Arrange jupiter = Planet(name="Jupiter", description="The largest planet. A gas giant.", pos_from_sun=5) mercury = Planet(name="Mercury", description="The smallest planet.", pos_from_sun=1) db.session.add_all([jupiter, mercury]) db.session.commit()
def two_saved_planets(app): # Arrange mercury = Planet(name="Mercury", description="The hot planet", order_from_sun=1) venus = Planet(name="Venus", description="Goddess of beauty", order_from_sun=2) db.session.add_all([mercury, venus]) db.session.commit()
def two_planets(app): # Arrange planet_mars = Planet(title="Mars", description="Mars is the fourth planet from the Sun and the second-smallest planet in the Solar System, being larger than only Mercury.") planet_saturn = Planet(title="Saturn", description="Saturn is the sixth planet from the Sun and the second-largest in the Solar System, after Jupiter.") db.session.add_all([planet_saturn, planet_mars]) # Alternatively, we could do # db.session.add(planet_mars) # db.session.add(planet_saturn) db.session.commit()
def two_saved_planets(app): # Arrange pig_planet = Planet(name="Pigsty", description="oink oink", size="HUGE") bat_planet = Planet(name="Batcave", description="screescree!", size="teenyteeny") db.session.add_all([pig_planet, bat_planet]) # Alternatively, we could do # db.session.add(ocean_book) # db.session.add(mountain_book) db.session.commit()
def two_saved_planets(app): # Arrange water_planet = Planet( name="Callisto", description="This planet has water all over the surface") mysterious_planet = Planet(name="Ego", description="A new planet the has many secrets") db.session.add_all([water_planet, mysterious_planet]) # Alternatively, we could do # db.session.add(ocean_book) # db.session.add(mountain_book) db.session.commit()
def two_saved_planets(app): # Arrange one_planet = Planet(name="One Planet", description="watr 4evr", radius=123.1) two_planet = Planet(name="Two Planet", description="watr 4evr", radius=223.1) db.session.add_all([one_planet, two_planet]) # Alternatively, we could do # db.session.add(one_planet) # db.session.add(two-planet) db.session.commit()
def two_saved_planets(app): jupiter = Planet(id=1, name="Jupiter", description="Largest planet in our solar system", type="So much GAS THO") saturn = Planet(id=2, name="Saturn", description="The rings one, duh.", type="gas giant") db.session.add_all([jupiter, saturn]) db.session.commit()
def two_saved_planets(app): # Arrange ocean_Planet = Planet(name="Ocean Planet", description="watr 4evr", size="500") mountain_Planet = Planet(name="Mountain Planet", description="i luv 2 climb rocks", size="1") db.session.add_all([ocean_Planet, mountain_Planet]) # Alternatively, we could do # db.session.add(ocean_Planet) # db.session.add(mountain_Planet) db.session.commit()
def two_saved_planets(app): # Arrange mars_planet = Planet(name="Mars", description="4th planet", habitable="False" ) venus_planet = Planet(name="Venus", description="2nd planet", habitable="False" ) db.session.add_all([mars_planet, venus_planet]) # Alternatively, we could do # db.session.add(ocean_book) # db.session.add(mountain_book) db.session.commit()
def handle_planets(): if request.method == "GET": planets = Planet.query.all() planets_response = [] for planet in planets: planets_response.append({ "id": planet.id, "name": planet.name, "description": planet.description, "pos_from_sun": planet.pos_from_sun }) return jsonify(planets_response), 200 elif request.method == "POST": request_body = request.get_json() new_planet = Planet(name=request_body["name"], description=request_body["description"], pos_from_sun=request_body["pos_from_sun"]) db.session.add(new_planet) db.session.commit() return jsonify({ "success": True, "message": f"Planet {new_planet.name} has been created." }), 201
def two_saved_planets(app): # Arrange mars_planet = Planet( name="Mars", description= "Mars is the fourth planet from the Sun and the second-smallest planet in the Solar System", diameter=6787) venus_planet = Planet( name="Venus", description= "Venus is the second planet from the Sun. It is named after the Roman goddess of love and beauty", diameter=12104) db.session.add_all([mars_planet, venus_planet]) db.session.commit()
def handle_planets(): if request.method == "GET": habitable_query = request.args.get("habitable") if habitable_query: planets = Planet.query.filter_by(habitable=habitable_query) else: planets = Planet.query.all() planets_response = [] for planet in planets: planets_response.append({ "id": planet.id, "name": planet.name, "description": planet.description, "habitable" : planet.habitable }) return jsonify(planets_response) elif request.method == "POST": request_body = request.get_json() new_planet = Planet(name=request_body["name"], description=request_body["description"], habitable=request_body["habitable"]) db.session.add(new_planet) db.session.commit() return make_response(jsonify(f"Planet {new_planet.name} successfully created"),201)
def handle_planets(): if request.method == "GET": planets = Planet.query.all() planets_response = [] for planet in planets: planets_response.append({ "id": planet.id, "name": planet.name, "description": planet.description, "distance from Earth": planet.distance_from_earth }) return jsonify(planets_response), 200 elif request.method == "POST": request_body = request.get_json() new_planet = Planet(name=request_body["name"], description=request_body["description"], distance_from_earth=request_body["distance from Earth"]) db.session.add(new_planet) db.session.commit() return make_response(f"Planet {new_planet.name} successfully created", 201)
def create_planet(): request_body = request.get_json() new_planet = Planet(name = request_body["name"], description = request_body["description"]) db.session.add(new_planet) db.session.commit() return make_response(f"Planet {new_planet.name} successfully created", 201)
def add_planet(): request_body = request.get_json() new_planet = Planet(name=request_body["name"], description=request_body["description"], type=request_body["type"]) db.session.add(new_planet) db.session.commit() return make_response(jsonify(f"Planet {new_planet.name} has been added."), 201)
def planets(): request_body = request.get_json() new_planet = Planet(name=request_body["name"], description=request_body["description"], size=request_body["size"]) db.session.add(new_planet) db.session.commit() return { "success": True, "message": f"Planet {new_planet.name} successfully created" }, 201
def insert(): payload = request.get_json(force=True) # Initialize a Planet object planet = Planet(payload['name'], payload['climate'], payload['terrain']) # Search if this planet already registered on database planet_on_db = mongo.db.planets.find_one({'name': planet.name}) if planet_on_db: return send_response( Response( False, f'The planet "{planet.name}" has already been registered in our database' )) # Call SWAPI to get more data query_params = {'search': planet.name} swapi_response = requests.get('https://swapi.co/api/planets/', query_params) # Try to get id and films from SWAPI if swapi_response.status_code == 200: json_resp = swapi_response.json() if 'count' in json_resp and json_resp['count'] > 0: planet_data = next( filter(lambda x: x['name'].upper() == planet.name.upper(), json_resp['results'])) planet.films = len(planet_data['films']) planet.swapi_id = int( list(filter(None, planet_data['url'].split('/')))[-1]) else: return send_response(Response(False, 'This planet does not exist')) # Insert Planet on mongo res = mongo.db.planets.insert_one(planet.to_dict()) return send_response(Response(res.acknowledged))
def create_a_planet(app): Earth = Planet(name="Earth", description="home", distance_from_earth=0) db.session.add_all([Earth]) db.session.commit() # @pytest.fixture # def post_a_planet(app): # Earth = Planet(name="Earth", # description="home", # distance_from_earth=0) # db.session.add_all([Earth]) # db.session.commit()
def index(): # Get pagination parameters page = int(request.args.get('page', 1)) page_size = int(request.args.get('page_size', 5)) page = 1 if page < 1 else page page_size = 5 if page_size < 5 else 10 if page_size > 10 else page_size # Find planets on db planets_db = list( mongo.db.planets.find( {}, projection=Planet.remove_invisible_fields(ignore_id=False)).sort([ ('name', 1) ]).skip((page - 1) * page_size).limit(page_size)) return send_response(Response(True, data=planets_db))
def handle_planets(): if request.method == "GET": planets = Planet.query.all() planets_response = [] for planet in planets: planets_response.append({ "id": planet.id, "name": planet.name, "description": planet.description }) return jsonify(planets_response) elif request.method == "POST": request_body = request.get_json() new_planet = Planet(name=request_body["name"], description=request_body["description"]) db.session.add(new_planet) db.session.commit() return {"Message":f"Planet {new_planet.name} successfully created"}, 201
def planet(): if request.method == "GET": planet = Planet.query.all() planet_response = [] for planet in planet: planet_response.append({ "id": planet.id, "title": planet.title, "description": planet.description }) return jsonify(planet_response), 200 else: request_body = request.get_json() new_planet = Planet(title=request_body["title"], description=request_body["description"]) db.session.add(new_planet) db.session.commit() return jsonify(f"Planet {new_planet.title} successfully created", 201)