Skip to content

Commit

Permalink
Final changes - Issue dci-python-backend-assignments#18 -Elina +Wasi
Browse files Browse the repository at this point in the history
  • Loading branch information
“elinamat” committed Jul 7, 2022
1 parent 37639a0 commit 6e8b5f5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 16 deletions.
5 changes: 5 additions & 0 deletions class_social/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def load_users():
return []


def save_posts(posts):
with open('posts.pickle', 'wb') as file:
pickle.dump(posts, file)


def load_posts():
try:
with open('posts.pickle', 'rb') as file:
Expand Down
9 changes: 5 additions & 4 deletions class_social/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class User(BaseModel):
address: str
website: Optional[str]
connections: Optional[List['User']]
# connections: Optional[List['User']]
about: Optional[str]
role: Optional[str]

Expand Down Expand Up @@ -71,8 +72,8 @@ class Post(BaseModel):
creator: User
creation_date: datetime
comments_on_this_post: Optional[List[str]]
likes_for_this_post: Optional[List[Union[Student, Teacher, Institution]]]
shares_for_this_post: Optional[List[Union[Student, Teacher, Institution]]]
likes_for_this_post: Optional[List[str]]
#likes_for_this_post: Optional[List[Union['Student', 'Teacher', 'Institution']]]
#shares_for_this_post: Optional[List[Union['Student', 'Teacher', 'Institution']]]
shares_for_this_post: Optional[List[str]]
type_of_post: str


42 changes: 32 additions & 10 deletions class_social/posts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from fastapi import APIRouter, HTTPException
from fastapi import HTTPException, APIRouter

from class_social import db
from class_social.models import Post
from class_social.db import DBException


Expand All @@ -8,33 +10,53 @@ class PostControllerError(Exception):


class PostController:
def get_posts_by_creator_role(self, role):
result_post = []

posts_list = db.load_posts()
def post_a_post(self, post):
try:
post_list = db.load_posts()
post_list.append(post)
db.save_posts(post_list)
except DBException:
raise PostControllerError('Wrong format')

def get_posts_by_username(self, username):
posts_list = db.load_posts()
user_posts = []
for post in posts_list:
if post.creator.username == username:
user_posts.append(post)
return user_posts

def get_posts_by_creator_role(self, role):
result_post = []
posts_list = db.load_posts()
for post in posts_list:
if post.creator.role == role:

result_post.append(post)

return result_post



# API Routes

posts_routes = APIRouter()
post_controller = PostController()

@post_routes.post('/posts')
def insert_posts(post: Post) -> Post:
post = post_controller.post_a_post(post)
return post

@post_routes.get('/posts/{username}')
def get_posts_by_username(username: str):
post = post_controller.get_posts_by_username(username)
if post is not None:
return post

raise HTTPException(status_code=404)

@posts_routes.get('/posts/creator/{role}')
def get_posts_by_creator_role(role: str):
post = post_controller.get_posts_by_creator_role(role)

if post is not None:
return post

raise HTTPException(status_code=404)

40 changes: 38 additions & 2 deletions tests/test_post_routes.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
import datetime
from unittest.mock import patch, Mock

import pytest
from fastapi.testclient import TestClient

from class_social.main import app


@pytest.fixture
def http_test_client():
return TestClient(app)

post_user = {
"id": '1234',
"name": 'Wasi',
"username": 'wasi',
"password": 'somepass',
"email": 'wasi@mathias',
"created_on": "2023-03-27T00:00:00.000+00:00",
"is_active": True,
"address": 'some address 123'
}
# {"w1","wasi", "123456w","[email protected]","2023-03-27T00:00:00.000+00:00",True,"Wasi","someaddress 123","wasi.co","con1","about me about me"}, # TODO a user dictionary here OR remove it completely!!


valid_post = {
"title": 'Our First Post',
"body": 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam' \
' nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, s' \
'ed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd g' \
'ubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, ' \
'sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo' \
' duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
"creator": post_user, # TODO a user dictionary here OR remove it completely!!
"creation_date": '2023-03-27T00:00:00.000+00:00',
"type_of_post": 'some type'
}

valid_post_list = [{
'title': 'Some_title',
'body': 'text_content',
Expand Down Expand Up @@ -42,3 +66,15 @@ def test_given_a_nonexistent_creator_role_system_must_return_404_ok(http_test_cl
controller_mock.get_posts_by_creator_role = Mock(return_value=None)
response = http_test_client.get('/posts/creator/anything')
assert response.status_code == 404


def test_given_invalid_new_post_format_422(http_test_client):
response = http_test_client.post('/posts', json={})
assert response.status_code == 422


def test_given_valid_new_post_entry_the_system_must_submit_the_post_and_return_200_ok(http_test_client):
with patch('class_social.posts.post_controller') as controller_mock:
controller_mock.post_a_post = Mock(return_value=None)
response = http_test_client.post('/posts', json=valid_post)
assert response.status_code == 200

0 comments on commit 6e8b5f5

Please sign in to comment.