Skip to content

Commit

Permalink
Filter project by completion status (#139)
Browse files Browse the repository at this point in the history
* Filter project by completion status

* filtering projects by completion status
  • Loading branch information
peterghrong authored Mar 14, 2021
1 parent 2ebc8bf commit 5db2a58
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
15 changes: 15 additions & 0 deletions backend/app/project/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ def get_projects():

projects = list(filter(lambda project: (project.type == type), projects))

is_completed = request.args.get("is-completed")
if is_completed is not None:
if is_completed == "":
abort(404, "completion status invalid")

# query param is of string type, therefore must convert to boolean first
if is_completed.lower() == "true":
is_completed = True
else:
is_completed = False

projects = list(
filter(lambda project: (project.is_completed == is_completed), projects)
)

# add additional request arguments below

return jsonify(Project.serialize_list(projects))
Expand Down
99 changes: 99 additions & 0 deletions backend/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,106 @@ def test_get_specific_type(self):
json_response = json.loads(response3.get_data(as_text=True))
self.assertEqual(len(json_response), 2)

# test get endpoint by completion status
def test_get_projects_by_completion_status(self):
p1 = Project(
address="dummy address",
city="dummy city",
province="dummy province",
postal_code="dummy postal_code",
neighbourhood="dummy neighbourhood",
year=2020,
name="dummy name",
type="dummy type",
contacts=[],
is_completed=True,
)
p2 = Project(
address="dummy address",
city="dummy city",
province="dummy province",
postal_code="dummy postal_code",
neighbourhood="dummy neighbourhood",
year=2020,
name="dummy name",
type="dummy type2",
contacts=[],
is_completed=True,
)
p3 = Project(
address="dummy address",
city="dummy city",
province="dummy province",
postal_code="dummy postal_code",
neighbourhood="dummy neighbourhood",
year=2020,
name="dummy name",
type="dummy type2",
contacts=[],
is_completed=False,
)
db.session.add_all([p1, p2, p3])
db.session.commit()

tester1 = {
"address": "dummy address",
"city": "dummy city",
"province": "dummy province",
"postal_code": "dummy postal_code",
"neighbourhood": "dummy neighbourhood",
"year": 2020,
"name": "dummy name",
"type": "dummy type",
"contacts": [],
"is_completed": True,
}
tester2 = {
"address": "dummy address",
"city": "dummy city",
"province": "dummy province",
"postal_code": "dummy postal_code",
"neighbourhood": "dummy neighbourhood",
"year": 2020,
"name": "dummy name",
"type": "dummy type2",
"contacts": [],
"is_completed": False,
}
tester3 = {
"address": "dummy address",
"city": "dummy city",
"province": "dummy province",
"postal_code": "dummy postal_code",
"neighbourhood": "dummy neighbourhood",
"year": 2020,
"name": "dummy name",
"type": "dummy type2",
"contacts": [],
"is_completed": True,
}
# testing missing param
response = self.client.get("/project?is-completed=")
self.assertEqual(response.status_code, 404)

# testing only get by completion status
response2 = self.client.get("/project?is-completed={}".format(True))
json_response2 = json.loads(response2.get_data(as_text=True))
self.assertEqual(len(json_response2), 2)
self.assertDictContainsSubset(tester1, json_response2[0])

response3 = self.client.get("/project?is-completed={}".format(False))
json_response3 = json.loads(response3.get_data(as_text=True))
self.assertEqual(len(json_response3), 1)
self.assertDictContainsSubset(tester2, json_response3[0])

# testing get by completion status with type filtering
response4 = self.client.get("/project?is-completed=True&type=dummy type2")
json_response4 = json.loads(response4.get_data(as_text=True))
self.assertEqual(len(json_response4), 1)
self.assertDictContainsSubset(tester3, json_response4[0])

# test get_all_projects endpoint

def test_get_all_projects(self):
# pre-populate database
p_id = uuid.uuid4()
Expand Down

0 comments on commit 5db2a58

Please sign in to comment.