Exemplo n.º 1
0
def r_cell(source, validate=False):
    """Get command specification for a R cell. Primarily intended for unit
    tests.

    Parameters
    ----------
    source: string
        R code for cell body
    validate: bool, optional
        If true, the command is validated

    Returns
    -------
    vizier.viztrail.command.ModuleCommand
    """
    # If the validate flag is true create a package index that contains the
    # r package declaration
    if validate:
        packages = {r.PACKAGE_R: pckg.PackageIndex(r.R_COMMANDS)}
    else:
        packages = None
    return md.ModuleCommand(
        r.PACKAGE_R,
        r.R_CODE,
        arguments=[md.ARG(id=r.PARA_R_SOURCE, value=source)],
        packages=packages)
Exemplo n.º 2
0
def sql_cell(source, output_dataset=None, validate=False):
    """Get command specification for a SQL cell. Primarily intended for unit
    tests.

    Parameters
    ----------
    source: string
        SQL code for cell body
    output_dataset: string, optional
        Optional dataset name. If given result is materialized as new dataset.
    validate: bool, optional
        If true, the command is validated

    Returns
    -------
    vizier.viztrail.command.ModuleCommand
    """
    # If the validate flag is true create a package index that contains the
    # SQL package declaration
    if validate:
        packages = {sql.PACKAGE_SQL: pckg.PackageIndex(sql.SQL_COMMANDS)}
    else:
        packages = None
    arguments = [md.ARG(id=sql.PARA_SQL_SOURCE, value=source)]
    if not output_dataset is None:
        arguments.append(
            md.ARG(id=sql.PARA_OUTPUT_DATASET, value=output_dataset)
        )
    return md.ModuleCommand(
        sql.PACKAGE_SQL,
        sql.SQL_QUERY,
        arguments=arguments,
        packages=packages
    )
Exemplo n.º 3
0
def PACKAGE(validate=False):
    """Depending on the validate flag return a package dictionary that contains
    the mimir package declaration or None.

    Parameters
    ----------
    validate: bool, optional

    Returns
    ------
    dict
    """
    if validate:
        return {mimir.PACKAGE_MIMIR: pckg.PackageIndex(mimir.MIMIR_LENSES)}
    else:
        return None
Exemplo n.º 4
0
def PACKAGE(validate=False):
    """Depending on the validate flag return a package dictionary that contains
    the vizual package declaration or None.

    Parameters
    ----------
    validate: bool, optional

    Returns
    ------
    dict
    """
    if validate:
        return {
            vizual.PACKAGE_VIZUAL: pckg.PackageIndex(vizual.VIZUAL_COMMANDS)
        }
    else:
        return None
"""Test validation and external representation for commands in the SQL
package.
"""

import unittest

from vizier.engine.packages.sql.command import sql_cell

import vizier.engine.packages.base as pckg
import vizier.engine.packages.sql.base as sql
import vizier.viztrail.command as md


PACKAGE = pckg.PackageIndex(sql.SQL_COMMANDS)


class TestValidateSQL(unittest.TestCase):

    def test_sql_cell(self):
        """Test validation of the SQL cell."""
        cmd = sql_cell(
            source='SELECT * FROM dataset',
            validate=True
        ).to_external_form(
            command=PACKAGE.get(sql.SQL_QUERY)
        )
        self.assertEqual(cmd, 'SELECT * FROM dataset')
        # Validate with given output dataset
        cmd = sql_cell(
            source='SELECT * FROM dataset',
            output_dataset='result',
from vizier.datastore.dataset import DatasetColumn, DatasetDescriptor

import vizier.engine.packages.base as pckg
import vizier.engine.packages.mimir.base as mimir
import vizier.viztrail.command as md

DATASETS = {
    'ds':
    DatasetDescriptor(identifier='0000',
                      name='ds',
                      columns=[
                          DatasetColumn(identifier=2, name='Some Name'),
                          DatasetColumn(identifier=1, name='Street')
                      ])
}
PACKAGE = pckg.PackageIndex(mimir.MIMIR_LENSES)


class TestValidateMimir(unittest.TestCase):
    def test_mimir_geocode(self):
        """Test validation of Mimir geocode lens."""
        cmd = mimir_geocode(dataset_name='ds',
                            geocoder='GOOGLE',
                            street=1,
                            city=2,
                            materialize_input=False,
                            validate=True).to_external_form(
                                command=PACKAGE.get(mimir.MIMIR_GEOCODE),
                                datasets=DATASETS)
        self.assertEqual(
            cmd,
"""Test validation of commands in the plot package."""

import unittest

from vizier.engine.packages.plot.command import create_plot
from vizier.viztrail.command import ModuleCommand, ARG, ARG_ID
import vizier.engine.packages.base as pckg
import vizier.engine.packages.plot.base as plot

PACKAGES = {plot.PACKAGE_PLOT: pckg.PackageIndex(plot.PLOT_COMMANDS)}


class TestPlotCommandValidation(unittest.TestCase):
    def test_create_plot(self):
        """Test validation of the create plot command."""
        create_plot(dataset_name='ds',
                    chart_name='My Chart',
                    series=[{
                        'column': 1
                    }],
                    validate=True)
        create_plot(dataset_name='ds',
                    chart_name='My Chart',
                    series=[{
                        'column': 1
                    }],
                    xaxis_range='0:10',
                    validate=True)
        # Have an error raised if values of invalid data type are given
        with self.assertRaises(ValueError):
            create_plot(dataset_name='ds',
"""Test validation and external representation for commands in the PyCell
package.
"""

import unittest

from vizier.engine.packages.pycell.command import python_cell

import vizier.engine.packages.base as pckg
import vizier.engine.packages.pycell.base as pycell
import vizier.viztrail.command as md

PACKAGE = pckg.PackageIndex(pycell.PYTHON_COMMANDS)


class TestValidatePyCell(unittest.TestCase):
    def test_python_cell(self):
        """Test validation of Pyhton cell."""
        cmd = python_cell(source='print 2+2', validate=True).to_external_form(
            command=PACKAGE.get(pycell.PYTHON_CODE))
        self.assertEqual(cmd, 'print 2+2')


if __name__ == '__main__':
    unittest.main()
Exemplo n.º 9
0
import vizier.engine.packages.base as pckg
import vizier.engine.packages.plot.base as plot
import vizier.viztrail.command as md


DATASETS = {
    'ds': DatasetDescriptor(
        identifier='0000',
        name='ds',
        columns=[
            DatasetColumn(identifier=2, name='Some Name'),
            DatasetColumn(identifier=1, name='Street')
        ]
    )
}
PACKAGE = pckg.PackageIndex(plot.PLOT_COMMANDS)


class TestValidatePlot(unittest.TestCase):

    def test_plot(self):
        """Test validation of create plot command."""
        cmd = create_plot(
            dataset_name='ds',
            chart_name='My Chart',
            series=[{'column': 1, 'range': '0:10', 'label': 'A'}, {'column': 2}],
            xaxis_range='0:100',
            xaxis_column=1,
            validate=True
        ).to_external_form(
            command=PACKAGE.get(plot.PLOT_SIMPLE_CHART),
"""Test validation of commands in the python cell package."""

import unittest

from vizier.engine.packages.pycell.command import python_cell
from vizier.viztrail.command import ModuleCommand, ARG, ARG_ID
import vizier.engine.packages.base as pckg
import vizier.engine.packages.pycell.base as pycell

PACKAGES = {pycell.PACKAGE_PYTHON: pckg.PackageIndex(pycell.PYTHON_COMMANDS)}


class TestPyCellCommandValidation(unittest.TestCase):
    def test_python_cell(self):
        """Test validation of the python cell command."""
        python_cell(source='ABC', validate=True)
        # Have an error raised if values of invalid data type are given
        with self.assertRaises(ValueError):
            python_cell(source=[], validate=True)
        # Get dictionary serialization of command arguments. Ensure that we
        # can create a valid command instance from the returned result.
        obj = python_cell(source='ABC', validate=True).arguments.to_list()
        ModuleCommand(package_id=pycell.PACKAGE_PYTHON,
                      command_id=pycell.PYTHON_CODE,
                      arguments=obj,
                      packages=PACKAGES)
        # Delete the only mandatory element from the serialization to ensure
        # that validation fails
        del obj[0]
        with self.assertRaises(ValueError):
            ModuleCommand(package_id=pycell.PACKAGE_PYTHON,
"""Test validation of commands in the scala package."""

import unittest

from vizier.engine.packages.scala.command import scala_cell
from vizier.viztrail.command import ModuleCommand, ARG, ARG_ID
import vizier.engine.packages.base as pckg
import vizier.engine.packages.scala.base as scala

PACKAGES = {scala.PACKAGE_SCALA: pckg.PackageIndex(scala.SCALA_COMMANDS)}


class TestScalaCommandValidation(unittest.TestCase):
    def test_scala_cell(self):
        """Test validation of the scala command."""
        scala_cell(source='ABC', validate=True)
        # Have an error raised if values of invalid data type are given
        with self.assertRaises(ValueError):
            scala_cell(source=[], validate=True)
        # Get dictionary serialization of command arguments. Ensure that we
        # can create a valid command instance from the returned result.
        obj = scala_cell(source='ABC', validate=True).arguments.to_list()
        ModuleCommand(package_id=scala.PACKAGE_SCALA,
                      command_id=scala.SCALA_CODE,
                      arguments=obj,
                      packages=PACKAGES)
        # Delete the only mandatory element from the serialization to ensure
        # that validation fails
        del obj[0]
        with self.assertRaises(ValueError):
            ModuleCommand(package_id=scala.PACKAGE_SCALA,
Exemplo n.º 12
0
def create_plot(dataset_name,
                chart_name,
                series,
                chart_type='Bar Chart',
                chart_grouped=False,
                xaxis_range=None,
                xaxis_column=None,
                validate=False):
    """Create an instance of a create plot command.

    Parameters
    ----------
    dataset_name: string
        Dataset name
    chart_name: string
        Name of the chart
    series: list
        Specification of data series. Each data series is specified by a
        dictionary that contains the mandadtory element 'column' and
        the optional elements 'range' and 'label'
    chart_type: string
        Identifier for chart type
    chart_grouped: bool
        Group multiple series into a single chart
    xaxis_range: string, optional
        Column value range definition
    xaxis_column: int, optional
        Column identifier
    validate: bool, optional
        If true, the command is validated

    Returns
    -------
    vizier.engine.viztrail.command.ModuleCommand
    """
    # If the validate flag is true create a package index that contains the
    # plot package declaration
    if validate:
        packages = {plot.PACKAGE_PLOT: pckg.PackageIndex(plot.PLOT_COMMANDS)}
    else:
        packages = None
    # Create a record for each series specification
    series_elements = list()
    for s in series:
        items = list()
        items.append(md.ARG(id=plot.PARA_SERIES_COLUMN, value=s['column']))
        if 'label' in s:
            items.append(md.ARG(id=plot.PARA_SERIES_LABEL, value=s['label']))
        if 'range' in s:
            items.append(md.ARG(id=plot.PARA_SERIES_RANGE, value=s['range']))
        series_elements.append(items)
    # Create list of arguments
    arguments = [
        md.ARG(id=pckg.PARA_DATASET, value=dataset_name),
        md.ARG(id=pckg.PARA_NAME, value=chart_name),
        md.ARG(id=plot.PARA_SERIES, value=series_elements),
        md.ARG(id=plot.PARA_CHART,
               value=[
                   md.ARG(plot.PARA_CHART_TYPE, value=chart_type),
                   md.ARG(id=plot.PARA_CHART_GROUPED, value=chart_grouped)
               ])
    ]
    # Only add xaxis record if at least one of the two arguments are given
    if not xaxis_range is None or not xaxis_column is None:
        items = list()
        if not xaxis_column is None:
            items.append(md.ARG(id=plot.PARA_XAXIS_COLUMN, value=xaxis_column))
        if not xaxis_range is None:
            items.append(md.ARG(id=plot.PARA_XAXIS_RANGE, value=xaxis_range))
        arguments.append(md.ARG(id=plot.PARA_XAXIS, value=items))
    return md.ModuleCommand(package_id=plot.PACKAGE_PLOT,
                            command_id=plot.PLOT_SIMPLE_CHART,
                            arguments=arguments,
                            packages=packages)
Exemplo n.º 13
0
from vizier.filestore.fs.base import FileSystemFilestore, METADATA_FILENAME

import vizier.engine.packages.base as pckg
import vizier.engine.packages.vizual.base as vizual
import vizier.filestore.base as fs
import vizier.viztrail.command as md

DATASETS = {
    'ds':
    DatasetDescriptor(identifier='0000',
                      columns=[
                          DatasetColumn(identifier=2, name='Some Name'),
                          DatasetColumn(identifier=1, name='Street')
                      ])
}
PACKAGE = pckg.PackageIndex(vizual.VIZUAL_COMMANDS)

SERVER_DIR = './.tmp'
CSV_FILE = './.files/dataset.csv'


class TestValidateVizual(unittest.TestCase):
    def setUp(self):
        """Create an empty file server repository."""
        # Drop project descriptor directory
        if os.path.isdir(SERVER_DIR):
            shutil.rmtree(SERVER_DIR)

    def tearDown(self):
        """Clean-up by dropping file server directory.
        """
Exemplo n.º 14
0
"""Test validation and external representation for commands in the Scala
package.
"""

import unittest

from vizier.engine.packages.scala.command import scala_cell

import vizier.engine.packages.base as pckg
import vizier.engine.packages.scala.base as scala
import vizier.viztrail.command as md

PACKAGE = pckg.PackageIndex(scala.SCALA_COMMANDS)


class TestValidateScala(unittest.TestCase):
    def test_scala_cell(self):
        """Test validation of Scala cell."""
        cmd = scala_cell(source='println("Hello, world!")',
                         validate=True).to_external_form(
                             command=PACKAGE.get(scala.SCALA_CODE))
        self.assertEqual(cmd, 'println("Hello, world!")')


if __name__ == '__main__':
    unittest.main()