def get(self): """ Returns all of the Customers """ app.logger.info('Request for customers list...') customers = [] args = customer_args.parse_args() if args['fname']: app.logger.info('Filtering by first name: %s', args['fname']) customers = Customer.find_by_first_name(args['fname']) elif args['lname']: app.logger.info('Filtering by last name: %s', args['lname']) customers = Customer.find_by_last_name(args['lname']) elif args['city']: app.logger.info('Filtering by city: %s', args['city']) customers = Address.find_by_city(args['city']) elif args['state']: app.logger.info('Filtering by state: %s', args['state']) customers = Address.find_by_state(args['state']) elif args['zip_code']: app.logger.info('Filtering by zip code: %s', args['zip_code']) customers = Address.find_by_zip(args['zip_code']) else: app.logger.info('Getting all customers') customers = Customer.all() results = [cust.serialize() for cust in customers] return results, status.HTTP_200_OK
def test_add_a_customer(self): """ Create a address and add it to the database, then create a customer with the address.id and add it to the database """ custs = Customer.all() self.assertEqual(custs, []) cust = Customer(first_name="Marry", last_name="Wang", user_id="marrywang", password="******", active=True) self.assertTrue(cust != None) self.assertEqual(cust.customer_id, None) self.assertEqual(cust.address_id, None) cust.save() addr = Address( street="100 W 100th St.", apartment="100", city="New York", state="New York", zip_code="10035", ) addr.customer_id = cust.customer_id addr.save() cust.address_id = addr.id # Asert that it was assigned an id and shows up in the database self.assertEqual(addr.id, 1) custs = Customer.all() self.assertEqual(len(custs), 1) self.assertEqual(cust.customer_id, 1) custs = Customer.all() self.assertEqual(len(custs), 1)
def test_serialize_a_customer(self): """ Test serialization of a customer """ cust = Customer( first_name="Marry", last_name="Wang", user_id="marrywang", password="******", active=True, ) cust.save() addr = Address( street="48 John St", apartment="1B", city="New York", state="New York", zip_code="22890", customer_id=cust.customer_id, ) addr.save() cust.address_id = addr.id cust.save() data = cust.serialize() self.assertNotEqual(data, None) self.assertIn('first_name', data) self.assertEqual(data['first_name'], "Marry") self.assertIn('last_name', data) self.assertEqual(data['last_name'], "Wang") self.assertIn('user_id', data) self.assertEqual(data['user_id'], "marrywang")
def test_remove_all_addresses(self): """ Remove all addresses """ custs = Customer.all() self.assertEqual(custs, []) cust = Customer(first_name="Marry", last_name="Wang", user_id="marrywang", password="******", active=True) self.assertTrue(cust != None) self.assertEqual(cust.customer_id, None) self.assertEqual(cust.address_id, None) cust.save() addr = Address( street="100 W 100th St.", apartment="100", city="New York", state="New York", zip_code="10035", ) addr.customer_id = cust.customer_id addr.save() all_addresses = Address.all() self.assertEquals(len(all_addresses), 1) addr.remove_all() all_addresses = Address.all() self.assertEquals(len(all_addresses), 0)
def test_deserialize_address_key_error(self): """ Test address deserialization from incomplete data """ data = { "street": "100 W 100 St.", "apartment": "100", "state": "New York", "zip_code": "100" } addr = Address() with self.assertRaises(DataValidationError) as error: addr.deserialize(data) self.assertEqual(str(error.exception), 'Invalid address: '\ 'missing city')
def create_addresses(account_id): """ Create an Address on an Account This endpoint will add an address to an account """ app.logger.info("Request to add an address to an account") check_content_type("application/json") account = Account.find_or_404(account_id) address = Address() address.deserialize(request.get_json()) account.addresses.append(address) account.save() message = address.serialize() return make_response(jsonify(message), status.HTTP_201_CREATED)
def test_query_by_zip(self): """ Query Customers by Zip Code """ customers = self._create_customers(10) test_zip = Address.find(customers[0].address_id)['zip_code'] zip_customers = [ cust for cust in customers if Address.find(cust.address_id)['zip_code'] == test_zip ] resp = self.app.get('/customers', query_string='zip_code={}'.format(test_zip)) self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), len(zip_customers)) # check the data just to be sure for customer in data: self.assertEqual(customer['address']['zip_code'], test_zip)
def test_save_unit_address(self): first_address = Address() first_address.house_number = '3509' first_address.road = 'pelican brief ln' first_address.unit = '2B' first_address.postcode = '89084' first_address.city = "north las vegas" first_address.state = 'nevada' first_address.save() saved_addresses = Address.objects.all() first_saved_address = saved_addresses[0] self.assertEqual( str(first_saved_address), '3509 Pelican Brief Ln, 2B, North Las Vegas, NV 89084')
def test_queries(self): """ Find active/inactive customers with filter """ cust = Customer(first_name="Peter", last_name="Parker", user_id="pparker", password="******", active=True) cust.save() addr = Address(apartment="1R", city="Chicago", state="Illinois", street="6721 5th Ave", zip_code="10030", customer_id=cust.customer_id) addr.save() cust.address_id = addr.id cust.save() """ Find by first name """ cust_fname = Customer.find_by_first_name(cust.first_name) self.assertEqual(cust_fname[0].customer_id, cust.customer_id) """ Find by last name """ cust_lname = Customer.find_by_last_name(cust.last_name) self.assertEqual(cust_lname[0].customer_id, cust.customer_id) """ Find by status """ cust_active = Customer.find_by_status(False) self.assertEqual(len(cust_active), 0) """ Find by city """ cust_city = Address.find_by_city(cust.city) self.assertEqual(cust_city[0].customer_id, cust.customer_id) """ Find by state """ cust_state = Address.find_by_state(cust.state) self.assertEqual(cust_state[0].customer_id, cust.customer_id) """ Find by zip code """ cust_zip = Address.find_zip(cust.zip_code) self.assertEqual(cust_zip[0].customer_id, cust.customer_id)
def test_delete_a_customer(self): """ Delete a customer """ customer = Customer(first_name="Marry", last_name="Wang", user_id="marrywang", password="******", active=True) customer.save() address = Address(street="100 W 100 St.", apartment="100", city="New York", state="New York", zip_code="100") address.customer_id = customer.customer_id address.save() customer.address_id = address.id customer.save() self.assertEqual(len(Customer.all()), 1) self.assertEqual(len(Address.all()), 1) # delete the customer and make sure it isn't in the database customer.delete() self.assertEqual(len(Customer.all()), 0) self.assertEqual(len(Address.all()), 0)
def get_addresses(account_id, address_id): """ Get an Address This endpoint returns just an address """ app.logger.info("Request to get an address with id: %s", address_id) address = Address.find_or_404(address_id) return make_response(jsonify(address.serialize()), status.HTTP_200_OK)
def test_deserialize_an_address(self): """ Test deserialization of a customer """ data = { "street": "100 W 100 St.", "apartment": "100", "city": "New York", "state": "New York", "zip_code": "100" } addr = Address() addr.deserialize(data) self.assertNotEqual(addr, None) self.assertEqual(addr.customer_id, None) self.assertEqual(addr.street, "100 W 100 St.") self.assertEqual(addr.apartment, "100") self.assertEqual(addr.city, "New York") self.assertEqual(addr.state, "New York") self.assertEqual(addr.zip_code, "100")
def _find_locality(identifier): work = _find_coordinates(identifier) if not work: return None row = work[0] coord = Coordinates(float(row[0]), float(row[1])) addr = Address(row[5], row[6], row[7], row[8], row[9], row[10], row[3], row[4], None, row[11], row[2]) locality = Locality(addr, coord) return locality
def test_serialize_an_address(self): """ Test serialization of a customer """ addr = Address(street="100 W 100 St.", apartment="100", city="New York", state="New York", zip_code="100") data = addr.serialize() self.assertNotEqual(data, None) self.assertIn('street', data) self.assertEqual(data['street'], "100 W 100 St.") self.assertIn('apartment', data) self.assertEqual(data['apartment'], "100") self.assertIn('city', data) self.assertEqual(data['city'], "New York") self.assertEqual(data['state'], "New York") self.assertIn('state', data) self.assertIn('zip_code', data) self.assertEqual(data['zip_code'], "100")
def delete_addresses(account_id, address_id): """ Delete an Address This endpoint will delete an Address based the id specified in the path """ app.logger.info("Request to delete account with id: %s", account_id) address = Address.find(address_id) if address: address.delete() return make_response("", status.HTTP_204_NO_CONTENT)
def _create_address(self): """ Creates fake addresses from factory """ fake_address = AddressFactory() address = Address(name=fake_address.name, street=fake_address.street, city=fake_address.city, state=fake_address.state, postalcode=fake_address.postalcode) self.assertTrue(address != None) self.assertEqual(address.id, None) return address
def update_addresses(account_id, address_id): """ Update an Address This endpoint will update an Address based the body that is posted """ app.logger.info("Request to update address with id: %s", address_id) check_content_type("application/json") address = Address.find_or_404(address_id) address.deserialize(request.get_json()) address.id = address_id address.save() return make_response(jsonify(address.serialize()), status.HTTP_200_OK)
def test_creating_containers_with_addresses(self): first_address = Address() first_address.house_number = '3509' first_address.road = 'Pelican Brief Ln' first_address.postcode = '89084' first_address.city = 'North Las Vegas' first_address.state = 'Nevada' first_address.country = 'USA' first_address.save() first_container = Container() first_container.name = 'Tardis' first_container.save() first_container.address.add(first_address) saved_containers = Container.objects.all() self.assertEqual(saved_containers.count(), 1) first_saved_container = saved_containers[0] self.assertEqual(first_saved_container.address.all()[0].city, 'North Las Vegas')
def test_create_a_address(self): """ Create an address and assert that it exists """ addr = Address(street="100 W 100th St.", apartment="100", city="New York", state="New York", zip_code="10035", customer_id=1) self.assertTrue(addr != None) self.assertEqual(addr.id, None) self.assertEqual(addr.street, "100 W 100th St.") self.assertEqual(addr.apartment, "100") self.assertEqual(addr.city, "New York") self.assertEqual(addr.zip_code, "10035")
def test_update_customer(self): """ Update an existing Customer """ # create a customer to update customers = self._create_customers(5) test_customer = customers[0] test_customer.first_name = 'Cow' cust_json = test_customer.internal_serialize() cust_json["address"] = Address.find(test_customer.address_id) resp = self.app.put('/customers/{}'.format(test_customer.user_id), json=cust_json, content_type='application/json') print(resp.get_json()) self.assertEqual(resp.status_code, status.HTTP_200_OK) updated_customer = resp.get_json() self.assertEqual(updated_customer['first_name'], 'Cow')
def _find_coordinates_by_address(dictionary): if "district_number" in dictionary: if dictionary["district_number"] != "": dictionary[ "district_number"] = "Praha " + dictionary["district_number"] first = True sql = "SELECT " + ADDRESSPOINTS_COLUMNS_FIND_COORD + \ " FROM " + ADDRESSPOINTS_TABLE_NAME + \ " WHERE " for key in dictionary: if dictionary[key] != "": if first: sql += ITEM_TO_FIELD[key] + " = '" + dictionary[key] + "'" first = False else: sql += " AND " + ITEM_TO_FIELD[key] + " = '" + dictionary[ key] + "'" sql += "LIMIT " + str(MAX_COUNT) cur = execute_sql(DATABASE_NAME_RUIAN, sql) rows = cur.fetchall() coordinates = [] localities = [] for row in rows: if (row[0] is not None) and (row[1] is not None): (house_number, record_number) = analyse_row(row[7], number_to_string(row[6])) coordinates.append( (str("{:10.2f}".format(row[0])).strip(), str("{:10.2f}".format(row[1])).strip(), row[2], row[3], none_to_string(row[4]), none_to_string(row[5]), house_number, record_number, number_to_string(row[8]), none_to_string(row[9]), number_to_string(row[10]), number_value(none_to_string(row[11])))) # latitude, longitude, gid, nazev_obce, nazev_casti_obce, nazev_ulice, cislo_domovni, typ_so, # cislo_orientacni, znak_cisla_orientacniho, psc, nazev_mop coord = Coordinates(row[0], row[1]) addr = Address(row[5], house_number, record_number, row[8], row[9], row[10], row[3], row[4], None, row[11], row[2]) loc = Locality(addr, coord) localities.append(loc) else: # co se ma stat kdyz adresa nema souradnice? pass cur.close() # return coordinates return localities
def dictionary_to_locality(dictionary): locality = None if dictionary: coordinates = None if dictionary[ADDR_X] and dictionary[ADDR_Y]: coordinates = Coordinates(dictionary[ADDR_Y], dictionary[ADDR_X]) address = Address(dictionary[ADDR_STREET], dictionary[ADDR_HOUSE_NUMBER], dictionary[ADDR_RECORD_NUMBER], dictionary[ADDR_ORIENTATION_NUMBER], dictionary[ADDR_ORIENTATION_NUMBER_CHARACTER], dictionary[ADDR_ZIP_CODE], dictionary[ADDR_LOCALITY], dictionary[ADDR_LOCALITY_PART], dictionary[ADDR_DISTRICT_NUMBER], dictionary[ADDR_DISTRICT], dictionary[ADDR_ID]) locality = Locality(address, coordinates) return locality
def _find_address(identifier): sql = "SELECT " + ADDRESSPOINTS_COLUMNS_FIND + \ " FROM " + ADDRESSPOINTS_TABLE_NAME + \ " WHERE gid = " + str(identifier) cur = execute_sql(DATABASE_NAME_RUIAN, sql) row = cur.fetchone() if row: (house_number, record_number) = analyse_row(row[2], number_to_string(row[1])) a = number_value(none_to_string(row[8])) address = Address(none_to_string(row[0]), house_number, record_number, number_to_string(row[3]), none_to_string(row[4]), number_to_string(row[5]), none_to_string(row[6]), none_to_string(row[7]), a, none_to_string(row[8]), identifier) return address else: return None
def full_text_search_address_object(address): result = full_text_search_address(address) if result: out = [] for item in result: typ_so = item[4] record_number = "" house_number = "" if typ_so == "č.p.": house_number = item[5] else: record_number = item[5] a = Address(item[3], house_number, record_number, item[6], item[7], item[8], item[1], item[2], None, item[9], item[0]) out.append(a) return out else: return []
def post(self): """ Creates a Customer This endpoint will create a Customer based the data in the body that is posted """ app.logger.info('Request to create a customer') check_content_type('application/json') cust = Customer() cust.deserialize(api.payload) cust.save() customer_id = cust.customer_id addr = Address() addr.deserialize(api.payload['address']) addr.customer_id = customer_id addr.save() cust.address_id = addr.id cust.save() message = cust.serialize() location_url = api.url_for(CustomerResource, user_id=cust.user_id, _external=True) return message, status.HTTP_201_CREATED, {'Location': location_url}
def customers_reset(): """ Removes all customers from the database """ Address.remove_all() Customer.remove_all() return make_response('', status.HTTP_204_NO_CONTENT)
def test_modified_save_function_super_save(self): first_address = Address() first_address.house_number = '3509' first_address.road = 'Pelican Brief Ln' first_address.postcode = '89084' first_address.city = "North Las Vegas" first_address.state = 'NV' first_address.save() saved_addresses = Address.objects.all() self.assertEqual(saved_addresses.count(), 1) first_saved_address = saved_addresses[0] first_saved_address.house_number = '3520' first_saved_address.save(update_fields=['house_number']) self.assertEqual(str(first_saved_address), '3520 Pelican Brief Ln, North Las Vegas, NV 89084') second_address = Address() second_address.po_box = '17' second_address.city = 'henderson' second_address.state = 'nv' second_address.postcode = '89119' second_address.county = 'Clark' second_address.save() saved_addresses = Address.objects.all() self.assertEqual(saved_addresses.count(), 2) second_saved_address = saved_addresses[1] self.assertEqual(second_saved_address.county, 'Clark')
def test_modified_save_function_pobox_vs_house(self): first_address = Address() first_address.house_number = '3509' first_address.road = 'pelican brief ln' first_address.postcode = '89084' first_address.city = "north las vegas" first_address.state = 'nevada' first_address.save() second_address = Address() second_address.po_box = '17' second_address.city = 'henderson' second_address.state = 'nv' second_address.postcode = '89119' second_address.save() saved_addresses = Address.objects.all() self.assertEqual(saved_addresses.count(), 2) first_saved_address = saved_addresses[0] second_saved_address = saved_addresses[1] self.assertEqual(str(first_saved_address), '3509 Pelican Brief Ln, North Las Vegas, NV 89084') self.assertEqual(str(second_saved_address), 'PO Box 17, Henderson, NV 89119')
def test_deserialize_address_key_error(self): """ Deserialize an address with a KeyError """ address = Address() self.assertRaises(DataValidationError, address.deserialize, {})
def test_deserialize_address_type_error(self): """ Deserialize an address with a TypeError """ address = Address() self.assertRaises(DataValidationError, address.deserialize, [])