Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
PEDASI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Research Software Group
PEDASI
Commits
bfcd9884
Commit
bfcd9884
authored
6 years ago
by
James Graham
Browse files
Options
Downloads
Patches
Plain Diff
Add unmanaged MongoDB connector and tests -
#19
parent
b6d78946
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
datasources/connectors/mongo.py
+35
-0
35 additions, 0 deletions
datasources/connectors/mongo.py
datasources/tests/test_connectors.py
+58
-0
58 additions, 0 deletions
datasources/tests/test_connectors.py
with
93 additions
and
0 deletions
datasources/connectors/mongo.py
0 → 100644
+
35
−
0
View file @
bfcd9884
import
json
import
typing
import
pymongo
from
.base
import
DataSetConnector
,
DummyRequestsResponse
class
UnmanagedMongoConnector
(
DataSetConnector
):
"""
This connector handles requests for data from a MongoDB collection managed outside of PEDASI.
"""
def
__init__
(
self
,
location
:
str
,
api_key
:
typing
.
Optional
[
str
]
=
None
,
auth
:
typing
.
Optional
[
typing
.
Callable
]
=
None
,
**
kwargs
):
super
().
__init__
(
location
,
api_key
,
auth
,
**
kwargs
)
# TODO validate db url
host
,
database
,
collection
=
location
.
rsplit
(
'
/
'
,
2
)
self
.
_client
=
pymongo
.
MongoClient
(
host
)
self
.
_database
=
self
.
_client
[
database
]
self
.
_collection
=
self
.
_database
[
collection
]
def
get_response
(
self
,
params
:
typing
.
Optional
[
typing
.
Mapping
[
str
,
str
]]
=
None
):
"""
Select and return all contents of table.
"""
result
=
self
.
_collection
.
find
(
filter
=
params
)
return
DummyRequestsResponse
(
json
.
dumps
([
dict
(
row
)
for
row
in
result
],
default
=
str
),
200
,
content_type
=
'
application/json
'
)
This diff is collapsed.
Click to expand it.
datasources/tests/test_connectors.py
+
58
−
0
View file @
bfcd9884
...
@@ -143,3 +143,61 @@ class ConnectorUnmanagedSqlTest(TestCase):
...
@@ -143,3 +143,61 @@ class ConnectorUnmanagedSqlTest(TestCase):
for
record
in
result
:
for
record
in
result
:
self
.
assertEqual
(
'
datasources
'
,
record
[
'
app
'
])
self
.
assertEqual
(
'
datasources
'
,
record
[
'
app
'
])
class
UnmanagedMongoConnectorTest
(
TestCase
):
# Use Django migrations table as test data
location
=
'
mongodb://localhost/prov/django_migrations
'
def
_get_connection
(
self
)
->
BaseDataConnector
:
return
self
.
plugin
(
self
.
location
)
def
setUp
(
self
):
BaseDataConnector
.
load_plugins
(
'
datasources/connectors
'
)
self
.
plugin
=
BaseDataConnector
.
get_plugin
(
'
UnmanagedMongoConnector
'
)
def
test_get_plugin
(
self
):
self
.
assertIsNotNone
(
self
.
plugin
)
def
test_plugin_init
(
self
):
connection
=
self
.
_get_connection
()
self
.
assertEqual
(
connection
.
location
,
self
.
location
)
def
test_plugin_type
(
self
):
connection
=
self
.
_get_connection
()
self
.
assertFalse
(
connection
.
is_catalogue
)
def
test_plugin_get_data
(
self
):
connection
=
self
.
_get_connection
()
result
=
connection
.
get_data
()
self
.
assertEqual
(
list
,
type
(
result
))
self
.
assertLessEqual
(
1
,
len
(
result
))
first
=
result
[
0
]
self
.
assertEqual
(
dict
,
type
(
first
))
self
.
assertIn
(
'
id
'
,
first
)
self
.
assertIn
(
'
app
'
,
first
)
self
.
assertIn
(
'
name
'
,
first
)
self
.
assertIn
(
'
applied
'
,
first
)
def
test_plugin_get_data_query
(
self
):
connection
=
self
.
_get_connection
()
result
=
connection
.
get_data
(
params
=
{
'
app
'
:
'
datasources
'
})
self
.
assertEqual
(
list
,
type
(
result
))
self
.
assertLessEqual
(
1
,
len
(
result
))
first
=
result
[
0
]
self
.
assertEqual
(
dict
,
type
(
first
))
self
.
assertIn
(
'
id
'
,
first
)
self
.
assertIn
(
'
app
'
,
first
)
self
.
assertIn
(
'
name
'
,
first
)
self
.
assertIn
(
'
applied
'
,
first
)
for
record
in
result
:
self
.
assertEqual
(
'
datasources
'
,
record
[
'
app
'
])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment