Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
force-bdss
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Adham Hashibon
force-bdss
Commits
34436a8e
Commit
34436a8e
authored
6 years ago
by
Stefano Borini
Browse files
Options
Downloads
Patches
Plain Diff
Changed data source to new design
parent
34f0baba
No related branches found
No related tags found
1 merge request
!130
Safer plugin import - 2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
force_bdss/data_sources/base_data_source_factory.py
+14
-18
14 additions, 18 deletions
force_bdss/data_sources/base_data_source_factory.py
force_bdss/data_sources/tests/test_base_data_source_factory.py
+49
-29
49 additions, 29 deletions
..._bdss/data_sources/tests/test_base_data_source_factory.py
with
63 additions
and
47 deletions
force_bdss/data_sources/base_data_source_factory.py
+
14
−
18
View file @
34436a8e
...
...
@@ -40,11 +40,11 @@ class BaseDataSourceFactory(ABCHasStrictTraits):
name
=
Str
()
#: The data source to be instantiated. Define this to your DataSource
data_source_class
=
Type
(
BaseDataSource
)
data_source_class
=
Type
(
BaseDataSource
,
allow_none
=
False
)
#: The model associated to the data source.
#: Define this to your DataSourceModel
model_class
=
Type
(
BaseDataSourceModel
)
model_class
=
Type
(
BaseDataSourceModel
,
allow_none
=
False
)
#: Reference to the plugin that carries this factory
#: This is automatically set by the system. you should not define it
...
...
@@ -59,7 +59,17 @@ class BaseDataSourceFactory(ABCHasStrictTraits):
self
.
model_class
=
self
.
get_model_class
()
self
.
name
=
self
.
get_name
()
identifier
=
self
.
get_identifier
()
self
.
id
=
factory_id
(
self
.
plugin
.
id
,
identifier
)
try
:
id
=
factory_id
(
self
.
plugin
.
id
,
identifier
)
except
ValueError
:
raise
ValueError
(
"
Invalid identifier {} returned by
"
"
{}.get_identifier()
"
.
format
(
identifier
,
self
.
__class__
.
__name__
)
)
self
.
id
=
id
def
get_data_source_class
(
self
):
"""
Must be reimplemented to return the DataSource class.
...
...
@@ -89,7 +99,7 @@ class BaseDataSourceFactory(ABCHasStrictTraits):
to be unique across the plugin data sources.
"""
raise
NotImplementedError
(
"
get_
name
was not implemented in factory {}
"
.
format
(
"
get_
identifier
was not implemented in factory {}
"
.
format
(
self
.
__class__
))
def
create_data_source
(
self
):
...
...
@@ -101,13 +111,6 @@ class BaseDataSourceFactory(ABCHasStrictTraits):
BaseDataSource
The specific instance of the generated DataSource
"""
if
self
.
data_source_class
is
None
:
msg
=
(
"
data_source_class cannot be None in {}. Either define
"
"
data_source_class or reimplement create_data_source on
"
"
your factory class.
"
.
format
(
self
.
__class__
.
__name__
))
log
.
error
(
msg
)
raise
RuntimeError
(
msg
)
return
self
.
data_source_class
(
self
)
def
create_model
(
self
,
model_data
=
None
):
...
...
@@ -130,11 +133,4 @@ class BaseDataSourceFactory(ABCHasStrictTraits):
if
model_data
is
None
:
model_data
=
{}
if
self
.
model_class
is
None
:
msg
=
(
"
model_class cannot be None in {}. Either define
"
"
model_class or reimplement create_model on your
"
"
factory class.
"
.
format
(
self
.
__class__
.
__name__
))
log
.
error
(
msg
)
raise
RuntimeError
(
msg
)
return
self
.
model_class
(
self
,
**
model_data
)
This diff is collapsed.
Click to expand it.
force_bdss/data_sources/tests/test_base_data_source_factory.py
+
49
−
29
View file @
34436a8e
import
unittest
from
traits.trait_errors
import
TraitError
from
force_bdss.data_sources.tests.test_base_data_source
import
DummyDataSource
from
force_bdss.data_sources.tests.test_base_data_source_model
import
\
DummyDataSourceModel
...
...
@@ -17,46 +19,64 @@ from force_bdss.data_sources.base_data_source_factory import \
class
DummyDataSourceFactory
(
BaseDataSourceFactory
):
id
=
"
foo
"
name
=
"
bar
"
def
create_data_source
(
self
):
pass
def
create_model
(
self
,
model_data
=
None
):
pass
def
get_identifier
(
self
):
return
"
foo
"
def
get_name
(
self
):
return
"
bar
"
class
DummyDataSourceFactoryFast
(
BaseDataSourceFactory
):
id
=
"
foo
"
def
get_model_class
(
self
):
return
DummyDataSourceModel
name
=
"
bar
"
model_class
=
DummyDataSourceModel
data_source_class
=
DummyDataSource
def
get_data_source_class
(
self
):
return
DummyDataSource
class
TestBaseDataSourceFactory
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
plugin
=
mock
.
Mock
(
spec
=
Plugin
,
id
=
"
pid
"
)
def
test_initialization
(
self
):
factory
=
DummyDataSourceFactory
(
mock
.
Mock
(
spec
=
P
lugin
)
)
self
.
assertEqual
(
factory
.
id
,
'
foo
'
)
factory
=
DummyDataSourceFactory
(
self
.
p
lugin
)
self
.
assertEqual
(
factory
.
id
,
'
pid.factory.
foo
'
)
self
.
assertEqual
(
factory
.
name
,
'
bar
'
)
def
test_fast_specification
(
self
):
factory
=
DummyDataSourceFactoryFast
(
mock
.
Mock
(
spec
=
Plugin
))
self
.
assertEqual
(
factory
.
model_class
,
DummyDataSourceModel
)
self
.
assertEqual
(
factory
.
data_source_class
,
DummyDataSource
)
self
.
assertIsInstance
(
factory
.
create_data_source
(),
DummyDataSource
)
self
.
assertIsInstance
(
factory
.
create_model
(),
DummyDataSourceModel
)
def
test_
fast_specification_errors
(
self
):
factory
=
DummyDataSourceFactory
Fast
(
mock
.
Mock
(
spec
=
Plugin
)
)
factory
.
model_class
=
None
factory
.
data_source_class
=
None
def
test_
initialization_errors_invalid_identifier
(
self
):
class
Broken
(
DummyDataSourceFactory
)
:
def
get_identifier
(
self
):
return
None
with
testfixtures
.
LogCapture
():
with
self
.
assertRaises
(
Runtim
eError
):
factory
.
create_data_source
(
)
with
self
.
assertRaises
(
Valu
eError
):
Broken
(
self
.
plugin
)
with
self
.
assertRaises
(
RuntimeError
):
factory
.
create_model
()
def
test_initialization_errors_invalid_name
(
self
):
class
Broken
(
DummyDataSourceFactory
):
def
get_name
(
self
):
return
None
with
testfixtures
.
LogCapture
():
with
self
.
assertRaises
(
TraitError
):
Broken
(
self
.
plugin
)
def
test_initialization_errors_invalid_model_class
(
self
):
class
Broken
(
DummyDataSourceFactory
):
def
get_model_class
(
self
):
return
None
with
testfixtures
.
LogCapture
():
with
self
.
assertRaises
(
TraitError
):
Broken
(
self
.
plugin
)
def
test_initialization_errors_invalid_data_source_class
(
self
):
class
Broken
(
DummyDataSourceFactory
):
def
get_data_source_class
(
self
):
return
None
with
testfixtures
.
LogCapture
():
with
self
.
assertRaises
(
TraitError
):
Broken
(
self
.
plugin
)
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