def test_time_listings(self): link = make_link( thing_id=1, # strip decimals to avoid string formatting issues timestamp=int(time.time()), author_id=2, url="https://www.foo.com/", sr_id=10, ) l = make_thing_row(["author_id", "sr_id", "url"], link) stdin = StringIO(l) stdout = StringIO() MrTop("link", fd=stdin, out=stdout).time_listings(["hour"]) # order is not important for the response: res = dict(x.split("\t", 1) for x in stdout.getvalue().splitlines()) # all of the values end with the created date and the thing fullname for sort in ("top", "controversial"): value = "{score}\t{timestamp:.1f}\tt3_{thing_id}".format( score="1" if sort == "top" else "0.0", timestamp=link.timestamp, thing_id=link.thing_id) for k in ( "domain/link/{sort}/hour/foo.com", "domain/link/{sort}/hour/www.foo.com", "user/link/{sort}/hour/{author_id}", "sr/link/{sort}/hour/{sr_id}", ): key = k.format(sort=sort, **link) self.assertEqual(res.pop(key), value) self.assertEqual(res, {})
def test_join_things(self): fields = ['author_id', 'sr_id', 'url'] link = make_link(thing_id=1, timestamp=100.01, url="foo", sr_id=10, author_id=2) dump = make_pg_dump(fields, link) stdin = StringIO(dump) stdout = StringIO() MrTop("link", fd=stdin, out=stdout, err=StringIO()).join_things() self.assertEqual(stdout.getvalue().strip(), "1 link 1 0 f f 100.01 2 10 foo".replace(" ", "\t"))
def test_time_listings_too_old(self): link = make_link( thing_id=1, timestamp=100., author_id=2, url="https://www.foo.com/", sr_id=10, ) l = make_thing_row(["author_id", "sr_id", "url"], link) stdin = StringIO(l) stdout = StringIO() MrTop("link", fd=stdin, out=stdout).time_listings(["hour"]) # the item is too old and should be dropped res = dict(x.split("\t", 1) for x in stdout.getvalue().splitlines()) self.assertEqual(res, {})