Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/repositories/relevant_tweet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl RelevantTweetRepository {
}

pub async fn get_newest_tweet_id(&self) -> Result<Option<String>, DbError> {
let row = sqlx::query("SELECT id FROM relevant_tweets ORDER BY created_at DESC LIMIT 1")
let row = sqlx::query("SELECT id FROM relevant_tweets WHERE created_at >= NOW() - INTERVAL '7 days' ORDER BY created_at DESC LIMIT 1")
.fetch_optional(&self.pool)
.await?;

Expand Down Expand Up @@ -244,7 +244,7 @@ mod tests {
utils::test_db::reset_database,
Config,
};
use chrono::Utc;
use chrono::{Duration, Utc};
use sqlx::PgPool;

// --- Helpers to create dummy data ---
Expand Down Expand Up @@ -291,7 +291,6 @@ mod tests {
}

// --- Tests ---

#[tokio::test]
async fn test_upsert_and_find_by_id() {
let (repo, author_repo) = setup_test_repository().await;
Expand Down Expand Up @@ -347,4 +346,39 @@ mod tests {
assert!(existing.contains("t2"));
assert!(!existing.contains("t3"));
}

#[tokio::test]
async fn test_get_newest_tweet_id_returns_none_when_no_recent_tweets() {
let (repo, author_repo) = setup_test_repository().await;
let author_id = "author_old";
seed_author(&author_repo, author_id, "stale_user").await;

let mut old_tweet = create_payload("tweet_old", author_id, "stale tweet");
old_tweet.created_at = Utc::now() - Duration::days(8);
repo.upsert_many(&vec![old_tweet]).await.unwrap();

let newest_tweet_id = repo.get_newest_tweet_id().await.unwrap();
assert_eq!(
newest_tweet_id, None,
"Should return None when no tweet is within the last 7 days"
);
}

#[tokio::test]
async fn test_get_newest_tweet_id_returns_some_when_recent_tweet_exists() {
let (repo, author_repo) = setup_test_repository().await;
let author_id = "author_recent";
seed_author(&author_repo, author_id, "active_user").await;

let mut recent_tweet = create_payload("tweet_recent", author_id, "recent tweet");
recent_tweet.created_at = Utc::now() - Duration::days(2);
repo.upsert_many(&vec![recent_tweet]).await.unwrap();

let newest_tweet_id = repo.get_newest_tweet_id().await.unwrap();
assert_eq!(
newest_tweet_id,
Some("tweet_recent".to_string()),
"Should return the newest tweet id when at least one tweet is within the last 7 days"
);
}
}
Loading