MCPcopy Create free account
hub / github.com/actix/examples / test

Function test

databases/mongodb/src/test.rs:10–52  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

8#[actix_web::test]
9#[ignore = "requires MongoDB instance running"]
10async fn test() {
11 let uri = std::env::var("MONGODB_URI").unwrap_or_else(|_| "mongodb://localhost:27017".into());
12
13 let client = Client::with_uri_str(uri).await.expect("failed to connect");
14
15 // Clear any data currently in the users collection.
16 client
17 .database(DB_NAME)
18 .collection::<User>(COLL_NAME)
19 .drop()
20 .await
21 .expect("drop collection should succeed");
22
23 let app = init_service(
24 App::new()
25 .app_data(web::Data::new(client))
26 .service(add_user)
27 .service(get_user),
28 )
29 .await;
30
31 let user = User {
32 first_name: "Jane".into(),
33 last_name: "Doe".into(),
34 username: "janedoe".into(),
35 email: "example@example.com".into(),
36 };
37
38 let req = TestRequest::post()
39 .uri("/add_user")
40 .set_form(&user)
41 .to_request();
42
43 let response = call_and_read_body(&app, req).await;
44 assert_eq!(response, Bytes::from_static(b"user added"));
45
46 let req = TestRequest::get()
47 .uri(&format!("/get_user/{}", user.username))
48 .to_request();
49
50 let response: User = call_and_read_body_json(&app, req).await;
51 assert_eq!(response, user);
52}

Callers

nothing calls this directly

Calls 1

postFunction · 0.85

Tested by

no test coverage detected