Skip to content

Commit 3b800fa

Browse files
committed
Remove mock client (#17)
1 parent e4efe92 commit 3b800fa

File tree

4 files changed

+24
-37
lines changed

4 files changed

+24
-37
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ async-graphql-axum = "7.0.17"
1414
url = "2.5.7"
1515
config = "0.15.16"
1616
clap = { version = "4.5.48", features = ["derive"] }
17+
18+
[dev-dependencies]
19+
httpmock = "0.8.2"

src/clients.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
use std::fmt;
2-
#[cfg(test)]
3-
use std::fs::File;
4-
#[cfg(test)]
5-
use std::path::PathBuf;
62

73
use reqwest::Url;
84
use serde::de::DeserializeOwned;
@@ -37,29 +33,6 @@ impl Client for TiledClient {
3733
}
3834
}
3935

40-
#[cfg(test)]
41-
pub struct MockTiledClient {
42-
pub dir_path: PathBuf,
43-
}
44-
45-
#[cfg(test)]
46-
impl MockTiledClient {
47-
async fn deserialize_from_file<T: DeserializeOwned>(&self, filename: &str) -> ClientResult<T> {
48-
println!("Requesting data from mock");
49-
50-
let path = self.dir_path.join(filename);
51-
let file = File::open(&path)?;
52-
53-
Ok(serde_json::from_reader(file)?)
54-
}
55-
}
56-
#[cfg(test)]
57-
impl Client for MockTiledClient {
58-
async fn metadata(&self) -> ClientResult<Metadata> {
59-
self.deserialize_from_file("tiled_metadata.json").await
60-
}
61-
}
62-
6336
#[derive(Debug)]
6437
pub enum ClientError {
6538
Parse(url::ParseError),

src/handlers.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::clients::Client;
88
use crate::model::TiledQuery;
99

1010
pub async fn graphql_handler<T: Client + Send + Sync + 'static>(
11-
schema: Extension<Schema<TiledQuery<T>, EmptyMutation, EmptySubscription>>,
11+
schema: Extension<Schema<TiledQuery, EmptyMutation, EmptySubscription>>,
1212
req: GraphQLRequest,
1313
) -> GraphQLResponse {
1414
let query = req.into_inner().query;
@@ -23,15 +23,27 @@ pub async fn graphiql_handler() -> impl IntoResponse {
2323
#[cfg(test)]
2424
mod tests {
2525
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
26+
use httpmock::MockServer;
27+
use url::Url;
2628

2729
use crate::TiledQuery;
28-
use crate::clients::MockTiledClient;
30+
use crate::clients::TiledClient;
2931

3032
#[tokio::test]
3133
async fn test_api_version_query() {
34+
let mock_server = MockServer::start();
35+
36+
let mock = mock_server
37+
.mock_async(|when, then| {
38+
when.method("GET").path("/api/v1/");
39+
then.status(200)
40+
.body_from_file("resources/tiled_metadata.json");
41+
})
42+
.await;
43+
3244
let schema = Schema::build(
33-
TiledQuery(MockTiledClient {
34-
dir_path: "./resources".into(),
45+
TiledQuery(TiledClient {
46+
address: Url::parse(&mock_server.base_url()).unwrap(),
3547
}),
3648
EmptyMutation,
3749
EmptySubscription,
@@ -40,8 +52,7 @@ mod tests {
4052

4153
let response = schema.execute("{metadata { apiVersion } }").await;
4254

43-
println!("{:?}", response.data.to_string());
44-
45-
assert!(response.data.to_string() == "{metadata: {apiVersion: 0}}")
55+
assert_eq!(response.data.to_string(), "{metadata: {apiVersion: 0}}");
56+
mock.assert();
4657
}
4758
}

src/model.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ pub(crate) mod metadata;
22

33
use async_graphql::Object;
44

5-
use crate::clients::{Client, ClientError};
5+
use crate::clients::{Client, ClientError, TiledClient};
66

7-
pub(crate) struct TiledQuery<T>(pub T);
7+
pub(crate) struct TiledQuery(pub TiledClient);
88

99
#[Object]
10-
impl<T: Client + Send + Sync + 'static> TiledQuery<T> {
10+
impl TiledQuery {
1111
async fn metadata(&self) -> async_graphql::Result<metadata::Metadata, ClientError> {
1212
self.0.metadata().await
1313
}

0 commit comments

Comments
 (0)