Ejemplo n.º 1
0
def read_settings(filename=None, defs=None):
    # READ SETTINGS
    if filename:
        settings_file = File(filename)
        if not settings_file.exists:
            Log.error("Can not file settings file {{filename}}",
                      {"filename": settings_file.abspath})
        settings = mo_json_config.get("file:///" + settings_file.abspath)
        if defs:
            settings.args = argparse(defs)
        return settings
    else:
        defs = listwrap(defs)
        defs.append({
            "name": ["--settings", "--settings-file", "--settings_file"],
            "help":
            "path to JSON file with settings",
            "type":
            str,
            "dest":
            "filename",
            "default":
            "./settings.json",
            "required":
            False
        })
        args = argparse(defs)
        settings = mo_json_config.get("file://" +
                                      args.filename.replace(os.sep, "/"))
        settings.args = args
        return settings
Ejemplo n.º 2
0
def read_settings(filename=None, defs=None, env_filename=None):
    """
    :param filename: Force load a file
    :param defs: arguments you want to accept
    :param env_filename: A config file from an environment variable (a fallback config file, if no other provided)
    :return:
    """
    # READ SETTINGS
    if filename:
        settings_file = File(filename)
        if not settings_file.exists:
            Log.error("Can not file settings file {{filename}}", {
                "filename": settings_file.abspath
            })
        settings = mo_json_config.get("file:///" + settings_file.abspath)
        if defs:
            settings.args = argparse(defs)
        return settings
    else:
        defs = listwrap(defs)
        defs.append({
            "name": ["--config", "--settings", "--settings-file", "--settings_file"],
            "help": "path to JSON file with settings",
            "type": str,
            "dest": "filename",
            "default": "./config.json",
            "required": False
        })
        args = argparse(defs)

        if env_filename:
            args.filename = env_filename
        settings = mo_json_config.get("file://" + args.filename.replace(os.sep, "/"))
        settings.args = args
        return settings
Ejemplo n.º 3
0
def read_settings(filename=None, defs=None):
    # READ SETTINGS
    if filename:
        settings_file = File(filename)
        if not settings_file.exists:
            Log.error("Can not file settings file {{filename}}", {
                "filename": settings_file.abspath
            })
        settings = mo_json_config.get("file:///" + settings_file.abspath)
        if defs:
            settings.args = argparse(defs)
        return settings
    else:
        defs = listwrap(defs)
        defs.append({
            "name": ["--settings", "--settings-file", "--settings_file"],
            "help": "path to JSON file with settings",
            "type": str,
            "dest": "filename",
            "default": "./settings.json",
            "required": False
        })
        args = argparse(defs)
        settings = mo_json_config.get("file://" + args.filename.replace(os.sep, "/"))
        settings.args = args
        return settings
Ejemplo n.º 4
0
 def test_leaves_w_array(self):
     url = URL(self.resources + "/test_ref_w_deep_parameters.json")
     url.query = {
         "value": {
             "one": {
                 "two": [{
                     "test": 1
                 }, {
                     "test": 2
                 }, "3"]
             }
         }
     }
     result = mo_json_config.get(url)
     expected = {
         "a": {
             "two": [{
                 "test": 1
             }, {
                 "test": 2
             }, "3"]
         },
         "b": [{
             "test": 1
         }, {
             "test": 2
         }, "3"]
     }
     self.assertEqual(result, expected,
                      "expecting proper parameter expansion")
Ejemplo n.º 5
0
    def test_array_expansion(self):
        # BETTER TEST OF RECURSION
        doc = mo_json_config.get(self.resources + "/test_array.json")

        self.assertEqual(
            doc, {
                "a": "some_value",
                "list": {
                    "deep": [{
                        "a": "a",
                        "test_key": "test_value"
                    }, {
                        "a": "b",
                        "test_key": "test_value"
                    }, {
                        "a": "c",
                        "test_key": "test_value"
                    }, {
                        "a": "d",
                        "test_key": "test_value"
                    }, {
                        "a": "e",
                        "test_key": "test_value"
                    }]
                }
            })
Ejemplo n.º 6
0
    def fill_container(self, subtest, tjson=False):
        """
        RETURN SETTINGS THAT CAN BE USED TO POINT TO THE INDEX THAT'S FILLED
        """
        subtest = wrap(subtest)
        _settings = self._es_test_settings  # ALREADY COPIED AT setUp()
        # _settings.index = "testing_" + Random.hex(10).lower()
        # settings.type = "test_result"

        try:
            url = "file://resources/schema/basic_schema.json.template?{{.|url}}"
            url = expand_template(url, {
                "type": _settings.type,
                "metadata": subtest.metadata
            })
            _settings.schema = mo_json_config.get(url)

            # MAKE CONTAINER
            container = self._es_cluster.get_or_create_index(tjson=tjson,
                                                             kwargs=_settings)
            container.add_alias(_settings.index)

            # INSERT DATA
            container.extend([{"value": v} for v in subtest.data])
            container.flush()
            # ENSURE query POINTS TO CONTAINER
            frum = subtest.query["from"]
            if isinstance(frum, basestring):
                subtest.query["from"] = frum.replace(TEST_TABLE,
                                                     _settings.index)
            else:
                Log.error("Do not know how to handle")
        except Exception, e:
            Log.error("can not load {{data}} into container",
                      {"data": subtest.data}, e)
Ejemplo n.º 7
0
    def setUpClass(cls):
        BaseTestCase.setUpClass(assume_server_started=False)

        # START DIRECT-TO-ACTIVEDATA-ES SERVICE
        global ES_CLUSTER_LOCATION

        app_config = mo_json_config.get("file://"+APP_CONFIG_FILE)
        global_settings.service_url = "http://localhost:"+unicode(app_config.flask.port)+"/query"
        ES_CLUSTER_LOCATION = app_config.elasticsearch.host
Ejemplo n.º 8
0
 def test_leaves(self):
     url = self.resources + "/test_ref_w_deep_parameters.json?&value.one.two=42"
     result = mo_json_config.get(url)
     self.assertEqual(result, {
         "a": {
             "two": 42
         },
         "b": 42
     }, "expecting proper parameter expansion")
Ejemplo n.º 9
0
    def test_read_home(self):
        file = "~/___test_file.json"
        source = File.new_instance(
            get_stacktrace(0)[0]["file"], "../resources/simple.json")
        File.copy(File(source), File(file))
        content = mo_json_config.get("file:///" + file)

        try:
            self.assertEqual(content, {"test_key": "test_value"})
        finally:
            File(file).delete()
Ejemplo n.º 10
0
    def test_doc2(self):
        # BETTER TEST OF RECURSION
        doc = mo_json_config.get(self.resources + "/test_ref2.json")

        self.assertEqual(
            doc, {
                "a": "some_value",
                "test_key": "test_value",
                "b": {
                    "test_key": "test_value"
                }
            })
Ejemplo n.º 11
0
    def fill_container(self, subtest, typed=True):
        """
        RETURN SETTINGS THAT CAN BE USED TO POINT TO THE INDEX THAT'S FILLED
        """
        subtest = wrap(subtest)
        _settings = self._es_test_settings  # ALREADY COPIED AT setUp()

        try:
            url = "file://resources/schema/basic_schema.json.template?{{.|url}}"
            url = expand_template(url, {
                "type": _settings.type,
                "metadata": subtest.metadata
            })
            _settings.schema = mo_json_config.get(url)

            # MAKE CONTAINER
            container = self._es_cluster.get_or_create_index(
                typed=typed,
                schema=subtest.schema or _settings.schema,
                kwargs=_settings
            )
            container.add_alias(_settings.index)

            _settings.alias = container.settings.alias
            _settings.index = container.settings.index
            ESUtils.indexes.append(_settings.index)

            # INSERT DATA
            if '"null"' in value2json(subtest.data):
                Log.error("not expected")
            container.extend([{"value": d} for d in subtest.data])
            container.flush()

            now = Date.now()
            namespace = ElasticsearchMetadata(self._es_cluster.settings)
            namespace.get_columns(_settings.alias, after=now)  # FORCE A RELOAD

            # ENSURE query POINTS TO CONTAINER
            frum = subtest.query["from"]
            if frum == None:
                subtest.query["from"] = _settings.alias
            elif is_text(frum):
                subtest.query["from"] = frum.replace(test_jx.TEST_TABLE, _settings.alias)
            else:
                Log.error("Do not know how to handle")

        except Exception as e:
            Log.error("can not load {{data}} into container", data=subtest.data, cause=e)

        return _settings
Ejemplo n.º 12
0
    def test_doc1(self):
        os.environ["test_variable"] = "abc"

        doc = mo_json_config.get(self.resources + "/test_ref1.json")

        self.assertEqual(doc.env_variable, "abc")
        self.assertEqual(doc.relative_file1, "*_ts")
        self.assertEqual(doc.relative_file2, "*_ts")
        self.assertEqual(doc.relative_doc, "value")
        self.assertEqual(doc.absolute_doc, "another value")
        self.assertEqual(doc.env_variable, "abc")
        self.assertEqual(doc.relative_object_doc, {
            "key": "new value",
            "another_key": "another value"
        })
Ejemplo n.º 13
0
    def test_inner_doc(self):
        doc = mo_json_config.get(self.resources + "/inner.json")

        self.assertEqual(
            doc, {
                "area": {
                    "color": {
                        "description": "css color"
                    },
                    "border": {
                        "properties": {
                            "color": {
                                "description": "css color"
                            }
                        }
                    }
                },
                "definitions": {
                    "object_style": {
                        "color": {
                            "description": "css color"
                        },
                        "border": {
                            "properties": {
                                "color": {
                                    "description": "css color"
                                }
                            }
                        }
                    },
                    "style": {
                        "properties": {
                            "color": {
                                "description": "css color"
                            }
                        }
                    }
                }
            }, "expecting proper expansion")
Ejemplo n.º 14
0
        line = proc.stdout.readline()
        if not line:
            continue
        if line.find(" * Running on") >= 0:
            server_is_ready.go()
        Log.note("SERVER: {{line}}", {"line": line.strip()})

    proc.send_signal(signal.CTRL_C_EVENT)


if test_jx.utils is None:
    # read_alternate_settings
    try:
        filename = os.environ.get("TEST_CONFIG")
        if filename:
            test_jx.global_settings = mo_json_config.get("file://" + filename)
            constants.set(test_jx.global_settings.constants)
        else:
            Log.alert(
                "No TEST_CONFIG environment variable to point to config file.  Using ./tests/config/sqlite.json"
            )
            test_jx.global_settings = mo_json_config.get(
                "file://tests/config/sqlite.json")
            constants.set(test_jx.global_settings.constants)

        if not test_jx.global_settings.use:
            Log.error('Must have a {"use": type} set in the config file')

        Log.start(test_jx.global_settings.debug)
        test_jx.utils = SQLiteUtils(test_jx.global_settings)
    except Exception as e:
Ejemplo n.º 15
0
        result = jx.run(data)
        output_bytes = unicode2utf8(value2json(result))
        return wrap({
            "status_code": 200,
            "all_content": output_bytes,
            "content": output_bytes
        })


container_types = Data(elasticsearch=ESUtils, )

try:
    # read_alternate_settings
    filename = os.environ.get("TEST_CONFIG")
    if filename:
        test_jx.global_settings = mo_json_config.get("file://" + filename)
    else:
        Log.alert(
            "No TEST_CONFIG environment variable to point to config file.  Using "
            + DEFAULT_TEST_CONFIG)
        test_jx.global_settings = mo_json_config.get("file://" +
                                                     DEFAULT_TEST_CONFIG)
    constants.set(test_jx.global_settings.constants)
    Log.start(test_jx.global_settings.debug)

    if not test_jx.global_settings.use:
        Log.error('Must have a {"use": type} set in the config file')

    test_jx.global_settings.elasticsearch.version = Cluster(
        test_jx.global_settings.elasticsearch).version
    test_jx.utils = container_types[test_jx.global_settings.use](
Ejemplo n.º 16
0
            return wrap({
                "status_code": 400
            })

        text = convert.utf82unicode(body)
        data = convert.json2value(text)
        result = jx.run(data)
        output_bytes = convert.unicode2utf8(convert.value2json(result))
        return wrap({
            "status_code": 200,
            "all_content": output_bytes,
            "content": output_bytes
        })


test_jx.global_settings = mo_json_config.get("file://tests/config/elasticsearch.json")
constants.set(test_jx.global_settings.constants)
Log.alert("Resetting test count")
NEXT = 0

container_types = Data(
    elasticsearch=ESUtils,
)


try:
    # read_alternate_settings
    filename = os.environ.get("TEST_CONFIG")
    if filename:
        test_jx.global_settings = mo_json_config.get("file://"+filename)
    else:
Ejemplo n.º 17
0
 def test_json_parameter(self):
     url = URL(self.resources + "/test_ref_w_parameters.json")
     url.query = {"metadata": ["a", "b"]}
     result = mo_json_config.get(url)
     self.assertEqual(result, {"a": ["a", "b"]},
                      "expecting proper parameter expansion")
Ejemplo n.º 18
0
 def test_empty_object_as_json_parameter(self):
     url = URL(self.resources + "/test_ref_w_parameters.json")
     url.query = {"metadata": Data()}
     result = mo_json_config.get(url)
     self.assertEqual(result, {}, "expecting proper parameter expansion")
Ejemplo n.º 19
0
    def test_grandparent_reference(self):
        doc = mo_json_config.get(self.resources +
                                 "/child/grandchild/simple.json")

        self.assertEqual(doc, {"test_key": "test_value"})