(Almost) full test coverage
This commit is contained in:
parent
0639c5ca12
commit
544e380835
8 changed files with 417 additions and 100 deletions
|
|
@ -160,19 +160,71 @@ impl Channel {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Adapter, AdapterBuilder};
|
||||
use crate::{
|
||||
db::{
|
||||
Feed,
|
||||
User,
|
||||
},
|
||||
test_utils::{
|
||||
FEED1, FEED2, CHANNEL_TITLE, CHANNEL_DESC, USERNAME, FEED_TITLE,
|
||||
FEED_TITLE2, ITEM_GUID, ITEM_GUID2,
|
||||
setup_adapter,
|
||||
setup_channel,
|
||||
},
|
||||
};
|
||||
use chrono::TimeZone;
|
||||
|
||||
#[test]
|
||||
fn parse_unparsed_item() {
|
||||
const CHANNEL_ID: i64 = 1;
|
||||
let date: DateTime<Utc> = Utc.with_ymd_and_hms(2020,1,1,0,0,0).unwrap();
|
||||
|
||||
const FEED1: &str = "https://example.com/feed";
|
||||
const FEED2: &str = "https://example2.com/feed";
|
||||
|
||||
async fn setup_adapter() -> Adapter {
|
||||
AdapterBuilder::new()
|
||||
.database_url("sqlite::memory:")
|
||||
.create().await.unwrap()
|
||||
let raw_channel = UnparsedChannel {
|
||||
id: CHANNEL_ID,
|
||||
title: CHANNEL_TITLE.to_string(),
|
||||
link: FEED1.to_string(),
|
||||
description: Some(CHANNEL_DESC.to_string()),
|
||||
last_fetched: Some(date.to_rfc2822()),
|
||||
};
|
||||
let channel = raw_channel.parse().unwrap();
|
||||
|
||||
assert_eq!(channel.id.0, CHANNEL_ID);
|
||||
assert_eq!(channel.title, CHANNEL_TITLE);
|
||||
assert_eq!(channel.link.as_str(), FEED1);
|
||||
assert_eq!(channel.description, Some(CHANNEL_DESC.to_string()));
|
||||
assert_eq!(channel.last_fetched, Some(date));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_channel() {
|
||||
async fn get_all() {
|
||||
let adapter = setup_adapter().await;
|
||||
let pool = adapter.get_pool();
|
||||
let url1 = Url::parse(FEED1).unwrap();
|
||||
let url2 = Url::parse(FEED2).unwrap();
|
||||
Channel::get_or_create(pool, url1).await.unwrap();
|
||||
Channel::get_or_create(pool, url2).await.unwrap();
|
||||
|
||||
let channels = Channel::get_all(pool).await.unwrap();
|
||||
assert_eq!(channels.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get() {
|
||||
let adapter = setup_adapter().await;
|
||||
let pool = adapter.get_pool();
|
||||
let channel_a = setup_channel(pool).await;
|
||||
|
||||
let channel_b = Channel::get(pool, channel_a.id()).await.unwrap();
|
||||
|
||||
assert_eq!(channel_a.id, channel_b.id);
|
||||
assert_eq!(channel_a.title, channel_b.title);
|
||||
assert_eq!(channel_a.link, channel_b.link);
|
||||
assert_eq!(channel_a.last_fetched, channel_b.last_fetched);
|
||||
assert_eq!(channel_a.description, channel_b.description);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create() {
|
||||
let adapter = setup_adapter().await;
|
||||
let pool = adapter.get_pool();
|
||||
let url_feed = Url::parse(FEED1).unwrap();
|
||||
|
|
@ -213,4 +265,54 @@ mod tests {
|
|||
|
||||
assert_eq!(channels.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_feed_channels() {
|
||||
let adapter = setup_adapter().await;
|
||||
let pool = adapter.get_pool();
|
||||
let channel = setup_channel(pool).await;
|
||||
|
||||
let user = User::create(pool, USERNAME).await.unwrap();
|
||||
let feed1 = Feed::create(pool, user.id(), FEED_TITLE).await.unwrap();
|
||||
let feed2 = Feed::create(pool, user.id(), FEED_TITLE2).await.unwrap();
|
||||
|
||||
feed1.add_channel(pool, channel.id).await.unwrap();
|
||||
feed2.add_channel(pool, channel.id).await.unwrap();
|
||||
|
||||
let fc_list = channel.get_feed_channels(pool).await.unwrap();
|
||||
|
||||
assert_eq!(fc_list.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_channels() {
|
||||
let adapter = setup_adapter().await;
|
||||
let pool = adapter.get_pool();
|
||||
let channel = setup_channel(pool).await;
|
||||
|
||||
let user = User::create(pool, USERNAME).await.unwrap();
|
||||
let feed1 = Feed::create(pool, user.id(), FEED_TITLE).await.unwrap();
|
||||
let feed2 = Feed::create(pool, user.id(), FEED_TITLE2).await.unwrap();
|
||||
|
||||
feed1.add_channel(pool, channel.id).await.unwrap();
|
||||
feed2.add_channel(pool, channel.id).await.unwrap();
|
||||
|
||||
let fc_list = channel.get_feed_channels(pool).await.unwrap();
|
||||
|
||||
assert_eq!(fc_list.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_items() {
|
||||
let adapter = setup_adapter().await;
|
||||
let pool = adapter.get_pool();
|
||||
let channel = setup_channel(pool).await;
|
||||
|
||||
Item::get_or_create(pool, channel.id(), ITEM_GUID).await.unwrap();
|
||||
Item::get_or_create(pool, channel.id(), ITEM_GUID2).await.unwrap();
|
||||
|
||||
let items = channel.get_items(pool).await.unwrap();
|
||||
|
||||
assert_eq!(items.len(), 2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue