def addresses(): ''' GET: Returns all addresses. POST: Creates an address. ''' if request.method == 'POST': json = request.get_json() address = Address.create(json) return jsonify(address.json), status.HTTP_201_CREATED addresses = Address.query.all() return jsonify({'addresses': unpack_json(addresses)}), status.HTTP_200_OK
def post(self): json_data = request.get_json() if not json_data: raise RequestException("No input data", 400) try: data = address_schema.load(json_data) except ValidationError as err: raise RequestException("Invalid input data", 400, err.messages) address = Address.create(**data) result = address_schema.dump(address) response = jsonify({ APIConst.MESSAGE: 'created new address', APIConst.DATA: result }) return response
def load(self, data, many=None, partial=None): de_data = super().load(data, many=many, partial=partial) de_data = [de_data] if not many else de_data for de_data_item in de_data: location_dicts = de_data_item.get('locations') if location_dicts is None: continue locations = [] for loc_dict in location_dicts: id = loc_dict.get('id') print(id) loc = None if id: loc = Address.get_with_id(id) else: loc = Address.create(commit=False, **loc_dict) locations.append(loc) de_data_item['locations'] = locations return de_data if many else de_data[0]
def test_db(app): db.drop_all() db.create_all() # define categories State.create(id=1, name="North Carolina", state_short="NC") State.create(id=2, name="New York", state_short="NY") Sector.create(id=1, name="Home Services") Sector.create(id=2, name="Food & Drink") c1 = Category.create(id=1, name="Electrician", sector_id=1) c2 = Category.create(id=2, name="Plumber", sector_id=1) Category.create(id=3, name="Mexican Restaurant", sector_id=2) a1 = Address.create( line1="13 Brook St", city="Lakewood", zip="14750", state_id=2, latitude=42.100201, longitude=-79.340303 ) # add test users u1 = User.create( id=1, username="******", first_name="Sarah", last_name="Smith", email="*****@*****.**", address=a1 ) u2 = User.create( id=2, username="******", first_name="John", last_name="Jones", email="*****@*****.**", address=Address( line1="7708 covey chase Dr", line2='', city="Charlotte", zip="28210", state_id=1, latitude=35.123949, longitude=-80.864783 ) ) u3 = User.create( id=3, username="******", first_name="Mark", last_name="Johnson", email="*****@*****.**", address=Address( line1="7718 Covey Chase Dr", line2='', city="Charlotte", zip="28210", state_id=1, latitude=35.123681, longitude=-80.865045 ) ) User.create( id=4, username="******", first_name="Hyman", last_name="Rickover", email="*****@*****.**", address=Address( line1="7920 Covey Chase Dr", line2='', city="Charlotte", zip="28210", state_id=1, latitude=35.120759, longitude=-80.865781 ) ) # add test providers p1 = Provider.create( id=1, name="Douthit Electrical", telephone="704-726-3329", email="*****@*****.**", website='https://www.douthitelectrical.com/', categories=[c1, c2], address=Address( line1="6000 Fairview Rd", line2="suite 1200", city="Charlotte", zip="28210", state_id=1, latitude=35.150495, longitude=-80.838958 ) ) Provider.create( id=2, name="Evers Electric", telephone="7048431910", email='', website='http://www.everselectric.com/', categories=[c1], address=Address( line1="3924 Cassidy Drive", line2="", city="Waxhaw", zip="28173", state_id=1, latitude=34.938645, longitude=-80.760691 ) ) p3 = Provider.create( id=3, name="Preferred Electric Co", telephone="7043470446", email="*****@*****.**", categories=[c1], address=Address( line1="4113 Yancey Rd", line2='', city="charlotte", zip="28217", state_id=1, latitude=35.186947, longitude=-80.880459 ) ) p4 = Provider.create( id=4, name="Basic Electric Co", telephone="7044070077", email="*****@*****.**", is_active=False, categories=[c1], address=Address( line1="7708 Covey Chase Dr", line2='', city="Charlotte", zip="28217", state_id=1, latitude=35.123949, longitude=-80.865781 ) ) # add test groups g1 = Group.create( id=1, name="QHIV HOA", description="Hoa for the neighborhood", admin_id=2 ) Group.create( id=2, name="Shannon's Bees", description="Insects that like to make honey", admin_id=2 ) Group.create( id=3, name="Shawshank Redemption Fans", description="test", admin_id=3 ) # add test reviews Review.create( id=1, user=u2, provider=p1, category=c1, rating=3, cost=3, description="fixed a light bulb", comments="satisfactory work.", pictures=[ Picture( path=os.path.join( app.config['MEDIA_FOLDER'], '2', 'test1.jpg' ), name='test1.jpg' ) ] ) Review.create( id=2, user=u3, provider=p1, category=c1, rating=5, cost=5, price_paid="", description="installed breaker box", comments="very clean" ) Review.create( id=3, user=u1, provider=p1, category=c1, rating=1, cost=5, price_paid="", description="test", comments="Test" ) Review.create( id=4, user=u2, provider=p3, category=c1, rating=5, cost=2, price_paid="", description="test", comments="Test123", service_date=date(2019, 5, 1) ) Review.create( id=5, user=u2, provider=p3, category=c1, rating=4, cost=3, price_paid="", description="moretest", comments="Test123456", service_date=date(2019, 5, 1) ) Review.create( id=6, user=u1, provider=p1, category=c1, rating=1, cost=5, price_paid="", description="yetanothertest", comments="Testing" ) Review.create( id=7, user=u2, provider=p4, category=c1, rating=1, cost=1, price_paid=100, description="getting electrocuted", comments="terrible" ) # add test relationships u2.add(u1) u2.add(g1) u3.add(g1) # set user passwords u1.set_password("password1234") u2.set_password("password") u3.set_password("password5678") # set starting messages Message.send_new( dict(user_id=1), dict(user_id=2), "test subject", "test body" ) time.sleep(1) Message.send_new( dict(user_id=2), dict(full_name="admin"), "test admin subject", "test adminbody", msg_type="admin" ) Message.send_new( dict(user_id=1), dict(user_id=2), "yet another test subject", " yet another test body" ) yield db db.session.remove() db.drop_all()
def test_missingRequiredUnknownFalse(self, key, newAddressDict, mockGeoResponse): newAddressDict[key] = None with pytest.raises(AssertionError): Address.create(**newAddressDict)
def test_newRequiredEmptyString(self, key, newAddressDict, mockGeoResponse): newAddressDict[key] = "" with pytest.raises(IntegrityError): Address.create(**newAddressDict)
def test_newMissingRequired(self, key, newAddressDict, mockGeoResponse): newAddressDict[key] = None with pytest.raises(IntegrityError): Address.create(**newAddressDict)