Skip to content
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

#189 atualizar avaliacoes #195

Merged
merged 6 commits into from
May 10, 2021
Merged
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
14 changes: 13 additions & 1 deletion app/model/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ class Post(db.Model):
db.Integer, nullable=False, primary_key=True, autoincrement=True
)
post_date = db.Column(db.Date, default=date.today(), nullable=False)
rating = db.Column(db.Float, nullable=False)
didactic = db.Column(db.Integer, nullable=False)
metod = db.Column(db.Integer, nullable=False)
avaliations = db.Column(db.Integer, nullable=False)
disponibility = db.Column(db.Integer, nullable=False)
content = db.Column(db.String(480), nullable=False)
is_anonymous = db.Column(db.Boolean, nullable=False)

Expand All @@ -68,3 +71,12 @@ class Post(db.Model):

agrees = db.relationship("Student", secondary="agree_student_post")
disagrees = db.relationship("Student", secondary="disagree_student_post")

@property
def rating(self):
sum = 0
sum += self.didactic
sum += self.metod
sum += self.avaliations
sum += self.disponibility
return sum / 4
40 changes: 40 additions & 0 deletions app/model/professor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,43 @@ def rating(self):
for post in self.posts:
sum += post.rating
return sum / len(self.posts)

@property
def didactic(self):
if len(self.posts) == 0:
return

sum = 0
for post in self.posts:
sum += post.didactic
return sum / len(self.posts)

@property
def metod(self):
if len(self.posts) == 0:
return

sum = 0
for post in self.posts:
sum += post.metod
return sum / len(self.posts)

@property
def avaliations(self):
if len(self.posts) == 0:
return

sum = 0
for post in self.posts:
sum += post.avaliations
return sum / len(self.posts)

@property
def disponibility(self):
if len(self.posts) == 0:
return

sum = 0
for post in self.posts:
sum += post.disponibility
return sum / len(self.posts)
5 changes: 4 additions & 1 deletion app/schemas/post_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class Meta:
required=True, validate=validate.Length(max=80), load_only=True
)
content = fields.String(required=True, validate=validate.Length(min=1, max=480))
rating = fields.Float(required=True, validate=validate.Range(min=0, max=10))
didactic = fields.Integer(required=True, validate=validate.Range(min=1, max=5))
metod = fields.Integer(required=True, validate=validate.Range(min=1, max=5))
avaliations = fields.Integer(required=True, validate=validate.Range(min=1, max=5))
disponibility = fields.Integer(required=True, validate=validate.Range(min=1, max=5))
is_anonymous = fields.Boolean(required=True)
post_date = fields.Date()

Expand Down
2 changes: 1 addition & 1 deletion app/schemas/professor_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class ProfessorSchema(ma.SQLAlchemySchema):
class Meta:
model = Professor
additional = ["rating"]
additional = ["rating", "didactic", "metod", "avaliations", "disponibility"]

id_professor = fields.Integer(validate=validate.Range(min=0))
reg_professor = fields.Integer(
Expand Down
5 changes: 4 additions & 1 deletion app/services/post_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ def register_post(post):
id_professor=post.get("id_professor"),
discipline_code=post.get("discipline_code"),
content=post.get("content"),
rating=post.get("rating"),
didactic=post.get("didactic"),
metod=post.get("metod"),
avaliations=post.get("avaliations"),
disponibility=post.get("disponibility"),
is_anonymous=post.get("is_anonymous"),
)
db.session.add(post_db)
Expand Down
5 changes: 4 additions & 1 deletion database/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ CREATE TABLE IF NOT EXISTS post (
id_professor int UNSIGNED NOT NULL,
content varchar(480) NOT NULL,
post_date date NOT NULL,
rating float NOT NULL,
didactic TINYINT UNSIGNED NOT NULL,
metod TINYINT UNSIGNED NOT NULL,
avaliations TINYINT UNSIGNED NOT NULL,
disponibility TINYINT UNSIGNED NOT NULL,
discipline_code varchar(80) NOT NULL DEFAULT '',
is_anonymous TINYINT(1) NOT NULL,
PRIMARY KEY (id_post),
Expand Down
36 changes: 29 additions & 7 deletions tests/tests_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ def valid_post(self):
"id_professor": self.professor["id_professor"],
"reg_student": self.student["reg_student"],
"discipline_code": self.discipline["discipline_code"],
"content": "Professor nota dez",
"rating": "5",
"content": "Professor nota cinco",
"didactic": "5",
"metod": "5",
"avaliations": "5",
"disponibility": "5",
"is_anonymous": True,
}

Expand Down Expand Up @@ -75,36 +78,55 @@ def test_api_must_validate_empty_attributes(self):
response = register_post(self, post=post)

expected_json_keys = [
"avaliations",
"content",
"didactic",
"discipline_code",
"disponibility",
"id_professor",
"is_anonymous",
"rating",
"metod",
"reg_student",
]
json_keys = list(response.json.keys())

self.assertEqual(response.status_code, 400)
self.assertEqual(json_keys, expected_json_keys)

def test_api_must_validate_attributes_min(self):
post = valid_post(self)

post["content"] = ""
post["rating"] = -1
post["didactic"] = 0
post["metod"] = 0
post["avaliations"] = 0
post["disponibility"] = 0

response = register_post(self, post=post)

self.assertEqual(response.status_code, 400)
self.assertIsNotNone(response.json["content"])
self.assertIsNotNone(response.json["rating"])
self.assertIsNotNone(response.json["didactic"])
self.assertIsNotNone(response.json["metod"])
self.assertIsNotNone(response.json["avaliations"])
self.assertIsNotNone(response.json["disponibility"])

def test_api_must_validate_attributes_max(self):
post = valid_post(self)
post["content"] = "a" * 481
post["rating"] = 11
post["didactic"] = 6
post["metod"] = 6
post["avaliations"] = 6
post["disponibility"] = 6

response = register_post(self, post=post)

self.assertEqual(response.status_code, 400)
self.assertIsNotNone(response.json["content"])
self.assertIsNotNone(response.json["rating"])
self.assertIsNotNone(response.json["didactic"])
self.assertIsNotNone(response.json["metod"])
self.assertIsNotNone(response.json["avaliations"])
self.assertIsNotNone(response.json["disponibility"])

def test_api_must_validate_professor_relationship_not_found(self):
post = valid_post(self)
Expand Down
12 changes: 11 additions & 1 deletion tests/tests_professor.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,17 @@ def test_api_must_return_professors_by_name_substring(self):
response = self.get(name_substring, headers)

json_attributes = list(response.json[0].keys())
expected_json_attributes = ["disciplines", "id_professor", "name", "rating"]
expected_json_attributes = [
"avaliations",
"didactic",
"disciplines",
"disponibility",
"id_professor",
"metod",
"name",
"rating",
]

self.assertEqual(response.status_code, 200)
self.assertEqual(json_attributes, expected_json_attributes)
self.assertEqual(response.json[0]["name"], self.professor["name"])
Expand Down