Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
Openlobby Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository 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
TO
openlobby
Openlobby Server
Commits
2a113baa
Commit
2a113baa
authored
7 years ago
by
Michal Holub
Browse files
Options
Downloads
Patches
Plain Diff
added unit tests for `sorted` method
parent
a759c7f7
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
openlobby/core/api/schema.py
+3
-5
3 additions, 5 deletions
openlobby/core/api/schema.py
openlobby/core/models.py
+8
-5
8 additions, 5 deletions
openlobby/core/models.py
tests/test_models.py
+45
-6
45 additions, 6 deletions
tests/test_models.py
with
56 additions
and
16 deletions
openlobby/core/api/schema.py
+
3
−
5
View file @
2a113baa
import
graphene
import
graphene
from
graphene
import
relay
from
django.db.models
import
Count
,
Q
from
django.db.models
import
Count
,
Q
from
graphene
import
relay
from
.
import
types
from
.
import
types
from
..models
import
OpenIdClient
from
.paginator
import
Paginator
from
.paginator
import
Paginator
from
.sanitizers
import
extract_text
from
.sanitizers
import
extract_text
from
..
import
search
from
..
import
search
from
..models
import
OpenIdClient
from
..models
import
User
,
Report
from
..models
import
User
,
Report
AUTHOR_SORT_LAST_NAME_ID
=
1
AUTHOR_SORT_LAST_NAME_ID
=
1
...
@@ -73,9 +73,7 @@ class Query:
...
@@ -73,9 +73,7 @@ class Query:
authors
=
User
.
objects
\
authors
=
User
.
objects
\
.
sorted
(
**
kwargs
)
\
.
sorted
(
**
kwargs
)
\
.
filter
(
is_author
=
True
)
\
.
filter
(
is_author
=
True
)[
paginator
.
slice_from
:
paginator
.
slice_to
]
.
annotate
(
total_reports
=
Count
(
'
report
'
,
filter
=
Q
(
report__is_draft
=
False
)))[
paginator
.
slice_from
:
paginator
.
slice_to
]
page_info
=
paginator
.
get_page_info
(
total
)
page_info
=
paginator
.
get_page_info
(
total
)
...
...
This diff is collapsed.
Click to expand it.
openlobby/core/models.py
+
8
−
5
View file @
2a113baa
import
time
from
django.conf
import
settings
from
django.conf
import
settings
from
django.contrib.auth.base_user
import
BaseUserManager
from
django.contrib.auth.base_user
import
BaseUserManager
from
django.db
import
models
from
django.contrib.auth.models
import
AbstractUser
from
django.contrib.auth.models
import
AbstractUser
from
django.contrib.postgres.fields
import
JSONField
from
django.contrib.postgres.fields
import
JSONField
from
django.db
import
models
from
django.db.models
import
Count
from
django.db.models
import
Q
from
django.utils
import
timezone
from
django.utils
import
timezone
import
time
class
UserManager
(
BaseUserManager
):
class
UserManager
(
BaseUserManager
):
def
sorted
(
self
,
**
kwargs
):
def
sorted
(
self
,
**
kwargs
):
# inline import intentionally
# inline import intentionally
from
openlobby.core.api.schema
import
AUTHOR_SORT_LAST_NAME_ID
,
AUTHOR_SORT_TOTAL_REPORTS_ID
from
openlobby.core.api.schema
import
AUTHOR_SORT_LAST_NAME_ID
,
AUTHOR_SORT_TOTAL_REPORTS_ID
qs
=
self
.
get_queryset
()
qs
=
self
.
get_queryset
()
.
annotate
(
total_reports
=
Count
(
'
report
'
,
filter
=
Q
(
report__is_draft
=
False
)))
sort_field
=
kwargs
.
get
(
'
sort
'
,
AUTHOR_SORT_LAST_NAME_ID
)
sort_field
=
kwargs
.
get
(
'
sort
'
,
AUTHOR_SORT_LAST_NAME_ID
)
if
sort_field
==
AUTHOR_SORT_LAST_NAME_ID
:
if
sort_field
==
AUTHOR_SORT_LAST_NAME_ID
:
return
qs
.
order_by
(
'
{}last_name
'
.
format
(
'
-
'
if
kwargs
.
get
(
'
reversed
'
,
False
)
else
''
),
'
first_name
'
)
return
qs
.
order_by
(
'
{}last_name
'
.
format
(
'
-
'
if
kwargs
.
get
(
'
reversed
'
,
False
)
else
''
),
'
first_name
'
)
...
@@ -34,7 +37,7 @@ class User(AbstractUser):
...
@@ -34,7 +37,7 @@ class User(AbstractUser):
# deal with first name and last name collisions
# deal with first name and last name collisions
if
self
.
is_author
:
if
self
.
is_author
:
collisions
=
User
.
objects
.
filter
(
first_name
=
self
.
first_name
,
last_name
=
self
.
last_name
,
collisions
=
User
.
objects
.
filter
(
first_name
=
self
.
first_name
,
last_name
=
self
.
last_name
,
is_author
=
True
).
exclude
(
id
=
self
.
id
)
is_author
=
True
).
exclude
(
id
=
self
.
id
)
if
collisions
.
count
()
>
0
:
if
collisions
.
count
()
>
0
:
self
.
has_colliding_name
=
True
self
.
has_colliding_name
=
True
collisions
.
update
(
has_colliding_name
=
True
)
collisions
.
update
(
has_colliding_name
=
True
)
...
...
This diff is collapsed.
Click to expand it.
tests/test_models.py
+
45
−
6
View file @
2a113baa
import
pytest
import
arrow
from
django.conf
import
settings
from
unittest.mock
import
patch
from
unittest.mock
import
patch
from
openlobby.core.models
import
Report
,
User
,
OpenIdClient
,
LoginAttempt
import
arrow
import
pytest
from
django.conf
import
settings
from
openlobby.core.api.schema
import
AUTHOR_SORT_LAST_NAME_ID
,
AUTHOR_SORT_TOTAL_REPORTS_ID
from
openlobby.core.documents
import
ReportDoc
from
openlobby.core.documents
import
ReportDoc
from
openlobby.core.models
import
Report
,
User
,
OpenIdClient
,
LoginAttempt
pytestmark
=
[
pytest
.
mark
.
django_db
,
pytest
.
mark
.
usefixtures
(
'
django_es
'
)]
pytestmark
=
[
pytest
.
mark
.
django_db
,
pytest
.
mark
.
usefixtures
(
'
django_es
'
)]
...
@@ -82,7 +82,7 @@ def test_login_attempt__default_expiration():
...
@@ -82,7 +82,7 @@ def test_login_attempt__default_expiration():
client
=
OpenIdClient
.
objects
.
create
(
name
=
'
a
'
,
client_id
=
'
b
'
,
client_secret
=
'
c
'
)
client
=
OpenIdClient
.
objects
.
create
(
name
=
'
a
'
,
client_id
=
'
b
'
,
client_secret
=
'
c
'
)
with
patch
(
'
openlobby.core.models.time.time
'
,
return_value
=
10000
):
with
patch
(
'
openlobby.core.models.time.time
'
,
return_value
=
10000
):
attempt
=
LoginAttempt
.
objects
.
create
(
openid_client
=
client
,
state
=
'
foo
'
,
attempt
=
LoginAttempt
.
objects
.
create
(
openid_client
=
client
,
state
=
'
foo
'
,
app_redirect_uri
=
'
http://openlobby/app
'
)
app_redirect_uri
=
'
http://openlobby/app
'
)
assert
attempt
.
expiration
==
10000
+
settings
.
LOGIN_ATTEMPT_EXPIRATION
assert
attempt
.
expiration
==
10000
+
settings
.
LOGIN_ATTEMPT_EXPIRATION
...
@@ -118,3 +118,42 @@ def test_user__name_collision_excludes_self_on_update():
...
@@ -118,3 +118,42 @@ def test_user__name_collision_excludes_self_on_update():
u
=
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
u
=
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
u
.
save
()
u
.
save
()
assert
User
.
objects
.
get
(
username
=
'
a
'
).
has_colliding_name
is
False
assert
User
.
objects
.
get
(
username
=
'
a
'
).
has_colliding_name
is
False
def
test_user__sorted_default
():
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
False
,
first_name
=
'
Ryan
'
,
last_name
=
'
AGosling
'
)
User
.
objects
.
create
(
username
=
'
b
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
BGosling
'
)
User
.
objects
.
create
(
username
=
'
c
'
,
is_author
=
False
,
first_name
=
'
Ryan
'
,
last_name
=
'
CGosling
'
)
assert
User
.
objects
.
sorted
()[
0
].
username
==
'
a
'
def
test_user__sorted_default_reversed
():
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
False
,
first_name
=
'
Ryan
'
,
last_name
=
'
AGosling
'
)
User
.
objects
.
create
(
username
=
'
b
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
BGosling
'
)
User
.
objects
.
create
(
username
=
'
c
'
,
is_author
=
False
,
first_name
=
'
Ryan
'
,
last_name
=
'
CGosling
'
)
assert
User
.
objects
.
sorted
(
reversed
=
True
)[
0
].
username
==
'
c
'
def
test_user__sorted_last_name
():
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
False
,
first_name
=
'
Ryan
'
,
last_name
=
'
AGosling
'
)
User
.
objects
.
create
(
username
=
'
b
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
BGosling
'
)
User
.
objects
.
create
(
username
=
'
c
'
,
is_author
=
False
,
first_name
=
'
Ryan
'
,
last_name
=
'
CGosling
'
)
assert
User
.
objects
.
sorted
(
sort
=
AUTHOR_SORT_LAST_NAME_ID
)[
0
].
username
==
'
a
'
assert
User
.
objects
.
sorted
(
sort
=
AUTHOR_SORT_LAST_NAME_ID
,
reversed
=
False
)[
0
].
username
==
'
a
'
assert
User
.
objects
.
sorted
(
sort
=
AUTHOR_SORT_LAST_NAME_ID
,
reversed
=
True
)[
0
].
username
==
'
c
'
def
test_user__sorted_total_reports
():
author
=
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
AGosling
'
)
User
.
objects
.
create
(
username
=
'
b
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
BGosling
'
)
date
=
arrow
.
get
(
2018
,
1
,
1
).
datetime
Report
.
objects
.
create
(
id
=
7
,
author
=
author
,
date
=
date
,
published
=
date
,
body
=
'
Lorem ipsum.
'
,
)
assert
User
.
objects
.
sorted
(
sort
=
AUTHOR_SORT_TOTAL_REPORTS_ID
)[
0
].
username
==
'
a
'
assert
User
.
objects
.
sorted
(
sort
=
AUTHOR_SORT_TOTAL_REPORTS_ID
,
reversed
=
False
)[
0
].
username
==
'
a
'
assert
User
.
objects
.
sorted
(
sort
=
AUTHOR_SORT_TOTAL_REPORTS_ID
,
reversed
=
True
)[
0
].
username
==
'
b
'
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