Skip to content

Commit

Permalink
Add host_network_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo1003 committed Oct 17, 2024
1 parent cd55e62 commit fad19c8
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/common/features/host_network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "host_network")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub hostname: String,
pub ipaddress: IpNetwork,
#[sea_orm(column_type = "Cidr")]
pub network: IpNetwork,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
2 changes: 2 additions & 0 deletions tests/common/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod custom_active_model;
pub mod dyn_table_name_lazy_static;
pub mod edit_log;
pub mod event_trigger;
pub mod host_network;
pub mod insert_default;
pub mod json_struct;
pub mod json_vec;
Expand Down Expand Up @@ -41,6 +42,7 @@ pub use collection_expanded::Entity as CollectionExpanded;
pub use dyn_table_name_lazy_static::Entity as DynTableNameLazyStatic;
pub use edit_log::Entity as EditLog;
pub use event_trigger::Entity as EventTrigger;
pub use host_network::Entity as HostNetwork;
pub use insert_default::Entity as InsertDefault;
pub use json_struct::Entity as JsonStruct;
pub use json_vec::Entity as JsonVec;
Expand Down
31 changes: 31 additions & 0 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
create_collection_table(db).await?;
create_event_trigger_table(db).await?;
create_categories_table(db).await?;
create_host_network_table(db).await?;
}

Ok(())
Expand Down Expand Up @@ -471,6 +472,36 @@ pub async fn create_collection_table(db: &DbConn) -> Result<ExecResult, DbErr> {
create_table(db, &stmt, Collection).await
}

pub async fn create_host_network_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
.table(host_network::Entity)
.col(
ColumnDef::new(host_network::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(host_network::Column::Hostname)
.string()
.not_null(),
)
.col(
ColumnDef::new(host_network::Column::Ipaddress)
.inet()
.not_null(),
)
.col(
ColumnDef::new(host_network::Column::Network)
.cidr()
.not_null(),
)
.to_owned();

create_table(db, &stmt, HostNetwork).await
}

pub async fn create_pi_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
.table(pi::Entity)
Expand Down
64 changes: 64 additions & 0 deletions tests/host_network_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![allow(unused_imports, dead_code)]

pub mod common;

use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use std::net::{Ipv4Addr, Ipv6Addr};

#[sea_orm_macros::test]
#[cfg(feature = "sqlx-postgres")]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("host_network_tests").await;
create_tables(&ctx.db).await?;
create_and_update_host_network(&ctx.db).await?;
ctx.delete().await;

Ok(())
}

async fn create_and_update_host_network(db: &DatabaseConnection) -> Result<(), DbErr> {
let addr = IpNetwork::new(Ipv4Addr::new(192, 168, 0, 20).into(), 24).unwrap();
let net = IpNetwork::new(addr.network(), addr.prefix()).unwrap();

let host = host_network::Model {
id: 1,
hostname: "example.com".to_owned(),
ipaddress: addr,
network: net,
};
let res = host.clone().into_active_model().insert(db).await?;

let model = HostNetwork::find().one(db).await?.unwrap();
assert_eq!(model, res);
assert_eq!(model, host.clone());

let addrv6 = IpNetwork::new(
Ipv6Addr::new(0xfd89, 0x1926, 0x4cae, 0x8abd, 0, 0, 0, 0x6f52).into(),
64,
)
.unwrap();
let netv6 = IpNetwork::new(addr.network(), addr.prefix()).unwrap();

let res = host_network::ActiveModel {
id: Set(1),
ipaddress: Set(addrv6),
network: Set(netv6),
..Default::default()
}
.update(db)
.await?;

assert_eq!(
res,
host_network::Model {
id: 1,
hostname: "example.com".to_owned(),
ipaddress: addrv6,
network: netv6,
}
);

Ok(())
}

0 comments on commit fad19c8

Please sign in to comment.