Skip to content

Commit 757053e

Browse files
committed
Finished working on the leap challenge, started working on the rest_api challenge
1 parent 5795f69 commit 757053e

File tree

7 files changed

+265
-0
lines changed

7 files changed

+265
-0
lines changed

leap/__pycache__/leap.cpython-37.pyc

341 Bytes
Binary file not shown.
Binary file not shown.

leap/leap.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
def leap_year(year):
2+
3+
if year%4 != 0:
4+
result = False
5+
elif year%100 != 0:
6+
result = True
7+
elif year%400 != 0:
8+
result = False
9+
else:
10+
result = True
11+
return result
212
pass

rest-api/.exercism/metadata.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"python","exercise":"rest-api","id":"a232ed577ade49f0aea7645540dd275d","url":"https://exercism.io/my/solutions/a232ed577ade49f0aea7645540dd275d","handle":"sinanata","is_requester":true,"auto_approve":false}

rest-api/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Rest Api
2+
3+
Implement a RESTful API for tracking IOUs.
4+
5+
Four roommates have a habit of borrowing money from each other frequently, and have trouble remembering who owes whom, and how much.
6+
7+
Your task is to implement a simple [RESTful API](https://en.wikipedia.org/wiki/Representational_state_transfer) that receives [IOU](https://en.wikipedia.org/wiki/IOU)s as POST requests, and can deliver specified summary information via GET requests.
8+
9+
### API Specification
10+
11+
#### User object
12+
```json
13+
{
14+
"name": "Adam",
15+
"owes": {
16+
"Bob": 12.0,
17+
"Chuck": 4.0,
18+
"Dan": 9.5
19+
},
20+
"owed_by": {
21+
"Bob": 6.5,
22+
"Dan": 2.75,
23+
},
24+
"balance": "<(total owed by other users) - (total owed to other users)>"
25+
}
26+
```
27+
28+
#### Methods
29+
30+
| Description | HTTP Method | URL | Payload Format | Response w/o Payload | Response w/ Payload |
31+
| --- | --- | --- | --- | --- | --- |
32+
| List of user information | GET | /users | `{"users":["Adam","Bob"]}` | `{"users":<List of all User objects>}` | `{"users":<List of User objects for <users> (sorted by name)}` |
33+
| Create user | POST | /add | `{"user":<name of new user (unique)>}` | N/A | `<User object for new user>` |
34+
| Create IOU | POST | /iou | `{"lender":<name of lender>,"borrower":<name of borrower>,"amount":5.25}` | N/A | `{"users":<updated User objects for <lender> and <borrower> (sorted by name)>}` |
35+
36+
### Other Resources:
37+
- https://restfulapi.net/
38+
- Example RESTful APIs
39+
- [GitHub](https://developer.github.com/v3/)
40+
- [Reddit](https://www.reddit.com/dev/api/)
41+
42+
## Exception messages
43+
44+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
45+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
46+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
47+
a message.
48+
49+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
50+
`raise Exception`, you should write:
51+
52+
```python
53+
raise Exception("Meaningful message indicating the source of the error")
54+
```
55+
56+
## Running the tests
57+
58+
To run the tests, run `pytest rest_api_test.py`
59+
60+
Alternatively, you can tell Python to run the pytest module:
61+
`python -m pytest rest_api_test.py`
62+
63+
### Common `pytest` options
64+
65+
- `-v` : enable verbose output
66+
- `-x` : stop running tests on first failure
67+
- `--ff` : run failures from previous test before running other test cases
68+
69+
For other options, see `python -m pytest -h`
70+
71+
## Submitting Exercises
72+
73+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/rest-api` directory.
74+
75+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
76+
77+
For more detailed information about running tests, code style and linting,
78+
please see [Running the Tests](http://exercism.io/tracks/python/tests).
79+
80+
## Submitting Incomplete Solutions
81+
82+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

rest-api/rest_api.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class RestAPI:
2+
def __init__(self, database=None):
3+
pass
4+
5+
def get(self, url, payload=None):
6+
pass
7+
8+
def post(self, url, payload=None):
9+
pass

rest-api/rest_api_test.py

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import unittest
2+
3+
from rest_api import RestAPI
4+
5+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.1
6+
import json
7+
8+
9+
class RestApiTest(unittest.TestCase):
10+
def test_no_users(self):
11+
database = {"users": []}
12+
api = RestAPI(database)
13+
14+
response = api.get("/users")
15+
expected = {"users": []}
16+
self.assertDictEqual(json.loads(response), expected)
17+
18+
def test_add_user(self):
19+
database = {"users": []}
20+
api = RestAPI(database)
21+
payload = json.dumps({"user": "Adam"})
22+
response = api.post("/add", payload)
23+
expected = {"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0}
24+
self.assertDictEqual(json.loads(response), expected)
25+
26+
def test_get_single_user(self):
27+
database = {
28+
"users": [
29+
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
30+
{"name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0},
31+
]
32+
}
33+
api = RestAPI(database)
34+
payload = json.dumps({"users": ["Bob"]})
35+
response = api.get("/users", payload)
36+
expected = {
37+
"users": [{"name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0}]
38+
}
39+
self.assertDictEqual(json.loads(response), expected)
40+
41+
def test_both_users_have_0_balance(self):
42+
database = {
43+
"users": [
44+
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
45+
{"name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0},
46+
]
47+
}
48+
api = RestAPI(database)
49+
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 3.0})
50+
response = api.post("/iou", payload)
51+
expected = {
52+
"users": [
53+
{"name": "Adam", "owes": {}, "owed_by": {"Bob": 3.0}, "balance": 3.0},
54+
{"name": "Bob", "owes": {"Adam": 3.0}, "owed_by": {}, "balance": -3.0},
55+
]
56+
}
57+
self.assertDictEqual(json.loads(response), expected)
58+
59+
def test_borrower_has_negative_balance(self):
60+
database = {
61+
"users": [
62+
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
63+
{"name": "Bob", "owes": {"Chuck": 3.0}, "owed_by": {}, "balance": -3.0},
64+
{"name": "Chuck", "owes": {}, "owed_by": {"Bob": 3.0}, "balance": 3.0},
65+
]
66+
}
67+
api = RestAPI(database)
68+
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 3.0})
69+
response = api.post("/iou", payload)
70+
expected = {
71+
"users": [
72+
{"name": "Adam", "owes": {}, "owed_by": {"Bob": 3.0}, "balance": 3.0},
73+
{
74+
"name": "Bob",
75+
"owes": {"Adam": 3.0, "Chuck": 3.0},
76+
"owed_by": {},
77+
"balance": -6.0,
78+
},
79+
]
80+
}
81+
self.assertDictEqual(json.loads(response), expected)
82+
83+
def test_lender_has_negative_balance(self):
84+
database = {
85+
"users": [
86+
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
87+
{"name": "Bob", "owes": {"Chuck": 3.0}, "owed_by": {}, "balance": -3.0},
88+
{"name": "Chuck", "owes": {}, "owed_by": {"Bob": 3.0}, "balance": 3.0},
89+
]
90+
}
91+
api = RestAPI(database)
92+
payload = json.dumps({"lender": "Bob", "borrower": "Adam", "amount": 3.0})
93+
response = api.post("/iou", payload)
94+
expected = {
95+
"users": [
96+
{"name": "Adam", "owes": {"Bob": 3.0}, "owed_by": {}, "balance": -3.0},
97+
{
98+
"name": "Bob",
99+
"owes": {"Chuck": 3.0},
100+
"owed_by": {"Adam": 3.0},
101+
"balance": 0.0,
102+
},
103+
]
104+
}
105+
self.assertDictEqual(json.loads(response), expected)
106+
107+
def test_lender_owes_borrower(self):
108+
database = {
109+
"users": [
110+
{"name": "Adam", "owes": {"Bob": 3.0}, "owed_by": {}, "balance": -3.0},
111+
{"name": "Bob", "owes": {}, "owed_by": {"Adam": 3.0}, "balance": 3.0},
112+
]
113+
}
114+
api = RestAPI(database)
115+
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 2.0})
116+
response = api.post("/iou", payload)
117+
expected = {
118+
"users": [
119+
{"name": "Adam", "owes": {"Bob": 1.0}, "owed_by": {}, "balance": -1.0},
120+
{"name": "Bob", "owes": {}, "owed_by": {"Adam": 1.0}, "balance": 1.0},
121+
]
122+
}
123+
self.assertDictEqual(json.loads(response), expected)
124+
125+
def test_lender_owes_borrower_less_than_new_loan(self):
126+
database = {
127+
"users": [
128+
{"name": "Adam", "owes": {"Bob": 3.0}, "owed_by": {}, "balance": -3.0},
129+
{"name": "Bob", "owes": {}, "owed_by": {"Adam": 3.0}, "balance": 3.0},
130+
]
131+
}
132+
api = RestAPI(database)
133+
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 4.0})
134+
response = api.post("/iou", payload)
135+
expected = {
136+
"users": [
137+
{"name": "Adam", "owes": {}, "owed_by": {"Bob": 1.0}, "balance": 1.0},
138+
{"name": "Bob", "owes": {"Adam": 1.0}, "owed_by": {}, "balance": -1.0},
139+
]
140+
}
141+
self.assertDictEqual(json.loads(response), expected)
142+
143+
def test_lender_owes_borrower_same_as_new_loan(self):
144+
database = {
145+
"users": [
146+
{"name": "Adam", "owes": {"Bob": 3.0}, "owed_by": {}, "balance": -3.0},
147+
{"name": "Bob", "owes": {}, "owed_by": {"Adam": 3.0}, "balance": 3.0},
148+
]
149+
}
150+
api = RestAPI(database)
151+
payload = json.dumps({"lender": "Adam", "borrower": "Bob", "amount": 3.0})
152+
response = api.post("/iou", payload)
153+
expected = {
154+
"users": [
155+
{"name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0},
156+
{"name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0},
157+
]
158+
}
159+
self.assertDictEqual(json.loads(response), expected)
160+
161+
162+
if __name__ == "__main__":
163+
unittest.main()

0 commit comments

Comments
 (0)