def test_update_account_address(self): """ Update an accounts address """ accounts = Account.all() self.assertEqual(accounts, []) address = self._create_address() account = self._create_account(addresses=[address]) account.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(account.id, 1) accounts = Account.all() self.assertEqual(len(accounts), 1) # Fetch it back account = Account.find(account.id) old_address = account.addresses[0] self.assertEqual(old_address.city, address.city) old_address.city = "XX" account.save() # Fetch it back again account = Account.find(account.id) address = account.addresses[0] self.assertEqual(address.city, "XX")
def setUpClass(cls): """ This runs once before the entire test suite """ app.config['TESTING'] = True app.config['DEBUG'] = False app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI app.logger.setLevel(logging.CRITICAL) Account.init_db(app)
def test_add_a_account(self): """ Create an account and add it to the database """ accounts = Account.all() self.assertEqual(accounts, []) account = self._create_account() account.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(account.id, 1) accounts = Account.all() self.assertEqual(len(accounts), 1)
def test_deserialize_an_account(self): """ Deserialize an account """ address = self._create_address() account = self._create_account(addresses=[address]) serial_account = account.serialize() new_account = Account() new_account.deserialize(serial_account) self.assertEqual(new_account.id, account.id) self.assertEqual(new_account.name, account.name) self.assertEqual(new_account.email, account.email) self.assertEqual(new_account.phone_number, account.phone_number) self.assertEqual(new_account.date_joined, account.date_joined)
def list_accounts(): """ Returns all of the Accounts """ app.logger.info("Request for Account list") accounts = [] name = request.args.get("name") if name: accounts = Account.find_by_name(name) else: accounts = Account.all() results = [account.serialize() for account in accounts] return make_response(jsonify(results), status.HTTP_200_OK)
def test_delete_an_account(self): """ Delete an account from the database """ accounts = Account.all() self.assertEqual(accounts, []) account = self._create_account() account.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(account.id, 1) accounts = Account.all() self.assertEqual(len(accounts), 1) account = accounts[0] account.delete() accounts = Account.all() self.assertEqual(len(accounts), 0)
def test_update_account(self): """ Update an account """ account = self._create_account() account.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(account.id, 1) # Fetch it back account = Account.find(account.id) account.email = "*****@*****.**" account.save() # Fetch it back again account = Account.find(account.id) self.assertEqual(account.email, "*****@*****.**")
def get_accounts(account_id): """ Retrieve a single Account This endpoint will return an Account based on it's id """ app.logger.info("Request for Account with id: %s", account_id) account = Account.find_or_404(account_id) return make_response(jsonify(account.serialize()), status.HTTP_200_OK)
def test_find_by_name(self): """ Find by name """ account = self._create_account() account.create() # Fetch it back by name same_account = Account.find_by_name(account.name)[0] self.assertEqual(same_account.id, account.id) self.assertEqual(same_account.name, account.name)
def test_find_or_404(self): """ Find or throw 404 error """ account = self._create_account() account.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(account.id, 1) # Fetch it back account = Account.find_or_404(account.id) self.assertEqual(account.id, 1)
def _create_account(self, addresses=[]): """ Creates an account from a Factory """ fake_account = AccountFactory() account = Account(name=fake_account.name, email=fake_account.email, phone_number=fake_account.phone_number, date_joined=fake_account.date_joined, addresses=addresses) self.assertTrue(account != None) self.assertEqual(account.id, None) return account
def delete_accounts(account_id): """ Delete an Account This endpoint will delete an Account based the id specified in the path """ app.logger.info("Request to delete account with id: %s", account_id) account = Account.find(account_id) if account: account.delete() return make_response("", status.HTTP_204_NO_CONTENT)
def test_create_an_account(self): """ Create a Account and assert that it exists """ fake_account = AccountFactory() account = Account(name=fake_account.name, email=fake_account.email, phone_number=fake_account.phone_number, date_joined=fake_account.date_joined) self.assertTrue(account != None) self.assertEqual(account.id, None) self.assertEqual(account.name, fake_account.name) self.assertEqual(account.email, fake_account.email) self.assertEqual(account.phone_number, fake_account.phone_number) self.assertEqual(account.date_joined, fake_account.date_joined)
def test_add_account_address(self): """ Create an account with an address and add it to the database """ accounts = Account.all() self.assertEqual(accounts, []) account = self._create_account() address = self._create_address() account.addresses.append(address) account.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(account.id, 1) accounts = Account.all() self.assertEqual(len(accounts), 1) new_account = Account.find(account.id) self.assertEqual(account.addresses[0].name, address.name) address2 = self._create_address() account.addresses.append(address2) account.save() new_account = Account.find(account.id) self.assertEqual(len(account.addresses), 2) self.assertEqual(account.addresses[1].name, address2.name)
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 update_accounts(account_id): """ Update an Account This endpoint will update an Account based the body that is posted """ app.logger.info("Request to update account with id: %s", account_id) check_content_type("application/json") account = Account.find(account_id) if not account: raise NotFound( "Account with id '{}' was not found.".format(account_id)) account.deserialize(request.get_json()) account.id = account_id account.save() return make_response(jsonify(account.serialize()), status.HTTP_200_OK)
def create_accounts(): """ Creates an Account This endpoint will create an Account based the data in the body that is posted """ app.logger.info("Request to create an Account") check_content_type("application/json") account = Account() account.deserialize(request.get_json()) account.create() message = account.serialize() location_url = url_for("get_accounts", account_id=account.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {"Location": location_url})
def init_db(): """ Initialies the SQLAlchemy app """ global app Account.init_db(app)
def list_addresses(account_id): """ Returns all of the Addresses for an Account """ app.logger.info("Request for Account Addresses...") account = Account.find_or_404(account_id) results = [address.serialize() for address in account.addresses] return make_response(jsonify(results), status.HTTP_200_OK)
def test_deserialize_with_key_error(self): """ Deserialize an account with a KeyError """ account = Account() self.assertRaises(DataValidationError, account.deserialize, {})
def test_deserialize_with_type_error(self): """ Deserialize an account with a TypeError """ account = Account() self.assertRaises(DataValidationError, account.deserialize, [])