Skip to content

Commit 03db9c3

Browse files
authored
Merge pull request #61 from ProgrammingBuddies/setup_tests
Setup tests
2 parents 8477a42 + 9802905 commit 03db9c3

File tree

14 files changed

+563
-4
lines changed

14 files changed

+563
-4
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ flask-dance = "*"
1515
pyopenssl = "*"
1616
flask-swagger = "*"
1717
flask-swagger-ui = "*"
18+
pytest = "*"
1819

1920
[requires]
2021
python_version = "3.7"

Pipfile.lock

Lines changed: 75 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ Install docker-compose and do `docker-compose up`. Don't forget to setup your
4040

4141
Your `.env` file should now look something like [example.env](https://github.com/ProgrammingBuddies/programmingbuddies-api/blob/develop/example.env)
4242

43+
### Testing
44+
45+
- to run multiple tests just specify the directory which contains them for example `pipenv run pytest tests/`
46+
- - this will run all the tests in the `tests` directory
47+
- if you want to run test cases only in a particular file, then just give the full file path `pipenv run pytest tests/example.py`
48+
4349
## Milestones
4450
- [ ] build DB and endpoints with basic CRUD
4551
- [ ] add security for app (ie - bots, and non-human actors/clients)

src/api/controllers/projectController.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def update_project(self, id, **kwargs):
1919

2020
if project == None:
2121
return None
22-
22+
2323
for key, value in kwargs.items():
2424
if not hasattr(project, key):
2525
return None
@@ -69,6 +69,7 @@ def create_link(self, project_id, **kwargs):
6969

7070
return link
7171
except:
72+
self.session.rollback()
7273
return None
7374

7475
def update_link(self, project_id, link_id, **kwargs):
@@ -113,6 +114,7 @@ def create_feedback(self, project_id, **kwargs):
113114

114115
return feedback
115116
except:
117+
self.session.rollback()
116118
return None
117119

118120
def get_all_feedbacks(self, project_id):

src/api/controllers/userController.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def create_user(self, **kwargs):
1212

1313
return user
1414
except:
15+
self.session.rollback()
1516
return None
1617

1718
def update_user(self, id, **kwargs):
@@ -69,6 +70,7 @@ def create_link(self, user_id, **kwargs):
6970

7071
return link
7172
except:
73+
self.session.rollback()
7274
return None
7375

7476
def update_link(self, user_id, link_id, **kwargs):
@@ -113,6 +115,7 @@ def create_feedback(self, user_id, **kwargs):
113115

114116
return feedback
115117
except:
118+
self.session.rollback()
116119
return None
117120

118121
def get_all_feedbacks(self, user_id):

tests/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
import sys
3+
4+
# todo: change this importing solution
5+
sys.path.insert(0, os.getcwd()+'/src')
6+
7+
from api import app
8+
from api.models import db
9+
from api.models import User, Project, UserFeedback, ProjectFeedback, UserLink, ProjectLink
10+
11+
sys.path.insert(0, os.getcwd()+'/tests')

tests/api/__init__.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
"""
3+
4+
Creates valid model objects for testing purposes:
5+
6+
- insert data to the database which could be used for testing
7+
8+
- these functions will give back the created objects in dict format which is hardcoded at every model's as_dict instance method
9+
10+
- call these functions form any test case.
11+
12+
"""
13+
14+
from tests import db, Project, User, UserLink, ProjectLink, UserFeedback
15+
16+
def create_project_for_test_cases(data):
17+
new_project = Project(**data)
18+
db.session.add(new_project)
19+
db.session.commit()
20+
21+
return new_project.as_dict()
22+
23+
def create_project_link_for_test_cases(data):
24+
new_project_link = ProjectLink(**data)
25+
db.session.add(new_project_link)
26+
db.session.commit()
27+
28+
return new_project_link.as_dict()
29+
30+
def create_user_for_test_cases(data):
31+
new_user = User(**data)
32+
db.session.add(new_user)
33+
db.session.commit()
34+
35+
return new_user.as_dict()
36+
37+
def create_user_link_for_test_cases(data):
38+
new_user_link = UserLink(**data)
39+
db.session.add(new_user_link)
40+
db.session.commit()
41+
42+
return new_user_link.as_dict()
43+
44+
def create_user_feedback_for_test_cases(user1, user2):
45+
feedback_data = {
46+
"author_id": user1["id"],
47+
"user_id": user2["id"],
48+
"description": "This is the description",
49+
"rating": 1
50+
}
51+
52+
new_user_feedback = UserFeedback(**feedback_data)
53+
db.session.add(new_user_feedback)
54+
db.session.commit()
55+
56+
return new_user_feedback.as_dict()

tests/api/models/test_UserModel.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from tests.conftest import client
2+
from tests import db, User
3+
4+
class TestUserModel(object):
5+
pass

tests/api/views/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)