Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
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-platform
Commits
9b44d9d4
Commit
9b44d9d4
authored
Jul 03, 2013
by
Miles Steele
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add format_instances method (unused)
parent
522cdd21
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
2 deletions
+79
-2
lms/djangoapps/analytics/csvs.py
+20
-0
lms/djangoapps/analytics/tests/test_csvs.py
+59
-2
No files found.
lms/djangoapps/analytics/csvs.py
View file @
9b44d9d4
...
...
@@ -74,3 +74,23 @@ def format_dictlist(dictlist):
'header'
:
header
,
'datarows'
:
datarows
,
}
def
format_instances
(
instances
,
features
):
"""
Convert a list of instances into a header list and datarows list.
`header` is just `features` e.g. ['username', 'email']
`datarows` is a list of lists, each sublist representing a row in a table
e.g. [['username1', 'email1@email.com'], ['username2', 'email2@email.com']]
for `instances` of length 2.
`instances` is a list of instances, e.g. list of User's
`features` is a list of features
a feature is a string for which getattr(obj, feature) is valid
Returns header and datarows, formatted for input in create_csv_response
"""
header
=
features
datarows
=
[[
getattr
(
x
,
f
)
for
f
in
features
]
for
x
in
instances
]
return
header
,
datarows
lms/djangoapps/analytics/tests/test_csvs.py
View file @
9b44d9d4
""" Tests for analytics.csvs """
from
django.test
import
TestCase
from
nose.tools
import
raises
from
analytics.csvs
import
create_csv_response
,
format_dictlist
from
analytics.csvs
import
create_csv_response
,
format_dictlist
,
format_instances
class
TestAnalyticsCSVS
(
TestCase
):
'''Test analytics rendering of csv files.'''
""" Test analytics rendering of csv files."""
def
test_create_csv_response_nodata
(
self
):
header
=
[
'Name'
,
'Email'
]
...
...
@@ -35,6 +36,10 @@ class TestAnalyticsCSVS(TestCase):
self
.
assertEqual
(
res
[
'Content-Disposition'
],
'attachment; filename={0}'
.
format
(
'robot.csv'
))
self
.
assertEqual
(
res
.
content
.
strip
(),
''
)
class
TestAnalyticsFormatDictlist
(
TestCase
):
""" Test format_dictlist method """
def
test_format_dictlist
(
self
):
data_in
=
[
{
...
...
@@ -64,3 +69,55 @@ class TestAnalyticsCSVS(TestCase):
'header'
:
[],
'datarows'
:
[],
})
def
test_create_csv_response
(
self
):
header
=
[
'Name'
,
'Email'
]
datarows
=
[[
'Jim'
,
'jim@edy.org'
],
[
'Jake'
,
'jake@edy.org'
],
[
'Jeeves'
,
'jeeves@edy.org'
]]
res
=
create_csv_response
(
'robot.csv'
,
header
,
datarows
)
self
.
assertEqual
(
res
[
'Content-Type'
],
'text/csv'
)
self
.
assertEqual
(
res
[
'Content-Disposition'
],
'attachment; filename={0}'
.
format
(
'robot.csv'
))
self
.
assertEqual
(
res
.
content
.
strip
(),
'"Name","Email"
\r\n
"Jim","jim@edy.org"
\r\n
"Jake","jake@edy.org"
\r\n
"Jeeves","jeeves@edy.org"'
)
class
TestAnalyticsFormatInstances
(
TestCase
):
""" test format_instances method """
class
TestDataClass
(
object
):
""" Test class to generate objects for format_instances """
def
__init__
(
self
):
self
.
a_var
=
'aval'
self
.
b_var
=
'bval'
self
.
c_var
=
'cval'
@property
def
d_var
(
self
):
""" accessor to see if they work too """
return
'dval'
def
setUp
(
self
):
self
.
instances
=
[
self
.
TestDataClass
()
for
_
in
xrange
(
5
)]
def
test_format_instances_response
(
self
):
features
=
[
'a_var'
,
'c_var'
,
'd_var'
]
header
,
datarows
=
format_instances
(
self
.
instances
,
features
)
self
.
assertEqual
(
header
,
[
'a_var'
,
'c_var'
,
'd_var'
])
self
.
assertEqual
(
datarows
,
[[
'aval'
,
'cval'
,
'dval'
,
]
for
_
in
xrange
(
len
(
self
.
instances
))])
def
test_format_instances_response_noinstances
(
self
):
features
=
[
'a_var'
]
header
,
datarows
=
format_instances
([],
features
)
self
.
assertEqual
(
header
,
features
)
self
.
assertEqual
(
datarows
,
[])
def
test_format_instances_response_nofeatures
(
self
):
header
,
datarows
=
format_instances
(
self
.
instances
,
[])
self
.
assertEqual
(
header
,
[])
self
.
assertEqual
(
datarows
,
[[]
for
_
in
xrange
(
len
(
self
.
instances
))])
@raises
(
AttributeError
)
def
test_format_instances_response_nonexistantfeature
(
self
):
format_instances
(
self
.
instances
,
[
'robot_not_a_real_feature'
])
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