Pull out feed_channel, add more tests

This commit is contained in:
Julia Lange 2026-01-23 16:35:43 -08:00
parent 0bb9a81d60
commit 0639c5ca12
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
4 changed files with 184 additions and 42 deletions

View file

@ -2,6 +2,8 @@ mod user;
pub use user::User; pub use user::User;
mod feed; mod feed;
pub use feed::Feed; pub use feed::Feed;
mod feed_channel;
pub use feed_channel::FeedChannel;
mod channel; mod channel;
pub use channel::Channel; pub use channel::Channel;
mod item; mod item;
@ -10,7 +12,7 @@ pub use item::Item;
macro_rules! define_id { macro_rules! define_id {
($name:ident) => { ($name:ident) => {
#[derive(Copy, Clone)] #[derive(PartialEq, Debug, Copy, Clone)]
pub struct $name(i64); pub struct $name(i64);
impl From<$name> for i64 { fn from(id: $name) -> Self { id.0 } } impl From<$name> for i64 { fn from(id: $name) -> Self { id.0 } }
}; };

View file

@ -6,7 +6,8 @@ use crate::{
db::{ db::{
ChannelId, ChannelId,
Item, Item,
FeedId, FeedChannel,
feed_channel::UnparsedFeedChannel,
item::UnparsedItem, item::UnparsedItem,
}, },
fetch::FetchedRSSChannel, fetch::FetchedRSSChannel,
@ -34,40 +35,6 @@ impl UnparsedChannel {
} }
} }
pub struct UnparsedFeedChannel {
pub channel_id: i64,
pub feed_id: i64,
}
impl UnparsedFeedChannel {
pub fn parse(self) -> Result<FeedChannel> {
Ok(FeedChannel {
channel_id: ChannelId(self.channel_id),
feed_id: FeedId(self.feed_id)
})
}
}
pub struct FeedChannel {
channel_id: ChannelId,
feed_id: FeedId,
}
impl FeedChannel {
pub async fn add_item(
&self, pool: &AdapterPool, item: &Item
) -> Result<()> {
let int_item_id = i64::from(item.id());
let int_feed_id = i64::from(self.feed_id);
sqlx::query!(
"INSERT OR IGNORE INTO feed_items (feed_id, item_id, score)
VALUES (?, ?, 5)", // TODO: Add in scoring featuress
int_feed_id, int_item_id
).execute(&pool.0).await?;
Ok(())
}
}
pub struct Channel { pub struct Channel {
id: ChannelId, id: ChannelId,
title: String, title: String,

View file

@ -0,0 +1,131 @@
use crate::{
Result,
AdapterPool,
db::{
Channel,
ChannelId,
Feed,
FeedId,
Item,
},
};
pub struct UnparsedFeedChannel {
pub channel_id: i64,
pub feed_id: i64,
}
impl UnparsedFeedChannel {
pub fn parse(self) -> Result<FeedChannel> {
Ok(FeedChannel {
channel_id: ChannelId(self.channel_id),
feed_id: FeedId(self.feed_id)
})
}
}
pub struct FeedChannel {
channel_id: ChannelId,
feed_id: FeedId,
}
impl FeedChannel {
pub async fn get_channel(&self, pool: &AdapterPool) -> Result<Channel> {
Channel::get(pool, self.channel_id).await
}
pub async fn get_feed(&self, pool: &AdapterPool) -> Result<Feed> {
Feed::get(pool, self.feed_id).await
}
pub async fn add_item(
&self, pool: &AdapterPool, item: &Item
) -> Result<()> {
let int_item_id = i64::from(item.id());
let int_feed_id = i64::from(self.feed_id);
sqlx::query!(
"INSERT OR IGNORE INTO feed_items (feed_id, item_id, score)
VALUES (?, ?, 5)", // TODO: Add in scoring featuress
int_feed_id, int_item_id
).execute(&pool.0).await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use reqwest::Url;
use crate::{
Adapter,
AdapterBuilder,
db::{
Channel,
FeedId,
User
}
};
const FEED1: &str = "https://example.com/feed";
async fn setup_adapter() -> Adapter {
AdapterBuilder::new()
.database_url("sqlite::memory:")
.create().await.unwrap()
}
// FeedChannel Tests
#[tokio::test]
async fn get_channel() {
let adapter = setup_adapter().await;
let pool = adapter.get_pool();
let url = Url::parse(FEED1).unwrap();
let channel = Channel::get_or_create(pool, url).await.unwrap();
let fc = FeedChannel {
channel_id: channel.id(),
feed_id: FeedId(1), // Fake Feed
};
let channel_from_fc = fc.get_channel(pool).await.unwrap();
assert_eq!(channel_from_fc.id(), channel.id());
}
#[tokio::test]
async fn get_feed() {
let adapter = setup_adapter().await;
let pool = adapter.get_pool();
let user = User::create(pool, "Alice").await.unwrap();
let feed = Feed::create(pool, user.id(), "My Feed").await.unwrap();
let fc = FeedChannel {
channel_id: ChannelId(1), // Fake Channel
feed_id: feed.id(),
};
let feed_from_fc = fc.get_feed(pool).await.unwrap();
assert_eq!(feed_from_fc.id(), feed.id());
}
#[tokio::test]
pub async fn add_item() {
let adapter = setup_adapter().await;
let pool = adapter.get_pool();
let user = User::create(pool, "Alice").await.unwrap();
let feed = Feed::create(pool, user.id(), "My Feed").await.unwrap();
let url = Url::parse(FEED1).unwrap();
let channel = Channel::get_or_create(pool, url).await.unwrap();
let fc = FeedChannel {
channel_id: channel.id(),
feed_id: feed.id(),
};
let item = Item::get_or_create(pool, channel.id(), "item-guid").await.unwrap();
fc.add_item(pool, &item).await.unwrap();
let items = feed.get_items(pool, 1, 0).await.unwrap();
assert_eq!(items[0].id(), item.id());
}
}

View file

@ -24,10 +24,11 @@ impl UnparsedItem {
Ok(Item { Ok(Item {
id: ItemId(self.id), id: ItemId(self.id),
channel_id: ChannelId(self.channel_id), channel_id: ChannelId(self.channel_id),
fetched_at: self.fetched_at.as_deref() fetched_at: match self.fetched_at {
.map(DateTime::parse_from_rfc2822) Some(dt_str) => Some(DateTime::parse_from_rfc2822(&dt_str)?
.transpose()? .with_timezone(&Utc)),
.map(|dt| dt.with_timezone(&Utc)), None => None,
},
title: self.title, title: self.title,
description: self.description, description: self.description,
@ -40,6 +41,7 @@ pub struct Item {
id: ItemId, id: ItemId,
channel_id: ChannelId, channel_id: ChannelId,
#[allow(dead_code)] // TODO: Use for score decay calculations later
fetched_at: Option<DateTime<Utc>>, fetched_at: Option<DateTime<Utc>>,
title: Option<String>, title: Option<String>,
description: Option<String>, description: Option<String>,
@ -61,8 +63,8 @@ impl Item {
let item = sqlx::query_as!( let item = sqlx::query_as!(
UnparsedItem, UnparsedItem,
"INSERT INTO items (channel_id, guid) "INSERT INTO items (channel_id, guid)
VALUES(?, ?) VALUES (?, ?)
ON CONFLICT(id) DO UPDATE SET id = id ON CONFLICT(channel_id, guid) DO UPDATE SET channel_id = channel_id
RETURNING id as `id!`, channel_id, fetched_at, title, description, RETURNING id as `id!`, channel_id, fetched_at, title, description,
content", content",
int_channel_id, guid int_channel_id, guid
@ -91,3 +93,43 @@ impl Item {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use reqwest::Url;
use crate::{
Adapter,
AdapterBuilder,
db::{
Channel,
}
};
const FEED1: &str = "https://example.com/feed";
const ITEM_GUID: &str = "item-guid";
async fn setup_adapter() -> Adapter {
AdapterBuilder::new()
.database_url("sqlite::memory:")
.create().await.unwrap()
}
async fn setup_channel(pool: &AdapterPool) -> Channel {
let url = Url::parse(FEED1).unwrap();
Channel::get_or_create(pool, url).await.unwrap()
}
// Item Tests
#[tokio::test]
pub async fn get_or_create_duplicate() {
let adapter = setup_adapter().await;
let pool = adapter.get_pool();
let channel = setup_channel(pool).await;
let item1 = Item::get_or_create(pool, channel.id(), ITEM_GUID).await.unwrap();
let item2 = Item::get_or_create(pool, channel.id(), ITEM_GUID).await.unwrap();
assert_eq!(item1.id(), item2.id());
}
}