Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gyuricska Milán
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
762a6f06
authored
Mar 14, 2013
by
Dudás Ádám
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
school: some tests
parent
45b1ffcb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
18 deletions
+54
-18
school/models.py
+24
-6
school/tests.py
+30
-12
No files found.
school/models.py
View file @
762a6f06
...
...
@@ -6,27 +6,45 @@ from django.core.exceptions import ValidationError
from
datetime
import
datetime
from
django.conf
import
settings
import
one.models
import
logging
LANGUAGE_CODE
=
settings
.
LANGUAGE_CODE
LANGUAGE_CHOICES
=
((
'hu'
,
_
(
'Hungarian'
)),
(
'en'
,
_
(
'English'
)))
logger
=
logging
.
getLogger
(
__name__
)
def
create_user_profile
(
sender
,
instance
,
created
,
**
kwargs
):
"""
User creation hook.
Ensure that the specified user has an associated profile.
@param sender: The model class.
@type instance: User
@param instance: The instance being saved.
@type created: Boolean
@param created: True if a new record was created.
"""
if
created
:
try
:
p
=
Person
.
objects
.
get
(
code
=
instance
.
username
)
except
Exception
:
except
Person
.
DoesNotExist
:
p
=
Person
.
objects
.
create
(
code
=
instance
.
username
)
except
:
except
Exception
as
e
:
logger
.
warning
(
"Couldn't create profile for user:
%(username)
s"
"
\n
Reason:
%(exception)
s"
,
{
"username"
:
instance
.
username
,
"exception"
:
e
})
return
p
.
c
ode
=
instance
.
username
p
.
c
lean
()
p
.
save
()
post_save
.
connect
(
create_user_profile
,
sender
=
User
)
class
Person
(
models
.
Model
):
user
=
models
.
ForeignKey
(
User
,
null
=
True
,
blank
=
True
,
unique
=
True
)
language
=
models
.
CharField
(
verbose_name
=
_
(
'language'
),
blank
=
False
,
max_length
=
10
,
choices
=
LANGUAGE_CHOICES
,
default
=
LANGUAGE_CODE
)
language
=
models
.
CharField
(
verbose_name
=
_
(
'language'
),
blank
=
False
,
max_length
=
10
,
choices
=
LANGUAGE_CHOICES
,
default
=
LANGUAGE_CODE
)
code
=
models
.
CharField
(
_
(
'code'
),
max_length
=
30
,
unique
=
True
)
def
get_owned_shares
(
self
):
...
...
@@ -51,7 +69,7 @@ class Person(models.Model):
if
u
.
last_name
and
u
.
first_name
:
# TRANSLATORS: full name format used in enumerations
return
_
(
"
%(first)
s
%(last)
s"
)
%
{
'first'
:
u
.
first_name
,
'last'
:
u
.
last_name
}
'last'
:
u
.
last_name
}
else
:
return
u
.
username
...
...
school/tests.py
View file @
762a6f06
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
from
django.test
import
TestCase
from
models
import
create_user_profile
,
Person
Replace this with more appropriate tests for your application.
"""
class
MockUser
:
username
=
"testuser"
class
CreateUserProfileTestCase
(
TestCase
):
def
setUp
(
self
):
for
p
in
Person
.
objects
.
all
():
p
.
delete
()
def
test_new_profile
(
self
):
"""Test profile creation functionality for new user."""
user
=
MockUser
()
create_user_profile
(
user
.
__class__
,
user
,
True
)
self
.
assertEqual
(
Person
.
objects
.
filter
(
code
=
user
.
username
)
.
count
(),
1
)
def
test_existing_profile
(
self
):
"""Test profile creation functionality when it already exists."""
user
=
MockUser
()
Person
.
objects
.
create
(
code
=
user
.
username
)
create_user_profile
(
user
.
__class__
,
user
,
True
)
self
.
assertEqual
(
Person
.
objects
.
filter
(
code
=
user
.
username
)
.
count
(),
1
)
from
django.test
import
TestCase
class
PersonTestCase
(
TestCase
):
def
setUp
(
self
):
Person
.
objects
.
create
()
class
SimpleTest
(
TestCase
):
def
test_basic_addition
(
self
):
"""
Tests that 1 + 1 always equals 2.
"""
self
.
assertEqual
(
1
+
1
,
2
)
def
test_language_code_in_choices
(
self
):
"""Test whether the default value for language is a valid choice."""
language_field
=
Person
.
objects
.
all
()[
0
]
.
_meta
.
get_field
(
'language'
)
choice_codes
=
[
code
for
(
code
,
_
)
in
language_field
.
choices
]
self
.
assertIn
(
language_field
.
default
,
choice_codes
)
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