Skip to content

Merge attempt #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions class_social/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from fastapi import FastAPI

from class_social.users import users_routes
from class_social.posts import posts_routes
from class_social.posts import post_routes

app = FastAPI()
app.include_router(users_routes)
app.include_router(posts_routes)
app.include_router(post_routes)


if __name__ == '__main__':
Expand Down
11 changes: 6 additions & 5 deletions class_social/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class User(BaseModel):
name: str
address: str
website: Optional[str]
connections: Optional[List['User']]
connections: Optional[List[str]]
# 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


47 changes: 35 additions & 12 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,54 @@ 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_routes = APIRouter()
#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}')
@post_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