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
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
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
17 deletions
+53
-17
school/models.py
+23
-5
school/tests.py
+30
-12
No files found.
school/models.py
View file @
762a6f06
...
@@ -6,27 +6,45 @@ from django.core.exceptions import ValidationError
...
@@ -6,27 +6,45 @@ from django.core.exceptions import ValidationError
from
datetime
import
datetime
from
datetime
import
datetime
from
django.conf
import
settings
from
django.conf
import
settings
import
one.models
import
one.models
import
logging
LANGUAGE_CODE
=
settings
.
LANGUAGE_CODE
LANGUAGE_CODE
=
settings
.
LANGUAGE_CODE
LANGUAGE_CHOICES
=
((
'hu'
,
_
(
'Hungarian'
)),
(
'en'
,
_
(
'English'
)))
LANGUAGE_CHOICES
=
((
'hu'
,
_
(
'Hungarian'
)),
(
'en'
,
_
(
'English'
)))
logger
=
logging
.
getLogger
(
__name__
)
def
create_user_profile
(
sender
,
instance
,
created
,
**
kwargs
):
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
:
if
created
:
try
:
try
:
p
=
Person
.
objects
.
get
(
code
=
instance
.
username
)
p
=
Person
.
objects
.
get
(
code
=
instance
.
username
)
except
Exception
:
except
Person
.
DoesNotExist
:
p
=
Person
.
objects
.
create
(
code
=
instance
.
username
)
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
return
p
.
c
ode
=
instance
.
username
p
.
c
lean
()
p
.
save
()
p
.
save
()
post_save
.
connect
(
create_user_profile
,
sender
=
User
)
post_save
.
connect
(
create_user_profile
,
sender
=
User
)
class
Person
(
models
.
Model
):
class
Person
(
models
.
Model
):
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
,
max_length
=
10
,
language
=
models
.
CharField
(
verbose_name
=
_
(
'language'
),
blank
=
False
,
choices
=
LANGUAGE_CHOICES
,
default
=
LANGUAGE_CODE
)
max_length
=
10
,
choices
=
LANGUAGE_CHOICES
,
default
=
LANGUAGE_CODE
)
code
=
models
.
CharField
(
_
(
'code'
),
max_length
=
30
,
unique
=
True
)
code
=
models
.
CharField
(
_
(
'code'
),
max_length
=
30
,
unique
=
True
)
def
get_owned_shares
(
self
):
def
get_owned_shares
(
self
):
...
...
school/tests.py
View file @
762a6f06
"""
from
django.test
import
TestCase
This file demonstrates writing tests using the unittest module. These will pass
from
models
import
create_user_profile
,
Person
when you run "manage.py test".
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_language_code_in_choices
(
self
):
def
test_basic_addition
(
self
):
"""Test whether the default value for language is a valid choice."""
"""
language_field
=
Person
.
objects
.
all
()[
0
]
.
_meta
.
get_field
(
'language'
)
Tests that 1 + 1 always equals 2.
choice_codes
=
[
code
for
(
code
,
_
)
in
language_field
.
choices
]
"""
self
.
assertIn
(
language_field
.
default
,
choice_codes
)
self
.
assertEqual
(
1
+
1
,
2
)
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