def test_table(mock_ast):
    mock_ast.Table.return_value = Mock()

    result = query.table("foo", foo="foo")
    mock_ast.Table.assert_called_once_with("foo", foo="foo")

    assert result == mock_ast.Table.return_value
Пример #2
0
 def put(self):
     """ Serialize this document to JSON and put it in the database
     """
     with self._get_connection() as conn:
         result = table(self._table_name()).insert(self._to_db(), conflict="update").run(conn)
     if 'errors' in result and result['errors'] > 0:
         raise IOError(dumps(result))
     elif result['inserted'] == 1.0:
         self.id = result.get('generated_keys', [self.id])[0]
     return result
Пример #3
0
    def _sync_table(cls):
        """ Create a table for this model if it doesn't already exist, override
        the table name by setting _table on the class. Updates simple indexes as
        defined by the properties.

        Complex indexes need to be created separately.
        """
        if cls.__name__ == 'Model':
            return  # skip call on this class

        with cls._get_connection() as conn:
            tables = table_list().run(conn)
            if not cls._table_name() in tables:
                table_create(cls._table_name()).run(conn)

            indexes = table(cls._table_name()).index_list().run(conn)
            for name, attr in cls._meta.iteritems():
                if attr._indexed and attr._name not in indexes:
                    table(cls._table_name()).index_create(attr._name).run(conn)
                elif not attr._indexed and attr._name in indexes:
                    table(cls._table_name()).index_drop(attr._name).run(conn, noreply=True)
Пример #4
0
 def query(cls):
     """ The rethinkdb query object. Exposes RQL queries for this table
     """
     return table(cls._table_name())