diff --git a/HISTORY.rst b/HISTORY.rst index 68465be0..d8d64d69 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,12 @@ History ------- +4.6.0 (2016-04-10) +~~~~~~~~~~~~~~~~~~ + +- Adding REST methods to manage new types of whizzml resources: scripts, + executions and libraries. + 4.5.2 (2016-03-24) ~~~~~~~~~~~~~~~~~~ diff --git a/bigml/__init__.py b/bigml/__init__.py index d04f92aa..52fde385 100644 --- a/bigml/__init__.py +++ b/bigml/__init__.py @@ -1 +1 @@ -__version__ = '4.5.2' +__version__ = '4.6.0' diff --git a/bigml/api.py b/bigml/api.py index aae450b7..ba6ca80a 100644 --- a/bigml/api.py +++ b/bigml/api.py @@ -59,6 +59,9 @@ from bigml.logistichandler import LogisticRegressionHandler from bigml.associationhandler import AssociationHandler from bigml.associationsethandler import AssociationSetHandler +from bigml.scripthandler import ScriptHandler +from bigml.executionhandler import ExecutionHandler +from bigml.libraryhandler import LibraryHandler # Repeating constants and functions for backwards compatibility @@ -83,8 +86,9 @@ SAMPLE_PATH, SAMPLE_RE, CORRELATION_PATH, CORRELATION_RE, STATISTICAL_TEST_PATH, STATISTICAL_TEST_RE, LOGISTIC_REGRESSION_PATH, LOGISTIC_REGRESSION_RE, ASSOCIATION_PATH, - ASSOCIATION_RE, ASSOCIATION_SET_PATH, ASSOCIATION_SET_RE) - + ASSOCIATION_RE, ASSOCIATION_SET_PATH, ASSOCIATION_SET_RE, + SCRIPT_PATH, SCRIPT_RE, + EXECUTION_PATH, EXECUTION_RE, LIBRARY_PATH, LIBRARY_RE) from bigml.resourcehandler import ( get_resource, get_resource_type, check_resource_type, get_source_id, @@ -94,7 +98,8 @@ get_batch_anomaly_score_id, get_resource_id, resource_is_ready, get_status, check_resource, http_ok, get_project_id, get_sample_id, get_correlation_id, get_statistical_test_id, get_logistic_regression_id, - get_association_id, get_association_set_id) + get_association_id, get_association_set_id, get_script_id, + get_execution_id, get_library_id) # Map status codes to labels @@ -120,7 +125,8 @@ def count(listing): return listing['meta']['query_total'] -class BigML(AssociationSetHandler, AssociationHandler, +class BigML(LibraryHandler, ExecutionHandler, ScriptHandler, + AssociationSetHandler, AssociationHandler, LogisticRegressionHandler, StatisticalTestHandler, CorrelationHandler, SampleHandler, ProjectHandler, @@ -190,6 +196,9 @@ def __init__(self, username=None, api_key=None, dev_mode=False, LogisticRegressionHandler.__init__(self) AssociationHandler.__init__(self) AssociationSetHandler.__init__(self) + ScriptHandler.__init__(self) + ExecutionHandler.__init__(self) + LibraryHandler.__init__(self) self.getters = {} for resource_type in RESOURCE_RE: diff --git a/bigml/constants.py b/bigml/constants.py index 2e658236..42752263 100644 --- a/bigml/constants.py +++ b/bigml/constants.py @@ -42,6 +42,9 @@ LOGISTIC_REGRESSION_PATH = 'logisticregression' ASSOCIATION_PATH = 'association' ASSOCIATION_SET_PATH = 'associationset' +SCRIPT_PATH = 'script' +EXECUTION_PATH = 'execution' +LIBRARY_PATH = 'library' # Resource Ids patterns @@ -81,6 +84,12 @@ (ASSOCIATION_PATH, ID_PATTERN, ASSOCIATION_PATH, SHARED_PATTERN)) ASSOCIATION_SET_RE = re.compile(r'^%s/%s$' % \ (ASSOCIATION_SET_PATH, ID_PATTERN)) +SCRIPT_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \ + (SCRIPT_PATH, ID_PATTERN, SCRIPT_PATH, SHARED_PATTERN)) +EXECUTION_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \ + (EXECUTION_PATH, ID_PATTERN, EXECUTION_PATH, SHARED_PATTERN)) +LIBRARY_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \ + (LIBRARY_PATH, ID_PATTERN, LIBRARY_PATH, SHARED_PATTERN)) RESOURCE_RE = { SOURCE_PATH: SOURCE_RE, @@ -102,7 +111,10 @@ STATISTICAL_TEST_PATH: STATISTICAL_TEST_RE, LOGISTIC_REGRESSION_PATH: LOGISTIC_REGRESSION_RE, ASSOCIATION_PATH: ASSOCIATION_RE, - ASSOCIATION_SET_PATH: ASSOCIATION_SET_RE} + ASSOCIATION_SET_PATH: ASSOCIATION_SET_RE, + SCRIPT_PATH: SCRIPT_RE, + EXECUTION_PATH: EXECUTION_RE, + LIBRARY_PATH: LIBRARY_RE} RENAMED_RESOURCES = { diff --git a/bigml/executionhandler.py b/bigml/executionhandler.py new file mode 100644 index 00000000..2477fb98 --- /dev/null +++ b/bigml/executionhandler.py @@ -0,0 +1,134 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Base class for whizzml script executions' REST calls + + https://bigml.com/developers/executions + +""" + +try: + import simplejson as json +except ImportError: + import json + + +from bigml.resourcehandler import ResourceHandler +from bigml.resourcehandler import (check_resource_type, + get_execution_id, get_resource_type, + get_script_id, check_resource) +from bigml.constants import (EXECUTION_PATH, SCRIPT_PATH, + TINY_RESOURCE) + + +class ExecutionHandler(ResourceHandler): + """This class is used by the BigML class as + a mixin that provides the executions' REST calls. It should not + be instantiated independently. + + """ + def __init__(self): + """Initializes the ExecutionHandler. This class is intended to be + used as a mixin on ResourceHandler, that inherits its + attributes and basic method from BigMLConnection, and must not be + instantiated independently. + + """ + self.execution_url = self.url + EXECUTION_PATH + + def create_execution(self, origin_resource, args=None, + wait_time=3, retries=10): + """Creates an execution from a `script` or a list of `scripts`. + + """ + + create_args = {} + if args is not None: + create_args.update(args) + + if isinstance(origin_resource, basestring): + # single script + scripts = [origin_resource] + else: + scripts = origin_resource + + script_ids = [get_script_id(script) for script in scripts] + if all([get_resource_type(script_id) == SCRIPT_PATH for + script_id in script_ids]): + for script in scripts: + check_resource(script, + query_string=TINY_RESOURCE, + wait_time=wait_time, retries=retries, + raise_on_error=True, api=self) + else: + raise Exception("A script id or a list of them is needed to create" + " a script execution. %s found." % + get_resource_type(origin_resource)) + + if len(scripts) > 1: + create_args.update({ + "scripts": script_ids}) + else: + create_args.update({ + "script": script_ids[0]}) + + body = json.dumps(create_args) + return self._create(self.execution_url, body) + + def get_execution(self, execution, query_string=''): + """Retrieves an execution. + + The execution parameter should be a string containing the + execution id or the dict returned by create_execution. + As execution is an evolving object that is processed + until it reaches the FINISHED or FAULTY state, the function will + return a dict that encloses the execution contents and state info + available at the time it is called. + """ + check_resource_type(execution, EXECUTION_PATH, + message="An execution id is needed.") + execution_id = get_execution_id(execution) + if execution_id: + return self._get("%s%s" % (self.url, execution_id), + query_string=query_string) + + def list_executions(self, query_string=''): + """Lists all your executions. + + """ + return self._list(self.execution_url, query_string) + + def update_execution(self, execution, changes): + """Updates an execution. + + """ + check_resource_type(execution, EXECUTION_PATH, + message="An execution id is needed.") + execution_id = get_execution_id(execution) + if execution_id: + body = json.dumps(changes) + return self._update("%s%s" % (self.url, execution_id), body) + + def delete_execution(self, execution): + """Deletes an execution. + + """ + check_resource_type(execution, EXECUTION_PATH, + message="An execution id is needed.") + execution_id = get_execution_id(execution) + if execution_id: + return self._delete("%s%s" % (self.url, execution_id)) diff --git a/bigml/fields.py b/bigml/fields.py index 4d0063d9..f1ef507b 100644 --- a/bigml/fields.py +++ b/bigml/fields.py @@ -44,6 +44,7 @@ import sys import json + from bigml.util import invert_dictionary, python_map_type, find_locale from bigml.util import DEFAULT_LOCALE from bigml.api import get_resource_type @@ -588,7 +589,6 @@ def new_fields_structure(self, csv_attributes_file=None, self.field_id(int(field_attributes[0])) new_fields_structure[field_id] = \ dict(zip(headers, field_attributes[1: 6])) - except ValueError: raise ValueError("The first column should contain either the" " column or ID of the fields. Failed to find" diff --git a/bigml/libraryhandler.py b/bigml/libraryhandler.py new file mode 100644 index 00000000..b1089d28 --- /dev/null +++ b/bigml/libraryhandler.py @@ -0,0 +1,139 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Base class for whizzml libraries' REST calls + + https://bigml.com/developers/libraries + +""" + +try: + import simplejson as json +except ImportError: + import json +import os + +from bigml.resourcehandler import ResourceHandler +from bigml.resourcehandler import (check_resource_type, + get_library_id, get_resource_type, + check_resource) +from bigml.constants import LIBRARY_PATH, TINY_RESOURCE + + +class LibraryHandler(ResourceHandler): + """This class is used by the BigML class as + a mixin that provides the whizzml libraries' REST calls. It should not + be instantiated independently. + + """ + def __init__(self): + """Initializes the LibraryHandler. This class is intended to be + used as a mixin on ResourceHandler, that inherits its + attributes and basic method from BigMLConnection, and must not be + instantiated independently. + + """ + self.library_url = self.url + LIBRARY_PATH + + def create_library(self, source_code=None, args=None, + wait_time=3, retries=10): + """Creates a whizzml library from its source code. The `source_code` + parameter can be a: + {library ID}: the ID for an existing whizzml library + {path}: the path to a file containing the source code + {string} : the string containing the source code for the library + + """ + create_args = {} + if args is not None: + create_args.update(args) + + if source_code is None: + raise Exception('A valid code string' + ' or a library id must be provided.') + resource_type = get_resource_type(source_code) + if resource_type == LIBRARY_PATH: + library_id = get_library_id(source_code) + if library_id: + check_resource(library_id, + query_string=TINY_RESOURCE, + wait_time=wait_time, retries=retries, + raise_on_error=True, api=self) + create_args.update({ + "origin": library_id}) + elif isinstance(source_code, basestring): + try: + if os.path.exists(source_code): + with open(source_code) as code_file: + source_code = code_file.read() + except IOError: + raise IOError("Could not open the source code file %s." % + source_code) + create_args.update({ + "source_code": source_code}) + else: + raise Exception("A library id or a valid source code" + " is needed to create a" + " library. %s found." % resource_type) + + + body = json.dumps(create_args) + return self._create(self.library_url, body) + + def get_library(self, library, query_string=''): + """Retrieves a library. + + The library parameter should be a string containing the + library id or the dict returned by create_script. + As library is an evolving object that is processed + until it reaches the FINISHED or FAULTY state, the function will + return a dict that encloses the library content and state info + available at the time it is called. + """ + check_resource_type(library, LIBRARY_PATH, + message="A library id is needed.") + library_id = get_library_id(library) + if library_id: + return self._get("%s%s" % (self.url, library_id), + query_string=query_string) + + def list_libraries(self, query_string=''): + """Lists all your libraries. + + """ + return self._list(self.library_url, query_string) + + def update_library(self, library, changes): + """Updates a library. + + """ + check_resource_type(library, LIBRARY_PATH, + message="A library id is needed.") + library_id = get_library_id(library) + if library_id: + body = json.dumps(changes) + return self._update("%s%s" % (self.url, library_id), body) + + def delete_library(self, library): + """Deletes a library. + + """ + check_resource_type(library, LIBRARY_PATH, + message="A library id is needed.") + library_id = get_library_id(library) + if library_id: + return self._delete("%s%s" % (self.url, library_id)) diff --git a/bigml/logistic.py b/bigml/logistic.py index e7a5d19d..cc19fded 100644 --- a/bigml/logistic.py +++ b/bigml/logistic.py @@ -134,7 +134,9 @@ def __init__(self, logistic_regression, api=None): try: self.dataset_field_types = logistic_regression.get( "dataset_field_types", {}) - objective_field = logistic_regression['objective_fields'] + objective_field = logistic_regression['objective_fields'] if \ + logistic_regression['objective_fields'] else \ + logistic_regression['objective_field'] except KeyError: raise ValueError("Failed to find the logistic regression expected " "JSON structure. Check your arguments.") @@ -219,7 +221,7 @@ def predict(self, input_data, by_name=True): probabilities = {} total = 0 - for category in self.categories[self.objective_id]: + for category in self.coefficients.keys(): coefficients = self.coefficients[category] probabilities[category] = self.category_probability( input_data, unique_terms, coefficients) diff --git a/bigml/resourcehandler.py b/bigml/resourcehandler.py index e72dd3f0..1cde5be7 100644 --- a/bigml/resourcehandler.py +++ b/bigml/resourcehandler.py @@ -28,11 +28,29 @@ from bigml.bigmlconnection import BigMLConnection + NO_QS = [c.EVALUATION_RE, c.PREDICTION_RE, c.BATCH_PREDICTION_RE, c.CENTROID_RE, c.BATCH_CENTROID_RE, c.ANOMALY_SCORE_RE, c.BATCH_ANOMALY_SCORE_RE, c.PROJECT_RE, c.ASSOCIATION_SET_RE] +# Resource status codes +WAITING = 0 +QUEUED = 1 +STARTED = 2 +IN_PROGRESS = 3 +SUMMARIZED = 4 +FINISHED = 5 +UPLOADING = 6 +FAULTY = -1 +UNKNOWN = -2 +RUNNABLE = -3 + + +# Minimum query string to get model fields +TINY_RESOURCE = "full=false" + + def get_resource_type(resource): """Returns the associated resource type for a resource @@ -244,6 +262,27 @@ def get_association_set_id(association_set): return get_resource(c.ASSOCIATION_SET_PATH, association_set) +def get_script_id(script): + """Returns a script/id. + + """ + return get_resource(c.SCRIPT_PATH, script) + + +def get_execution_id(execution): + """Returns a execution/id. + + """ + return get_resource(c.EXECUTION_PATH, execution) + + +def get_library_id(library): + """Returns a library/id. + + """ + return get_resource(c.LIBRARY_PATH, library) + + def get_resource_id(resource): """Returns the resource id if it falls in one of the registered types diff --git a/bigml/scripthandler.py b/bigml/scripthandler.py new file mode 100644 index 00000000..40348edc --- /dev/null +++ b/bigml/scripthandler.py @@ -0,0 +1,142 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Base class for whizzml script' REST calls + + https://bigml.com/developers/scripts + +""" + +import os +try: + import simplejson as json +except ImportError: + import json + + +from bigml.resourcehandler import ResourceHandler +from bigml.resourcehandler import (check_resource_type, + get_script_id, get_resource_type, + get_dataset_id, check_resource) +from bigml.constants import (SCRIPT_PATH, DATASET_PATH, + TINY_RESOURCE) +from bigml.util import is_url + + +class ScriptHandler(ResourceHandler): + """This class is used by the BigML class as + a mixin that provides the whizzml script' REST calls. It should not + be instantiated independently. + + """ + def __init__(self): + """Initializes the ScriptHandler. This class is intended to be + used as a mixin on ResourceHandler, that inherits its + attributes and basic method from BigMLConnection, and must not be + instantiated independently. + + """ + self.script_url = self.url + SCRIPT_PATH + + def create_script(self, source_code=None, args=None, + wait_time=3, retries=10): + """Creates a whizzml script from its source code. The `source_code` + parameter can be a: + {script ID}: the ID for an existing whizzml script + {path}: the path to a file containing the source code + {string} : the string containing the source code for the script + + """ + create_args = {} + if args is not None: + create_args.update(args) + + if source_code is None: + raise Exception('A valid code string' + ' or a script id must be provided.') + resource_type = get_resource_type(source_code) + if resource_type == SCRIPT_PATH: + script_id = get_script_id(source_code) + if script_id: + check_resource(script_id, + query_string=TINY_RESOURCE, + wait_time=wait_time, retries=retries, + raise_on_error=True, api=self) + create_args.update({ + "origin": script_id}) + elif isinstance(source_code, basestring): + try: + if os.path.exists(source_code): + with open(source_code) as code_file: + source_code = code_file.read() + except IOError: + raise IOError("Could not open the source code file %s." % + source_code) + create_args.update({ + "source_code": source_code}) + else: + raise Exception("A script id or a valid source code" + " is needed to create a" + " script. %s found." % resource_type) + + + body = json.dumps(create_args) + return self._create(self.script_url, body) + + def get_script(self, script, query_string=''): + """Retrieves a script. + + The script parameter should be a string containing the + script id or the dict returned by create_script. + As script is an evolving object that is processed + until it reaches the FINISHED or FAULTY state, the function will + return a dict that encloses the script content and state info + available at the time it is called. + """ + check_resource_type(script, SCRIPT_PATH, + message="A script id is needed.") + script_id = get_script_id(script) + if script_id: + return self._get("%s%s" % (self.url, script_id), + query_string=query_string) + + def list_scripts(self, query_string=''): + """Lists all your scripts. + + """ + return self._list(self.script_url, query_string) + + def update_script(self, script, changes): + """Updates a script. + + """ + check_resource_type(script, SCRIPT_PATH, + message="A script id is needed.") + script_id = get_script_id(script) + if script_id: + body = json.dumps(changes) + return self._update("%s%s" % (self.url, script_id), body) + + def delete_script(self, script): + """Deletes a script. + + """ + check_resource_type(script, SCRIPT_PATH, + message="A script id is needed.") + script_id = get_script_id(script) + if script_id: + return self._delete("%s%s" % (self.url, script_id)) diff --git a/bigml/tests/compare_predictions_steps.py b/bigml/tests/compare_predictions_steps.py index f4df9389..3d47f474 100644 --- a/bigml/tests/compare_predictions_steps.py +++ b/bigml/tests/compare_predictions_steps.py @@ -1,3 +1,21 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2012-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + import json import os from world import world, res_filename diff --git a/bigml/tests/create_anomaly_steps.py b/bigml/tests/create_anomaly_steps.py index 78416275..456253cb 100644 --- a/bigml/tests/create_anomaly_steps.py +++ b/bigml/tests/create_anomaly_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2014-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import time import json import os diff --git a/bigml/tests/create_cluster_steps.py b/bigml/tests/create_cluster_steps.py index b0873d84..5a3e53b2 100644 --- a/bigml/tests/create_cluster_steps.py +++ b/bigml/tests/create_cluster_steps.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- #!/usr/bin/env python # -# Copyright 2012 BigML +# Copyright 2012-2015 BigML # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -105,7 +105,7 @@ def cluster_from_shared_url(step): #@step(r'I check the cluster status using the model\'s shared key') def cluster_from_shared_key(step): - + username = os.environ.get("BIGML_USERNAME") world.cluster = world.api.get_cluster(world.cluster['resource'], shared_username=username, shared_api_key=world.sharing_key) diff --git a/bigml/tests/create_correlation_steps.py b/bigml/tests/create_correlation_steps.py index db35bcca..777ea507 100644 --- a/bigml/tests/create_correlation_steps.py +++ b/bigml/tests/create_correlation_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import time import json import os diff --git a/bigml/tests/create_dataset_steps.py b/bigml/tests/create_dataset_steps.py index f2260ba3..35766931 100644 --- a/bigml/tests/create_dataset_steps.py +++ b/bigml/tests/create_dataset_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2012-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import time import json from datetime import datetime, timedelta diff --git a/bigml/tests/create_ensemble_steps.py b/bigml/tests/create_ensemble_steps.py index 0ded93ba..0a13df73 100644 --- a/bigml/tests/create_ensemble_steps.py +++ b/bigml/tests/create_ensemble_steps.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- #!/usr/bin/env python # -# Copyright 2012 BigML +# Copyright 2012-2015 BigML # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain diff --git a/bigml/tests/create_execution_steps.py b/bigml/tests/create_execution_steps.py new file mode 100644 index 00000000..504551b2 --- /dev/null +++ b/bigml/tests/create_execution_steps.py @@ -0,0 +1,107 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import time +import json +import os +from datetime import datetime, timedelta +from world import world +from nose.tools import assert_equal + +from bigml.api import HTTP_CREATED +from bigml.api import HTTP_ACCEPTED +from bigml.api import FINISHED +from bigml.api import FAULTY +from bigml.api import get_status + +from read_execution_steps import i_get_the_execution + + +#@step(r'the script id is correct and the value of "(.*)" is "(.*)"') +def the_execution_and_attributes(step, param, param_value): + assert world.script['resource'] == world.execution['script'] + res_param_value = world.execution[param] + if res_param_value == param_value: + assert True + else: + assert False, ("The execution %s is %s " + "and the expected %s is %s" % + (param, param_value, param, param_value)) + + +#@step(r'the script ids are correct and the value of "(.*)" is "(.*)"') +def the_execution_ids_and_attributes(step, number_of_scripts, + param, param_value): + scripts = world.scripts[-number_of_scripts:] + assert scripts == world.execution['scripts'] + res_param_value = world.execution[param] + if res_param_value == param_value: + assert True + else: + assert False, ("The execution %s is %s " + "and the expected %s is %s" % + (param, param_value, param, param_value)) + +#@step(r'I create a whizzml execution from an existing script"$') +def i_create_an_execution(step): + resource = world.api.create_execution(world.script['resource']) + world.status = resource['code'] + assert_equal(world.status, HTTP_CREATED) + world.location = resource['location'] + world.execution = resource['object'] + world.executions.append(resource['resource']) + + +#@step(r'I create a whizzml execution from the last two scripts$') +def i_create_an_execution_from_list(step, number_of_scripts=2): + scripts = world.scripts[-number_of_scripts:] + resource = world.api.create_execution(scripts) + world.status = resource['code'] + assert_equal(world.status, HTTP_CREATED) + world.location = resource['location'] + world.execution = resource['object'] + world.executions.append(resource['resource']) + + +#@step(r'I update the execution with "(.*)", "(.*)"$') +def i_update_an_execution(step, param, param_value): + resource = world.api.update_execution(world.execution['resource'], + {param: param_value}) + world.status = resource['code'] + assert_equal(world.status, HTTP_ACCEPTED) + world.location = resource['location'] + world.execution = resource['object'] + + +#@step(r'I wait until the execution status code is either (\d) or (-\d) less than (\d+)') +def wait_until_execution_status_code_is(step, code1, code2, secs): + start = datetime.utcnow() + execution_id = world.execution['resource'] + i_get_the_execution(step, execution_id) + status = get_status(world.execution) + while (status['code'] != int(code1) and + status['code'] != int(code2)): + time.sleep(3) + assert datetime.utcnow() - start < timedelta(seconds=int(secs)) + i_get_the_execution(step, execution_id) + status = get_status(world.execution) + assert status['code'] == int(code1) + + +#@step(r'I wait until the script is ready less than (\d+)') +def the_execution_is_finished(step, secs): + wait_until_execution_status_code_is(step, FINISHED, FAULTY, secs) diff --git a/bigml/tests/create_library_steps.py b/bigml/tests/create_library_steps.py new file mode 100644 index 00000000..9fa8f080 --- /dev/null +++ b/bigml/tests/create_library_steps.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import time +import json +import os +from datetime import datetime, timedelta +from world import world + +from bigml.api import HTTP_CREATED +from bigml.api import HTTP_ACCEPTED +from bigml.api import FINISHED +from bigml.api import FAULTY +from bigml.api import get_status + +from read_library_steps import i_get_the_library + + +#@step(r'the library code is "(.*)" and the value of "(.*)" is "(.*)"') +def the_library_code_and_attributes(step, source_code, param, param_value): + res_param_value = world.library[param] + if res_param_value == param_value: + assert True + else: + assert False, ("The library %s is %s " + "and the expected %s is %s" % + (param, param_value, param, param_value)) + + +#@step(r'I create a whizzml library from a excerpt of code "(.*)"$') +def i_create_a_library(step, source_code): + resource = world.api.create_library(source_code) + world.status = resource['code'] + assert world.status == HTTP_CREATED + world.location = resource['location'] + world.library = resource['object'] + world.libraries.append(resource['resource']) + + +#@step(r'I update the library with "(.*)", "(.*)"$') +def i_update_a_library(step, param, param_value): + resource = world.api.update_library(world.library['resource'], + {param: param_value}) + world.status = resource['code'] + assert world.status == HTTP_ACCEPTED + world.location = resource['location'] + world.library = resource['object'] + + +#@step(r'I wait until the library status code is either (\d) or (-\d) less than (\d+)') +def wait_until_library_status_code_is(step, code1, code2, secs): + start = datetime.utcnow() + library_id = world.library['resource'] + i_get_the_library(step, library_id) + status = get_status(world.library) + while (status['code'] != int(code1) and + status['code'] != int(code2)): + time.sleep(3) + assert datetime.utcnow() - start < timedelta(seconds=int(secs)) + i_get_the_library(step, library_id) + status = get_status(world.library) + assert status['code'] == int(code1) + + +#@step(r'I wait until the library is ready less than (\d+)') +def the_library_is_finished(step, secs): + wait_until_library_status_code_is(step, FINISHED, FAULTY, secs) diff --git a/bigml/tests/create_multimodel_steps.py b/bigml/tests/create_multimodel_steps.py index c2d3902c..01c8a6cc 100644 --- a/bigml/tests/create_multimodel_steps.py +++ b/bigml/tests/create_multimodel_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2014-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + from world import world #@step(r'I store the dataset id in a list') @@ -13,4 +30,4 @@ def i_check_model_datasets_and_datasets_ids(step): assert False, ("The model contains only %s " "and the dataset ids are %s" % (",".join(model['datasets']), - ",".join(world.dataset_ids))) + ",".join(world.dataset_ids))) diff --git a/bigml/tests/create_project_steps.py b/bigml/tests/create_project_steps.py index 2fdf75bc..c017b182 100644 --- a/bigml/tests/create_project_steps.py +++ b/bigml/tests/create_project_steps.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- #!/usr/bin/env python # -# Copyright 2014 BigML +# Copyright 2014-2015 BigML # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain diff --git a/bigml/tests/create_sample_steps.py b/bigml/tests/create_sample_steps.py index cb72341b..8971ac5a 100644 --- a/bigml/tests/create_sample_steps.py +++ b/bigml/tests/create_sample_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import time import json import os @@ -58,7 +75,7 @@ def wait_until_sample_status_code_is(step, code1, code2, secs): i_get_the_sample(step, sample_id) status = get_status(world.sample) assert status['code'] == int(code1) - + #@step(r'I wait until the sample is ready less than (\d+)') def the_sample_is_finished_in_less_than(step, secs): diff --git a/bigml/tests/create_script_steps.py b/bigml/tests/create_script_steps.py new file mode 100644 index 00000000..554ca439 --- /dev/null +++ b/bigml/tests/create_script_steps.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import time +import json +import os +from datetime import datetime, timedelta +from world import world + +from bigml.api import HTTP_CREATED +from bigml.api import HTTP_ACCEPTED +from bigml.api import FINISHED +from bigml.api import FAULTY +from bigml.api import get_status + +from read_script_steps import i_get_the_script + + +#@step(r'the script code is "(.*)" and the value of "(.*)" is "(.*)"') +def the_script_code_and_attributes(step, source_code, param, param_value): + res_param_value = world.script[param] + if res_param_value == param_value: + assert True + else: + assert False, ("The script %s is %s " + "and the expected %s is %s" % + (param, param_value, param, param_value)) + + +#@step(r'I create a whizzml script from a excerpt of code "(.*)"$') +def i_create_a_script(step, source_code): + resource = world.api.create_script(source_code) + world.status = resource['code'] + assert world.status == HTTP_CREATED + world.location = resource['location'] + world.script = resource['object'] + world.scripts.append(resource['resource']) + + +#@step(r'I update the script with "(.*)", "(.*)"$') +def i_update_a_script(step, param, param_value): + resource = world.api.update_script(world.script['resource'], + {param: param_value}) + world.status = resource['code'] + assert world.status == HTTP_ACCEPTED + world.location = resource['location'] + world.script = resource['object'] + + +#@step(r'I wait until the script status code is either (\d) or (-\d) less than (\d+)') +def wait_until_script_status_code_is(step, code1, code2, secs): + start = datetime.utcnow() + script_id = world.script['resource'] + i_get_the_script(step, script_id) + status = get_status(world.script) + while (status['code'] != int(code1) and + status['code'] != int(code2)): + time.sleep(3) + assert datetime.utcnow() - start < timedelta(seconds=int(secs)) + i_get_the_script(step, script_id) + status = get_status(world.script) + assert status['code'] == int(code1) + + +#@step(r'I wait until the script is ready less than (\d+)') +def the_script_is_finished(step, secs): + wait_until_script_status_code_is(step, FINISHED, FAULTY, secs) diff --git a/bigml/tests/create_statistical_tst_steps.py b/bigml/tests/create_statistical_tst_steps.py index f956dfb8..1ba8fc0c 100644 --- a/bigml/tests/create_statistical_tst_steps.py +++ b/bigml/tests/create_statistical_tst_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import time import json import os diff --git a/bigml/tests/delete_project_steps.py b/bigml/tests/delete_project_steps.py index 386607c7..a9660785 100644 --- a/bigml/tests/delete_project_steps.py +++ b/bigml/tests/delete_project_steps.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- #!/usr/bin/env python # -# Copyright 2014 BigML +# Copyright 2014-2015 BigML # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain diff --git a/bigml/tests/inspect_model_steps.py b/bigml/tests/inspect_model_steps.py index 6c604066..e1fa06b8 100644 --- a/bigml/tests/inspect_model_steps.py +++ b/bigml/tests/inspect_model_steps.py @@ -65,6 +65,12 @@ def i_check_the_model_summary_with(step, file): output = io.BytesIO() world.local_model.summarize(out=output) world.output = output.getvalue() + if not os.path.exists('./tmp'): + os.mkdir('./tmp') + with open('./tmp/summary.txt', "w") as rules_file: + rules_file.write(world.output) + + i_check_if_the_output_is_like_expected_file(step, file) diff --git a/bigml/tests/read_anomaly_steps.py b/bigml/tests/read_anomaly_steps.py index 1ba72faf..4c065ebd 100644 --- a/bigml/tests/read_anomaly_steps.py +++ b/bigml/tests/read_anomaly_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2014-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import os from world import world @@ -9,4 +26,3 @@ def i_get_the_anomaly(step, anomaly): world.status = resource['code'] assert world.status == HTTP_OK world.anomaly = resource['object'] - diff --git a/bigml/tests/read_batch_prediction_steps.py b/bigml/tests/read_batch_prediction_steps.py index fd70646c..857e9dbe 100644 --- a/bigml/tests/read_batch_prediction_steps.py +++ b/bigml/tests/read_batch_prediction_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import os from world import world diff --git a/bigml/tests/read_cluster_steps.py b/bigml/tests/read_cluster_steps.py index 3b1f28fe..1af50065 100644 --- a/bigml/tests/read_cluster_steps.py +++ b/bigml/tests/read_cluster_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import os from world import world diff --git a/bigml/tests/read_dataset_steps.py b/bigml/tests/read_dataset_steps.py index 8d263d01..eebfcd89 100644 --- a/bigml/tests/read_dataset_steps.py +++ b/bigml/tests/read_dataset_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2012-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import json from world import world diff --git a/bigml/tests/read_ensemble_steps.py b/bigml/tests/read_ensemble_steps.py index f8ffc334..382183ae 100644 --- a/bigml/tests/read_ensemble_steps.py +++ b/bigml/tests/read_ensemble_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2012-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import os from world import world @@ -9,4 +26,3 @@ def i_get_the_ensemble(step, ensemble): world.status = resource['code'] assert world.status == HTTP_OK world.ensemble = resource['object'] - diff --git a/bigml/tests/read_evaluation_steps.py b/bigml/tests/read_evaluation_steps.py index 2721b8d8..36abb890 100644 --- a/bigml/tests/read_evaluation_steps.py +++ b/bigml/tests/read_evaluation_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2012-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import os from world import world @@ -9,4 +26,3 @@ def i_get_the_evaluation(step, evaluation): world.status = resource['code'] assert world.status == HTTP_OK world.evaluation = resource['object'] - diff --git a/bigml/tests/read_execution_steps.py b/bigml/tests/read_execution_steps.py new file mode 100644 index 00000000..971f05a8 --- /dev/null +++ b/bigml/tests/read_execution_steps.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from world import world +from bigml.api import HTTP_OK + +#@step(r'I get the execution "(.*)"') +def i_get_the_execution(step, resource): + resource = world.api.get_execution(resource) + world.status = resource['code'] + assert world.status == HTTP_OK + world.execution = resource['object'] diff --git a/bigml/tests/read_library_steps.py b/bigml/tests/read_library_steps.py new file mode 100644 index 00000000..24cf9be9 --- /dev/null +++ b/bigml/tests/read_library_steps.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from world import world +from bigml.api import HTTP_OK + +#@step(r'I get the library "(.*)"') +def i_get_the_library(step, resource): + resource = world.api.get_library(resource) + world.status = resource['code'] + assert world.status == HTTP_OK + world.library = resource['object'] diff --git a/bigml/tests/read_model_steps.py b/bigml/tests/read_model_steps.py index 56bd3c7a..e3a9f2a2 100644 --- a/bigml/tests/read_model_steps.py +++ b/bigml/tests/read_model_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2012-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import os from world import world @@ -10,6 +27,7 @@ def i_get_the_model(step, model): assert world.status == HTTP_OK world.model = resource['object'] + #@step(r'I get the logistic regression model "(.*)"') def i_get_the_logistic_model(step, model): resource = world.api.get_logistic_regression(model) diff --git a/bigml/tests/read_prediction_steps.py b/bigml/tests/read_prediction_steps.py index 0e3dc653..d662b5bf 100644 --- a/bigml/tests/read_prediction_steps.py +++ b/bigml/tests/read_prediction_steps.py @@ -1,3 +1,20 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2012-2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import os from world import world from bigml.api import HTTP_OK diff --git a/bigml/tests/read_project_steps.py b/bigml/tests/read_project_steps.py index 80e08f80..39089e2a 100644 --- a/bigml/tests/read_project_steps.py +++ b/bigml/tests/read_project_steps.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- #!/usr/bin/env python # -# Copyright 2014 BigML +# Copyright 2014-2015 BigML # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain diff --git a/bigml/tests/read_script_steps.py b/bigml/tests/read_script_steps.py new file mode 100644 index 00000000..d9cf22f7 --- /dev/null +++ b/bigml/tests/read_script_steps.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from world import world +from bigml.api import HTTP_OK + +#@step(r'I get the script "(.*)"') +def i_get_the_script(step, resource): + resource = world.api.get_script(resource) + world.status = resource['code'] + assert world.status == HTTP_OK + world.script = resource['object'] diff --git a/bigml/tests/read_source_steps.py b/bigml/tests/read_source_steps.py index 702bc9de..4c2c1514 100644 --- a/bigml/tests/read_source_steps.py +++ b/bigml/tests/read_source_steps.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- #!/usr/bin/env python # -# Copyright 2012 BigML +# Copyright 2012-2015 BigML # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain diff --git a/bigml/tests/test_05_compare_predictions.py b/bigml/tests/test_05_compare_predictions.py index 712ff72f..8fedcba1 100644 --- a/bigml/tests/test_05_compare_predictions.py +++ b/bigml/tests/test_05_compare_predictions.py @@ -257,11 +257,11 @@ def test_scenario5(self): Examples: | data | time_1 | time_2 | time_3 | options | data_input | centroid | distance | - | ../data/iris.csv | 20 | 20 | 30 | {"summary_fields": ["sepal width"]} |{"petal length": 1, "petal width": 1, "sepal length": 1, "species": "Iris-setosa"} | Cluster 3 | 0.7155571246307187 | + | ../data/iris.csv | 20 | 20 | 30 | {"summary_fields": ["sepal width"]} |{"petal length": 1, "petal width": 1, "sepal length": 1, "species": "Iris-setosa"} | Cluster 3 | 1.1176214402303308 | """ print self.test_scenario5.__doc__ examples = [ - ['data/iris.csv', '20', '20', '30', '{"summary_fields": ["sepal width"]}', '{"petal length": 1, "petal width": 1, "sepal length": 1, "species": "Iris-setosa"}', 'Cluster 3', '0.7155571246307187']] + ['data/iris.csv', '20', '20', '30', '{"summary_fields": ["sepal width"]}', '{"petal length": 1, "petal width": 1, "sepal length": 1, "species": "Iris-setosa"}', 'Cluster 3', '1.1176214402303308']] for example in examples: print "\nTesting with:\n", example source_create.i_upload_a_file(self, example[0]) diff --git a/bigml/tests/test_29_script.py b/bigml/tests/test_29_script.py new file mode 100644 index 00000000..47ab0a62 --- /dev/null +++ b/bigml/tests/test_29_script.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +""" Creating and updating scripts + +""" +from world import world, setup_module, teardown_module +import create_script_steps as script_create + +class TestScript(object): + + def setup(self): + """ + Debug information + """ + print "\n-------------------\nTests in: %s\n" % __name__ + + def teardown(self): + """ + Debug information + """ + print "\nEnd of tests in: %s\n-------------------\n" % __name__ + + def test_scenario1(self): + """ + Scenario: Successfully creating a whizzml script: + Given I create a whizzml script from a excerpt of code "" + And I wait until the script is ready less than secs + And I update the script with "", "" + And I wait until the script is ready less than secs + Then the script code is "" and the value of "" is "" + + Examples: + | source_code | time_1 | time_2 | param | param_value + | (+ 1 1) | 10 | 10 | name | my script + """ + print self.test_scenario1.__doc__ + examples = [ + ['(+ 1 1)', '10', '10', 'name', 'my script']] + for example in examples: + print "\nTesting with:\n", example + script_create.i_create_a_script(self, example[0]) + script_create.the_script_is_finished(self, example[1]) + script_create.i_update_a_script(self, example[3], example[4]) + script_create.the_script_is_finished(self, example[2]) + script_create.the_script_code_and_attributes(self, example[0], example[3], example[4]) diff --git a/bigml/tests/test_30_execution.py b/bigml/tests/test_30_execution.py new file mode 100644 index 00000000..4c9bb312 --- /dev/null +++ b/bigml/tests/test_30_execution.py @@ -0,0 +1,98 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +""" Creating and updating scripts + +""" +from world import world, setup_module, teardown_module +import create_script_steps as script_create +import create_execution_steps as execution_create + +class TestExecution(object): + + def setup(self): + """ + Debug information + """ + print "\n-------------------\nTests in: %s\n" % __name__ + + def teardown(self): + """ + Debug information + """ + print "\nEnd of tests in: %s\n-------------------\n" % __name__ + + def test_scenario1(self): + """ + Scenario: Successfully creating a whizzml script execution: + Given I create a whizzml script from a excerpt of code "" + And I wait until the script is ready less than secs + And I create a whizzml script execution from an existing script + And I wait until the execution is ready less than secs + And I update the execution with "", "" + And I wait until the execution is ready less than secs + Then the script id is correct and the value of "" is "" + + Examples: + | source_code | time_1 | time_2 | time_3 | param | param_value + | (+ 1 1) | 10 | 10 | 50 | name | my execution + """ + print self.test_scenario1.__doc__ + examples = [ + ['(+ 1 1)', '10', '10', '50', 'name', 'my execution']] + for example in examples: + print "\nTesting with:\n", example + script_create.i_create_a_script(self, example[0]) + script_create.the_script_is_finished(self, example[1]) + execution_create.i_create_an_execution(self) + execution_create.the_execution_is_finished(self, example[2]) + execution_create.i_update_an_execution(self, example[4], example[5]) + execution_create.the_execution_is_finished(self, example[3]) + execution_create.the_execution_and_attributes(self, example[4], example[5]) + + def test_scenario2(self): + """ + Scenario: Successfully creating a whizzml script execution from a list of scripts: + Given I create a whizzml script from a excerpt of code "" + And I wait until the script is ready less than secs + And I create a whizzml script from a excerpt of code "" + And I wait until the script is ready less than secs + And I create a whizzml script execution from the last two scripts + And I wait until the execution is ready less than secs + And I update the execution with "", "" + And I wait until the execution is ready less than secs + Then the script ids are correct and the value of "" is "" + + Examples: + | source_code | time_1 | time_2 | time_3 | param | param_value + | (+ 1 1) | 10 | 10 | 50 | name | my execution + """ + print self.test_scenario2.__doc__ + examples = [ + ['(+ 1 1)', '10', '10', '50', 'name', 'my execution']] + for example in examples: + print "\nTesting with:\n", example + script_create.i_create_a_script(self, example[0]) + script_create.the_script_is_finished(self, example[1]) + script_create.i_create_a_script(self, example[0]) + script_create.the_script_is_finished(self, example[1]) + execution_create.i_create_an_execution_from_list(self, 2) + execution_create.the_execution_is_finished(self, example[2]) + execution_create.i_update_an_execution(self, example[4], example[5]) + execution_create.the_execution_is_finished(self, example[3]) + execution_create.the_execution_ids_and_attributes(self, 2, example[4], example[5]) diff --git a/bigml/tests/test_31_library.py b/bigml/tests/test_31_library.py new file mode 100644 index 00000000..885e63f2 --- /dev/null +++ b/bigml/tests/test_31_library.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +# +# Copyright 2015 BigML +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +""" Creating and updating scripts + +""" +from world import world, setup_module, teardown_module +import create_library_steps as library_create + +class TestLibrary(object): + + def setup(self): + """ + Debug information + """ + print "\n-------------------\nTests in: %s\n" % __name__ + + def teardown(self): + """ + Debug information + """ + print "\nEnd of tests in: %s\n-------------------\n" % __name__ + + def test_scenario1(self): + """ + Scenario: Successfully creating a whizzml library: + Given I create a whizzml library from a excerpt of code "" + And I wait until the library is ready less than secs + And I update the library with "", "" + And I wait until the library is ready less than secs + Then the library code is "" and the value of "" is "" + + Examples: + | source_code | time_1 | time_2 | param | param_value + | (define (mu x) (+ x 1)) | 10 | 10 | name | my library + """ + print self.test_scenario1.__doc__ + examples = [ + ['(define (mu x) (+ x 1))', '10', '10', 'name', 'my library']] + for example in examples: + print "\nTesting with:\n", example + library_create.i_create_a_library(self, example[0]) + library_create.the_library_is_finished(self, example[1]) + library_create.i_update_a_library(self, example[3], example[4]) + library_create.the_library_is_finished(self, example[2]) + library_create.the_library_code_and_attributes(self, example[0], example[3], example[4]) diff --git a/bigml/tests/world.py b/bigml/tests/world.py index eff48f6f..242e5ed0 100644 --- a/bigml/tests/world.py +++ b/bigml/tests/world.py @@ -48,7 +48,10 @@ 'correlation', 'statisticaltest', 'logisticregression', - 'association' + 'association', + 'script', + 'execution', + 'library' ] IRREGULAR_PLURALS = { 'anomaly': 'anomalies', @@ -57,8 +60,10 @@ 'anomalyscore': 'anomaly_scores', 'batchanomalyscore': 'batch_anomaly_scores', 'statisticaltest': 'statistical_tests', - 'logisticregression': 'logistic_regressions' + 'logisticregression': 'logistic_regressions', + 'library': 'libraries' } + TRANSLATED_RESOURCES = { 'batchprediction': 'batch_prediction', 'batchcentroid': 'batch_centroid',