Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-notes-api
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-notes-api
Commits
cafe985a
Commit
cafe985a
authored
Dec 23, 2014
by
Oleg Marshev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests.
parent
3f18f96a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
88 additions
and
10 deletions
+88
-10
notesapi/v1/models.py
+20
-10
notesapi/v1/tests/test_models.py
+68
-0
No files found.
notesapi/v1/models.py
View file @
cafe985a
import
json
from
django.db
import
models
from
django.db
import
models
from
django.core.exceptions
import
ValidationError
class
Note
(
models
.
Model
):
class
Note
(
models
.
Model
):
...
@@ -17,24 +19,32 @@ class Note(models.Model):
...
@@ -17,24 +19,32 @@ class Note(models.Model):
def
clean
(
self
,
json_body
):
def
clean
(
self
,
json_body
):
"""
"""
Clean
s
the note object or raises a ValidationError.
Clean the note object or raises a ValidationError.
"""
"""
if
json_body
is
None
:
if
json_body
is
None
:
raise
ValidationError
(
'Note must have a body.'
)
raise
ValidationError
(
'Note must have a body.'
)
self
.
text
=
strip_tags
(
body
.
get
(
'text'
,
''
))
try
:
self
.
quote
=
strip_tags
(
body
.
get
(
'quote'
,
''
))
body
=
json
.
loads
(
json_body
)
except
(
ValueError
,
TypeError
)
as
error
:
raise
ValidationError
(
'Note must have a valid json.'
)
if
not
type
(
body
)
is
dict
:
raise
ValidationError
(
'Note body must be a dictionary.'
)
self
.
text
=
body
.
get
(
'text'
,
''
)
self
.
quote
=
body
.
get
(
'quote'
,
''
)
try
:
try
:
self
.
course_id
=
body
[
'course_id'
]
self
.
course_id
=
body
[
'course_id'
]
self
.
usage_id
=
body
[
'usage_id'
]
self
.
usage_id
=
body
[
'usage_id'
]
self
.
user_id
=
body
[
'user
_id
'
]
self
.
user_id
=
body
[
'user'
]
except
KeyError
as
error
:
except
KeyError
as
error
:
raise
ValidationError
(
'Note must have a course_id and usage_id and user_id.'
)
raise
ValidationError
(
'Note must have a course_id and usage_id and user_id.'
)
body
=
json
.
loads
(
json_body
)
ranges
=
body
.
get
(
'ranges'
)
if
not
type
(
body
)
is
dict
:
if
ranges
is
None
or
len
(
ranges
)
!=
1
:
raise
ValidationError
(
'Note
body must be a dictionary
.'
)
raise
ValidationError
(
'Note
must contain exactly one range
.'
)
self
.
range_start
=
ranges
[
0
][
'start'
]
self
.
range_start
=
ranges
[
0
][
'start'
]
self
.
range_start_offset
=
ranges
[
0
][
'startOffset'
]
self
.
range_start_offset
=
ranges
[
0
][
'startOffset'
]
...
@@ -47,7 +57,7 @@ class Note(models.Model):
...
@@ -47,7 +57,7 @@ class Note(models.Model):
"""
"""
return
{
return
{
'id'
:
self
.
pk
,
'id'
:
self
.
pk
,
'user'
:
self
.
user
,
'user'
:
self
.
user
_id
,
'course_id'
:
self
.
course_id
,
'course_id'
:
self
.
course_id
,
'usage_id'
:
self
.
usage_id
,
'usage_id'
:
self
.
usage_id
,
'text'
:
self
.
text
,
'text'
:
self
.
text
,
...
@@ -58,6 +68,6 @@ class Note(models.Model):
...
@@ -58,6 +68,6 @@ class Note(models.Model):
'end'
:
self
.
range_end
,
'end'
:
self
.
range_end
,
'endOffset'
:
self
.
range_end_offset
'endOffset'
:
self
.
range_end_offset
}],
}],
'created'
:
s
tr
(
self
.
created
)
,
'created'
:
s
elf
.
created
,
'updated'
:
s
tr
(
self
.
updated
)
'updated'
:
s
elf
.
updated
}
}
notesapi/v1/tests/test_models.py
0 → 100644
View file @
cafe985a
import
json
from
unittest
import
TestCase
from
notesapi.v1.models
import
Note
from
django.core.exceptions
import
ValidationError
class
NoteTest
(
TestCase
):
def
setUp
(
self
):
self
.
course_id
=
"test_course_id"
self
.
user_id
=
"test_user_id"
#self.usage = BlockUsageLocator.from_string("i4x://org/course/category/name")
self
.
usage
=
"test_usage_id"
self
.
note
=
{
"user"
:
u"test_user_id"
,
"usage_id"
:
u"test-usage-id"
,
"course_id"
:
u"test-course-id"
,
"text"
:
u"test note text"
,
"quote"
:
u"test note quote"
,
"ranges"
:
[
{
"start"
:
u"/p[1]"
,
"end"
:
u"/p[1]"
,
"startOffset"
:
0
,
"endOffset"
:
10
,
}
],
}
def
test_clean_valid_note
(
self
):
note
=
Note
()
note
.
clean
(
json
.
dumps
(
self
.
note
))
self
.
note
.
update
({
'id'
:
None
,
'created'
:
None
,
'updated'
:
None
,
})
self
.
assertEqual
(
note
.
as_dict
(),
self
.
note
)
def
test_clean_invalid_note
(
self
):
note
=
Note
()
for
empty_type
in
(
None
,
''
,
0
,
[]):
with
self
.
assertRaises
(
ValidationError
):
note
.
clean
(
empty_type
)
def
test_must_have_fields
(
self
):
note
=
Note
()
for
field
in
[
'user'
,
'usage_id'
,
'course_id'
]:
payload
=
self
.
note
.
copy
()
payload
.
pop
(
field
)
with
self
.
assertRaises
(
ValidationError
):
note
.
clean
(
json
.
dumps
(
payload
))
def
test_clean_many_ranges
(
self
):
note
=
Note
()
with
self
.
assertRaises
(
ValidationError
):
note
.
clean
(
json
.
dumps
({
'text'
:
'foo'
,
'quote'
:
'bar'
,
'ranges'
:
[{}
for
i
in
range
(
10
)]
# too many ranges
}))
def
test_save
(
self
):
note
=
Note
()
note
.
clean
(
json
.
dumps
(
self
.
note
))
note
.
save
()
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