Skip to content

Commit

Permalink
Add test for full db_access coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nnayk committed Dec 7, 2023
1 parent 8d2a31c commit a60ef6e
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions Backend/test_db_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def test_create_user_failure(self, MockUser):
response = create_user(data)
assert response.status_code == 500


# TEST check_user

def test_check_user_existing_username(self):
Expand Down Expand Up @@ -67,17 +66,16 @@ def test_get_user_found(self, MockUser):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.message, "User found.")


# TEST create_image (3)

@patch("db_access.User")
@patch("db_access.Image")
def test_create_image_success(self, MockImage, MockUser):
# Mocking the save method of the Image model
db_connect()
username = {"username" : "DONT_DELETE_CASEY"}
username = {"username": "DONT_DELETE_CASEY"}
user = get_user(username)

image_data = {
"creator": "6570d488e712f054d18ebebc",
"prompt": "test_password",
Expand All @@ -86,7 +84,6 @@ def test_create_image_success(self, MockImage, MockUser):
response = create_image(image_data)
assert response.status_code == 201


@patch("db_access.User")
def test_create_image_failure(self, MockUser):
# Mocking the save method of the User model
Expand All @@ -109,7 +106,7 @@ def test_create_image_creator_not_exist(self, MockUser, MockImage):
mock_user_instance.side_effect = DoesNotExist

image_data = {
"creator": "", # username will never exist
"creator": "", # username will never exist
"prompt": "test_prompt",
"data": "test_data",
}
Expand All @@ -120,8 +117,25 @@ def test_create_image_creator_not_exist(self, MockUser, MockImage):
self.assertEqual(response.status_code, 404)
self.assertEqual(response.message, "Creator user does not exist.")

# TEST get_portfolio (3)
@patch("db_access.Image")
@patch("db_access.User")
def test_create_image_internal_error(self, MockUser, MockImage):
# Mocking the User.objects.get method to raise DoesNotExist
mock_user_instance = MockUser.objects.get
mock_user_instance.side_effect = Exception("test exception")

image_data = {
"creator": "", # username will never exist
"prompt": "test_prompt",
"data": "test_data",
}

response = create_image(image_data)

# Assert that the response status code is 404 and the message matches the expected message
self.assertEqual(response.status_code, 500)

# TEST get_portfolio (3)


if __name__ == "__main__":
Expand Down

0 comments on commit a60ef6e

Please sign in to comment.