Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Backend
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
swp-unisport
team-warumkeinrust
Backend
Commits
e34ad583
Commit
e34ad583
authored
3 years ago
by
fu2662cw
Browse files
Options
Downloads
Patches
Plain Diff
Prevent Criterion-Rating to be recreated on change
parent
737df68c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
unisportomat/quiz/models.py
+7
-3
7 additions, 3 deletions
unisportomat/quiz/models.py
unisportomat/quiz/tests.py
+23
-7
23 additions, 7 deletions
unisportomat/quiz/tests.py
with
30 additions
and
10 deletions
unisportomat/quiz/models.py
+
7
−
3
View file @
e34ad583
...
@@ -12,7 +12,7 @@ class CriterionRating(models.Model):
...
@@ -12,7 +12,7 @@ class CriterionRating(models.Model):
"""
"""
rating
=
models
.
IntegerField
(
rating
=
models
.
IntegerField
(
validators
=
[
MaxValueValidator
(
10
),
MinValueValidator
(
0
)]
validators
=
[
MaxValueValidator
(
10
),
MinValueValidator
(
1
)]
)
)
criterion
=
models
.
ForeignKey
(
"
Criterion
"
,
on_delete
=
models
.
CASCADE
)
criterion
=
models
.
ForeignKey
(
"
Criterion
"
,
on_delete
=
models
.
CASCADE
)
sport
=
models
.
ForeignKey
(
"
Sport
"
,
on_delete
=
models
.
CASCADE
)
sport
=
models
.
ForeignKey
(
"
Sport
"
,
on_delete
=
models
.
CASCADE
)
...
@@ -34,8 +34,12 @@ class Sport(models.Model):
...
@@ -34,8 +34,12 @@ class Sport(models.Model):
def
rate
(
self
,
criterion
,
rating
):
def
rate
(
self
,
criterion
,
rating
):
"""
Defines how much (rating) the sport meets the given criterion
"""
"""
Defines how much (rating) the sport meets the given criterion
"""
rating
=
CriterionRating
(
sport
=
self
,
criterion
=
criterion
,
rating
=
rating
)
rating_obj
,
_
=
CriterionRating
.
objects
.
get_or_create
(
rating
.
save
()
sport
=
self
,
criterion
=
criterion
,
defaults
=
{
"
rating
"
:
rating
}
)
rating_obj
.
rating
=
rating
rating_obj
.
save
()
return
rating_obj
def
get_rating
(
self
,
criterion
):
def
get_rating
(
self
,
criterion
):
"""
Returns how much the sport meets the given criterion
"""
"""
Returns how much the sport meets the given criterion
"""
...
...
This diff is collapsed.
Click to expand it.
unisportomat/quiz/tests.py
+
23
−
7
View file @
e34ad583
...
@@ -9,8 +9,10 @@ class SportModelTest(TestCase):
...
@@ -9,8 +9,10 @@ class SportModelTest(TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
name
=
"
HIIT
"
self
.
name
=
"
HIIT
"
self
.
url
=
"
https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_
"
\
self
.
url
=
(
"
-_High_Intensity_Interval_Training___HOME.html
"
"
https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_
"
"
-_High_Intensity_Interval_Training___HOME.html
"
)
self
.
test_sport
=
Sport
(
self
.
test_sport
=
Sport
(
name
=
self
.
name
,
name
=
self
.
name
,
...
@@ -20,7 +22,7 @@ class SportModelTest(TestCase):
...
@@ -20,7 +22,7 @@ class SportModelTest(TestCase):
self
.
test_sport
.
save
()
self
.
test_sport
.
save
()
def
test_sport_can_be_created
(
self
):
def
test_sport_can_be_created
(
self
):
"""
New sport is written to the database
"""
"""
New sport is written to the database
"""
test_sport
=
Sport
.
objects
.
first
()
test_sport
=
Sport
.
objects
.
first
()
self
.
assertEqual
(
test_sport
.
name
,
self
.
name
)
self
.
assertEqual
(
test_sport
.
name
,
self
.
name
)
self
.
assertEqual
(
test_sport
.
url
,
self
.
url
)
self
.
assertEqual
(
test_sport
.
url
,
self
.
url
)
...
@@ -28,10 +30,13 @@ class SportModelTest(TestCase):
...
@@ -28,10 +30,13 @@ class SportModelTest(TestCase):
class
CriterionRatingTest
(
TestCase
):
class
CriterionRatingTest
(
TestCase
):
"""
Tests the Relation between Sport and Criterion
"""
"""
Tests the Relation between Sport and Criterion
"""
def
setUp
(
self
):
def
setUp
(
self
):
self
.
name
=
"
HIIT
"
self
.
name
=
"
HIIT
"
self
.
url
=
"
https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_
"
\
self
.
url
=
(
"
-_High_Intensity_Interval_Training___HOME.html
"
"
https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_
"
"
-_High_Intensity_Interval_Training___HOME.html
"
)
self
.
test_sport
=
Sport
(
self
.
test_sport
=
Sport
(
name
=
self
.
name
,
name
=
self
.
name
,
...
@@ -44,17 +49,28 @@ class CriterionRatingTest(TestCase):
...
@@ -44,17 +49,28 @@ class CriterionRatingTest(TestCase):
self
.
criterion
.
save
()
self
.
criterion
.
save
()
def
test_can_rate_criterion_for_sport
(
self
):
def
test_can_rate_criterion_for_sport
(
self
):
"""
A rating for a specific criterion can be added to a sport
"""
"""
A rating for a specific criterion can be added to a sport
"""
self
.
test_sport
.
rate
(
self
.
criterion
,
10
)
self
.
test_sport
.
rate
(
self
.
criterion
,
10
)
self
.
assertEqual
(
self
.
test_sport
.
criteria_ratings
.
first
(),
self
.
criterion
)
self
.
assertEqual
(
self
.
test_sport
.
criteria_ratings
.
first
(),
self
.
criterion
)
self
.
assertEqual
(
self
.
test_sport
.
get_rating
(
self
.
criterion
),
10
)
self
.
assertEqual
(
self
.
test_sport
.
get_rating
(
self
.
criterion
),
10
)
def
test_rating_can_be_changed
(
self
):
"""
If a sport is rated again then the corresponding relation is changed,
instead of recreated
"""
first_rating_object
=
self
.
test_sport
.
rate
(
self
.
criterion
,
10
)
second_rating_object
=
self
.
test_sport
.
rate
(
self
.
criterion
,
8
)
self
.
assertEqual
(
first_rating_object
,
second_rating_object
)
self
.
assertEqual
(
self
.
test_sport
.
get_rating
(
criterion
=
self
.
criterion
),
8
)
class
CriterionModelTest
(
TestCase
):
class
CriterionModelTest
(
TestCase
):
"""
Tests the Criterion model
"""
"""
Tests the Criterion model
"""
def
test_criterion_can_be_created
(
self
):
def
test_criterion_can_be_created
(
self
):
"""
New criterion is saved to the db
"""
"""
New criterion is saved to the db
"""
name
=
"
Einzelsport
"
name
=
"
Einzelsport
"
Criterion
(
name
=
name
).
save
()
Criterion
(
name
=
name
).
save
()
test_criterion
=
Criterion
.
objects
.
first
()
test_criterion
=
Criterion
.
objects
.
first
()
self
.
assertEqual
(
test_criterion
.
name
,
name
)
self
.
assertEqual
(
test_criterion
.
name
,
name
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment