def test(tiny_view, database): query = Query(database, 'all_lists') result = query.retrieve() assert result is True assert '// Hello World' in query.data['views']['tiny']['map']
def food_query(database): """ Populates the database with some foods and builds a query, all written to Sync Gateway. View does not need hotting up because docs are in place when it is created. Returns: Query: With 'all' view populated where key will search for the foods where the first letter of the name of the food matches. """ for name, data in [ ('lightbulb', { 'type': 'fixture', 'name': 'Lightbulb', }), ('apple', { 'type': 'food', 'name': 'apple', }), ('banana', { 'type': 'food', 'name': 'banana', }), ('apricot', { 'type': 'food', 'name': 'apricot', }), ('walrus', { 'type': 'animal', 'name': 'I AM THE WALRUS', }), ('almond', { 'type': 'food', 'name': 'almond', }), ('pumpkin', { 'type': 'food', 'name': 'pumpkin', }), ]: doc = database.get_document(name) doc.data = data doc.create_update() query = Query(database, 'food_index') query.data = { 'views': { 'all': { 'map': """ function(doc, meta) { if(doc.type == "food" && doc.name) { emit(doc.name[0], doc) } } """, }, }, } query.create_update() return query
def test(query, database): view_func = """ function (doc, meta){ if(meta.id.indexOf("projects")>-1) { emit(meta.id.substring(meta.id.indexOf("|") + 1), doc); } } """ data = { 'views': { 'all_lists': { 'map': view_func, }, }, } query.data = data result = query.create_update() assert result is True clean_query = Query(database, 'all_lists') clean_query.retrieve() assert list(clean_query.data) == ['views'] assert list(clean_query.data['views']) == ['all_lists'] assert list(clean_query.data['views']['all_lists']) == ['map'] assert view_func in clean_query.data['views']['all_lists']['map']
def all_query(database_with_doc): """ Returns: Query: Called 'all' and containing a single view called 'everything' to retrieve all documents (just like 'all_docs' but manual), written to Sync Gateway. """ query = Query(database_with_doc, "all") query.data = { "views": { "everything": { "map": "function(doc,meta){emit(meta.id,doc);}" } }, } query.create_update() return query
def test_update(existing_query, database): existing_query.data = { 'views': { 'one': { 'map': 'function(doc, meta){1==1;}', }, }, } result = existing_query.create_update() assert result is True clean_query = Query(database, 'all_lists') clean_query.retrieve() assert list(clean_query.data) == ['views'] assert list(clean_query.data['views']) == ['one'] assert list(clean_query.data['views']['one']) == ['map'] assert 'function(doc, meta){1==1;}' in clean_query.data['views']['one']['map']
def slow_view(database): """ A view that returns all documents, but slowly. This uses a horrible sleep-like function that locks up Walrus for 1.5s per document. Fixture populates the database with a document to ensure that calling the view takes at least 1 second in total. NOTE: On Circle, it looks like processing the view might be done in parallel because it is able to return a view containing 2 documents in just over the time in the delay function. Returns: Query: Called 'slow_lists', written to Sync Gateway, with a single view called 'all' that takes 1.5 second per document in the database. """ database.get_document('a').create_update() query = Query(database, 'slow_lists') query.data = { 'views': { 'all': { 'map': """ function(doc, meta) { function pausecomp(millis){ var date = new Date(); var curDate = null; do { curDate = new Date(); } while(curDate-date < millis); } pausecomp(1500); emit(meta.id,doc); } """, }, }, } query.create_update() return query
def query(database): """ Returns: Query: Not written to Sync Gateway. """ return Query(database, 'all_lists')
def test(database): result = database.get_query('test') assert result == Query(database, 'test')
def test(database): query = Query(database, '__DESIGN_DOC_ID__') result = query.build_view_url('__VIEW_NAME__') assert result == 'http://localhost:4985/db/_design/__DESIGN_DOC_ID__/_view/__VIEW_NAME__'
def test(database): result = Query(database, '__DESIGN_DOC_ID__') assert result.doc_id == '__DESIGN_DOC_ID__' assert result.url == 'http://localhost:4985/db/_design/__DESIGN_DOC_ID__'