Example #1
0
def test_execute_model_workflow_definition_invalidworkflow():

    PROJECT = RestObj({'name': 'Test Project', 'id': '98765'})
    WORKFLOWS = [{
        'name': 'Test W',
        'id': '12345'
    }, {
        'name':
        'TestW2',
        'id':
        '98765',
        'prompts': [{
            'id': '98765',
            'variableName': 'projectId',
            'variableType': 'string'
        }]
    }]

    with mock.patch(
            'sasctl._services.workflow.Workflow'
            '.list_enabled_definitions') as list_workflow_enableddefinitions:
        with mock.patch('sasctl._services.model_repository.ModelRepository'
                        '.get_project') as get_project:
            list_workflow_enableddefinitions.return_value = WORKFLOWS
            get_project.return_value = PROJECT
            with pytest.raises(ValueError):
                # Project missing
                _ = mm.execute_model_workflow_definition(
                    'TestLibrary', 'badworkflow')
Example #2
0
def test_pickle():
    import pickle

    obj = RestObj(name='test', id=1)

    pickled = pickle.dumps(obj)
    new_obj = pickle.loads(pickled)

    assert obj == new_obj
    def test_get_images(self, request):
        report = request.config.cache.get('REPORT', None)
        assert report is not None

        # Cache returns as dict
        report = RestObj(report)

        images = report_images.get_images(report, size=(800, 600))

        assert isinstance(images, list)
        assert len(images) > 0
        assert all(i.startswith(b'<svg ') for i in images)
    def test_get_elements(self, request):
        report = request.config.cache.get('REPORT', None)
        assert report is not None

        # Cache returns as dict
        report = RestObj(report)

        elements = reports.get_visual_elements(report)
        assert isinstance(elements, list)
        assert len(elements) > 0

        graph = [e for e in elements if e.type == 'Graph'][0]
        request.config.cache.set('GRAPH', graph)
    def test_get_single_element(self, request):
        report = request.config.cache.get('REPORT', None)
        graph = request.config.cache.get('GRAPH', None)
        assert report is not None
        assert graph is not None

        # Cache returns as dict
        report = RestObj(report)
        graph = RestObj(graph)

        # Get image with default size, specified as string
        images = report_images.get_images(report, size='640x480', elements=graph)
        assert isinstance(images, list)
        assert len(images) == 1

        image = images.pop()
        assert b'width="640"' in image
        assert b'height="480"' in image

        # Get image with default size, specified as tuple
        images = report_images.get_images(report, elements=[graph], size=(640, 480))

        assert isinstance(images, list)
        assert len(images) == 1

        image = images.pop()
        assert b'width="640"' in image
        assert b'height="480"' in image

        # Get image with specific size
        images = report_images.get_images(report, elements=(graph, (800, 600)))

        assert isinstance(images, list)
        assert len(images) == 1

        image = images.pop()
        assert b'width="800"' in image
        assert b'height="600"' in image
Example #6
0
def test_restobj():
    o = RestObj(a=1, b=2)

    assert o.a == 1
    assert o['a'] == 1
    assert o.b == 2
    assert o['b'] == 2

    with pytest.raises(AttributeError) as e:
        print(o.missingattribute)
    assert "missingattribute" in str(e.value)

    with pytest.raises(KeyError):
        print(o['c'])

    setattr(o, 'c', 'attribute')
    assert o.c == 'attribute'

    with pytest.raises(KeyError):
        print(o['c'])
def test_create_performance_definition():
    import copy
    from sasctl import current_session

    PROJECT = RestObj({'name': 'Test Project', 'id': '98765'})
    MODEL = RestObj({
        'name': 'Test Model',
        'id': '12345',
        'projectId': PROJECT['id']
    })
    USER = '******'

    with mock.patch('sasctl.core.requests.Session.request'):
        current_session('example.com', USER, 'password')

    with mock.patch('sasctl._services.model_repository.ModelRepository'
                    '.get_model') as get_model:
        with mock.patch('sasctl._services.model_repository.ModelRepository'
                        '.get_project') as get_project:
            with mock.patch('sasctl._services.model_management.ModelManagement'
                            '.post') as post:
                get_model.return_value = MODEL

                with pytest.raises(ValueError):
                    # Project missing all required properties
                    get_project.return_value = copy.deepcopy(PROJECT)
                    _ = mm.create_performance_definition(
                        'model', 'TestLibrary', 'TestData')

                with pytest.raises(ValueError):
                    # Project missing some required properties
                    get_project.return_value = copy.deepcopy(PROJECT)
                    get_project.return_value['targetVariable'] = 'target'
                    _ = mm.create_performance_definition(
                        'model', 'TestLibrary', 'TestData')

                with pytest.raises(ValueError):
                    # Project missing some required properties
                    get_project.return_value = copy.deepcopy(PROJECT)
                    get_project.return_value['targetLevel'] = 'interval'
                    _ = mm.create_performance_definition(
                        'model', 'TestLibrary', 'TestData')

                with pytest.raises(ValueError):
                    # Project missing some required properties
                    get_project.return_value = copy.deepcopy(PROJECT)
                    get_project.return_value[
                        'predictionVariable'] = 'predicted'
                    _ = mm.create_performance_definition(
                        'model', 'TestLibrary', 'TestData')

                get_project.return_value = copy.deepcopy(PROJECT)
                get_project.return_value['targetVariable'] = 'target'
                get_project.return_value['targetLevel'] = 'interval'
                get_project.return_value['predictionVariable'] = 'predicted'
                _ = mm.create_performance_definition('model',
                                                     'TestLibrary',
                                                     'TestData',
                                                     max_bins=3,
                                                     monitor_challenger=True,
                                                     monitor_champion=True)

            assert post.call_count == 1
            url, data = post.call_args

            assert PROJECT['id'] == data['json']['projectId']
            assert MODEL['id'] in data['json']['modelIds']
            assert 'TestLibrary' == data['json']['dataLibrary']
            assert 'TestData' == data['json']['dataPrefix']
            assert 'cas-shared-default' == data['json']['casServerId']
            assert data['json']['name'] is not None
            assert data['json']['description'] is not None
            assert data['json']['maxBins'] == 3
            assert data['json']['championMonitored'] == True
            assert data['json']['challengerMonitored'] == True

    def test_table_prefix_format():
        with pytest.raises(ValueError):
            # Underscores should not be allowed
            _ = mm.create_performance_definition('model', 'TestLibrary',
                                                 'invalid_name')
Example #8
0
def test_str():
    assert str(RestObj(name='test', id=1)) == 'test'
    assert str(RestObj(id=1)) == '1'
    assert str({'var': 'test'}) in str(RestObj(var='test'))
Example #9
0
def test_repr():
    d = dict(a=1, b=2)

    assert "'a': 1" in repr(RestObj(d))
    assert "'b': 2" in repr(RestObj(d))