Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d9b9640b
authored
May 21, 2013
by
Dudás Ádám
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
school: some refactoring and more test for Person
parent
235ff37f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
28 deletions
+52
-28
school/models.py
+6
-3
school/tests.py
+46
-25
No files found.
school/models.py
View file @
d9b9640b
...
@@ -31,7 +31,7 @@ def create_user_profile(sender, instance, created, **kwargs):
...
@@ -31,7 +31,7 @@ def create_user_profile(sender, instance, created, **kwargs):
p
=
Person
.
objects
.
get
(
code
=
instance
.
username
)
p
=
Person
.
objects
.
get
(
code
=
instance
.
username
)
except
Person
.
DoesNotExist
:
except
Person
.
DoesNotExist
:
p
=
Person
.
objects
.
create
(
code
=
instance
.
username
)
p
=
Person
.
objects
.
create
(
code
=
instance
.
username
)
except
Exception
as
e
:
except
Exception
as
e
:
# pragma: no cover
logger
.
warning
(
"Couldn't create profile for user:
%(username)
s"
logger
.
warning
(
"Couldn't create profile for user:
%(username)
s"
"
\n
Reason:
%(exception)
s"
,
"
\n
Reason:
%(exception)
s"
,
{
"username"
:
instance
.
username
,
{
"username"
:
instance
.
username
,
...
@@ -42,6 +42,9 @@ def create_user_profile(sender, instance, created, **kwargs):
...
@@ -42,6 +42,9 @@ def create_user_profile(sender, instance, created, **kwargs):
post_save
.
connect
(
create_user_profile
,
sender
=
User
)
post_save
.
connect
(
create_user_profile
,
sender
=
User
)
class
Person
(
models
.
Model
):
class
Person
(
models
.
Model
):
"""
Personal settings and attributes of a user.
"""
user
=
models
.
ForeignKey
(
User
,
null
=
True
,
blank
=
True
,
unique
=
True
)
user
=
models
.
ForeignKey
(
User
,
null
=
True
,
blank
=
True
,
unique
=
True
)
language
=
models
.
CharField
(
verbose_name
=
_
(
'language'
),
blank
=
False
,
language
=
models
.
CharField
(
verbose_name
=
_
(
'language'
),
blank
=
False
,
max_length
=
10
,
choices
=
LANGUAGE_CHOICES
,
default
=
LANGUAGE_CODE
)
max_length
=
10
,
choices
=
LANGUAGE_CHOICES
,
default
=
LANGUAGE_CODE
)
...
@@ -59,9 +62,9 @@ class Person(models.Model):
...
@@ -59,9 +62,9 @@ class Person(models.Model):
def
short_name
(
self
):
def
short_name
(
self
):
if
self
.
user
:
if
self
.
user
:
if
self
.
user
.
last_name
:
if
self
.
user
.
last_name
:
# pragma: no cover
return
self
.
user
.
last_name
return
self
.
user
.
last_name
else
:
else
:
# pragma: no cover
return
self
.
user
.
username
return
self
.
user
.
username
else
:
else
:
return
self
.
code
return
self
.
code
...
...
school/tests.py
View file @
d9b9640b
from
datetime
import
datetime
,
timedelta
from
django.test
import
TestCase
from
django.test
import
TestCase
from
django.contrib.auth.models
import
User
from
models
import
create_user_profile
,
Person
,
Course
,
Semester
,
Group
from
models
import
create_user_profile
,
Person
,
Course
,
Semester
,
Group
from
datetime
import
datetime
,
timedelta
class
MockUser
:
username
=
"testuser"
class
CreateUserProfileTestCase
(
TestCase
):
class
CreateUserProfileTestCase
(
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
user
=
MockUser
()
self
.
user
=
User
(
username
=
"testuser"
,
password
=
"testpass"
,
for
p
in
Person
.
objects
.
all
():
email
=
"test@mail.com"
,
first_name
=
"Test"
,
last_name
=
"User"
)
p
.
delete
()
Person
.
objects
.
all
()
.
delete
()
with
self
.
assertRaises
(
Person
.
DoesNotExist
):
Person
.
objects
.
get
(
code
=
self
.
user
.
username
)
def
test_new_profile
(
self
):
def
test_new_profile
(
self
):
"""Test profile creation functionality for new user."""
"""Test profile creation functionality for new user."""
create_user_profile
(
self
.
user
.
__class__
,
self
.
user
,
True
)
create_user_profile
(
self
.
user
.
__class__
,
self
.
user
,
True
)
self
.
assertEqual
(
Person
.
objects
.
filter
(
self
.
assertIsNotNone
(
Person
.
objects
.
get
(
code
=
self
.
user
.
username
))
code
=
self
.
user
.
username
)
.
count
(),
1
)
def
test_existing_profile
(
self
):
def
test_existing_profile
(
self
):
"""Test profile creation functionality when it already exists."""
"""Test profile creation functionality when it already exists."""
Person
.
objects
.
create
(
code
=
self
.
user
.
username
)
Person
.
objects
.
create
(
code
=
self
.
user
.
username
)
create_user_profile
(
self
.
user
.
__class__
,
self
.
user
,
True
)
create_user_profile
(
self
.
user
.
__class__
,
self
.
user
,
True
)
self
.
assertEqual
(
Person
.
objects
.
filter
(
self
.
assertIsNotNone
(
Person
.
objects
.
get
(
code
=
self
.
user
.
username
))
code
=
self
.
user
.
username
)
.
count
(),
1
)
class
PersonTestCase
(
TestCase
):
class
PersonTestCase
(
TestCase
):
def
setUp
(
self
):
"""Test 'static' Person facts."""
self
.
testperson
=
Person
.
objects
.
create
(
code
=
'testperson'
)
def
test_language_code_in_choices
(
self
):
def
test_language_code_in_choices
(
self
):
"""Test whether the default value for language is a valid choice."""
"""Test whether the default value for language is a valid choice."""
# TODO
person
=
Person
(
code
=
"test"
)
language_field
=
self
.
test
person
.
_meta
.
get_field
(
'language'
)
language_field
=
person
.
_meta
.
get_field
(
'language'
)
choice_codes
=
[
code
for
(
code
,
_
)
in
language_field
.
choices
]
choice_codes
=
[
code
for
(
code
,
_
)
in
language_field
.
choices
]
self
.
assertIn
(
language_field
.
default
,
choice_codes
)
self
.
assertIn
(
language_field
.
default
,
choice_codes
)
class
PersonWithUserTestCase
(
TestCase
):
"""Test Person entities which have their user attribute set."""
def
setUp
(
self
):
self
.
user
=
User
(
username
=
"testuser"
,
password
=
"testpass"
,
email
=
"test@mail.com"
,
first_name
=
"Test"
,
last_name
=
"User"
)
Person
.
objects
.
all
()
.
delete
()
self
.
person
=
Person
.
objects
.
create
(
code
=
'testcode'
,
user
=
self
.
user
)
def
test_get_owned_shares
(
self
):
self
.
assertIsNotNone
(
self
.
person
.
get_owned_shares
())
def
test_get_shares
(
self
):
self
.
assertIsNotNone
(
self
.
person
.
get_shares
())
def
test_short_name
(
self
):
self
.
assertIsNotNone
(
self
.
person
.
short_name
())
# without first or last name
self
.
person
.
user
.
first_name
=
None
self
.
person
.
user
.
last_name
=
None
self
.
assertIsNotNone
(
self
.
person
.
short_name
())
def
test_unicode
(
self
):
self
.
assertIsNotNone
(
self
.
person
.
__unicode__
())
class
PersonWithoutUserTestCase
(
TestCase
):
"""Test Person entities which doesn't have their user attribute set."""
def
setUp
(
self
):
Person
.
objects
.
all
()
.
delete
()
self
.
person
=
Person
.
objects
.
create
(
code
=
'testcode'
)
def
test_get_owned_shares
(
self
):
def
test_get_owned_shares
(
self
):
# TODO
self
.
assertIsNotNone
(
self
.
person
.
get_owned_shares
())
self
.
testperson
.
get_owned_shares
()
def
test_get_shares
(
self
):
def
test_get_shares
(
self
):
# TODO
self
.
assertIsNotNone
(
self
.
person
.
get_shares
())
self
.
testperson
.
get_shares
()
def
test_short_name
(
self
):
def
test_short_name
(
self
):
# TODO
self
.
assertIsNotNone
(
self
.
person
.
short_name
())
self
.
testperson
.
short_name
()
def
test_unicode
(
self
):
def
test_unicode
(
self
):
# TODO
self
.
assertIsNotNone
(
self
.
person
.
__unicode__
())
self
.
testperson
.
__unicode__
()
class
CourseTestCase
(
TestCase
):
class
CourseTestCase
(
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment