actix-admin/tests/post_create_and_edit_is_success.rs

177 lines
5.9 KiB
Rust
Raw Normal View History

mod test_setup;
2023-01-13 14:54:16 +01:00
use test_setup::prelude::*;
#[cfg(test)]
2023-01-13 14:54:16 +01:00
mod post_create_and_edit_is_success {
use actix_admin::prelude::*;
2023-09-14 15:51:29 +02:00
use actix_web::{
test,
App,
http::header::ContentType
};
use chrono::{ NaiveDateTime, NaiveDate };
use serde::{Serialize};
use sea_orm::{ PaginatorTrait, EntityTrait, prelude::Decimal};
use crate::{create_app};
2023-01-13 14:54:16 +01:00
#[derive(Serialize, Clone)]
pub struct CommentModel {
id: &'static str,
insert_date: &'static str,
comment: &'static str,
user: &'static str,
is_visible: &'static str,
post_id: Option<&'static str>,
2023-09-14 15:51:29 +02:00
my_decimal: &'static str
2023-01-13 14:54:16 +01:00
}
2023-01-13 14:54:16 +01:00
#[derive(Serialize, Clone)]
pub struct PostModel {
id: &'static str,
title: &'static str,
text: &'static str,
tea_mandatory: &'static str,
insert_date: &'static str,
}
2023-01-11 20:30:33 +01:00
#[actix_web::test]
2022-08-28 20:26:10 +02:00
async fn comment_create_and_edit() {
2023-01-13 14:54:16 +01:00
let db = super::setup_db(false).await;
2023-01-11 20:30:33 +01:00
let app = create_app!(db);
2022-08-29 11:01:00 +02:00
// create entity
let mut model = CommentModel {
2022-08-28 20:26:10 +02:00
id: "0",
insert_date: "1977-04-01T14:00",
comment: "test",
user: "test",
is_visible: "true",
post_id: None,
2023-09-14 15:51:29 +02:00
my_decimal: "113.141" // must be larger than 100
2022-08-28 20:26:10 +02:00
};
2023-09-14 15:51:29 +02:00
2022-08-28 20:26:10 +02:00
let req = test::TestRequest::post()
.insert_header(ContentType::form_url_encoded())
.uri("/admin/comment/create_post_from_plaintext")
2022-08-29 11:01:00 +02:00
.set_form(model.clone())
2022-08-28 20:26:10 +02:00
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_redirection());
let entities = super::test_setup::Comment::find()
2023-01-11 20:30:33 +01:00
.paginate(&db, 50)
2022-08-28 20:26:10 +02:00
.fetch_page(0)
.await
.expect("could not retrieve entities");
2023-01-13 14:54:16 +01:00
assert_eq!(entities.len(), 1, "After post, db does not contain 1 model");
2022-08-29 11:01:00 +02:00
let entity = entities.first().unwrap();
2023-01-13 14:54:16 +01:00
assert_eq!(entity.id, 1);
2023-09-14 15:51:29 +02:00
assert_eq!(entity.comment,"test");
2023-01-13 14:54:16 +01:00
assert_eq!(entity.user, "test");
2022-08-29 11:01:00 +02:00
assert!(entity.is_visible);
assert!(entity.post_id.is_none());
2023-01-13 14:54:16 +01:00
assert_eq!(entity.my_decimal, Decimal::new(113141, 3));
2023-09-14 15:51:29 +02:00
assert_eq!(entity.insert_date, NaiveDateTime::parse_from_str("1977-04-01T14:00", "%Y-%m-%dT%H:%M").unwrap());
2022-08-29 11:01:00 +02:00
// update entity
model.my_decimal = "213.141";
model.user = "updated";
model.comment = "updated";
model.insert_date = "1987-04-01T14:00";
model.is_visible = "false";
let edit_req = test::TestRequest::post()
.insert_header(ContentType::form_url_encoded())
.uri("/admin/comment/edit_post_from_plaintext/1")
.set_form(model.clone())
.to_request();
let resp = test::call_service(&app, edit_req).await;
assert!(resp.status().is_redirection());
let entities = super::test_setup::Comment::find()
2023-01-11 20:30:33 +01:00
.paginate(&db, 50)
2022-08-29 11:01:00 +02:00
.fetch_page(0)
.await
.expect("could not retrieve entities");
2022-08-28 20:26:10 +02:00
2023-09-14 15:51:29 +02:00
assert_eq!(entities.len(), 1, "After edit post, db does not contain 1 model");
2022-08-29 11:01:00 +02:00
let entity = entities.first().unwrap();
2023-01-11 20:30:33 +01:00
assert_eq!(entity.id, 1);
assert_eq!(entity.comment, "updated");
assert_eq!(entity.user, "updated");
2022-08-29 11:01:00 +02:00
assert!(!entity.is_visible);
assert!(entity.post_id.is_none());
2023-01-11 20:30:33 +01:00
assert_eq!(entity.my_decimal, Decimal::new(213141, 3));
2023-09-14 15:51:29 +02:00
assert_eq!(entity.insert_date, NaiveDateTime::parse_from_str("1987-04-01T14:00", "%Y-%m-%dT%H:%M").unwrap());
2022-08-28 20:26:10 +02:00
}
2023-09-14 15:51:29 +02:00
2022-08-28 20:26:10 +02:00
#[actix_web::test]
async fn post_create_and_edit() {
2023-01-13 14:54:16 +01:00
let db = super::setup_db(false).await;
2023-01-11 20:30:33 +01:00
let app = create_app!(db);
2022-08-29 11:01:00 +02:00
let mut model = PostModel {
2022-08-28 20:26:10 +02:00
id: "0",
insert_date: "1977-04-01",
title: "test",
text: "test",
2023-09-14 15:51:29 +02:00
tea_mandatory: "EverydayTea"
2022-08-28 20:26:10 +02:00
};
let req = test::TestRequest::post()
.insert_header(ContentType::form_url_encoded())
2022-08-28 20:26:10 +02:00
.uri("/admin/post/create_post_from_plaintext")
2022-08-29 11:01:00 +02:00
.set_form(model.clone())
.to_request();
let resp = test::call_service(&app, req).await;
2022-08-28 20:26:10 +02:00
assert!(resp.status().is_redirection());
let entities = super::test_setup::Post::find()
2023-01-11 20:30:33 +01:00
.paginate(&db, 50)
2022-08-28 20:26:10 +02:00
.fetch_page(0)
.await
.expect("could not retrieve entities");
2023-01-11 20:30:33 +01:00
assert_eq!(entities.len(), 1, "After post, db does not contain 1 model");
2022-08-29 11:01:00 +02:00
let entity = entities.first().unwrap();
2023-01-11 20:30:33 +01:00
assert_eq!(entity.id, 1);
2023-09-14 15:51:29 +02:00
assert_eq!(entity.tea_mandatory, super::test_setup::post::Tea::EverydayTea);
2023-01-11 20:30:33 +01:00
assert_eq!(entity.title, model.title);
assert_eq!(entity.text, model.text);
2023-09-14 15:51:29 +02:00
assert_eq!(entity.insert_date, NaiveDate::parse_from_str("1977-04-01", "%Y-%m-%d").unwrap());
2022-08-29 11:01:00 +02:00
// update entity
model.tea_mandatory = "BreakfastTea";
model.title = "updated";
model.text = "updated";
model.insert_date = "1987-04-01";
let edit_req = test::TestRequest::post()
.insert_header(ContentType::form_url_encoded())
.uri("/admin/post/edit_post_from_plaintext/1")
.set_form(model.clone())
.to_request();
let resp = test::call_service(&app, edit_req).await;
assert!(resp.status().is_redirection());
let entities = super::test_setup::Post::find()
2023-01-11 20:30:33 +01:00
.paginate(&db, 50)
2022-08-29 11:01:00 +02:00
.fetch_page(0)
.await
.expect("could not retrieve entities");
2023-09-14 15:51:29 +02:00
assert_eq!(entities.len(), 1, "After edit post, db does not contain 1 model");
2022-08-29 11:01:00 +02:00
let entity = entities.first().unwrap();
2023-01-11 20:30:33 +01:00
assert_eq!(entity.id, 1);
assert_eq!(entity.text, "updated");
assert_eq!(entity.title, "updated");
2023-09-14 15:51:29 +02:00
assert_eq!(entity.insert_date, NaiveDate::parse_from_str("1987-04-01", "%Y-%m-%d").unwrap());
}
}