-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restore 'Dataset' class. #660
Changes from 4 commits
d534fbf
c368db3
317ddfd
bb07591
2268a56
2f269ae
a14a936
3d2699e
510836a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Copyright 2014 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Convenience wrapper for invoking APIs/factories w/ a dataset ID.""" | ||
|
||
from gcloud.datastore.api import delete | ||
from gcloud.datastore.api import get | ||
from gcloud.datastore.api import put | ||
from gcloud.datastore.batch import Batch | ||
from gcloud.datastore.key import Key | ||
from gcloud.datastore.query import Query | ||
from gcloud.datastore.transaction import Transaction | ||
|
||
|
||
class Dataset(object): | ||
"""Convenience wrapper for invoking APIs/factories w/ a dataset ID. | ||
|
||
:type dataset_id: string | ||
:param dataset_id: (required) dataset ID to pass to proxied API methods. | ||
|
||
:type connection: :class:`gcloud.datastore.connection.Connection`, or None | ||
:param connection: (optional) connection to pass to proxied API methods | ||
""" | ||
|
||
def __init__(self, dataset_id, connection=None): | ||
if dataset_id is None: | ||
raise ValueError('dataset_id required') | ||
self.dataset_id = dataset_id | ||
self.connection = connection | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
def get(self, keys, missing=None, deferred=None): | ||
"""Proxy to :func:`gcloud.datastore.api.get`. | ||
|
||
Passes our ``dataset_id``. | ||
""" | ||
return get(keys, missing, deferred, self.connection, self.dataset_id) | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
def put(self, entities): | ||
"""Proxy to :func:`gcloud.datastore.api.put`. | ||
|
||
Passes our ``dataset_id``. | ||
""" | ||
return put(entities, self.connection, dataset_id=self.dataset_id) | ||
|
||
def delete(self, keys): | ||
"""Proxy to :func:`gcloud.datastore.api.delete`. | ||
|
||
Passes our ``dataset_id``. | ||
""" | ||
return delete(keys, self.connection, dataset_id=self.dataset_id) | ||
|
||
def key(self, *path_args, **kwargs): | ||
"""Proxy to :func:`gcloud.datastore.key.Key`. | ||
|
||
Passes our ``dataset_id``. | ||
""" | ||
dataset_id = kwargs.pop('dataset_id', None) | ||
if dataset_id not in (None, self.dataset_id): | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
raise ValueError('Conflicting dataset_id') | ||
kwargs['dataset_id'] = self.dataset_id | ||
return Key(*path_args, **kwargs) | ||
|
||
def batch(self): | ||
"""Proxy to :func:`gcloud.datastore.batch.Batch`. | ||
|
||
Passes our ``dataset_id``. | ||
""" | ||
return Batch(dataset_id=self.dataset_id, connection=self.connection) | ||
|
||
def transaction(self): | ||
"""Proxy to :func:`gcloud.datastore.transaction.Transaction`. | ||
|
||
Passes our ``dataset_id``. | ||
""" | ||
return Transaction(dataset_id=self.dataset_id, | ||
connection=self.connection) | ||
|
||
def query(self, | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
kind=None, | ||
namespace=None, | ||
ancestor=None, | ||
filters=(), | ||
projection=(), | ||
order=(), | ||
group_by=()): | ||
"""Proxy to :func:`gcloud.datastore.query.Query`. | ||
|
||
Passes our ``dataset_id``. | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
""" | ||
return Query(self.dataset_id, kind, namespace, ancestor, filters, | ||
projection, order, group_by) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
# Copyright 2014 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest2 | ||
|
||
|
||
class TestDataset(unittest2.TestCase): | ||
|
||
DATASET_ID = 'DATASET' | ||
|
||
def _getTargetClass(self): | ||
from gcloud.datastore.dataset import Dataset | ||
return Dataset | ||
|
||
def _makeOne(self, dataset_id=DATASET_ID, connection=None): | ||
return self._getTargetClass()(dataset_id, connection) | ||
|
||
def test_ctor_w_dataset_id_None(self): | ||
self.assertRaises(ValueError, self._makeOne, None) | ||
|
||
def test_ctor_w_dataset_id_no_connection(self): | ||
dataset = self._makeOne() | ||
self.assertEqual(dataset.dataset_id, self.DATASET_ID) | ||
|
||
def test_ctor_w_dataset_id_w_connection(self): | ||
conn = object() | ||
dataset = self._makeOne(connection=conn) | ||
self.assertEqual(dataset.dataset_id, self.DATASET_ID) | ||
self.assertTrue(dataset.connection is conn) | ||
|
||
def test_get_defaults(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
|
||
_called_with = [] | ||
|
||
def _get(*args, **kw): | ||
_called_with.append((args, kw)) | ||
|
||
dataset = self._makeOne() | ||
key = object() | ||
|
||
with _Monkey(MUT, get=_get): | ||
dataset.get([key]) | ||
|
||
args = ([key], None, None, None, self.DATASET_ID) | ||
self.assertEqual(_called_with, [(args, {})]) | ||
|
||
def test_get_explicit(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
|
||
_called_with = [] | ||
|
||
def _get(*args, **kw): | ||
_called_with.append((args, kw)) | ||
|
||
conn = object() | ||
dataset = self._makeOne(connection=conn) | ||
key, missing, deferred = object(), [], [] | ||
|
||
with _Monkey(MUT, get=_get): | ||
dataset.get([key], missing, deferred) | ||
|
||
args = ([key], missing, deferred, conn, self.DATASET_ID) | ||
self.assertEqual(_called_with, [(args, {})]) | ||
|
||
def test_put_wo_connection(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
|
||
_called_with = [] | ||
|
||
def _put(*args, **kw): | ||
_called_with.append((args, kw)) | ||
|
||
dataset = self._makeOne() | ||
entity = object() | ||
|
||
with _Monkey(MUT, put=_put): | ||
dataset.put([entity]) | ||
|
||
self.assertEqual(_called_with, | ||
[(([entity], None), {'dataset_id': self.DATASET_ID})]) | ||
|
||
def test_put_w_connection(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
|
||
_called_with = [] | ||
|
||
def _put(*args, **kw): | ||
_called_with.append((args, kw)) | ||
|
||
entity, conn = object(), object() | ||
dataset = self._makeOne(connection=conn) | ||
|
||
with _Monkey(MUT, put=_put): | ||
dataset.put([entity]) | ||
|
||
self.assertEqual(_called_with, | ||
[(([entity], conn), {'dataset_id': self.DATASET_ID})]) | ||
|
||
def test_delete_wo_connection(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
|
||
_called_with = [] | ||
|
||
def _delete(*args, **kw): | ||
_called_with.append((args, kw)) | ||
|
||
dataset = self._makeOne() | ||
key = object() | ||
|
||
with _Monkey(MUT, delete=_delete): | ||
dataset.delete([key]) | ||
|
||
self.assertEqual(_called_with, | ||
[(([key], None), {'dataset_id': self.DATASET_ID})]) | ||
|
||
def test_delete_w_connection(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
|
||
_called_with = [] | ||
|
||
def _delete(*args, **kw): | ||
_called_with.append((args, kw)) | ||
|
||
key, conn = object(), object() | ||
dataset = self._makeOne(connection=conn) | ||
with _Monkey(MUT, delete=_delete): | ||
dataset.delete([key]) | ||
|
||
self.assertEqual(_called_with, | ||
[(([key], conn), {'dataset_id': self.DATASET_ID})]) | ||
|
||
def test_key_w_conflicting_dataset_id(self): | ||
KIND = 'KIND' | ||
ID = 1234 | ||
dataset = self._makeOne() | ||
self.assertRaises(ValueError, | ||
dataset.key, KIND, ID, dataset_id='OTHER') | ||
|
||
def test_key_w_matching_dataset_id(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
KIND = 'KIND' | ||
ID = 1234 | ||
dataset = self._makeOne() | ||
|
||
with _Monkey(MUT, Key=_Dummy): | ||
key = dataset.key(KIND, ID, dataset_id=self.DATASET_ID) | ||
|
||
self.assertTrue(isinstance(key, _Dummy)) | ||
self.assertEqual(key.args, (KIND, ID)) | ||
self.assertEqual(key.kwargs, {'dataset_id': self.DATASET_ID}) | ||
|
||
def test_key_wo_dataset_id(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
KIND = 'KIND' | ||
ID = 1234 | ||
dataset = self._makeOne() | ||
|
||
with _Monkey(MUT, Key=_Dummy): | ||
key = dataset.key(KIND, ID) | ||
|
||
self.assertTrue(isinstance(key, _Dummy)) | ||
self.assertEqual(key.args, (KIND, ID)) | ||
self.assertEqual(key.kwargs, {'dataset_id': self.DATASET_ID}) | ||
|
||
def test_batch_wo_connection(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
dataset = self._makeOne() | ||
|
||
with _Monkey(MUT, Batch=_Dummy): | ||
batch = dataset.batch() | ||
|
||
self.assertTrue(isinstance(batch, _Dummy)) | ||
self.assertEqual(batch.args, ()) | ||
self.assertEqual(batch.kwargs, | ||
{'dataset_id': self.DATASET_ID, 'connection': None}) | ||
|
||
def test_batch_w_connection(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
conn = object() | ||
dataset = self._makeOne(connection=conn) | ||
|
||
with _Monkey(MUT, Batch=_Dummy): | ||
batch = dataset.batch() | ||
|
||
self.assertTrue(isinstance(batch, _Dummy)) | ||
self.assertEqual(batch.args, ()) | ||
self.assertEqual(batch.kwargs, | ||
{'dataset_id': self.DATASET_ID, 'connection': conn}) | ||
|
||
def test_transaction_wo_connection(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
dataset = self._makeOne() | ||
|
||
with _Monkey(MUT, Transaction=_Dummy): | ||
xact = dataset.transaction() | ||
|
||
self.assertTrue(isinstance(xact, _Dummy)) | ||
self.assertEqual(xact.args, ()) | ||
self.assertEqual(xact.kwargs, | ||
{'dataset_id': self.DATASET_ID, 'connection': None}) | ||
|
||
def test_transaction_w_connection(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
conn = object() | ||
dataset = self._makeOne(connection=conn) | ||
|
||
with _Monkey(MUT, Transaction=_Dummy): | ||
xact = dataset.transaction() | ||
|
||
self.assertTrue(isinstance(xact, _Dummy)) | ||
self.assertEqual(xact.args, ()) | ||
self.assertEqual(xact.kwargs, | ||
{'dataset_id': self.DATASET_ID, 'connection': conn}) | ||
|
||
def test_query_w_defaults(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
dataset = self._makeOne() | ||
|
||
with _Monkey(MUT, Query=_Dummy): | ||
query = dataset.query() | ||
|
||
self.assertTrue(isinstance(query, _Dummy)) | ||
args = (self.DATASET_ID, None, None, None, (), (), (), ()) | ||
self.assertEqual(query.args, args) | ||
self.assertEqual(query.kwargs, {}) | ||
|
||
def test_query_explicit(self): | ||
from gcloud.datastore import dataset as MUT | ||
from gcloud._testing import _Monkey | ||
KIND = 'KIND' | ||
NAMESPACE = 'NAMESPACE' | ||
ANCESTOR = object() | ||
FILTERS = [('PROPERTY', '==', 'VALUE')] | ||
PROJECTION = ['__key__'] | ||
ORDER = ['PROPERTY'] | ||
GROUP_BY = ['GROUPBY'] | ||
dataset = self._makeOne() | ||
|
||
with _Monkey(MUT, Query=_Dummy): | ||
query = dataset.query(KIND, NAMESPACE, ANCESTOR, FILTERS, | ||
PROJECTION, ORDER, GROUP_BY) | ||
|
||
self.assertTrue(isinstance(query, _Dummy)) | ||
args = (self.DATASET_ID, KIND, NAMESPACE, ANCESTOR, FILTERS, | ||
PROJECTION, ORDER, GROUP_BY) | ||
self.assertEqual(query.args, args) | ||
self.assertEqual(query.kwargs, {}) | ||
|
||
|
||
class _Dummy(object): | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.args = args | ||
self.kwargs = kwargs |
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.