Exemplo n.º 1
0
class CatalogueItemElementCommandsTestCase(TestCase):
    def get_uri(self, item_id):
        return reverse('catalogue:items.element', kwargs={'item_id': item_id})

    def setUp(self):
        ef.clear()

        self.app = Client()
        self.account = ef.account(type=AccountType.ADMIN.value)

        token = AuthToken.encode(self.account)
        self.headers = {'HTTP_AUTHORIZATION': f'Bearer {token}'}

    #
    # READ
    #
    def test_get_200(self):

        ci_0 = ef.catalogue_item(name='temperatures')
        # -- noise
        ci_1 = ef.catalogue_item(name='iot_features')  # noqa

        response = self.app.get(self.get_uri(ci_0.id), **self.headers)

        assert response.status_code == 200
        assert response.json() == {
            '@event': 'CATALOGUEITEM_READ',
            **CatalogueItemSerializer(ci_0).data,
        }

    def test_get_404(self):

        response = self.app.get(self.get_uri(69506), **self.headers)

        assert response.status_code == 404
        assert response.json() == {
            '@event': 'COULD_NOT_FIND_CATALOGUEITEM',
            '@type': 'error',
            '@authorizer': {
                'account_id': self.account.id,
            },
        }

    #
    # UPDATE
    #
    def test_put_200(self):

        a_0 = ef.account()
        a_1 = ef.account()
        ci = ef.catalogue_item(name='temperatures',
                               created_by=a_1,
                               updated_by=a_1)

        response = self.app.put(self.get_uri(ci.id),
                                data=json.dumps({
                                    'sample': [],
                                    'spec': [
                                        {
                                            'name': 'value',
                                            'type': 'FLOAT',
                                            'size': 19203,
                                            'is_nullable': False,
                                            'is_enum': False,
                                            'distribution': None,
                                        },
                                    ],
                                    'maintained_by_id':
                                    a_0.id,
                                    'executor_type':
                                    'DATABRICKS',
                                }),
                                content_type='application/json',
                                **self.headers)

        assert response.status_code == 200
        ci.refresh_from_db()
        assert response.json() == {
            '@event': 'CATALOGUEITEM_UPDATED',
            **CatalogueItemSerializer(ci).data
        }
        assert ci.name == 'temperatures'
        assert ci.spec == [
            {
                'name': 'value',
                'type': 'FLOAT',
                'size': 19203,
                'is_nullable': False,
                'is_enum': False,
                'distribution': None,
            },
        ]
        assert ci.maintained_by == a_0
        assert ci.created_by == a_1
        assert ci.updated_by == self.account
        assert ci.executor_type == 'DATABRICKS'

    def test_put_400(self):

        a = ef.account()
        ci = ef.catalogue_item(name='temperatures')

        response = self.app.put(self.get_uri(ci.id),
                                data=json.dumps({
                                    'name':
                                    'iot_events',
                                    'sample': [],
                                    'spec': [
                                        {
                                            'name': 'value',
                                            'size': 19203,
                                            'is_nullable': False,
                                            'is_enum': False,
                                            'distribution': None,
                                        },
                                    ],
                                    'maintained_by_id':
                                    a.id,
                                    'executor_type':
                                    'DATABRICKS',
                                }),
                                content_type='application/json',
                                **self.headers)

        assert response.status_code == 400
        assert response.json() == {
            '@event': 'BODY_DID_NOT_VALIDATE',
            '@type': 'error',
            'errors': {
                'spec': [
                    "JSON did not validate. PATH: '0' REASON: 'type' is a "
                    "required property",
                ],
            },
            '@authorizer': {
                'account_id': self.account.id,
            },
        }

    def test_put_404(self):

        response = self.app.put(self.get_uri(9022),
                                data=json.dumps({
                                    'name':
                                    'iot_events',
                                    'sample': [],
                                    'spec': [
                                        {
                                            'name': 'value',
                                            'type': 'FLOAT',
                                            'size': 19203,
                                            'is_nullable': False,
                                            'is_enum': False,
                                            'distribution': None,
                                        },
                                    ],
                                    'maintained_by_id':
                                    ef.account().id,
                                    'executor_type':
                                    'DATABRICKS',
                                }),
                                content_type='application/json',
                                **self.headers)

        assert response.status_code == 404
        assert response.json() == {
            '@event': 'COULD_NOT_FIND_CATALOGUEITEM',
            '@type': 'error',
            '@authorizer': {
                'account_id': self.account.id,
            },
        }

    #
    # DELETE
    #
    def test_delete_200(self):

        ci_0 = ef.catalogue_item(name='temperatures')
        # -- noise
        ci_1 = ef.catalogue_item(name='iot_features')  # noqa

        assert CatalogueItem.objects.all().count() == 2

        response = self.app.delete(self.get_uri(ci_0.id), **self.headers)

        assert CatalogueItem.objects.all().count() == 1
        assert response.status_code == 200
        assert response.json() == {
            '@event': 'CATALOGUEITEM_DELETED',
            '@type': 'empty',
        }

    def test_delete_400__not_cancelled_download_requests(self):

        ci_0 = ef.catalogue_item(name='temperatures',
                                 spec=[
                                     {
                                         'name': 'price',
                                         'type': 'INTEGER',
                                         'is_nullable': True,
                                         'is_enum': True,
                                         'distribution': None,
                                         'size': 1920,
                                     },
                                 ])
        ef.download_request(spec={
            'columns': ['price'],
            'filters': [],
            'randomize_ratio': 0.1,
        },
                            catalogue_item=ci_0)
        ef.download_request(spec={
            'columns': ['price'],
            'filters': [],
            'randomize_ratio': 0.2,
        },
                            catalogue_item=ci_0,
                            is_cancelled=True)

        # -- noise
        ci_1 = ef.catalogue_item(name='iot_features')  # noqa

        assert CatalogueItem.objects.all().count() == 2

        response = self.app.delete(self.get_uri(ci_0.id), **self.headers)

        assert CatalogueItem.objects.all().count() == 2
        assert response.status_code == 400
        assert response.json() == {
            '@authorizer': {
                'account_id': self.account.id
            },
            '@event': 'NOT_CANCELLED_DOWNLOAD_REQEUSTS_DETECTED',
            '@type': 'error',
            'item_id': ci_0.id,
            'not_cancelled_count': 1,
        }

    def test_delete_404(self):

        response = self.app.delete(self.get_uri(69506), **self.headers)

        assert response.status_code == 404
        assert response.json() == {
            '@event': 'COULD_NOT_FIND_CATALOGUEITEM',
            '@type': 'error',
            '@authorizer': {
                'account_id': self.account.id,
            },
        }
Exemplo n.º 2
0
class DownloadRequestElementCommandsTestCase(TestCase):
    def get_uri(self, request_id):
        return reverse('downloader:requests.element',
                       kwargs={'request_id': request_id})

    def setUp(self):
        ef.clear()

        self.app = Client()
        self.account = ef.account(type=AccountType.ADMIN.value)

        token = AuthToken.encode(self.account)
        self.headers = {'HTTP_AUTHORIZATION': f'Bearer {token}'}

        self.ci = ef.catalogue_item(spec=[
            {
                'name': 'product',
                'type': 'STRING',
                'is_nullable': True,
                'is_enum': True,
                'size': None,
                'distribution': None,
            },
            {
                'name': 'price',
                'type': 'INTEGER',
                'is_nullable': False,
                'is_enum': False,
                'size': None,
                'distribution': None,
            },
        ])

    #
    # READ
    #
    def test_get_200(self):

        d = DownloadRequest.objects.create(created_by=ef.account(),
                                           spec={
                                               'columns': ['product'],
                                               'filters': [{
                                                   'name': 'price',
                                                   'operator': '>=',
                                                   'value': 78
                                               }],
                                               'randomize_ratio':
                                               1,
                                           },
                                           catalogue_item=self.ci)
        d.waiters.add(self.account)

        response = self.app.get(self.get_uri(d.id), **self.headers)

        assert response.status_code == 200
        assert response.json() == {
            '@event': 'DOWNLOADREQUEST_READ',
            **DownloadRequestSerializer(d).data,
        }

    def test_get_404__wrong_user(self):

        d = DownloadRequest.objects.create(created_by=ef.account(),
                                           spec={
                                               'columns': ['product'],
                                               'filters': [{
                                                   'name': 'price',
                                                   'operator': '>=',
                                                   'value': 78
                                               }],
                                               'randomize_ratio':
                                               1,
                                           },
                                           catalogue_item=self.ci)

        response = self.app.get(self.get_uri(d.id), **self.headers)

        assert response.status_code == 404
        assert response.json() == {
            '@event': 'COULD_NOT_FIND_DOWNLOADREQUEST',
            '@type': 'error',
            '@authorizer': {
                'account_id': self.account.id,
            },
        }

    def test_get_404__wrong_id(self):

        response = self.app.get(self.get_uri(69506), **self.headers)

        assert response.status_code == 404
        assert response.json() == {
            '@event': 'COULD_NOT_FIND_DOWNLOADREQUEST',
            '@type': 'error',
            '@authorizer': {
                'account_id': self.account.id,
            },
        }

    #
    # DELETE
    #
    def test_delete_200(self):

        d = DownloadRequest.objects.create(created_by=ef.account(),
                                           spec={
                                               'columns': ['product'],
                                               'filters': [{
                                                   'name': 'price',
                                                   'operator': '>=',
                                                   'value': 78
                                               }],
                                               'randomize_ratio':
                                               1,
                                           },
                                           is_cancelled=False,
                                           catalogue_item=self.ci)
        d.waiters.add(self.account)
        assert DownloadRequest.objects.all().count() == 1

        response = self.app.delete(self.get_uri(d.id), **self.headers)

        assert response.status_code == 200
        assert response.json() == {
            '@event': 'DOWNLOADREQUEST_DELETED',
            '@type': 'empty',
        }
        assert DownloadRequest.objects.all().count() == 1
        d.refresh_from_db()
        assert d.waiters.count() == 0
        assert d.is_cancelled is True

    def test_delete_404(self):

        response = self.app.delete(self.get_uri(69506), **self.headers)

        assert response.status_code == 404
        assert response.json() == {
            '@event': 'COULD_NOT_FIND_DOWNLOADREQUEST',
            '@type': 'error',
            '@authorizer': {
                'account_id': self.account.id,
            },
        }