def test_write_permacache_sr(self): stdin = StringIO("\t".join([ "sr/link/top/hour/1", "1", "100.0", "t3_1", ])) stdout = StringIO() MrTop.write_permacache(fd=stdin, out=stdout) self.queries._get_links.assert_called_once_with(1, "top", "hour")
def test_write_permacache_sr(self): stdin = StringIO("\t".join([ "sr/link/top/hour/1", "1", "100.0", "t3_1", ])) stdout = StringIO() MrTop.write_permacache(fd=stdin, out=stdout) self.queries._get_links.assert_called_once_with(1, "top", "hour")
def test_write_permacache_domain(self): stdin = StringIO("\t".join([ "domain/link/top/hour/foo.com", "1", "100.0", "t3_1", ])) stdout = StringIO() MrTop.write_permacache(fd=stdin, out=stdout) self.queries.get_domain_links.assert_called_once_with( "foo.com", "top", "hour")
def test_write_permacache_domain(self): stdin = StringIO("\t".join([ "domain/link/top/hour/foo.com", "1", "100.0", "t3_1", ])) stdout = StringIO() MrTop.write_permacache(fd=stdin, out=stdout) self.queries.get_domain_links.assert_called_once_with( "foo.com", "top", "hour" )
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_write_no_limiting(self): stdin = StringIO("\n".join([ "\t".join([ "sr/link/top/hour/1", "1", "100.0", "t3_1", ]), "\t".join([ "sr/link/top/hour/1", "1", "100.0", "t3_2", ]), ])) stdout = StringIO() query = self.queries._get_links.return_value = MagicMock() MrTop.write_permacache(fd=stdin, out=stdout) self.assertEqual(len(query._replace.call_args[0][0]), 2)
def test_write_permacache_unknown(self): stdin = StringIO("\t".join([ "something/link/top/hour/1", "1", "100.0", "t3_1", ])) stdout = StringIO() self.assertRaises( AssertionError, lambda: MrTop.write_permacache(fd=stdin, out=stdout), )
def test_write_no_limiting(self): stdin = StringIO("\n".join([ "\t".join([ "sr/link/top/hour/1", "1", "100.0", "t3_1", ]), "\t".join([ "sr/link/top/hour/1", "1", "100.0", "t3_2", ]), ])) stdout = StringIO() query = self.queries._get_links.return_value = MagicMock() MrTop.write_permacache(fd=stdin, out=stdout) self.assertEqual(len(query._replace.call_args[0][0]), 2)
def test_write_permacache_unknown(self): stdin = StringIO("\t".join([ "something/link/top/hour/1", "1", "100.0", "t3_1", ])) stdout = StringIO() self.assertRaises( AssertionError, lambda: MrTop.write_permacache(fd=stdin, out=stdout), )
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, {})