def test_tweet_ordering(): now = datetime.now(timezone.utc) tweet_1 = Tweet("A", now) tweet_2 = Tweet("B", now + timedelta(hours=1)) tweet_3 = Tweet("C", now + timedelta(hours=2)) tweet_4 = Tweet("D", now + timedelta(hours=2)) tweet_5 = Tweet("D", now + timedelta(hours=2)) source = Source("foo", "bar") # explicit testing with pytest.raises(TypeError): tweet_1 < source with pytest.raises(TypeError): tweet_1 <= source with pytest.raises(TypeError): tweet_1 > source with pytest.raises(TypeError): tweet_1 >= source assert tweet_1 != source assert tweet_1 < tweet_2 assert tweet_1 <= tweet_2 assert tweet_2 > tweet_1 assert tweet_2 >= tweet_1 assert tweet_3 != tweet_4 assert tweet_5 == tweet_4 assert tweet_5 >= tweet_4 assert tweet_5 <= tweet_4 assert not (tweet_3 <= tweet_4) assert not (tweet_3 >= tweet_4)
def tweet(ctx, created_at, twtfile, text): """Append a new tweet to your twtxt file.""" tweet = Tweet(text, created_at) if created_at else Tweet(text) if not add_local_tweet(tweet, twtfile): click.echo("✗ Couldn’t write to file.") else: hook = ctx.obj["conf"].post_tweet_hook if hook: run_post_tweet_hook(hook, ctx.obj["conf"].options)
def test_tweet_relative_datetime(): tweet = Tweet("foobar") assert tweet.relative_datetime == "a moment ago" tweet = Tweet("foobar", datetime.now(timezone.utc) + timedelta(hours=1, minutes=1)) assert tweet.relative_datetime == "an hour from now" tweet = Tweet("foobar", datetime.now(timezone.utc) - timedelta(hours=1, minutes=1)) assert tweet.relative_datetime == "an hour ago"
def test_tweet_init(): with pytest.raises(ValueError) as e: Tweet("") assert "empty text" in str(e.value) with pytest.raises(TypeError) as e: Tweet("foobar", 0) assert "created_at is of invalid type" in str(e.value) source = Source("foo", "bar") created_at = datetime.now(timezone.utc) tweet = Tweet("foobar", created_at, source) assert tweet.text == "foobar" assert tweet.created_at == created_at.replace(microsecond=0) assert tweet.source == source
def tweet(ctx, created_at, twtfile, text): """Append a new tweet to your twtxt file.""" text = expand_mentions(text) tweet = Tweet(text, created_at) if created_at else Tweet(text) pre_tweet_hook = ctx.obj["conf"].pre_tweet_hook if pre_tweet_hook: if not run_pre_tweet_hook(pre_tweet_hook, ctx.obj["conf"].options): click.echo("✗ pre_tweet_hook returned non-zero") raise click.Abort if not add_local_tweet(tweet, twtfile): click.echo("✗ Couldn’t write to file.") else: post_tweet_hook = ctx.obj["conf"].post_tweet_hook if post_tweet_hook: run_post_tweet_hook(post_tweet_hook, ctx.obj["conf"].options)
def parse_tweet(raw_tweet, source, now=None): """ Parses a single raw tweet line from a twtxt file and returns a :class:`Tweet` object. :param str raw_tweet: a single raw tweet line :param Source source: the source of the given tweet :param Datetime now: the current datetime :returns: the parsed tweet :rtype: Tweet """ if now is None: now = datetime.now(timezone.utc) raw_created_at, text = raw_tweet.split("\t", 1) created_at = parse_iso8601(raw_created_at) if created_at > now: raise ValueError("Tweet is from the future") return Tweet(click.unstyle(text.strip()), created_at, source)
def test_tweet_limited_text(): tweet = Tweet("A " * 100) assert tweet.text == "A " * 100 assert len(tweet.limited_text) <= 140
def test_tweet_str(): tweet = Tweet("foobar", datetime(2000, 1, 1, 1, 1, 1, 1, tzinfo=timezone.utc)) assert str(tweet) == "2000-01-01T01:01:01+00:00\tfoobar"
def test_tweet_absolute_datetime(): tweet = Tweet("foobar", datetime(2000, 1, 1, 1, 1, 1, 1, tzinfo=timezone.utc)) assert tweet.absolute_datetime == "Sat, 01 Jan 2000 01:01:01"