Commit 5b1df662 authored by Ben Boeckel's avatar Ben Boeckel
Browse files

api: test helper types

parent 9d5490d8
......@@ -190,3 +190,117 @@ pub fn bool_str(b: bool) -> &'static str {
"false"
}
}
#[cfg(test)]
mod tests {
use crate::api::common::{
self, AccessLevel, EnableState, NameOrId, SortOrder, VisibilityLevel,
};
#[test]
fn access_level_as_str() {
let items = &[
(AccessLevel::Anonymous, "anonymous", 0),
(AccessLevel::Guest, "guest", 10),
(AccessLevel::Reporter, "reporter", 20),
(AccessLevel::Developer, "developer", 30),
(AccessLevel::Maintainer, "maintainer", 40),
(AccessLevel::Owner, "owner", 50),
(AccessLevel::Admin, "admin", 60),
];
for (i, s, u) in items {
assert_eq!(i.as_str(), *s);
assert_eq!(i.as_u64(), *u);
}
}
#[test]
fn access_level_ordering() {
let items = &[
AccessLevel::Anonymous,
AccessLevel::Guest,
AccessLevel::Reporter,
AccessLevel::Developer,
AccessLevel::Maintainer,
AccessLevel::Owner,
AccessLevel::Admin,
];
let mut last = None;
for item in items {
if let Some(prev) = last {
assert!(prev < item);
}
last = Some(item);
}
}
#[test]
fn sort_order_default() {
assert_eq!(SortOrder::default(), SortOrder::Descending);
}
#[test]
fn sort_order_as_str() {
let items = &[
(SortOrder::Ascending, "asc"),
(SortOrder::Descending, "desc"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn enable_state_as_str() {
let items = &[
(EnableState::Enabled, "enabled"),
(EnableState::Disabled, "disabled"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn name_or_id_as_str() {
let items: &[(NameOrId, _)] = &[
("user".into(), "user"),
("special/name".into(), "special%2Fname"),
(
"special/name?string".to_string().into(),
"special%2Fname%3Fstring",
),
(1.into(), "1"),
];
for (i, s) in items {
assert_eq!(i.to_string(), *s);
}
}
#[test]
fn visibility_level_as_str() {
let items = &[
(VisibilityLevel::Public, "public"),
(VisibilityLevel::Internal, "internal"),
(VisibilityLevel::Private, "private"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn bool_str() {
let items = &[(true, "true"), (false, "false")];
for (i, s) in items {
assert_eq!(common::bool_str(*i), *s);
}
}
}
......@@ -289,7 +289,8 @@ fn next_page_from_headers(headers: &HeaderMap) -> Result<Option<Url>, Pagination
#[cfg(test)]
mod tests {
use crate::api::paged::{LinkHeader, LinkHeaderParseError};
use crate::api::paged::LinkHeader;
use crate::api::{LinkHeaderParseError, Pagination};
#[test]
fn test_link_header_no_brackets() {
......@@ -339,4 +340,9 @@ mod tests {
assert_eq!(link.params[1].0, "param2");
assert_eq!(link.params[1].1, "value");
}
#[test]
fn pagination_default() {
assert_eq!(Pagination::default(), Pagination::All);
}
}
......@@ -109,7 +109,25 @@ impl<'a> Pageable for Jobs<'a> {}
#[cfg(test)]
mod tests {
use crate::api::projects::Jobs;
use crate::api::projects::{JobScope, Jobs};
#[test]
fn job_scope_as_str() {
let items = &[
(JobScope::Created, "created"),
(JobScope::Pending, "pending"),
(JobScope::Running, "running"),
(JobScope::Failed, "failed"),
(JobScope::Success, "success"),
(JobScope::Canceled, "canceled"),
(JobScope::Skipped, "skipped"),
(JobScope::Manual, "manual"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn project_is_needed() {
......
......@@ -134,7 +134,57 @@ impl<'a> Endpoint for CreatePipeline<'a> {
#[cfg(test)]
mod tests {
use crate::api::projects::pipelines::CreatePipeline;
use crate::api::projects::pipelines::{CreatePipeline, PipelineVariable, PipelineVariableType};
#[test]
fn pipeline_variable_type_default() {
assert_eq!(
PipelineVariableType::default(),
PipelineVariableType::EnvVar,
);
}
#[test]
fn pipeline_variable_type_as_str() {
let items = &[
(PipelineVariableType::EnvVar, "env_var"),
(PipelineVariableType::File, "file"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn pipeline_variable_key_and_value_are_necessary() {
let err = PipelineVariable::builder().build().unwrap_err();
assert_eq!(err, "`key` must be initialized");
}
#[test]
fn pipeline_variable_key_is_necessary() {
let err = PipelineVariable::builder()
.value("value")
.build()
.unwrap_err();
assert_eq!(err, "`key` must be initialized");
}
#[test]
fn pipeline_variable_value_is_necessary() {
let err = PipelineVariable::builder().key("key").build().unwrap_err();
assert_eq!(err, "`value` must be initialized");
}
#[test]
fn pipeline_variable_key_and_value_are_sufficient() {
PipelineVariable::builder()
.key("key")
.value("value")
.build()
.unwrap();
}
#[test]
fn project_and_ref_are_needed() {
......
......@@ -216,7 +216,62 @@ impl<'a> Pageable for Pipelines<'a> {}
#[cfg(test)]
mod tests {
use crate::api::projects::pipelines::Pipelines;
use crate::api::projects::pipelines::{
PipelineOrderBy, PipelineScope, PipelineStatus, Pipelines,
};
#[test]
fn pipeline_scope_as_str() {
let items = &[
(PipelineScope::Running, "running"),
(PipelineScope::Pending, "pending"),
(PipelineScope::Finished, "finished"),
(PipelineScope::Branches, "branches"),
(PipelineScope::Tags, "tags"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn pipeline_status_as_str() {
let items = &[
(PipelineStatus::Running, "running"),
(PipelineStatus::Pending, "pending"),
(PipelineStatus::Success, "success"),
(PipelineStatus::Failed, "failed"),
(PipelineStatus::Canceled, "canceled"),
(PipelineStatus::Skipped, "skipped"),
(PipelineStatus::Created, "created"),
(PipelineStatus::Manual, "manual"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn order_by_default() {
assert_eq!(PipelineOrderBy::default(), PipelineOrderBy::Id);
}
#[test]
fn order_by_as_str() {
let items = &[
(PipelineOrderBy::Id, "id"),
(PipelineOrderBy::Status, "status"),
(PipelineOrderBy::Ref, "ref"),
(PipelineOrderBy::UpdatedAt, "updated_at"),
(PipelineOrderBy::UserId, "user_id"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn project_is_needed() {
......
......@@ -256,7 +256,28 @@ impl<'a> Pageable for Projects<'a> {
#[cfg(test)]
mod tests {
use crate::api::projects::Projects;
use crate::api::projects::{ProjectOrderBy, Projects};
#[test]
fn order_by_default() {
assert_eq!(ProjectOrderBy::default(), ProjectOrderBy::CreatedAt);
}
#[test]
fn order_by_as_str() {
let items = &[
(ProjectOrderBy::Id, "id"),
(ProjectOrderBy::Name, "name"),
(ProjectOrderBy::Path, "path"),
(ProjectOrderBy::CreatedAt, "created_at"),
(ProjectOrderBy::UpdatedAt, "updated_at"),
(ProjectOrderBy::LastActivityAt, "last_activity_at"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn defaults_are_sufficient() {
......
......@@ -213,7 +213,57 @@ impl<'a> Pageable for Users<'a> {}
#[cfg(test)]
mod tests {
use crate::api::users::Users;
use crate::api::users::{ExternalProvider, UserOrderBy, Users};
#[test]
fn order_by_default() {
assert_eq!(UserOrderBy::default(), UserOrderBy::Id);
}
#[test]
fn order_by_as_str() {
let items = &[
(UserOrderBy::Id, "id"),
(UserOrderBy::Name, "name"),
(UserOrderBy::Username, "username"),
(UserOrderBy::CreatedAt, "created_at"),
(UserOrderBy::UpdatedAt, "updated_at"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn external_provider_id_and_name_are_necessary() {
let err = ExternalProvider::builder().build().unwrap_err();
assert_eq!(err, "`id` must be initialized");
}
#[test]
fn external_provider_id_is_necessary() {
let err = ExternalProvider::builder()
.name("name")
.build()
.unwrap_err();
assert_eq!(err, "`id` must be initialized");
}
#[test]
fn external_provider_name_is_necessary() {
let err = ExternalProvider::builder().id(1).build().unwrap_err();
assert_eq!(err, "`name` must be initialized");
}
#[test]
fn external_provider_id_and_name_are_sufficient() {
ExternalProvider::builder()
.id(1)
.name("name")
.build()
.unwrap();
}
#[test]
fn defaults_are_sufficient() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment