本文整理汇总了Python中cloudant.design_document.DesignDocument.get_view方法的典型用法代码示例。如果您正苦于以下问题:Python DesignDocument.get_view方法的具体用法?Python DesignDocument.get_view怎么用?Python DesignDocument.get_view使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cloudant.design_document.DesignDocument
的用法示例。
在下文中一共展示了DesignDocument.get_view方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_view
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_get_view(self):
"""
Test retrieval of a view from the DesignDocument
"""
ddoc = DesignDocument(self.db, "_design/ddoc001")
view_map = "function (doc) {\n emit(doc._id, 1);\n}"
view_reduce = "_count"
ddoc.add_view("view001", view_map)
ddoc.add_view("view002", view_map, view_reduce)
ddoc.add_view("view003", view_map)
self.assertIsInstance(ddoc.get_view("view002"), View)
self.assertEqual(
ddoc.get_view("view002"), {"map": "function (doc) {\n emit(doc._id, 1);\n}", "reduce": "_count"}
)
示例2: test_view_callable_with_invalid_javascript
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_view_callable_with_invalid_javascript(self):
"""
Test error condition when Javascript errors exist. This test is only
valid for CouchDB because the map function Javascript is validated on
the Cloudant server when attempting to save a design document so invalid
Javascript is not possible there.
"""
self.populate_db_with_documents()
ddoc = DesignDocument(self.db, 'ddoc001')
ddoc.add_view(
'view001',
'This is not valid Javascript'
)
ddoc.save()
# Verify that the ddoc and view were saved remotely
# along with the invalid Javascript
del ddoc
ddoc = DesignDocument(self.db, 'ddoc001')
ddoc.fetch()
view = ddoc.get_view('view001')
self.assertEqual(view.map, 'This is not valid Javascript')
try:
for row in view.result:
self.fail('Above statement should raise an Exception')
except requests.HTTPError as err:
self.assertEqual(err.response.status_code, 500)
示例3: test_get_view
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_get_view(self):
"""
Test retrieval of a view from the DesignDocument
"""
ddoc = DesignDocument(self.db, '_design/ddoc001')
view_map = 'function (doc) {\n emit(doc._id, 1);\n}'
view_reduce = '_count'
ddoc.add_view('view001', view_map)
ddoc.add_view('view002', view_map, view_reduce)
ddoc.add_view('view003', view_map)
self.assertIsInstance(ddoc.get_view('view002'), View)
self.assertEqual(
ddoc.get_view('view002'),
{
'map': 'function (doc) {\n emit(doc._id, 1);\n}',
'reduce': '_count'
}
)
示例4: test_make_result
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_make_result(self):
"""
Ensure that the view results are wrapped in a Result object
"""
self.populate_db_with_documents()
ddoc = DesignDocument(self.db, "ddoc001")
ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}")
ddoc.save()
view = ddoc.get_view("view001")
self.assertIsInstance(view.make_result(), Result)
示例5: raw_view
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def raw_view(self, view_path, params):
params = deepcopy(params)
params.pop('dynamic_properties', None)
if view_path == '_all_docs':
return self.cloudant_database.all_docs(**params)
else:
view_path = view_path.split('/')
assert len(view_path) == 4
ddoc = DesignDocument(self.cloudant_database, view_path[1])
ddoc.fetch()
view = ddoc.get_view(view_path[3])
return view(**params)
示例6: test_view_callable_raw_json
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_view_callable_raw_json(self):
"""
Test that the View __call__ method which is invoked by calling the
view object returns the appropriate raw JSON response.
"""
self.populate_db_with_documents()
ddoc = DesignDocument(self.db, "ddoc001")
ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}")
ddoc.save()
view = ddoc.get_view("view001")
ids = []
# view(limit=3) calls the view object and passes it the limit parameter
for row in view(limit=3)["rows"]:
ids.append(row["id"])
expected = ["julia000", "julia001", "julia002"]
self.assertTrue(all(x in ids for x in expected))
示例7: test_view_callable_view_result
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_view_callable_view_result(self):
"""
Test that by referencing the .result attribute the view callable
method is invoked and the data returned is wrapped as a Result.
"""
self.populate_db_with_documents()
ddoc = DesignDocument(self.db, "ddoc001")
ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}")
ddoc.save()
view = ddoc.get_view("view001")
rslt = view.result
self.assertIsInstance(rslt, Result)
ids = []
# rslt[:3] limits the Result to the first 3 elements
for row in rslt[:3]:
ids.append(row["id"])
expected = ["julia000", "julia001", "julia002"]
self.assertTrue(all(x in ids for x in expected))
示例8: test_custom_result_context_manager
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_custom_result_context_manager(self):
"""
Test that the context manager for custom results returns
the expected Results
"""
self.populate_db_with_documents()
ddoc = DesignDocument(self.db, "ddoc001")
ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}")
ddoc.save()
view = ddoc.get_view("view001")
# Return a custom result by including documents
with view.custom_result(include_docs=True, reduce=False) as rslt:
i = 0
for row in rslt:
self.assertEqual(row["doc"]["_id"], "julia{0:03d}".format(i))
self.assertTrue(row["doc"]["_rev"].startswith("1-"))
self.assertEqual(row["doc"]["name"], "julia")
self.assertEqual(row["doc"]["age"], i)
i += 1
self.assertEqual(i, 100)
示例9: test_get_view_callable_raw_json
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_get_view_callable_raw_json(self):
"""
Test that the GET request of the View __call__ method that is invoked
when calling the view object returns the appropriate raw JSON response.
"""
self.populate_db_with_documents()
ddoc = DesignDocument(self.db, 'ddoc001')
ddoc.add_view(
'view001',
'function (doc) {\n emit(doc._id, 1);\n}'
)
ddoc.save()
view = ddoc.get_view('view001')
ids = []
# view(limit=3) calls the view object and passes it the limit parameter
# where a HTTP GET request is made.
for row in view(limit=3)['rows']:
ids.append(row['id'])
expected = ['julia000', 'julia001', 'julia002']
self.assertTrue(all(x in ids for x in expected))
示例10: test_custom_result_context_manager
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_custom_result_context_manager(self):
"""
Test that the context manager for custom results returns
the expected Results
"""
self.populate_db_with_documents()
ddoc = DesignDocument(self.db, 'ddoc001')
ddoc.add_view(
'view001',
'function (doc) {\n emit(doc._id, 1);\n}'
)
ddoc.save()
view = ddoc.get_view('view001')
# Return a custom result by including documents
with view.custom_result(include_docs=True, reduce=False) as rslt:
i = 0
for row in rslt:
self.assertEqual(row['doc']['_id'], 'julia{0:03d}'.format(i))
self.assertTrue(row['doc']['_rev'].startswith('1-'))
self.assertEqual(row['doc']['name'], 'julia')
self.assertEqual(row['doc']['age'], i)
i += 1
self.assertEqual(i, 100)
示例11: test_view_callable_with_invalid_javascript
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_view_callable_with_invalid_javascript(self):
"""
Test error condition when Javascript errors exist
"""
self.populate_db_with_documents()
ddoc = DesignDocument(self.db, 'ddoc001')
ddoc.add_view(
'view001',
'This is not valid Javascript'
)
ddoc.save()
# Verify that the ddoc and view were saved remotely
# along with the invalid Javascript
del ddoc
ddoc = DesignDocument(self.db, 'ddoc001')
ddoc.fetch()
view = ddoc.get_view('view001')
self.assertEqual(view.map, 'This is not valid Javascript')
try:
for row in view.result:
self.fail('Above statement should raise an Exception')
except requests.HTTPError, err:
self.assertEqual(err.response.status_code, 500)
示例12: test_post_view_callable_raw_json_multiple_params
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
def test_post_view_callable_raw_json_multiple_params(self):
"""
Using "keys" and other parameters test that the POST request of the View
__call__ method that is invoked when calling the view object returns the
appropriate raw JSON response.
"""
# Create 200 documents with ids julia000, julia001, julia002, ..., julia199
self.populate_db_with_documents(200)
# Generate keys list for every other document created
# with ids julia000, julia002, julia004, ..., julia198
keys_list = ['julia{0:03d}'.format(i) for i in range(0, 200, 2)]
self.assertEqual(len(keys_list), 100)
ddoc = DesignDocument(self.db, 'ddoc001')
ddoc.add_view(
'view001',
'function (doc) {\n emit(doc._id, 1);\n}'
)
ddoc.save()
view = ddoc.get_view('view001')
# view(keys=keys_list, limit=3) calls the view object and passes keys
# and limit parameters
ids = [row['id'] for row in view(keys=keys_list, limit=3)['rows']]
self.assertTrue(all(x in ids for x in ['julia000', 'julia002', 'julia004']))
示例13: UnitTestDbBase
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
#.........这里部分代码省略.........
self.account = os.environ.get('CLOUDANT_ACCOUNT')
self.user = os.environ.get('DB_USER')
self.pwd = os.environ.get('DB_PASSWORD')
self.url = os.environ.get(
'DB_URL',
'https://{0}.cloudant.com'.format(self.account))
self.client = Cloudant(
self.user,
self.pwd,
url=self.url,
x_cloudant_user=self.account)
def tearDown(self):
"""
Ensure the client is new for each test
"""
del self.client
def db_set_up(self):
"""
Set up test attributes for Database tests
"""
self.client.connect()
self.test_dbname = self.dbname()
self.db = self.client._DATABASE_CLASS(self.client, self.test_dbname)
self.db.create()
def db_tear_down(self):
"""
Reset test attributes for each test
"""
self.db.delete()
self.client.disconnect()
del self.test_dbname
del self.db
def dbname(self, database_name='db'):
return '{0}-{1}'.format(database_name, unicode_(uuid.uuid4()))
def populate_db_with_documents(self, doc_count=100, **kwargs):
off_set = kwargs.get('off_set', 0)
docs = [
{'_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i}
for i in range(off_set, off_set + doc_count)
]
return self.db.bulk_docs(docs)
def create_views(self):
"""
Create a design document with views for use with tests.
"""
self.ddoc = DesignDocument(self.db, 'ddoc001')
self.ddoc.add_view(
'view001',
'function (doc) {\n emit(doc._id, 1);\n}'
)
self.ddoc.add_view(
'view002',
'function (doc) {\n emit(doc._id, 1);\n}',
'_count'
)
self.ddoc.add_view(
'view003',
'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}'
)
self.ddoc.add_view(
'view004',
'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}',
'_count'
)
self.ddoc.add_view(
'view005',
'function (doc) {\n emit([doc.name, doc.age], 1);\n}'
)
self.ddoc.add_view(
'view006',
'function (doc) {\n emit([doc.name, doc.age], 1);\n}',
'_count'
)
self.ddoc.save()
self.view001 = self.ddoc.get_view('view001')
self.view002 = self.ddoc.get_view('view002')
self.view003 = self.ddoc.get_view('view003')
self.view004 = self.ddoc.get_view('view004')
self.view005 = self.ddoc.get_view('view005')
self.view006 = self.ddoc.get_view('view006')
def create_search_index(self):
"""
Create a design document with search indexes for use
with search query tests.
"""
self.search_ddoc = DesignDocument(self.db, 'searchddoc001')
self.search_ddoc['indexes'] = {'searchindex001': {
'index': 'function (doc) {\n index("default", doc._id); \n '
'if (doc.name) {\n index("name", doc.name, {"store": true}); \n} '
'if (doc.age) {\n index("age", doc.age, {"facet": true}); \n} \n} '
}
}
self.search_ddoc.save()
示例14: ResultTests
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
class ResultTests(UnitTestDbBase):
"""
Result unit tests
"""
# TODO more Result test cases to be added to completely cover issue #42
def setUp(self):
"""
Set up test attributes
"""
super(ResultTests, self).setUp()
self.db_set_up()
self.ddoc = DesignDocument(self.db, 'ddoc001')
self.ddoc.add_view(
'view001',
'function (doc) {\n emit(doc._id, 1);\n}',
'_count'
)
self.ddoc.save()
def tearDown(self):
"""
Reset test attributes
"""
self.db_tear_down()
super(ResultTests, self).tearDown()
def test_constructor(self):
"""
Test instantiating a Result
"""
result = Result(
self.ddoc.get_view('view001'),
startkey='1',
endkey='9',
page_size=1000
)
self.assertIsInstance(result, Result)
self.assertEquals(result.options, {'startkey': '1', 'endkey': '9'})
def test_group_level(self):
"""
Test group_level option in Result
"""
self.populate_db_with_documents(10)
result_set = Result(self.ddoc.get_view('view001'), group_level=1)
self.assertIsInstance(result_set, Result)
self.assertEquals(result_set.options, {'group_level': 1})
# Test Result iteration
i = 0
for result in result_set:
self.assertEqual(
result,
{'key': 'julia{0:03d}'.format(i), 'value': 1}
)
i += 1
# Test Result key retrieval
self.assertEqual(
result_set['julia000'],
[{'key': 'julia000', 'value': 1}]
)
# Test Result key slicing
self.assertEqual(
result_set['julia001': 'julia003'],
[
{'key': 'julia001', 'value': 1},
{'key': 'julia002', 'value': 1},
{'key': 'julia003', 'value': 1}
]
)
# Test Result element slicing
self.assertEqual(
result_set[9:],
[
{'key': 'julia009', 'value': 1},
]
)
示例15: UnitTestDbBase
# 需要导入模块: from cloudant.design_document import DesignDocument [as 别名]
# 或者: from cloudant.design_document.DesignDocument import get_view [as 别名]
#.........这里部分代码省略.........
docs = [
{'_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i}
for i in range(off_set, off_set + doc_count)
]
return self.db.bulk_docs(docs)
def create_views(self):
"""
Create a design document with views for use with tests.
"""
self.ddoc = DesignDocument(self.db, 'ddoc001')
self.ddoc.add_view(
'view001',
'function (doc) {\n emit(doc._id, 1);\n}'
)
self.ddoc.add_view(
'view002',
'function (doc) {\n emit(doc._id, 1);\n}',
'_count'
)
self.ddoc.add_view(
'view003',
'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}'
)
self.ddoc.add_view(
'view004',
'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}',
'_count'
)
self.ddoc.add_view(
'view005',
'function (doc) {\n emit([doc.name, doc.age], 1);\n}'
)
self.ddoc.add_view(
'view006',
'function (doc) {\n emit([doc.name, doc.age], 1);\n}',
'_count'
)
self.ddoc.save()
self.view001 = self.ddoc.get_view('view001')
self.view002 = self.ddoc.get_view('view002')
self.view003 = self.ddoc.get_view('view003')
self.view004 = self.ddoc.get_view('view004')
self.view005 = self.ddoc.get_view('view005')
self.view006 = self.ddoc.get_view('view006')
def create_search_index(self):
"""
Create a design document with search indexes for use
with search query tests.
"""
self.search_ddoc = DesignDocument(self.db, 'searchddoc001')
self.search_ddoc['indexes'] = {'searchindex001': {
'index': 'function (doc) {\n index("default", doc._id); \n '
'if (doc.name) {\n index("name", doc.name, {"store": true}); \n} '
'if (doc.age) {\n index("age", doc.age, {"facet": true}); \n} \n} '
}
}
self.search_ddoc.save()
def load_security_document_data(self):
"""
Create a security document in the specified database and assign
attributes to be used during unit tests
"""
self.sdoc = {
'admins': {'names': ['foo'], 'roles': ['admins']},
'members': {'names': ['foo1', 'foo2'], 'roles': ['developers']}
}
self.mod_sdoc = {
'admins': {'names': ['bar'], 'roles': ['admins']},
'members': {'names': ['bar1', 'bar2'], 'roles': ['developers']}
}
if os.environ.get('RUN_CLOUDANT_TESTS') is not None:
self.sdoc = {
'cloudant': {
'foo1': ['_reader', '_writer'],
'foo2': ['_reader']
}
}
self.mod_sdoc = {
'cloudant': {
'bar1': ['_reader', '_writer'],
'bar2': ['_reader']
}
}
if os.environ.get('ADMIN_PARTY') == 'true':
resp = requests.put(
'/'.join([self.db.database_url, '_security']),
data=json.dumps(self.sdoc),
headers={'Content-Type': 'application/json'}
)
else:
resp = requests.put(
'/'.join([self.db.database_url, '_security']),
auth=(self.user, self.pwd),
data=json.dumps(self.sdoc),
headers={'Content-Type': 'application/json'}
)
self.assertEqual(resp.status_code, 200)