Exemplo n.º 1
0
    def test_delete_form_data_command(self, mock_g):
        mock_g.user = security_manager.find_user("admin")
        app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
            "REFRESH_TIMEOUT_ON_RETRIEVAL": True
        }

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        create_args = CommandParameters(
            datasource_id=dataset.id,
            datasource_type=DatasourceType.TABLE,
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        key = CreateFormDataCommand(create_args).run()

        delete_args = CommandParameters(key=key, )

        delete_command = DeleteFormDataCommand(delete_args)
        response = delete_command.run()

        assert response == True
Exemplo n.º 2
0
 def post(self) -> Response:
     """Stores a new form_data.
     ---
     post:
       description: >-
         Stores a new form_data.
       parameters:
       - in: query
         schema:
           type: integer
         name: tab_id
       requestBody:
         required: true
         content:
           application/json:
             schema:
               $ref: '#/components/schemas/FormDataPostSchema'
       responses:
         201:
           description: The form_data was stored successfully.
           content:
             application/json:
               schema:
                 type: object
                 properties:
                   key:
                     type: string
                     description: The key to retrieve the form_data.
         400:
           $ref: '#/components/responses/400'
         401:
           $ref: '#/components/responses/401'
         422:
           $ref: '#/components/responses/422'
         500:
           $ref: '#/components/responses/500'
     """
     try:
         item = self.add_model_schema.load(request.json)
         tab_id = request.args.get("tab_id")
         args = CommandParameters(
             actor=g.user,
             dataset_id=item["dataset_id"],
             chart_id=item.get("chart_id"),
             tab_id=tab_id,
             form_data=item["form_data"],
         )
         key = CreateFormDataCommand(args).run()
         return self.response(201, key=key)
     except ValidationError as ex:
         return self.response(400, message=ex.messages)
     except (
             ChartAccessDeniedError,
             DatasetAccessDeniedError,
             TemporaryCacheAccessDeniedError,
     ) as ex:
         return self.response(403, message=str(ex))
     except (ChartNotFoundError, DatasetNotFoundError) as ex:
         return self.response(404, message=str(ex))
Exemplo n.º 3
0
    def test_create_form_data_command(self, mock_g):
        mock_g.user = security_manager.find_user("admin")

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        args = CommandParameters(
            datasource_id=dataset.id,
            datasource_type=DatasourceType.TABLE,
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        command = CreateFormDataCommand(args)

        assert isinstance(command.run(), str)
Exemplo n.º 4
0
    def test_update_form_data_command(self, mock_g):
        mock_g.user = security_manager.find_user("admin")
        app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
            "REFRESH_TIMEOUT_ON_RETRIEVAL": True
        }

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        query = db.session.query(Query).filter_by(
            sql="select 1 as foo;").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        create_args = CommandParameters(
            actor=mock_g.user,
            datasource_id=dataset.id,
            datasource_type=DatasourceType.TABLE,
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        key = CreateFormDataCommand(create_args).run()

        query_datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        update_args = CommandParameters(
            actor=mock_g.user,
            datasource_id=query.id,
            datasource_type=DatasourceType.QUERY,
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": query_datasource}),
            key=key,
        )

        update_command = UpdateFormDataCommand(update_args)
        new_key = update_command.run()

        # it should return a key
        assert isinstance(new_key, str)
        # the updated key returned should be different from the old one
        assert new_key != key

        key_args = CommandParameters(actor=mock_g.user, key=key)
        get_command = GetFormDataCommand(key_args)

        cache_data = json.loads(get_command.run())

        assert cache_data.get("datasource") == query_datasource
Exemplo n.º 5
0
    def test_create_form_data_command_type_as_string(self, mock_g):
        mock_g.user = security_manager.find_user("admin")
        app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
            "REFRESH_TIMEOUT_ON_RETRIEVAL": True
        }

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        create_args = CommandParameters(
            actor=mock_g.user,
            datasource_id=dataset.id,
            datasource_type="table",
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        command = CreateFormDataCommand(create_args)

        assert isinstance(command.run(), str)
Exemplo n.º 6
0
    def test_create_form_data_command_invalid_type(self, mock_g):
        mock_g.user = security_manager.find_user("admin")
        app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
            "REFRESH_TIMEOUT_ON_RETRIEVAL": True
        }

        dataset = (db.session.query(SqlaTable).filter_by(
            table_name="dummy_sql_table").first())
        slice = db.session.query(Slice).filter_by(
            slice_name="slice_name").first()

        datasource = f"{dataset.id}__{DatasourceType.TABLE}"
        create_args = CommandParameters(
            datasource_id=dataset.id,
            datasource_type="InvalidType",
            chart_id=slice.id,
            tab_id=1,
            form_data=json.dumps({"datasource": datasource}),
        )
        with pytest.raises(DatasourceTypeInvalidError) as exc:
            CreateFormDataCommand(create_args).run()

        assert "Datasource type is invalid" in str(exc.value)