Skip to content

Commit 9ea039b

Browse files
add unit tests
1 parent 897a84a commit 9ea039b

File tree

2 files changed

+301
-0
lines changed

2 files changed

+301
-0
lines changed

src/models/create_deployment.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,93 @@ impl From<CreateDeploymentOptions> for atlas_local::models::CreateDeploymentOpti
6868
}
6969
}
7070
}
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use crate::models::list_deployments::{BindingType, CreationSourceType};
75+
76+
use super::*;
77+
78+
#[test]
79+
fn test_lib_create_deployment_options_from_create_deployment_options() {
80+
let create_deployment_options = CreateDeploymentOptions {
81+
name: Some("test_deployment".to_string()),
82+
image: Some("mongodb/mongodb-atlas-local".to_string()),
83+
mongodb_version: Some("8.0.0".to_string()),
84+
creation_source: Some(CreationSource {
85+
source_type: CreationSourceType::MCPServer,
86+
source: "MCPSERVER".to_string(),
87+
}),
88+
local_seed_location: Some("/host/seed-data".to_string()),
89+
mongodb_initdb_database: Some("testdb".to_string()),
90+
mongodb_initdb_root_password_file: Some("/run/secrets/password".to_string()),
91+
mongodb_initdb_root_password: Some("password123".to_string()),
92+
mongodb_initdb_root_username_file: Some("/run/secrets/username".to_string()),
93+
mongodb_initdb_root_username: Some("admin".to_string()),
94+
mongot_log_file: Some("/tmp/mongot.log".to_string()),
95+
runner_log_file: Some("/tmp/runner.log".to_string()),
96+
do_not_track: Some(false),
97+
telemetry_base_url: Some("https://telemetry.example.com".to_string()),
98+
mongodb_port_binding: Some(MongoDBPortBinding {
99+
binding_type: BindingType::Loopback,
100+
ip: "127.0.0.1".to_string(),
101+
port: 27017,
102+
}),
103+
};
104+
let lib_create_deployment_options: atlas_local::models::CreateDeploymentOptions =
105+
create_deployment_options.into();
106+
assert_eq!(
107+
lib_create_deployment_options.name,
108+
Some("test_deployment".to_string())
109+
);
110+
assert_eq!(
111+
lib_create_deployment_options.image,
112+
Some("mongodb/mongodb-atlas-local".to_string())
113+
);
114+
assert_eq!(
115+
lib_create_deployment_options.mongodb_version,
116+
Some(Version::new(8, 0, 0))
117+
);
118+
assert_eq!(
119+
lib_create_deployment_options.creation_source,
120+
Some(atlas_local::models::CreationSource::MCPServer)
121+
);
122+
assert_eq!(
123+
lib_create_deployment_options.local_seed_location,
124+
Some("/host/seed-data".to_string())
125+
);
126+
assert_eq!(
127+
lib_create_deployment_options.mongodb_initdb_database,
128+
Some("testdb".to_string())
129+
);
130+
assert_eq!(
131+
lib_create_deployment_options.mongodb_initdb_root_password_file,
132+
Some("/run/secrets/password".to_string())
133+
);
134+
assert_eq!(
135+
lib_create_deployment_options.mongodb_initdb_root_password,
136+
Some("password123".to_string())
137+
);
138+
assert_eq!(
139+
lib_create_deployment_options.mongodb_initdb_root_username_file,
140+
Some("/run/secrets/username".to_string())
141+
);
142+
assert_eq!(
143+
lib_create_deployment_options.mongodb_initdb_root_username,
144+
Some("admin".to_string())
145+
);
146+
assert_eq!(
147+
lib_create_deployment_options.mongot_log_file,
148+
Some("/tmp/mongot.log".to_string())
149+
);
150+
assert_eq!(
151+
lib_create_deployment_options.runner_log_file,
152+
Some("/tmp/runner.log".to_string())
153+
);
154+
assert_eq!(lib_create_deployment_options.do_not_track, Some(false));
155+
assert_eq!(
156+
lib_create_deployment_options.telemetry_base_url,
157+
Some("https://telemetry.example.com".to_string())
158+
);
159+
}
160+
}

src/models/list_deployments.rs

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct Deployment {
3737
}
3838

3939
#[napi(string_enum)]
40+
#[derive(PartialEq, Debug)]
4041
pub enum State {
4142
Created,
4243
Dead,
@@ -48,6 +49,7 @@ pub enum State {
4849
}
4950

5051
#[napi(object)]
52+
#[derive(PartialEq, Debug)]
5153
pub struct MongoDBPortBinding {
5254
#[napi(js_name = "type")]
5355
pub binding_type: BindingType,
@@ -56,26 +58,30 @@ pub struct MongoDBPortBinding {
5658
}
5759

5860
#[napi(string_enum)]
61+
#[derive(PartialEq, Debug)]
5962
pub enum BindingType {
6063
Loopback, // 127.0.0.1
6164
AnyInterface, // 0.0.0.0
6265
Specific, // Specific IP address
6366
}
6467

6568
#[napi(string_enum)]
69+
#[derive(PartialEq, Debug)]
6670
pub enum MongodbType {
6771
Community,
6872
Enterprise,
6973
}
7074

7175
#[napi(object)]
76+
#[derive(PartialEq, Debug)]
7277
pub struct CreationSource {
7378
#[napi(js_name = "type")]
7479
pub source_type: CreationSourceType,
7580
pub source: String,
7681
}
7782

7883
#[napi(string_enum)]
84+
#[derive(PartialEq, Debug)]
7985
pub enum CreationSourceType {
8086
AtlasCLI,
8187
Container,
@@ -210,3 +216,208 @@ impl From<CreationSource> for atlas_local::models::CreationSource {
210216
}
211217
}
212218
}
219+
220+
#[cfg(test)]
221+
mod tests {
222+
use semver::Version;
223+
224+
use super::*;
225+
226+
#[test]
227+
fn test_deployment_from_lib_deployment() {
228+
let lib_deployment = atlas_local::models::Deployment {
229+
container_id: "container_id".to_string(),
230+
name: Some("test_deployment".to_string()),
231+
state: atlas_local::models::State::Running,
232+
port_bindings: Some(atlas_local::models::MongoDBPortBinding {
233+
binding_type: atlas_local::models::BindingType::Loopback,
234+
port: 27017,
235+
}),
236+
mongodb_type: atlas_local::models::MongodbType::Community,
237+
mongodb_version: Version::new(8, 0, 0),
238+
creation_source: Some(atlas_local::models::CreationSource::AtlasCLI),
239+
local_seed_location: Some("/host/seed-data".to_string()),
240+
mongodb_initdb_database: Some("testdb".to_string()),
241+
mongodb_initdb_root_password_file: Some("/run/secrets/password".to_string()),
242+
mongodb_initdb_root_password: Some("password123".to_string()),
243+
mongodb_initdb_root_username_file: Some("/run/secrets/username".to_string()),
244+
mongodb_initdb_root_username: Some("admin".to_string()),
245+
mongot_log_file: Some("/tmp/mongot.log".to_string()),
246+
runner_log_file: Some("/tmp/runner.log".to_string()),
247+
do_not_track: Some("false".to_string()),
248+
telemetry_base_url: Some("https://telemetry.example.com".to_string()),
249+
};
250+
251+
let deployment: Deployment = lib_deployment.into();
252+
253+
assert_eq!(deployment.container_id, "container_id");
254+
assert_eq!(deployment.name, Some("test_deployment".to_string()));
255+
assert_eq!(deployment.state, State::Running);
256+
assert!(deployment.port_bindings.is_some());
257+
let port_binding = deployment.port_bindings.unwrap();
258+
assert_eq!(port_binding.binding_type, BindingType::Loopback);
259+
assert_eq!(port_binding.ip, "127.0.0.1");
260+
assert_eq!(port_binding.port, 27017);
261+
assert_eq!(deployment.mongodb_type, MongodbType::Community);
262+
assert_eq!(deployment.mongodb_version, "8.0.0");
263+
assert_eq!(
264+
deployment.creation_source,
265+
Some(CreationSource {
266+
source_type: CreationSourceType::AtlasCLI,
267+
source: "ATLASCLI".to_string(),
268+
})
269+
);
270+
assert_eq!(
271+
deployment.local_seed_location,
272+
Some("/host/seed-data".to_string())
273+
);
274+
assert_eq!(
275+
deployment.mongodb_initdb_database,
276+
Some("testdb".to_string())
277+
);
278+
assert_eq!(
279+
deployment.mongodb_initdb_root_password_file,
280+
Some("/run/secrets/password".to_string())
281+
);
282+
assert_eq!(
283+
deployment.mongodb_initdb_root_password,
284+
Some("password123".to_string())
285+
);
286+
assert_eq!(
287+
deployment.mongodb_initdb_root_username_file,
288+
Some("/run/secrets/username".to_string())
289+
);
290+
assert_eq!(
291+
deployment.mongodb_initdb_root_username,
292+
Some("admin".to_string())
293+
);
294+
assert_eq!(
295+
deployment.mongot_log_file,
296+
Some("/tmp/mongot.log".to_string())
297+
);
298+
assert_eq!(
299+
deployment.runner_log_file,
300+
Some("/tmp/runner.log".to_string())
301+
);
302+
assert_eq!(deployment.do_not_track, Some("false".to_string()));
303+
assert_eq!(
304+
deployment.telemetry_base_url,
305+
Some("https://telemetry.example.com".to_string())
306+
);
307+
}
308+
309+
#[test]
310+
fn test_mongodb_port_binding_from_lib_mongodb_port_binding_loopback() {
311+
let lib_mongodb_port_binding = atlas_local::models::MongoDBPortBinding {
312+
binding_type: atlas_local::models::BindingType::Loopback,
313+
port: 27017,
314+
};
315+
let mongodb_port_binding: MongoDBPortBinding = lib_mongodb_port_binding.into();
316+
assert_eq!(mongodb_port_binding.binding_type, BindingType::Loopback);
317+
assert_eq!(mongodb_port_binding.ip, "127.0.0.1");
318+
assert_eq!(mongodb_port_binding.port, 27017);
319+
}
320+
321+
#[test]
322+
fn test_mongodb_port_binding_from_lib_mongodb_port_binding_any_interface() {
323+
let lib_mongodb_port_binding = atlas_local::models::MongoDBPortBinding {
324+
binding_type: atlas_local::models::BindingType::AnyInterface,
325+
port: 27017,
326+
};
327+
let mongodb_port_binding: MongoDBPortBinding = lib_mongodb_port_binding.into();
328+
assert_eq!(mongodb_port_binding.binding_type, BindingType::AnyInterface);
329+
assert_eq!(mongodb_port_binding.ip, "0.0.0.0");
330+
assert_eq!(mongodb_port_binding.port, 27017);
331+
}
332+
333+
#[test]
334+
fn test_mongodb_port_binding_from_lib_mongodb_port_binding_specific() {
335+
let lib_mongodb_port_binding = atlas_local::models::MongoDBPortBinding {
336+
binding_type: atlas_local::models::BindingType::Specific("192.0.2.0".parse().unwrap()),
337+
port: 27017,
338+
};
339+
let mongodb_port_binding: MongoDBPortBinding = lib_mongodb_port_binding.into();
340+
assert_eq!(mongodb_port_binding.binding_type, BindingType::Specific);
341+
assert_eq!(mongodb_port_binding.ip, "192.0.2.0");
342+
assert_eq!(mongodb_port_binding.port, 27017);
343+
}
344+
345+
#[test]
346+
fn test_mongodb_port_binding_lib_into_mongodb_port_binding_loopback() {
347+
let mongodb_port_binding = MongoDBPortBinding {
348+
binding_type: BindingType::Loopback,
349+
ip: "127.0.0.1".to_string(),
350+
port: 27017,
351+
};
352+
let lib_mongodb_port_binding: atlas_local::models::MongoDBPortBinding =
353+
mongodb_port_binding.into();
354+
assert_eq!(
355+
lib_mongodb_port_binding.binding_type,
356+
atlas_local::models::BindingType::Loopback
357+
);
358+
assert_eq!(lib_mongodb_port_binding.port, 27017);
359+
}
360+
361+
#[test]
362+
fn test_mongodb_port_binding_lib_into_mongodb_port_binding_any_interface() {
363+
let mongodb_port_binding = MongoDBPortBinding {
364+
binding_type: BindingType::AnyInterface,
365+
ip: "0.0.0.0".to_string(),
366+
port: 27017,
367+
};
368+
let lib_mongodb_port_binding: atlas_local::models::MongoDBPortBinding =
369+
mongodb_port_binding.into();
370+
assert_eq!(
371+
lib_mongodb_port_binding.binding_type,
372+
atlas_local::models::BindingType::AnyInterface
373+
);
374+
assert_eq!(lib_mongodb_port_binding.port, 27017);
375+
}
376+
#[test]
377+
fn test_mongodb_port_binding_lib_into_mongodb_port_binding_specific() {
378+
let mongodb_port_binding = MongoDBPortBinding {
379+
binding_type: BindingType::Specific,
380+
ip: "192.0.2.0".to_string(),
381+
port: 27017,
382+
};
383+
let lib_mongodb_port_binding: atlas_local::models::MongoDBPortBinding =
384+
mongodb_port_binding.into();
385+
assert_eq!(
386+
lib_mongodb_port_binding.binding_type,
387+
atlas_local::models::BindingType::Specific("192.0.2.0".parse().unwrap())
388+
);
389+
assert_eq!(lib_mongodb_port_binding.port, 27017);
390+
}
391+
392+
#[test]
393+
fn test_creation_source_from_lib_creation_source_atlas_cli() {
394+
let lib_creation_source = atlas_local::models::CreationSource::AtlasCLI;
395+
let creation_source: CreationSource = lib_creation_source.into();
396+
assert_eq!(creation_source.source_type, CreationSourceType::AtlasCLI);
397+
assert_eq!(creation_source.source, "ATLASCLI");
398+
}
399+
400+
#[test]
401+
fn test_creation_source_from_lib_creation_source_container() {
402+
let lib_creation_source = atlas_local::models::CreationSource::Container;
403+
let creation_source: CreationSource = lib_creation_source.into();
404+
assert_eq!(creation_source.source_type, CreationSourceType::Container);
405+
assert_eq!(creation_source.source, "CONTAINER");
406+
}
407+
408+
#[test]
409+
fn test_creation_source_from_lib_creation_source_mcp_server() {
410+
let lib_creation_source = atlas_local::models::CreationSource::MCPServer;
411+
let creation_source: CreationSource = lib_creation_source.into();
412+
assert_eq!(creation_source.source_type, CreationSourceType::MCPServer);
413+
assert_eq!(creation_source.source, "MCPSERVER");
414+
}
415+
416+
#[test]
417+
fn test_creation_source_from_lib_creation_source_unknown() {
418+
let lib_creation_source = atlas_local::models::CreationSource::Unknown("test".to_string());
419+
let creation_source: CreationSource = lib_creation_source.into();
420+
assert_eq!(creation_source.source_type, CreationSourceType::Other);
421+
assert_eq!(creation_source.source, "test");
422+
}
423+
}

0 commit comments

Comments
 (0)