def test_composite_subs_by_title(ctx): from melkman.db.bucket import NewsBucket from melkman.db.composite import Composite, view_composite_subscriptions_by_title from random import shuffle cc = Composite.create(ctx) buckets = [] for i in range(10): bucket = NewsBucket.create(ctx) bucket.title = 'bucket %d' % i bucket.save() buckets.append(bucket) shuffled_buckets = list(buckets) shuffle(shuffled_buckets) for bucket in shuffled_buckets: cc.subscribe(bucket) cc.save() # should come out in alphabetical order for i, row in enumerate(view_composite_subscriptions_by_title(ctx.db, startkey=[cc.id, None], endkey=[cc.id, {}])): assert row.value['bucket_id'] == buckets[i].id assert i + 1 == len(buckets)
def test_init_subscription(ctx): from eventlet import sleep, spawn from melkman.aggregator.worker import run_aggregator from melkman.db.bucket import NewsBucket from melkman.db.composite import Composite agg = spawn(run_aggregator, ctx) c = Composite.create(ctx) c.save() entries = [] bucket = NewsBucket.create(ctx) for i in range(5): eid = random_id() entries.append(eid) bucket.add_news_item(eid) bucket.save() sleep(.5) c.subscribe(bucket) c.save() sleep(.5) c.reload() for eid in entries: assert c.has_news_item(eid) agg.kill() agg.wait()
def test_composites_by_sub(ctx): from melkman.db.bucket import NewsBucket from melkman.db.composite import Composite, view_composites_by_subscription c1 = Composite.create(ctx) c2 = Composite.create(ctx) bucket1 = NewsBucket.create(ctx) bucket1.save() bucket2 = NewsBucket.create(ctx) bucket2.save() bucket3 = NewsBucket.create(ctx) bucket3.save() c1.subscribe(bucket1) c1.save() c2.subscribe(bucket1) c2.subscribe(bucket2) c2.save() count = 0 seen = set() for r in view_composites_by_subscription(ctx.db, include_docs=True, startkey=bucket1.id, endkey=bucket1.id): comp = Composite.wrap(r.doc) seen.add(comp.id) count += 1 assert count == 2 assert c1.id in seen assert c2.id in seen count = 0 seen = set() for r in view_composites_by_subscription(ctx.db, include_docs=True, startkey=bucket2.id, endkey=bucket2.id): comp = Composite.from_doc(r.doc, ctx) seen.add(comp.id) count += 1 assert count == 1 assert c2.id in seen for r in view_composites_by_subscription(ctx.db, include_docs=True, startkey=bucket3.id, endkey=bucket3.id): assert False, 'unexpected subscription'
def test_modified_updates_composite(ctx): from eventlet import sleep, spawn from melkman.aggregator.worker import run_aggregator from melkman.db.bucket import NewsBucket from melkman.db.composite import Composite agg = spawn(run_aggregator, ctx) b = [] c = [] # make some buckets and composites. for i in range(3): bucket = NewsBucket.create(ctx) bucket.save() b.append(bucket) comp = Composite.create(ctx) comp.save() c.append(comp) # set up some subscriptions c[0].subscribe(b[0]) c[1].subscribe(b[1]) c[2].subscribe(c[0]) c[2].subscribe(c[1]) for i in range(3): c[i].save() id1 = random_id() b[0].add_news_item(id1) log.debug("updating bucket 0 (%s) with item %s..." % (b[0].id, id1)) b[0].save() sleep(1) # refresh them from the db... for i in range(3): c[i].reload() assert c[0].has_news_item(id1) assert not c[1].has_news_item(id1) assert c[2].has_news_item(id1) id2 = random_id() b[1].add_news_item(id2) log.debug("updating bucket 1 (%s) with item %s..." % (b[1].id, id2)) b[1].save() sleep(1) # refresh them from the db... for i in range(3): c[i].reload() assert not c[0].has_news_item(id2) assert c[1].has_news_item(id2) assert c[2].has_news_item(id2) id3 = random_id() b[2].add_news_item(id3) log.debug("updating bucket 2 (%s) with item %s..." % (b[2].id, id3)) b[2].save() sleep(1) # refresh them from the db... for i in range(3): c[i].reload() assert not c[0].has_news_item(id3) assert not c[1].has_news_item(id3) assert not c[2].has_news_item(id3) agg.kill() agg.wait()