def start_post_github_summary_task(task_owner: ApiUser, workspace_id: str, channel_name: str): boundary_dt = utc_now_minus(timedelta(days=1)) slack_installation = SlackInstallation.query.filter_by( workspace_id=workspace_id).first() if not slack_installation: raise ValidationError("workspace not found") job = fetch_github_summary_post_to_slack.queue(slack_installation.id, channel_name, boundary_dt) task = PostGitHubSummaryTask( job_id=job.id, name="Post GitHub Summary", description="Daily task to post GitHub Summary", user=task_owner, data={ "workspace_id": workspace_id, "slack_installation_id": slack_installation.id, "channel_name": channel_name, "boundary_dt": boundary_dt.isoformat(), }, ) db.session.add(task) db.session.commit()
def test_generate_summary(create_user): # Arrange user = create_user(slack_id="alysivji", github_username="******") user_events = GitHubUserEvents(user, utc_now_minus(timedelta(days=1))) # Act summary = user_events.generate_summary_text() assert "alysivji" in summary
def test_generates_empty_summary_if_no_events_found(create_user): # Arrange user = create_user(slack_id="raymondberg", github_username="******") user_events = GitHubUserEvents(user, utc_now_minus(timedelta(days=1))) # Act summary = user_events.generate_summary_text() assert summary == ""
def test_generate_summary_for_releases(session, factory): # Arrange user = factory.GitHubSummaryUser(slack_id="alysivji", github_username="******") user_events = GitHubUserEvents(user, utc_now_minus(timedelta(days=1))) # Act summary = user_events.generate_summary_text() assert "alysivji" in summary assert "2 new releases" in summary
def test_post_tweets_to_slack(mocker, factory, kv_store, patched_twitter, patched_slack): """ GIVEN: 3 tweets to post (2 within the window) WHEN: post_tweets_to_slack is called THEN: we post one tweet """ # Arrange kv_store.put_int(LAST_TWEET_KEY, 0) tweets = [ factory.Tweet(id=3, created_at=utc_now_minus(timedelta())), factory.Tweet(id=2, created_at=utc_now_minus(timedelta(days=1))), factory.Tweet(id=1, created_at=utc_now_minus(timedelta(days=1))), ] patched_twitter(tweets) # Act fetch_tweets_post_to_slack("test_channel", "test_username") # Assert assert len(patched_slack.mock.mock_calls) == 1 args, kwargs = patched_slack.mock.call_args assert "test_username/statuses/1" in args[0] assert "test_channel" in kwargs["channel"]
def test_post_tweets_to_slack(mocker, kv_store, patched_twitter, patched_slack): """ GIVEN: 3 tweets to post (2 within the window) WHEN: post_tweets_to_slack is called THEN: we post one tweet """ # Arrange kv_store.put_int(LAST_TWEET_KEY, 0) tweets = [ Tweet(3, utc_now_minus(timedelta())), Tweet(2, utc_now_minus(timedelta(days=1))), Tweet(1, utc_now_minus(timedelta(days=1))), ] patched_twitter(tweets) slack = patched_slack(mocker.MagicMock()) # Act fetch_tweets_post_to_slack("test_channel", "test_username") # Assert assert len(slack.mock_calls) == 1 args, kwargs = slack.post_message.call_args assert "test_username/statuses/1" in args[0] assert "test_channel" in kwargs["channel"]
def start_post_github_summary_task(task_owner: ApiUser, channel_name: str): boundary_dt = utc_now_minus(timedelta(days=1)) job = fetch_github_summary_post_to_slack.queue(channel_name, boundary_dt) task = PostGitHubSummaryTask( job_id=job.id, name="Post GitHub Summary", description="Daily task to post GitHub Summary", user=task_owner, data={ "channel_name": channel_name, "boundary_dt": boundary_dt.isoformat() }, ) db.session.add(task) db.session.commit()
def _exclude_tweets_inside_window(tweets, *, window: timedelta): """Buffer to delete tweets before retweeting to Slack""" boundary_dt = utc_now_minus(window) return [tweet for tweet in tweets if tweet.created_at <= boundary_dt]
def t_minus_one_day(): return utc_now_minus(timedelta(days=1))
def test_utc_now_minus(): today = datetime.utcnow() result = utc_now_minus(timedelta(days=1)) assert result.replace(tzinfo=None) == today - timedelta(days=1)