Skip to content

Commit 4174daf

Browse files
Merge pull request #9 from daniel-learns-with-coursera/dev-update-account
add all microservice endpoints with tests
2 parents 912755c + 36dbb3e commit 4174daf

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

service/routes.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,73 @@ def create_accounts():
6060
######################################################################
6161
# LIST ALL ACCOUNTS
6262
######################################################################
63+
@app.route("/accounts", methods=["GET"])
64+
def list_accounts():
65+
"""
66+
List all Accounts
67+
This endpoint will list all Accounts
68+
"""
69+
app.logger.info("Request to list Accounts")
6370

64-
# ... place you code here to LIST accounts ...
71+
accounts = Account.all()
72+
account_list = [account.serialize() for account in accounts]
6573

74+
app.logger.info("Returning [%s] accounts", len(account_list))
75+
return jsonify(account_list), status.HTTP_200_OK
6676

6777
######################################################################
6878
# READ AN ACCOUNT
6979
######################################################################
7080

71-
# ... place you code here to READ an account ...
81+
@app.route("/accounts/<int:account_id>", methods=["GET"])
82+
def get_accounts(account_id):
83+
"""
84+
Reads an Account
85+
This endpoint will read an Account based the account_id that is requested
86+
"""
87+
app.logger.info("Request to read an Account with id: %s", account_id)
88+
account = Account.find(account_id)
89+
if not account:
90+
abort(status.HTTP_404_NOT_FOUND, f"Account with id [{account_id}] could not be found.")
91+
return account.serialize(), status.HTTP_200_OK
7292

7393

7494
######################################################################
7595
# UPDATE AN EXISTING ACCOUNT
7696
######################################################################
97+
@app.route("/accounts/<int:account_id>", methods=["PUT"])
98+
def update_accounts(account_id):
99+
"""
100+
Update an Account
101+
This endpoint will update an Account based on the posted data
102+
"""
103+
app.logger.info("Request to update an Account with id: %s", account_id)
104+
105+
account = Account.find(account_id)
106+
if not account:
107+
abort(status.HTTP_404_NOT_FOUND, f"Account with id [{account_id}] could not be found.")
77108

78-
# ... place you code here to UPDATE an account ...
109+
account.deserialize(request.get_json())
110+
account.update()
79111

112+
return account.serialize(), status.HTTP_200_OK
80113

81114
######################################################################
82115
# DELETE AN ACCOUNT
83116
######################################################################
117+
@app.route("/accounts/<int:account_id>", methods=["DELETE"])
118+
def delete_accounts(account_id):
119+
"""
120+
Delete an Account
121+
This endpoint will delete an Account based on the account_id that is requested
122+
"""
123+
app.logger.info("Request to delete an Account with id: %s", account_id)
84124

85-
# ... place you code here to DELETE an account ...
125+
account = Account.find(account_id)
126+
if account:
127+
account.delete()
86128

129+
return "", status.HTTP_204_NO_CONTENT
87130

88131
######################################################################
89132
# U T I L I T Y F U N C T I O N S

tests/test_routes.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,52 @@ def test_unsupported_media_type(self):
124124
self.assertEqual(response.status_code, status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
125125

126126
# ADD YOUR TEST CASES HERE ...
127+
128+
def test_get_account(self):
129+
"""It should Read a single Account"""
130+
account = self._create_accounts(1)[0]
131+
resp = self.client.get(
132+
f"{BASE_URL}/{account.id}", content_type="application/json"
133+
)
134+
self.assertEqual(resp.status_code, status.HTTP_200_OK)
135+
data = resp.get_json()
136+
self.assertEqual(data["name"], account.name)
137+
138+
def test_get_account_not_found(self):
139+
"""It should not Read an Account that is not found"""
140+
resp = self.client.get(f"{BASE_URL}/0")
141+
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)
142+
143+
def test_get_account_list(self):
144+
"""It should Get a list of Accounts"""
145+
self._create_accounts(5)
146+
resp = self.client.get(BASE_URL)
147+
self.assertEqual(resp.status_code, status.HTTP_200_OK)
148+
data = resp.get_json()
149+
self.assertEqual(len(data), 5)
150+
151+
def test_update_account(self):
152+
"""It should Update an existing Account"""
153+
# create an Account to update
154+
test_account = AccountFactory()
155+
resp = self.client.post(BASE_URL, json=test_account.serialize())
156+
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
157+
158+
# update the account
159+
new_account = resp.get_json()
160+
new_account["name"] = "Something Known"
161+
resp = self.client.put(f"{BASE_URL}/{new_account['id']}", json=new_account)
162+
self.assertEqual(resp.status_code, status.HTTP_200_OK)
163+
updated_account = resp.get_json()
164+
self.assertEqual(updated_account["name"], "Something Known")
165+
166+
def test_delete_account(self):
167+
"""It should Delete an Account"""
168+
account = self._create_accounts(1)[0]
169+
resp = self.client.delete(f"{BASE_URL}/{account.id}")
170+
self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
171+
172+
def test_method_not_allowed(self):
173+
"""It should not allow an illegal method call"""
174+
resp = self.client.delete(BASE_URL)
175+
self.assertEqual(resp.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

0 commit comments

Comments
 (0)