Ejemplo n.º 1
0
    def test_change_existing_schema(self):
        # First we'll actually create a dataset

        rev = self.create_rev()
        input_schema = self.create_input_schema(rev = rev)
        output_schema = input_schema.get_latest_output_schema()
        (ok, job) = rev.apply(output_schema = output_schema)

        assert ok, job
        done = False
        attempts = 0
        while not done and attempts < 20:
            (ok, job) = job.show()
            attempts += 1
            assert ok, job

            done = job.attributes['status'] == 'successful'
            sleep(0.5)

        assert done, "Polling job never resulted in a successful completion: %s" % job

        (ok, rev) = self.view.revisions.create_replace_revision()
        assert ok, rev
        self.rev.discard()
        self.rev = rev

        # Ok, we've got a dataset.  Let's create a revision on it and mess with its schema!

        (ok, source) = rev.source_from_dataset()
        assert ok, source

        input_schema = source.get_latest_input_schema()
        output_schema = input_schema.get_latest_output_schema()
        (ok, new_output_schema) = output_schema.add_column('d', 'D', 'a + 5')\
                                               .drop_column('b')\
                                               .drop_column('c')\
                                               .run()
        assert ok, new_output_schema

        (ok, job) = rev.apply(output_schema = new_output_schema)
        assert ok, job

        done = False
        attempts = 0
        while not done and attempts < 20:
            (ok, job) = job.show()
            attempts += 1
            assert ok, job

            done = job.attributes['status'] == 'successful'
            sleep(0.5)

        assert done, "Polling job never resulted in a successful completion: %s" % job

        from socrata.http import get

        (ok, result) = get("https://{domain}/id/{fourfour}".format(domain = auth.domain, fourfour = rev.attributes['fourfour']), auth)
        assert ok, result

        self.assertEqual(result, [{'a': '1', 'd': '6'}, {'a': '2', 'd': '7'}, {'a': '3', 'd': '8'}, {'a': '4', 'd': '9'}])
Ejemplo n.º 2
0
    def validate_row_id(self, uri, field_name):
        """
        Set the row id. Note you must call `validate_row_id` before doing this.

        Args:
        ```
            field_name (str): The column to validate as the row id
        ```

        Returns:
        ```
            result (bool, dict): Returns an API Result; where the response says if it can be used as a row id
        ```
        """
        output_column = [
            oc for oc in self.attributes['output_columns']
            if oc['field_name'] == field_name
        ]
        if len(output_column):
            [output_column] = output_column
            transform_id = output_column['transform']['id']

            return get(self.path(uri.format(transform_id=transform_id)),
                       auth=self.auth)
        else:
            return (False, {
                "reason": "No column with field_name = %s" % field_name
            })
Ejemplo n.º 3
0
    def validate_row_id(self, uri, field_name):
        """
        Set the row id. Note you must call `validate_row_id` before doing this.

        Args:
        ```
            field_name (str): The column to validate as the row id
        ```

        Returns:
        ```
            boolean
        ```
        """
        output_column = [
            oc for oc in self.attributes['output_columns']
            if oc['field_name'] == field_name
        ]
        if len(output_column):
            [output_column] = output_column
            transform_id = output_column['transform']['id']

            return get(self.path(uri.format(transform_id=transform_id)),
                       auth=self.auth)['valid']
        else:
            return False
Ejemplo n.º 4
0
 def lookup(self, name):
     """
     Obtain a single ImportConfig by name
     """
     return self._subresource(Config, get(
         self.path() + '/' + name,
         auth = self.auth
     ))
Ejemplo n.º 5
0
 def list(self):
     """
     List all the ImportConfigs on this domain
     """
     return self._subresources(Config, get(
         self.path(),
         auth = self.auth
     ))
Ejemplo n.º 6
0
    def show_input_schema(self, uri, input_schema_id):
        (ok, res) = result = get(self.path(
            uri.format(input_schema_id=input_schema_id)),
                                 auth=self.auth)

        if ok:
            return self._subresource(InputSchema, result)
        return result
Ejemplo n.º 7
0
    def _get_rows(self, uri, offset, limit):
        resp = get(
            self.path(uri),
            params = {'limit': limit, 'offset': offset},
            auth = self.auth
        )

        rows = resp[1:]
        return [self._munge_row(row) for row in rows]
Ejemplo n.º 8
0
 def from_uri(cls, auth, uri):
     path = 'https://{domain}{uri}'.format(
         domain = auth.domain,
         uri = uri
     )
     (ok, resp) = result = get(path, auth = auth)
     if ok:
         return (ok, cls(auth, resp))
     return result
Ejemplo n.º 9
0
    def plan(self, uri):
        """
        Return the list of operations this revision will make when it is applied

        Returns:
        ```
            dict
        ```
        """
        return pluck_resource(get(self.path(uri), auth=self.auth))
Ejemplo n.º 10
0
    def list(self):
        """
        List all the revisions on the view

        Returns:
        ```
            list[Revision]
        ```
        """
        return self._subresources(Revision, get(self.path(), auth=self.auth))
Ejemplo n.º 11
0
    def latest_output(self, uri):
        """
        Get the latest (most recently created) OutputSchema
        which descends from this InputSchema

        Returns:
            OutputSchema
        """
        return self._subresource(OutputSchema, get(
            self.path(uri),
            auth = self.auth,
        ))
Ejemplo n.º 12
0
    def plan(self, uri):
        """
        Return the list of operations this revision will make when it is applied

        Returns:
        ```
            result (bool, dict): The revision plan
        ```
        """
        (ok, resource) = result = get(self.path(uri), auth = self.auth)
        if ok:
            return (ok, resource['resource'])
        return result
Ejemplo n.º 13
0
    def latest_output(self, uri):
        """
        Get the latest (most recently created) OutputSchema
        which descends from this InputSchema

        Returns:
            result (bool, OutputSchema | dict): Returns an API Result; the new OutputSchema or an error response
        """
        return self._subresource(OutputSchema,
                                 get(
                                     self.path(uri),
                                     auth=self.auth,
                                 ))
Ejemplo n.º 14
0
    def schema_errors_csv(self):
        """
        Get the errors that results in transforming into this output schema
        as a CSV stream.

        Note that this returns a Reponse, where Reponse
        is a python requests Reponse object
        """
        return get(
            self.path(self.schema_errors_uri),
            auth = self.auth,
            headers = {'accept': 'text/csv', 'content-type': 'text/csv'},
            stream = True
        )
Ejemplo n.º 15
0
    def lookup(self, fourfour):
        """
        Lookup the view by ID

        Args:
        ```
            fourfour (str): The view's identifier, ex: abcd-1234
        ```
        Returns:
        ```
            result (bool, Revision | dict): Returns an API Result; the View or an error response
        ```
        """
        return self._subresource(
            View, get(self.path() + '/' + fourfour, auth=self.auth))
Ejemplo n.º 16
0
    def lookup(self, source_id):
        """
        Lookup a source

        Args:
        ```
            source_id (int): The id
        ```

        Returns:
        ```
            Source: Returns the new Source The Source resulting from this API call, or an error
        ```
        """
        return self._subresource(
            Source, get(self.path() + '/' + str(source_id), auth=self.auth))
Ejemplo n.º 17
0
    def lookup(self, revision_seq):
        """
        Lookup a revision within the view based on the sequence number

        Args:
        ```
            revision_seq (int): The sequence number of the revision to lookup
        ```

        Returns:
        ```
            Revision The Revision resulting from this API call, or an error
        ```
        """
        return self._subresource(
            Revision, get(self.path() + '/' + str(revision_seq),
                          auth=self.auth))
Ejemplo n.º 18
0
    def lookup(self, fourfour):
        """
        Lookup the view by ID

        Args:
        ```
            fourfour (str): The view's identifier, ex: abcd-1234
        ```
        Returns:
        ```
            View
        ```
        """
        return self._subresource(View, get(
            self.path() + '/' + fourfour,
            auth = self.auth
        ))
Ejemplo n.º 19
0
 def from_uri(cls, auth, uri):
     path = 'https://{domain}{uri}'.format(domain=auth.domain, uri=uri)
     resp = get(path, auth=auth)
     return cls(auth, resp)
Ejemplo n.º 20
0
    def show_input_schema(self, uri, input_schema_id):
        res = get(self.path(uri.format(input_schema_id=input_schema_id)),
                  auth=self.auth)

        return self._subresource(InputSchema, res)
Ejemplo n.º 21
0
 def list_sources(self, uri):
     return self._subresources(Source, get(
         self.path(uri),
         auth = self.auth
     ))
Ejemplo n.º 22
0
 def show(self, uri):
     return self._mutate(get(self.path(uri), auth=self.auth))