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 test_tweet_ordering(): tweet_1 = Tweet("A", 1454493697) tweet_2 = Tweet("B", 1454493698) tweet_3 = Tweet("C", 1454493699) tweet_4 = Tweet("D", 1454493699) tweet_5 = Tweet("D", 1454493699) # explicit testing 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 test_tweet_init(): with pytest.raises(ValueError) as e: tweet = Tweet("", 1454334870) assert "empty text" in str(e.value) with pytest.raises(ValueError) as e: tweet = Tweet("foobar", "invalid") assert "invalid timestamp" in str(e.value) with pytest.raises(ValueError) as e: tweet = Tweet("foobar", 900000000000) assert "invalid timestamp" in str(e.value) source = Source("foo", "bar") tweet = Tweet("foobar", 1454334870, source) assert tweet.text == "foobar" assert tweet.timestamp == 1454334870 assert tweet.source == source
def parse_string(string, source, now=datetime.now(timezone.utc)): """Parses a multi-line string and returns extracted :class:`Tweet` objects.""" tweets = [] for line in string: try: parts = line.partition("\t") created_at = parse_iso8601(parts[0]) text = parts[2].lstrip().rstrip() if created_at > now: raise ValueError("tweet is from the future") tweet = Tweet(text, created_at, source) tweets.append(tweet) except (ValueError, OverflowError) as e: logger.debug(e) return tweets
def parse_string(string, source, limit=None): """Parses a multi-line string and returns extracted :class:`Tweet` objects.""" tweets = [] if limit: string = islice(string, limit) for line in string: try: parts = line.partition("\t") created_at = parse_iso8601(parts[0]) text = parts[2].lstrip().rstrip() tweet = Tweet(text, created_at, source) tweets.append(tweet) except (ValueError, OverflowError) as e: logger.debug(e) return tweets
def test_tweet_limited_text(): tweet = Tweet("A " * 100) assert tweet.text == "A " * 100 assert len(tweet.limited_text) <= 140
def test_tweet_relative_datetime(): tweet = Tweet("foobar") assert tweet.relative_datetime == "a moment ago"
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 tweet(ctx, created_at, output, text): """Append a new tweet to your twtxt file.""" tweet = Tweet(text, created_at) if created_at else Tweet(text) with open(output, "a") as fh: fh.write("{}\n".format(str(tweet)))