use derive_builder::Builder;
use chrono::NaiveDate;
use crate::api::common::NameOrId;
use crate::api::endpoint_prelude::*;
#[derive(Debug, Builder, Clone)]
#[builder(setter(strip_option))]
pub struct CreateProjectMilestone<'a> {
#[builder(setter(into))]
project: NameOrId<'a>,
#[builder(setter(into))]
title: Cow<'a, str>,
#[builder(setter(into), default)]
description: Option<Cow<'a, str>>,
#[builder(default)]
due_date: Option<NaiveDate>,
#[builder(default)]
start_date: Option<NaiveDate>,
}
impl<'a> CreateProjectMilestone<'a> {
pub fn builder() -> CreateProjectMilestoneBuilder<'a> {
CreateProjectMilestoneBuilder::default()
}
}
impl<'a> Endpoint for CreateProjectMilestone<'a> {
fn method(&self) -> Method {
Method::POST
}
fn endpoint(&self) -> Cow<'static, str> {
format!("projects/{}/milestones", self.project).into()
}
fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
let mut params = FormParams::default();
params
.push("title", &self.title)
.push_opt("description", self.description.as_ref())
.push_opt("due_date", self.due_date)
.push_opt("start_date", self.start_date);
params.into_body()
}
}
#[cfg(test)]
mod tests {
use chrono::NaiveDate;
use http::Method;
use crate::api::projects::milestones::{
CreateProjectMilestone, CreateProjectMilestoneBuilderError,
};
use crate::api::{self, Query};
use crate::test::client::{ExpectedUrl, SingleTestClient};
#[test]
fn project_and_title_are_necessary() {
let err = CreateProjectMilestone::builder().build().unwrap_err();
crate::test::assert_missing_field!(err, CreateProjectMilestoneBuilderError, "project");
}
#[test]
fn project_is_necessary() {
let err = CreateProjectMilestone::builder()
.title("title")
.build()
.unwrap_err();
crate::test::assert_missing_field!(err, CreateProjectMilestoneBuilderError, "project");
}
#[test]
fn title_is_necessary() {
let err = CreateProjectMilestone::builder()
.project("project")
.build()
.unwrap_err();
crate::test::assert_missing_field!(err, CreateProjectMilestoneBuilderError, "title");
}
#[test]
fn project_and_title_are_sufficient() {
CreateProjectMilestone::builder()
.project("project")
.title("title")
.build()
.unwrap();
}
#[test]
fn endpoint() {
let endpoint = ExpectedUrl::builder()
.method(Method::POST)
.endpoint("projects/simple%2Fproject/milestones")
.content_type("application/x-www-form-urlencoded")
.body_str("title=title")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = CreateProjectMilestone::builder()
.project("simple/project")
.title("title")
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_description() {
let endpoint = ExpectedUrl::builder()
.method(Method::POST)
.endpoint("projects/simple%2Fproject/milestones")
.content_type("application/x-www-form-urlencoded")
.body_str(concat!("title=title", "&description=description"))
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = CreateProjectMilestone::builder()
.project("simple/project")
.title("title")
.description("description")
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_due_date() {
let endpoint = ExpectedUrl::builder()
.method(Method::POST)
.endpoint("projects/simple%2Fproject/milestones")
.content_type("application/x-www-form-urlencoded")
.body_str(concat!("title=title", "&due_date=2020-01-01"))
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = CreateProjectMilestone::builder()
.project("simple/project")
.title("title")
.due_date(NaiveDate::from_ymd_opt(2020, 1, 1).unwrap())
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_start_date() {
let endpoint = ExpectedUrl::builder()
.method(Method::POST)
.endpoint("projects/simple%2Fproject/milestones")
.content_type("application/x-www-form-urlencoded")
.body_str(concat!("title=title", "&start_date=2020-01-01"))
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = CreateProjectMilestone::builder()
.project("simple/project")
.title("title")
.start_date(NaiveDate::from_ymd_opt(2020, 1, 1).unwrap())
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
}