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

@ -24,10 +24,11 @@ impl UnparsedItem {
Ok(Item {
id: ItemId(self.id),
channel_id: ChannelId(self.channel_id),
fetched_at: self.fetched_at.as_deref()
.map(DateTime::parse_from_rfc2822)
.transpose()?
.map(|dt| dt.with_timezone(&Utc)),
fetched_at: match self.fetched_at {
Some(dt_str) => Some(DateTime::parse_from_rfc2822(&dt_str)?
.with_timezone(&Utc)),
None => None,
},
title: self.title,
description: self.description,
@ -40,6 +41,7 @@ pub struct Item {
id: ItemId,
channel_id: ChannelId,
#[allow(dead_code)] // TODO: Use for score decay calculations later
fetched_at: Option<DateTime<Utc>>,
title: Option<String>,
description: Option<String>,
@ -61,8 +63,8 @@ impl Item {
let item = sqlx::query_as!(
UnparsedItem,
"INSERT INTO items (channel_id, guid)
VALUES(?, ?)
ON CONFLICT(id) DO UPDATE SET id = id
VALUES (?, ?)
ON CONFLICT(channel_id, guid) DO UPDATE SET channel_id = channel_id
RETURNING id as `id!`, channel_id, fetched_at, title, description,
content",
int_channel_id, guid
@ -91,3 +93,43 @@ impl Item {
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());
}
}