Add some constraint at database level (#342)
* Add some constraint at database level Fixes #79 and should fix #201 and #113 as well * Fix tests Delete duplicated data before adding constraints (only with Postgres, there is no way to do it with Sqlite with complex constraints like the one we are using) Remove the constraint on media path * We don't need to drop the media unique constraint anymore Because we deleted it
This commit is contained in:
+60
-61
@@ -513,36 +513,35 @@ pub(crate) mod tests {
|
||||
use search::tests::get_searcher;
|
||||
use Connection as Conn;
|
||||
|
||||
pub(crate) fn fill_database(conn: &Conn) -> Vec<Blog> {
|
||||
pub(crate) fn fill_database(conn: &Conn) -> (Vec<User>, Vec<Blog>) {
|
||||
instance_tests::fill_database(conn);
|
||||
let users = usersTests::fill_database(conn);
|
||||
let blogs = vec![
|
||||
NewBlog::new_local(
|
||||
"BlogName".to_owned(),
|
||||
"Blog name".to_owned(),
|
||||
"This is a small blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
NewBlog::new_local(
|
||||
let blog1 = Blog::insert(conn, NewBlog::new_local(
|
||||
"BlogName".to_owned(),
|
||||
"Blog name".to_owned(),
|
||||
"This is a small blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
));
|
||||
blog1.update_boxes(conn);
|
||||
let blog2 = Blog::insert(conn, NewBlog::new_local(
|
||||
"MyBlog".to_owned(),
|
||||
"My blog".to_owned(),
|
||||
"Welcome to my blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
NewBlog::new_local(
|
||||
));
|
||||
blog2.update_boxes(conn);
|
||||
let blog3 = Blog::insert(conn, NewBlog::new_local(
|
||||
"WhyILikePlume".to_owned(),
|
||||
"Why I like Plume".to_owned(),
|
||||
"In this blog I will explay you why I like Plume so much".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
].into_iter()
|
||||
.map(|nb| Blog::insert(conn, nb))
|
||||
.collect::<Vec<_>>();
|
||||
));
|
||||
blog3.update_boxes(conn);
|
||||
|
||||
BlogAuthor::insert(
|
||||
conn,
|
||||
NewBlogAuthor {
|
||||
blog_id: blogs[0].id,
|
||||
blog_id: blog1.id,
|
||||
author_id: users[0].id,
|
||||
is_owner: true,
|
||||
},
|
||||
@@ -551,7 +550,7 @@ pub(crate) mod tests {
|
||||
BlogAuthor::insert(
|
||||
conn,
|
||||
NewBlogAuthor {
|
||||
blog_id: blogs[0].id,
|
||||
blog_id: blog1.id,
|
||||
author_id: users[1].id,
|
||||
is_owner: false,
|
||||
},
|
||||
@@ -560,7 +559,7 @@ pub(crate) mod tests {
|
||||
BlogAuthor::insert(
|
||||
conn,
|
||||
NewBlogAuthor {
|
||||
blog_id: blogs[1].id,
|
||||
blog_id: blog2.id,
|
||||
author_id: users[1].id,
|
||||
is_owner: true,
|
||||
},
|
||||
@@ -569,12 +568,12 @@ pub(crate) mod tests {
|
||||
BlogAuthor::insert(
|
||||
conn,
|
||||
NewBlogAuthor {
|
||||
blog_id: blogs[2].id,
|
||||
blog_id: blog3.id,
|
||||
author_id: users[2].id,
|
||||
is_owner: true,
|
||||
},
|
||||
);
|
||||
blogs
|
||||
(users, vec![ blog1, blog2, blog3 ])
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -604,29 +603,29 @@ pub(crate) mod tests {
|
||||
fn authors() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let user = usersTests::fill_database(conn);
|
||||
fill_database(conn);
|
||||
let (user, _) = fill_database(conn);
|
||||
|
||||
let blog = vec![
|
||||
Blog::insert(
|
||||
conn,
|
||||
NewBlog::new_local(
|
||||
"SomeName".to_owned(),
|
||||
"Some name".to_owned(),
|
||||
"This is some blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
let b1 = Blog::insert(
|
||||
conn,
|
||||
NewBlog::new_local(
|
||||
"SomeName".to_owned(),
|
||||
"Some name".to_owned(),
|
||||
"This is some blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
Blog::insert(
|
||||
conn,
|
||||
NewBlog::new_local(
|
||||
"Blog".to_owned(),
|
||||
"Blog".to_owned(),
|
||||
"I've named my blog Blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
);
|
||||
b1.update_boxes(conn);
|
||||
let b2 = Blog::insert(
|
||||
conn,
|
||||
NewBlog::new_local(
|
||||
"Blog".to_owned(),
|
||||
"Blog".to_owned(),
|
||||
"I've named my blog Blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
];
|
||||
);
|
||||
b2.update_boxes(conn);
|
||||
let blog = vec![ b1, b2 ];
|
||||
|
||||
BlogAuthor::insert(
|
||||
conn,
|
||||
@@ -756,7 +755,7 @@ pub(crate) mod tests {
|
||||
fn delete() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let blogs = fill_database(conn);
|
||||
let (_, blogs) = fill_database(conn);
|
||||
|
||||
blogs[0].delete(conn, &get_searcher());
|
||||
assert!(Blog::get(conn, blogs[0].id).is_none());
|
||||
@@ -770,29 +769,29 @@ pub(crate) mod tests {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let searcher = get_searcher();
|
||||
let user = usersTests::fill_database(conn);
|
||||
fill_database(conn);
|
||||
let (user, _) = fill_database(conn);
|
||||
|
||||
let blog = vec![
|
||||
Blog::insert(
|
||||
conn,
|
||||
NewBlog::new_local(
|
||||
"SomeName".to_owned(),
|
||||
"Some name".to_owned(),
|
||||
"This is some blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
let b1 = Blog::insert(
|
||||
conn,
|
||||
NewBlog::new_local(
|
||||
"SomeName".to_owned(),
|
||||
"Some name".to_owned(),
|
||||
"This is some blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
Blog::insert(
|
||||
conn,
|
||||
NewBlog::new_local(
|
||||
"Blog".to_owned(),
|
||||
"Blog".to_owned(),
|
||||
"I've named my blog Blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
);
|
||||
b1.update_boxes(conn);
|
||||
let b2 = Blog::insert(
|
||||
conn,
|
||||
NewBlog::new_local(
|
||||
"Blog".to_owned(),
|
||||
"Blog".to_owned(),
|
||||
"I've named my blog Blog".to_owned(),
|
||||
Instance::local_id(conn),
|
||||
),
|
||||
];
|
||||
);
|
||||
b2.update_boxes(conn);
|
||||
let blog = vec![ b1, b2 ];
|
||||
|
||||
BlogAuthor::insert(
|
||||
conn,
|
||||
|
||||
@@ -220,7 +220,7 @@ pub(crate) mod tests {
|
||||
use users::tests as usersTests;
|
||||
use Connection as Conn;
|
||||
|
||||
pub(crate) fn fill_database(conn: &Conn) -> Vec<Media> {
|
||||
pub(crate) fn fill_database(conn: &Conn) -> (Vec<User>, Vec<Media>) {
|
||||
let mut wd = current_dir().unwrap().to_path_buf();
|
||||
while wd.pop() {
|
||||
if wd.join(".git").exists() {
|
||||
@@ -236,7 +236,7 @@ pub(crate) mod tests {
|
||||
let f2 = "static/media/2.mp3".to_owned();
|
||||
fs::write(f1.clone(), []).unwrap();
|
||||
fs::write(f2.clone(), []).unwrap();
|
||||
vec![
|
||||
(users, vec![
|
||||
NewMedia {
|
||||
file_path: f1,
|
||||
alt_text: "some alt".to_owned(),
|
||||
@@ -266,7 +266,7 @@ pub(crate) mod tests {
|
||||
},
|
||||
].into_iter()
|
||||
.map(|nm| Media::insert(conn, nm))
|
||||
.collect()
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub(crate) fn clean(conn: &Conn) {
|
||||
@@ -282,8 +282,7 @@ pub(crate) mod tests {
|
||||
fn delete() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let user = usersTests::fill_database(conn)[0].id;
|
||||
fill_database(conn);
|
||||
let user = fill_database(conn).0[0].id;
|
||||
|
||||
let path = "static/media/test_deletion".to_owned();
|
||||
fs::write(path.clone(), []).unwrap();
|
||||
@@ -316,10 +315,9 @@ pub(crate) mod tests {
|
||||
fn set_owner() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let users = usersTests::fill_database(conn);
|
||||
let (users, _) = fill_database(conn);
|
||||
let u1 = &users[0];
|
||||
let u2 = &users[1];
|
||||
fill_database(conn);
|
||||
|
||||
let path = "static/media/test_set_owner".to_owned();
|
||||
fs::write(path.clone(), []).unwrap();
|
||||
|
||||
@@ -117,7 +117,7 @@ pub(crate) mod tests {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let searcher = get_searcher();
|
||||
let blog = &fill_database(conn)[0];
|
||||
let blog = &fill_database(conn).1[0];
|
||||
let author = &blog.list_authors(conn)[0];
|
||||
|
||||
let title = random_hex()[..8].to_owned();
|
||||
|
||||
+31
-33
@@ -998,39 +998,37 @@ pub(crate) mod tests {
|
||||
|
||||
pub(crate) fn fill_database(conn: &Conn) -> Vec<User> {
|
||||
instance_tests::fill_database(conn);
|
||||
let local_user = vec![
|
||||
NewUser::new_local(
|
||||
conn,
|
||||
"admin".to_owned(),
|
||||
"The admin".to_owned(),
|
||||
true,
|
||||
"Hello there, I'm the admin",
|
||||
"admin@example.com".to_owned(),
|
||||
"invalid_admin_password".to_owned(),
|
||||
),
|
||||
NewUser::new_local(
|
||||
conn,
|
||||
"user".to_owned(),
|
||||
"Some user".to_owned(),
|
||||
false,
|
||||
"Hello there, I'm no one",
|
||||
"user@example.com".to_owned(),
|
||||
"invalid_user_password".to_owned(),
|
||||
),
|
||||
NewUser::new_local(
|
||||
conn,
|
||||
"other".to_owned(),
|
||||
"Another user".to_owned(),
|
||||
false,
|
||||
"Hello there, I'm someone else",
|
||||
"other@example.com".to_owned(),
|
||||
"invalid_other_password".to_owned(),
|
||||
),
|
||||
];
|
||||
for u in local_user.iter() {
|
||||
u.update_boxes(conn);
|
||||
}
|
||||
local_user
|
||||
let admin = NewUser::new_local(
|
||||
conn,
|
||||
"admin".to_owned(),
|
||||
"The admin".to_owned(),
|
||||
true,
|
||||
"Hello there, I'm the admin",
|
||||
"admin@example.com".to_owned(),
|
||||
"invalid_admin_password".to_owned(),
|
||||
);
|
||||
admin.update_boxes(conn);
|
||||
let user = NewUser::new_local(
|
||||
conn,
|
||||
"user".to_owned(),
|
||||
"Some user".to_owned(),
|
||||
false,
|
||||
"Hello there, I'm no one",
|
||||
"user@example.com".to_owned(),
|
||||
"invalid_user_password".to_owned(),
|
||||
);
|
||||
user.update_boxes(conn);
|
||||
let other = NewUser::new_local(
|
||||
conn,
|
||||
"other".to_owned(),
|
||||
"Another user".to_owned(),
|
||||
false,
|
||||
"Hello there, I'm someone else",
|
||||
"other@example.com".to_owned(),
|
||||
"invalid_other_password".to_owned(),
|
||||
);
|
||||
other.update_boxes(conn);
|
||||
vec![ admin, user, other ]
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user