Add unit tests for main model parts (#310)

Add tests for following models:
- Blog
- Instance
- Media
- User
This commit is contained in:
fdb-hiroshima
2018-11-24 12:44:17 +01:00
committed by Baptiste Gelez
parent 0b9727ed28
commit 8a4702df92
30 changed files with 3779 additions and 1123 deletions
+30 -23
View File
@@ -1,16 +1,16 @@
use chrono::NaiveDateTime;
use diesel::{self, RunQueryDsl, QueryDsl, ExpressionMethods};
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use serde_json;
use Connection;
use comments::Comment;
use follows::Follow;
use likes::Like;
use mentions::Mention;
use posts::Post;
use reshares::Reshare;
use users::User;
use schema::notifications;
use users::User;
use Connection;
pub mod notification_kind {
pub const COMMENT: &'static str = "COMMENT";
@@ -26,7 +26,7 @@ pub struct Notification {
pub user_id: i32,
pub creation_date: NaiveDateTime,
pub kind: String,
pub object_id: i32
pub object_id: i32,
}
#[derive(Insertable)]
@@ -34,7 +34,7 @@ pub struct Notification {
pub struct NewNotification {
pub user_id: i32,
pub kind: String,
pub object_id: i32
pub object_id: i32,
}
impl Notification {
@@ -42,14 +42,20 @@ impl Notification {
get!(notifications);
pub fn find_for_user(conn: &Connection, user: &User) -> Vec<Notification> {
notifications::table.filter(notifications::user_id.eq(user.id))
notifications::table
.filter(notifications::user_id.eq(user.id))
.order_by(notifications::creation_date.desc())
.load::<Notification>(conn)
.expect("Notification::find_for_user: notification loading error")
}
pub fn page_for_user(conn: &Connection, user: &User, (min, max): (i32, i32)) -> Vec<Notification> {
notifications::table.filter(notifications::user_id.eq(user.id))
pub fn page_for_user(
conn: &Connection,
user: &User,
(min, max): (i32, i32),
) -> Vec<Notification> {
notifications::table
.filter(notifications::user_id.eq(user.id))
.order_by(notifications::creation_date.desc())
.offset(min.into())
.limit((max - min).into())
@@ -58,7 +64,8 @@ impl Notification {
}
pub fn find<S: Into<String>>(conn: &Connection, kind: S, obj: i32) -> Option<Notification> {
notifications::table.filter(notifications::kind.eq(kind.into()))
notifications::table
.filter(notifications::kind.eq(kind.into()))
.filter(notifications::object_id.eq(obj))
.get_result::<Notification>(conn)
.ok()
@@ -67,25 +74,23 @@ impl Notification {
pub fn to_json(&self, conn: &Connection) -> serde_json::Value {
let mut json = json!(self);
json["object"] = json!(match self.kind.as_ref() {
notification_kind::COMMENT => Comment::get(conn, self.object_id).map(|comment|
json!({
notification_kind::COMMENT => Comment::get(conn, self.object_id).map(|comment| json!({
"post": comment.get_post(conn).to_json(conn),
"user": comment.get_author(conn).to_json(conn),
"id": comment.id
})
),
notification_kind::FOLLOW => Follow::get(conn, self.object_id).map(|follow|
})),
notification_kind::FOLLOW => Follow::get(conn, self.object_id).map(|follow| {
json!({
"follower": User::get(conn, follow.follower_id).map(|u| u.to_json(conn))
})
),
notification_kind::LIKE => Like::get(conn, self.object_id).map(|like|
}),
notification_kind::LIKE => Like::get(conn, self.object_id).map(|like| {
json!({
"post": Post::get(conn, like.post_id).map(|p| p.to_json(conn)),
"user": User::get(conn, like.user_id).map(|u| u.to_json(conn))
})
),
notification_kind::MENTION => Mention::get(conn, self.object_id).map(|mention|
}),
notification_kind::MENTION => Mention::get(conn, self.object_id).map(|mention| {
json!({
"user": mention.get_user(conn).map(|u| u.to_json(conn)),
"url": mention.get_post(conn).map(|p| p.to_json(conn)["url"].clone())
@@ -95,19 +100,21 @@ impl Notification {
json!(format!("{}#comment-{}", post["url"].as_str().expect("Notification::to_json: post url error"), comment.id))
})
})
),
notification_kind::RESHARE => Reshare::get(conn, self.object_id).map(|reshare|
}),
notification_kind::RESHARE => Reshare::get(conn, self.object_id).map(|reshare| {
json!({
"post": reshare.get_post(conn).map(|p| p.to_json(conn)),
"user": reshare.get_user(conn).map(|u| u.to_json(conn))
})
),
_ => Some(json!({}))
}),
_ => Some(json!({})),
});
json
}
pub fn delete(&self, conn: &Connection) {
diesel::delete(self).execute(conn).expect("Notification::delete: notification deletion error");
diesel::delete(self)
.execute(conn)
.expect("Notification::delete: notification deletion error");
}
}