예제 #1
0
def test_handle_task(tmpdir):
    storage.initialize(tmpdir)
    storage.register_app(
        tmpdir, "example_app", {
            "tableA": {
                "description": "This table contains A.",
                "columns": {
                    "some_var": {
                        "type": "float",
                        "description": "This column contains 1."
                    }
                }
            }
        })
    storage.insert_payload(
        tmpdir, "example_app", {
            "tableA": [
                {
                    "some_var": 1.0
                },
                {
                    "some_var": 2.0
                },
                {
                    "some_var": 3.0
                },
                {
                    "some_var": 4.0
                },
                {
                    "some_var": 5.0
                },
            ]
        })

    task = {
        "type": "basic",
        "epsilon": 1.0,
        "featurizer": "SELECT sum(some_var) FROM example_app.tableA",
        "aggregator": "mean"
    }
    result, err = handler.handle_task(tmpdir, task)
    assert result[0] == 15.0
예제 #2
0
def test_insert_payload_1(tmpdir):
    storage.initialize(tmpdir)
    storage.register_app(
        tmpdir, "example_app", {
            "tableA": {
                "description": "This table contains A.",
                "columns": {
                    "some_var": {
                        "type": "float",
                        "description": "This column contains 1."
                    },
                    "other_var": {
                        "type": "float",
                        "description": "This column contains 2."
                    }
                }
            }
        })
    storage.insert_payload(
        tmpdir, "example_app", {
            "tableA": [
                {
                    "some_var": 0.0,
                    "other_var": 1.0
                },
                {
                    "some_var": 10.0,
                    "other_var": 1.0
                },
            ]
        })
    data = storage.execute_sql(tmpdir, "SELECT * FROM example_app.tableA")
    assert len(data) == 2, "Expected two rows of data"
    for row in data:
        assert "some_var" in row
        assert "other_var" in row
    assert sum(row["some_var"] for row in data) == 10.0
    assert sum(row["other_var"] for row in data) == 2.0
예제 #3
0
def test_register_app(tmpdir):
    storage.initialize(tmpdir)
    storage.register_app(
        tmpdir, "example_app", {
            "tableA": {
                "description": "This table contains A.",
                "columns": {
                    "column1": {
                        "type": "float",
                        "description": "This column contains 1."
                    },
                    "column2": {
                        "type": "float",
                        "description": "This column contains 2."
                    }
                }
            }
        })
    app_columns = storage.app_columns(tmpdir)
    assert len(app_columns) == 2
    for app_column in app_columns:
        assert app_column["app_name"] == 'example_app'
        assert app_column["table_name"] == 'tableA'
        assert app_column["column_name"] in ["column1", "column2"]
예제 #4
0
def storage_server(storage_dir, storage_port, server_url, whitelist, blacklist, synthetic):
    api = Bottle()
    initialize(storage_dir)
    if synthetic:
        create_synthetic_dataset(storage_dir)
    create_dummy_dataset(storage_dir)
    api.config['storage_dir'] = storage_dir

    @api.route("/")
    def _index():
        root = os.path.dirname(__file__)
        return static_file('client.html', root=root)

    @api.route("/tasks")
    def _tasks():
        tasks = tiresias.server.remote.list_tasks(server_url)
        response.content_type = "application/json"
        for task_id, task in list(tasks.items()):
            task["accepted"] = task_id in whitelist
            task["rejected"] = task_id in blacklist
            task["preview"] = execute_sql(storage_dir, task["featurizer"])
        return tasks

    @api.route("/whitelist/<task_id>")
    def _whitelist_task(task_id):
        whitelist.add(task_id)
        if task_id in blacklist:
            blacklist.remove(task_id)
        return ""

    @api.route("/blacklist/<task_id>")
    def _blacklist_task(task_id):
        blacklist.add(task_id)
        return ""

    @api.route("/app")
    def _app():
        """
        This REST endpoint returns a JSON array containing a list of the columns stored on the device.
        """
        rows = app_columns(api.config['storage_dir'])
        response.content_type = "application/json"
        return dumps(rows, indent=2)

    @api.route("/app/<app_name>/register")
    def _register(app_name):
        """
        This REST endpoint allows a new application to register by providing their database schema. The
        `schema` parameter is a JSON object.
        """
        schema = loads(request.params.get("schema"))
        register_app(api.config['storage_dir'], app_name, schema)
        return ""

    @api.route("/app/<app_name>/insert")
    def _insert(app_name):
        """
        This REST endpoint allows an application to append rows to their database by submitting a JSON 
        object in the `payload` field.
        """
        payload = loads(request.params.get("payload"))
        insert_payload(api.config['storage_dir'], app_name, payload)
        return ""

    if synthetic:
        api.run(host="0.0.0.0", port=storage_port, quiet=True)
    else:
        api.run(host="localhost", port=storage_port, quiet=True)
예제 #5
0
def test_insert_payload_2(tmpdir):
    storage.initialize(tmpdir)

    storage.register_app(
        tmpdir, "app1", {
            "tableA": {
                "description": "This table contains A.",
                "columns": {
                    "some_var": {
                        "type": "float",
                        "description": "This column contains 1."
                    }
                }
            }
        })
    storage.insert_payload(
        tmpdir, "app1", {
            "tableA": [
                {
                    "some_var": 1.0
                },
                {
                    "some_var": 2.0
                },
                {
                    "some_var": 3.0
                },
                {
                    "some_var": 4.0
                },
                {
                    "some_var": 5.0
                },
            ]
        })

    storage.register_app(
        tmpdir, "app2", {
            "tableA": {
                "description": "This table contains A.",
                "columns": {
                    "some_var": {
                        "type": "float",
                        "description": "This column contains 1."
                    }
                }
            },
            "tableB": {
                "description": "This table contains B.",
                "columns": {
                    "some_var": {
                        "type": "float",
                        "description": "This column contains 1."
                    },
                    "other_var": {
                        "type": "float",
                        "description": "This column contains 2."
                    }
                }
            },
        })
    storage.insert_payload(
        tmpdir, "app2", {
            "tableA": [
                {
                    "some_var": 3.0
                },
            ],
            "tableB": [
                {
                    "some_var": 0.0,
                    "other_var": 1.0
                },
                {
                    "some_var": 10.0,
                    "other_var": 1.0
                },
            ]
        })

    data = storage.execute_sql(tmpdir, "SELECT * FROM app1.tableA")
    assert len(data) == 5, "Expected five rows of data"

    data = storage.execute_sql(tmpdir, "SELECT * FROM app2.tableA")
    assert len(data) == 1, "Expected one row of data"

    data = storage.execute_sql(tmpdir, "SELECT * FROM app2.tableB")
    assert len(data) == 2, "Expected two rows of data"
예제 #6
0
def test_initialize(tmpdir):
    storage.initialize(tmpdir)
    assert tmpdir.ensure("metadata.db"), "Metadata database was not created."