Skip to content

Commit

Permalink
Setup db access test infra w/mock support
Browse files Browse the repository at this point in the history
  • Loading branch information
nnayk committed Dec 6, 2023
1 parent 3ac7e02 commit d965385
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
5 changes: 3 additions & 2 deletions Backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from db_access import User
from db_access import Image
from db_access import get_user, create_user
from db_access import check_user, create_user, db_connect


app = Flask(__name__)
Expand Down Expand Up @@ -218,7 +218,7 @@ def register():
"email": email,
"password": hashed_password,
}
response = get_user(user_data)
response = check_user(user_data)
if response.status_code == 401:
print(f"responsey={response.message}")
return (
Expand All @@ -237,6 +237,7 @@ def register():


if __name__ == "__main__":
db_connect()
try:
app.run(port=5000, debug=True)
except OSError as e:
Expand Down
15 changes: 7 additions & 8 deletions Backend/db_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

app = Flask(__name__)

# MongoDB connection
mongo_uri = os.environ.get("MONGO_URI")
connect(host=mongo_uri)

def db_connect(DB_URL=None):
# MongoDB connection
mongo_uri = DB_URL or os.environ.get("MONGO_URI")
connect(host=mongo_uri)
return True


class Response:
Expand Down Expand Up @@ -59,23 +62,19 @@ def create_user(data):
)
try:
user.save()
print(f"successfully added user {user.username}")
return Response("User created successfully!", 201)
except Exception as e:
return Response(f"Internal server error {e}", 500)


def get_user(data):
# data = json.loads(data.decode("utf-8"))
def check_user(data):
user = User.objects(username=data["username"]).first()
print(f"user={user}")
if user:
return Response(
"Username already exists. Choose another.",
401,
)
user = User.objects(email=data["email"]).first()
print(f"user={user}")
if user:
return Response(
"Email already exists. Choose another.",
Expand Down
3 changes: 2 additions & 1 deletion Backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ mongoengine
werkzeug
requests
flask_jwt_extended
python-dotenv
python-dotenv
mongomock
53 changes: 39 additions & 14 deletions Backend/test_db_access.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
import unittest

from unittest.mock import MagicMock, patch
import coverage
from db_access import *


class TestDBAccess(unittest.TestCase):
def test_create_user(self):
def test_db_connect(self):
assert db_connect() == 1

@patch("db_access.User")
def test_create_user_success(self, MockUser):
# Mocking the save method of the User model
data = {
"username": "test_user",
"email": "test@gmail.com",
"password": "test_password",
"email": "test@example.com",
}
response = create_user(data)
print(f"response={response.message},response={response.status_code}")
assert (
response.status_code == 201
and response.message == "User created successfully!"
)
assert response.status_code == 201

@patch("db_access.User")
def test_create_user_failure(self, MockUser):
# Mocking the save method of the User model
mock_user_instance = MockUser.return_value
mock_user_instance.save.side_effect = Exception("test exception")

def test_get_user(self):
data = {
"username": "test_user",
"password": "test_password",
"email": "test@example.com",
}
response = create_user(data)
assert response.status_code == 500

def test_check_user_existing_username(self):
db_connect()
data = {"username": "existing_user"}
response = get_user(data)
assert (
response.status_code == 401
and response.message == "Username already exists. Choose another."
)
response = check_user(data)
assert response.status_code == 401 and "Username" in response.message

def test_check_user_existing_email(self):
db_connect()
data = {"username": "", "email": "existing_user@gmail.com"}
response = check_user(data)
assert response.status_code == 401 and "Email" in response.message

def test_check_user_unique(self):
data = {"username": "", "email": ""}
response = check_user(data)
assert response.status_code == 200


if __name__ == "__main__":
Expand Down

0 comments on commit d965385

Please sign in to comment.