From f9799437a5f92bfb51941d1e74e6b65671bc8ced Mon Sep 17 00:00:00 2001 From: mogery Date: Wed, 7 Feb 2024 18:08:46 +0000 Subject: [PATCH 1/5] feat(execution_spec): add and use MockFileSystem --- tests/execution_spec.rs | 154 +++++++++++++++++++++++++--------------- 1 file changed, 96 insertions(+), 58 deletions(-) diff --git a/tests/execution_spec.rs b/tests/execution_spec.rs index f201041ec7..0febaaf56c 100644 --- a/tests/execution_spec.rs +++ b/tests/execution_spec.rs @@ -18,14 +18,14 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use tailcall::async_graphql_hyper::{GraphQLBatchRequest, GraphQLRequest}; use tailcall::blueprint::{self, Blueprint, Upstream}; -use tailcall::cli::{init_file, init_hook_http, init_http, init_in_memory_cache, init_runtime}; +use tailcall::cli::{init_hook_http, init_http, init_in_memory_cache, init_runtime}; use tailcall::config::reader::ConfigReader; use tailcall::config::{Config, ConfigModule, Source}; use tailcall::http::{handle_request, AppContext, Method, Response}; use tailcall::print_schema::print_schema; use tailcall::target_runtime::TargetRuntime; use tailcall::valid::{Cause, ValidationError, Validator as _}; -use tailcall::{EnvIO, HttpIO}; +use tailcall::{EnvIO, FileIO, HttpIO}; use url::Url; static INIT: Once = Once::new(); @@ -139,6 +139,7 @@ struct ExecutionSpec { mock: Option>, env: Option>, assert: Option>, + files: BTreeMap, // Annotations for the runner runner: Option, @@ -217,6 +218,7 @@ impl ExecutionSpec { let mut server: Vec<(Source, String)> = Vec::with_capacity(2); let mut mock: Option> = None; let mut env: Option> = None; + let mut files: BTreeMap = BTreeMap::new(); let mut assert: Option> = None; let mut runner: Option = None; let mut check_identity = false; @@ -308,64 +310,71 @@ impl ExecutionSpec { return Err(anyhow!("Unexpected non-code block node or EOF after component definition in {:?}", path)); }; - let lang = match lang { - Some(x) => Ok(x), - None => { - Err(anyhow!("Unexpected languageless code block in {:?}", path)) - } - }?; - - let source = Source::from_str(&lang)?; - // Parse component name if let Some(Node::Text(text)) = heading.children.first() { let name = text.value.as_str(); - match name { - "server:" => { - // Server configs are only parsed if the test isn't skipped. - server.push((source, content)); + if name.starts_with("file:") { + let name = &name[5..]; + if files.insert(name.to_string(), content).is_some() { + return Err(anyhow!("Double declaration of file {:?} in {:#?}", name, path)); } - "mock:" => { - if mock.is_none() { - mock = match source { - Source::Json => Ok(serde_json::from_str(&content)?), - Source::Yml => Ok(serde_yaml::from_str(&content)?), - _ => Err(anyhow!("Unexpected language in mock block in {:?} (only JSON and YAML are supported)", path)), - }?; - } else { - return Err(anyhow!("Unexpected number of mock blocks in {:?} (only one is allowed)", path)); + } else { + let lang = match lang { + Some(x) => Ok(x), + None => { + Err(anyhow!("Unexpected languageless code block in {:?}", path)) } - } - "env:" => { - if env.is_none() { - env = match source { - Source::Json => Ok(serde_json::from_str(&content)?), - Source::Yml => Ok(serde_yaml::from_str(&content)?), - _ => Err(anyhow!("Unexpected language in env block in {:?} (only JSON and YAML are supported)", path)), - }?; - } else { - return Err(anyhow!("Unexpected number of env blocks in {:?} (only one is allowed)", path)); + }?; + + let source = Source::from_str(&lang)?; + + match name { + "server:" => { + // Server configs are only parsed if the test isn't skipped. + server.push((source, content)); } - } - "assert:" => { - if assert.is_none() { - assert = match source { - Source::Json => Ok(serde_json::from_str(&content)?), - Source::Yml => Ok(serde_yaml::from_str(&content)?), - _ => Err(anyhow!("Unexpected language in assert block in {:?} (only JSON and YAML are supported)", path)), - }?; - } else { - return Err(anyhow!("Unexpected number of assert blocks in {:?} (only one is allowed)", path)); + "mock:" => { + if mock.is_none() { + mock = match source { + Source::Json => Ok(serde_json::from_str(&content)?), + Source::Yml => Ok(serde_yaml::from_str(&content)?), + _ => Err(anyhow!("Unexpected language in mock block in {:?} (only JSON and YAML are supported)", path)), + }?; + } else { + return Err(anyhow!("Unexpected number of mock blocks in {:?} (only one is allowed)", path)); + } + } + "env:" => { + if env.is_none() { + env = match source { + Source::Json => Ok(serde_json::from_str(&content)?), + Source::Yml => Ok(serde_yaml::from_str(&content)?), + _ => Err(anyhow!("Unexpected language in env block in {:?} (only JSON and YAML are supported)", path)), + }?; + } else { + return Err(anyhow!("Unexpected number of env blocks in {:?} (only one is allowed)", path)); + } + } + "assert:" => { + if assert.is_none() { + assert = match source { + Source::Json => Ok(serde_json::from_str(&content)?), + Source::Yml => Ok(serde_yaml::from_str(&content)?), + _ => Err(anyhow!("Unexpected language in assert block in {:?} (only JSON and YAML are supported)", path)), + }?; + } else { + return Err(anyhow!("Unexpected number of assert blocks in {:?} (only one is allowed)", path)); + } + } + _ => { + return Err(anyhow!( + "Unexpected component {:?} in {:?}: {:#?}", + name, + path, + heading + )) } - } - _ => { - return Err(anyhow!( - "Unexpected component {:?} in {:?}: {:#?}", - name, - path, - heading - )) } } } else { @@ -404,6 +413,7 @@ impl ExecutionSpec { mock, env, assert, + files, runner, check_identity, @@ -438,7 +448,7 @@ impl ExecutionSpec { let runtime = TargetRuntime { http, http2_only, - file: init_file(), + file: Arc::new(MockFileSystem::new(self.clone())), env: Arc::new(Env::init(env)), cache: Arc::new(init_in_memory_cache()), }; @@ -564,10 +574,35 @@ impl HttpIO for MockHttpClient { } } +struct MockFileSystem { + spec: ExecutionSpec +} + +impl MockFileSystem { + fn new(spec: ExecutionSpec) -> MockFileSystem { + MockFileSystem { spec } + } +} + +#[async_trait::async_trait] +impl FileIO for MockFileSystem { + async fn write<'a>(&'a self, _path: &'a str, _content: &'a [u8]) -> anyhow::Result<()> { + Err(anyhow!("Cannot write to a file in an execution spec")) + } + + async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result { + let base = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/execution/"); + let path = path.strip_prefix(&base.to_string_lossy().to_string()).unwrap_or(path); + + match self.spec.files.get(path) { + Some(x) => Ok(x.to_owned()), + None => Err(anyhow!("No such file or directory (os error 2)")), + } + } +} + async fn assert_spec(spec: ExecutionSpec) { let will_insta_panic = std::env::var("INSTA_FORCE_PASS").is_err(); - let runtime = init_runtime(&Upstream::default(), None); - let reader = ConfigReader::init(runtime); // Parse and validate all server configs + check for identity log::info!("{} {} ...", spec.name, spec.path.display()); @@ -584,7 +619,8 @@ async fn assert_spec(spec: ExecutionSpec) { let config = match config { Ok(config) => { - let runtime = init_runtime(&blueprint::Upstream::default(), None); + let mut runtime = init_runtime(&blueprint::Upstream::default(), None); + runtime.file = Arc::new(MockFileSystem::new(spec.clone())); let reader = ConfigReader::init(runtime); match reader .resolve(config, Some(spec.path.to_string_lossy().to_string())) @@ -684,9 +720,11 @@ async fn assert_spec(spec: ExecutionSpec) { log::info!("\tmerged ok"); } - dbg!(spec.path.to_string_lossy().to_string()); - // Resolve all configs + let mut runtime = init_runtime(&Upstream::default(), None); + runtime.file = Arc::new(MockFileSystem::new(spec.clone())); + let reader = ConfigReader::init(runtime); + let server: Vec = join_all( server .into_iter() From 6ee4a3dee3ca3ca07f75d3439f474fe3d117c351 Mon Sep 17 00:00:00 2001 From: mogery Date: Wed, 7 Feb 2024 18:09:01 +0000 Subject: [PATCH 2/5] chore: convert tests to use mock fs --- tests/execution/grpc-batch.md | 39 ++++++++++++++++- .../grpc-override-url-from-upstream.md | 39 ++++++++++++++++- tests/execution/grpc-simple.md | 39 ++++++++++++++++- tests/execution/grpc-url-from-upstream.md | 39 ++++++++++++++++- .../test-add-link-to-empty-config.md | 29 ++++++++++++- tests/execution/test-duplicated-link.md | 42 +++++++++++++++--- tests/execution/test-expr-success.md | 39 ++++++++++++++++- tests/execution/test-grpc-group-by.md | 39 ++++++++++++++++- tests/execution/test-grpc-missing-fields.md | 39 ++++++++++++++++- tests/execution/test-grpc-nested-data.md | 39 ++++++++++++++++- tests/execution/test-grpc-nested-optional.md | 39 ++++++++++++++++- tests/execution/test-grpc-optional.md | 39 ++++++++++++++++- tests/execution/test-grpc-service-method.md | 39 ++++++++++++++++- tests/execution/test-grpc-service.md | 39 ++++++++++++++++- tests/execution/test-grpc.md | 39 ++++++++++++++++- tests/execution/test-js-hello-world.md | 43 ++++++++++++++++++- tests/execution/test-js-multiple-scripts.md | 43 ++++++++++++++++++- tests/execution/test-js-request-reponse.md | 43 ++++++++++++++++++- .../test-missing-argument-on-all-resolvers.md | 39 ++++++++++++++++- tests/execution/test-nested-link.md | 40 ++++++++++++++++- .../execution_spec__grpc-batch.md_merged.snap | 2 +- ...-override-url-from-upstream.md_merged.snap | 2 +- ...execution_spec__grpc-simple.md_merged.snap | 2 +- ...pec__grpc-url-from-upstream.md_merged.snap | 2 +- ...st-add-link-to-empty-config.md_merged.snap | 2 +- ...ion_spec__test-expr-success.md_merged.snap | 2 +- .../execution_spec__test-grpc.md_merged.snap | 2 +- ...n_spec__test-js-hello-world.md_merged.snap | 2 +- ...ec__test-js-request-reponse.md_merged.snap | 2 +- ...tion_spec__test-nested-link.md_merged.snap | 2 +- 30 files changed, 772 insertions(+), 34 deletions(-) diff --git a/tests/execution/grpc-batch.md b/tests/execution/grpc-batch.md index 33f0b942d1..e5b15750e5 100644 --- a/tests/execution/grpc-batch.md +++ b/tests/execution/grpc-batch.md @@ -1,12 +1,49 @@ # Grpc datasource with batching +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql schema @server(port: 8000, graphiql: true) @upstream(httpCache: true, batch: {delay: 10}) - @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { + @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/grpc-override-url-from-upstream.md b/tests/execution/grpc-override-url-from-upstream.md index 1564259a79..ea4a165843 100644 --- a/tests/execution/grpc-override-url-from-upstream.md +++ b/tests/execution/grpc-override-url-from-upstream.md @@ -1,12 +1,49 @@ # Grpc datasource +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql schema @server(port: 8000, graphiql: true) @upstream(httpCache: true, batch: {delay: 10}, baseURL: "http://not-a-valid-grpc-url.com") - @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { + @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/grpc-simple.md b/tests/execution/grpc-simple.md index d617835832..8a28d3a5e3 100644 --- a/tests/execution/grpc-simple.md +++ b/tests/execution/grpc-simple.md @@ -1,12 +1,49 @@ # Grpc datasource +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql schema @server(port: 8000, graphiql: true) @upstream(httpCache: true, batch: {delay: 10}) - @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { + @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/grpc-url-from-upstream.md b/tests/execution/grpc-url-from-upstream.md index 2a82aa93a0..c61e130223 100644 --- a/tests/execution/grpc-url-from-upstream.md +++ b/tests/execution/grpc-url-from-upstream.md @@ -1,12 +1,49 @@ # Grpc datasource +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql schema @server(port: 8000, graphiql: true) @upstream(httpCache: true, batch: {delay: 10}, baseURL: "http://localhost:50051") - @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { + @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-add-link-to-empty-config.md b/tests/execution/test-add-link-to-empty-config.md index 5425efa42e..f46b66ba3d 100644 --- a/tests/execution/test-add-link-to-empty-config.md +++ b/tests/execution/test-add-link-to-empty-config.md @@ -2,10 +2,37 @@ ###### check identity +#### file:link-const.graphql +```graphql +schema @server @upstream { + query: Query +} + +type Query { + hello: String @const(data: "Hello from server") +} +``` + +#### file:link-enum.graphql +```graphql +schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") { + query: Query +} + +enum Foo { + BAR + BAZ +} + +type Query { + foo: Foo @http(path: "/foo") +} +``` + #### server: ```graphql -schema @server @upstream @link(src: "../fixtures/link-const.graphql", type: Config) @link(src: "../fixtures/link-enum.graphql", type: Config) { +schema @server @upstream @link(src: "link-const.graphql", type: Config) @link(src: "link-enum.graphql", type: Config) { query: Query } ``` diff --git a/tests/execution/test-duplicated-link.md b/tests/execution/test-duplicated-link.md index f5ff15c1c7..074ec9c333 100644 --- a/tests/execution/test-duplicated-link.md +++ b/tests/execution/test-duplicated-link.md @@ -2,15 +2,47 @@ ###### sdl error +#### file:jsonplaceholder.graphql +```graphql +schema + @server(port: 8000, graphiql: true, hostname: "0.0.0.0") + @upstream(baseURL: "http://jsonplaceholder.typicode.com", httpCache: true, batch: {delay: 100}) { + query: Query +} + +type Query { + posts: [Post] @http(path: "/posts") + users: [User] @http(path: "/users") + user(id: Int!): User @http(path: "/users/{{args.id}}") +} + +type User { + id: Int! + name: String! + username: String! + email: String! + phone: String + website: String +} + +type Post { + id: Int! + userId: Int! + title: String! + body: String! + user: User @http(path: "/users/{{value.userId}}") +} +``` + #### server: ```graphql schema - @link(type: Config, src: "../../examples/jsonplaceholder.graphql", id: "placeholder") - @link(type: Config, src: "../../examples/jsonplaceholder.graphql", id: "placeholder1") - @link(type: Config, src: "../../examples/jsonplaceholder.graphql", id: "placeholder1") - @link(type: Config, src: "../../examples/jsonplaceholder.graphql", id: "placeholder2") - @link(type: Config, src: "../../examples/jsonplaceholder.graphql", id: "placeholder2") { + @link(type: Config, src: "jsonplaceholder.graphql", id: "placeholder") + @link(type: Config, src: "jsonplaceholder.graphql", id: "placeholder1") + @link(type: Config, src: "jsonplaceholder.graphql", id: "placeholder1") + @link(type: Config, src: "jsonplaceholder.graphql", id: "placeholder2") + @link(type: Config, src: "jsonplaceholder.graphql", id: "placeholder2") { query: Query } diff --git a/tests/execution/test-expr-success.md b/tests/execution/test-expr-success.md index 92ad96c06a..72d5674301 100644 --- a/tests/execution/test-expr-success.md +++ b/tests/execution/test-expr-success.md @@ -2,10 +2,47 @@ ###### check identity +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql -schema @server(port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 1000}) @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { +schema @server(port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 1000}) @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-grpc-group-by.md b/tests/execution/test-grpc-group-by.md index c44f5d2fb9..ddd5dcecd6 100644 --- a/tests/execution/test-grpc-group-by.md +++ b/tests/execution/test-grpc-group-by.md @@ -2,13 +2,50 @@ ###### sdl error +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql schema @server(port: 8000, graphiql: true) @upstream(httpCache: true, batch: {delay: 10}) - @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { + @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-grpc-missing-fields.md b/tests/execution/test-grpc-missing-fields.md index 4de407fb32..fe5632a9b3 100644 --- a/tests/execution/test-grpc-missing-fields.md +++ b/tests/execution/test-grpc-missing-fields.md @@ -2,10 +2,47 @@ ###### sdl error +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql -schema @link(id: "news", src: "../graphql/errors/proto/news.proto", type: Protobuf) { +schema @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-grpc-nested-data.md b/tests/execution/test-grpc-nested-data.md index 6d60a0fad7..3eeafaca7c 100644 --- a/tests/execution/test-grpc-nested-data.md +++ b/tests/execution/test-grpc-nested-data.md @@ -2,13 +2,50 @@ ###### sdl error +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql schema @server(port: 8000, graphiql: true) @upstream(httpCache: true, batch: {delay: 10}) - @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { + @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-grpc-nested-optional.md b/tests/execution/test-grpc-nested-optional.md index 7459b21a50..bfa0bb7f5b 100644 --- a/tests/execution/test-grpc-nested-optional.md +++ b/tests/execution/test-grpc-nested-optional.md @@ -2,10 +2,47 @@ ###### sdl error +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql -schema @link(id: "news", src: "../graphql/errors/proto/news.proto", type: Protobuf) { +schema @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-grpc-optional.md b/tests/execution/test-grpc-optional.md index 27b4b62051..f910b9c5cc 100644 --- a/tests/execution/test-grpc-optional.md +++ b/tests/execution/test-grpc-optional.md @@ -2,10 +2,47 @@ ###### sdl error +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql -schema @link(id: "news", src: "../graphql/errors/proto/news.proto", type: Protobuf) { +schema @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-grpc-service-method.md b/tests/execution/test-grpc-service-method.md index 8a0a614753..048cdb94dd 100644 --- a/tests/execution/test-grpc-service-method.md +++ b/tests/execution/test-grpc-service-method.md @@ -2,10 +2,47 @@ ###### sdl error +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql -schema @link(id: "news", src: "../graphql/errors/proto/news.proto", type: Protobuf) { +schema @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-grpc-service.md b/tests/execution/test-grpc-service.md index 2863a7ede2..4a2a2dadb0 100644 --- a/tests/execution/test-grpc-service.md +++ b/tests/execution/test-grpc-service.md @@ -2,10 +2,47 @@ ###### sdl error +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql -schema @link(id: "news", src: "../graphql/errors/proto/news.proto", type: Protobuf) { +schema @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-grpc.md b/tests/execution/test-grpc.md index 81ee92d151..7943437bb4 100644 --- a/tests/execution/test-grpc.md +++ b/tests/execution/test-grpc.md @@ -2,10 +2,47 @@ ###### check identity +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql -schema @server(port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 1000}) @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { +schema @server(port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 1000}) @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-js-hello-world.md b/tests/execution/test-js-hello-world.md index 7811a38e04..a147237a36 100644 --- a/tests/execution/test-js-hello-world.md +++ b/tests/execution/test-js-hello-world.md @@ -1,9 +1,50 @@ # Js Hello World +#### file:test.js +```js +// TODO: get rid of this function and do it automatically +function str2ab(str) { + var buf = new ArrayBuffer(str.length) // 2 bytes for each char + var bufView = new Uint8Array(buf) + for (var i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i) + } + return buf +} +function onEvent(event) { + if (event.message.response) { + return event + } + if (event.message.request.method === "GET" && event.message.request.url === "http://localhost:3000/hello") { + return { + message: { + response: { + status: 200, + headers: { + "Content-Type": "application/json", + }, + body: str2ab(JSON.stringify("hello world")), + }, + }, + } + } else if (event.message.request.method === "GET" && event.message.request.url === "http://localhost:3000/hi") { + return { + message: { + request: { + url: "http://localhost:3000/bye", + headers: {}, + method: "GET", + }, + }, + } + } +} +``` + #### server: ```graphql -schema @server @link(type: Script, src: "../http/scripts/test.js") { +schema @server @link(type: Script, src: "test.js") { query: Query } diff --git a/tests/execution/test-js-multiple-scripts.md b/tests/execution/test-js-multiple-scripts.md index 37edbd84ec..6b4abd380c 100644 --- a/tests/execution/test-js-multiple-scripts.md +++ b/tests/execution/test-js-multiple-scripts.md @@ -2,10 +2,51 @@ ###### sdl error +#### file:test.js +```js +// TODO: get rid of this function and do it automatically +function str2ab(str) { + var buf = new ArrayBuffer(str.length) // 2 bytes for each char + var bufView = new Uint8Array(buf) + for (var i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i) + } + return buf +} +function onEvent(event) { + if (event.message.response) { + return event + } + if (event.message.request.method === "GET" && event.message.request.url === "http://localhost:3000/hello") { + return { + message: { + response: { + status: 200, + headers: { + "Content-Type": "application/json", + }, + body: str2ab(JSON.stringify("hello world")), + }, + }, + } + } else if (event.message.request.method === "GET" && event.message.request.url === "http://localhost:3000/hi") { + return { + message: { + request: { + url: "http://localhost:3000/bye", + headers: {}, + method: "GET", + }, + }, + } + } +} +``` + #### server: ```graphql -schema @server @link(type: Script, src: "../http/scripts/test.js") @link(type: Script, src: "../http/scripts/test.js") { +schema @server @link(type: Script, src: "test.js") @link(type: Script, src: "test.js") { query: Query } diff --git a/tests/execution/test-js-request-reponse.md b/tests/execution/test-js-request-reponse.md index 98271a1ae9..772dac51c9 100644 --- a/tests/execution/test-js-request-reponse.md +++ b/tests/execution/test-js-request-reponse.md @@ -1,9 +1,50 @@ # Js Request Response Hello World +#### file:test.js +```js +// TODO: get rid of this function and do it automatically +function str2ab(str) { + var buf = new ArrayBuffer(str.length) // 2 bytes for each char + var bufView = new Uint8Array(buf) + for (var i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i) + } + return buf +} +function onEvent(event) { + if (event.message.response) { + return event + } + if (event.message.request.method === "GET" && event.message.request.url === "http://localhost:3000/hello") { + return { + message: { + response: { + status: 200, + headers: { + "Content-Type": "application/json", + }, + body: str2ab(JSON.stringify("hello world")), + }, + }, + } + } else if (event.message.request.method === "GET" && event.message.request.url === "http://localhost:3000/hi") { + return { + message: { + request: { + url: "http://localhost:3000/bye", + headers: {}, + method: "GET", + }, + }, + } + } +} +``` + #### server: ```graphql -schema @server @link(type: Script, src: "../http/scripts/test.js") { +schema @server @link(type: Script, src: "test.js") { query: Query } diff --git a/tests/execution/test-missing-argument-on-all-resolvers.md b/tests/execution/test-missing-argument-on-all-resolvers.md index 441dc588b4..ce666d907b 100644 --- a/tests/execution/test-missing-argument-on-all-resolvers.md +++ b/tests/execution/test-missing-argument-on-all-resolvers.md @@ -2,12 +2,49 @@ ###### sdl error +#### file:news.proto +```protobuf +syntax = "proto3"; + +import "google/protobuf/empty.proto"; + +package news; + +message News { + int32 id = 1; + string title = 2; + string body = 3; + string postImage = 4; +} + +service NewsService { + rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} + rpc GetNews (NewsId) returns (News) {} + rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} + rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} + rpc EditNews (News) returns (News) {} + rpc AddNews (News) returns (News) {} +} + +message NewsId { + int32 id = 1; +} + +message MultipleNewsId { + repeated NewsId ids = 1; +} + +message NewsList { + repeated News news = 1; +} +``` + #### server: ```graphql schema @upstream(baseURL: "http://jsonplaceholder.typicode.com") - @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { + @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-nested-link.md b/tests/execution/test-nested-link.md index 66cc5fff90..dd1fb1b7d5 100644 --- a/tests/execution/test-nested-link.md +++ b/tests/execution/test-nested-link.md @@ -2,10 +2,48 @@ ###### check identity +#### file:link-enum.graphql +```graphql +schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") { + query: Query +} + +enum Foo { + BAR + BAZ +} + +type Query { + foo: Foo @http(path: "/foo") +} +``` + +#### file:graphql-with-link.graphql +```graphql +schema @server @upstream(baseURL: "http://localhost:8000/graphql") @link(src: "link-enum.graphql", type: Config) { + query: Query +} + +type Post { + id: Int! + userId: Int! + user: User @graphQL(args: [{key: "id", value: "{{value.userId}}"}], name: "user") +} + +type Query { + post(id: Int!): Post @http(baseURL: "http://jsonplaceholder.typicode.com", path: "/posts/{{args.id}}") +} + +type User { + id: Int + name: String +} +``` + #### server: ```graphql -schema @server @upstream @link(src: "../fixtures/graphql-with-link.graphql", type: Config) { +schema @server @upstream @link(src: "graphql-with-link.graphql", type: Config) { query: Query } ``` diff --git a/tests/snapshots/execution_spec__grpc-batch.md_merged.snap b/tests/snapshots/execution_spec__grpc-batch.md_merged.snap index d272360066..ff66f16446 100644 --- a/tests/snapshots/execution_spec__grpc-batch.md_merged.snap +++ b/tests/snapshots/execution_spec__grpc-batch.md_merged.snap @@ -2,7 +2,7 @@ source: tests/execution_spec.rs expression: merged --- -schema @server(graphiql: true, port: 8000) @upstream(batch: {delay: 10, headers: [], maxSize: 100}, httpCache: true) @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { +schema @server(graphiql: true, port: 8000) @upstream(batch: {delay: 10, headers: [], maxSize: 100}, httpCache: true) @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/snapshots/execution_spec__grpc-override-url-from-upstream.md_merged.snap b/tests/snapshots/execution_spec__grpc-override-url-from-upstream.md_merged.snap index 1e5ae99f3a..bae43667cc 100644 --- a/tests/snapshots/execution_spec__grpc-override-url-from-upstream.md_merged.snap +++ b/tests/snapshots/execution_spec__grpc-override-url-from-upstream.md_merged.snap @@ -2,7 +2,7 @@ source: tests/execution_spec.rs expression: merged --- -schema @server(graphiql: true, port: 8000) @upstream(baseURL: "http://not-a-valid-grpc-url.com", batch: {delay: 10, headers: [], maxSize: 100}, httpCache: true) @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { +schema @server(graphiql: true, port: 8000) @upstream(baseURL: "http://not-a-valid-grpc-url.com", batch: {delay: 10, headers: [], maxSize: 100}, httpCache: true) @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/snapshots/execution_spec__grpc-simple.md_merged.snap b/tests/snapshots/execution_spec__grpc-simple.md_merged.snap index 63b01cbb94..cbe04640ea 100644 --- a/tests/snapshots/execution_spec__grpc-simple.md_merged.snap +++ b/tests/snapshots/execution_spec__grpc-simple.md_merged.snap @@ -2,7 +2,7 @@ source: tests/execution_spec.rs expression: merged --- -schema @server(graphiql: true, port: 8000) @upstream(batch: {delay: 10, headers: [], maxSize: 100}, httpCache: true) @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { +schema @server(graphiql: true, port: 8000) @upstream(batch: {delay: 10, headers: [], maxSize: 100}, httpCache: true) @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/snapshots/execution_spec__grpc-url-from-upstream.md_merged.snap b/tests/snapshots/execution_spec__grpc-url-from-upstream.md_merged.snap index 076cc174c5..aa679b9369 100644 --- a/tests/snapshots/execution_spec__grpc-url-from-upstream.md_merged.snap +++ b/tests/snapshots/execution_spec__grpc-url-from-upstream.md_merged.snap @@ -2,7 +2,7 @@ source: tests/execution_spec.rs expression: merged --- -schema @server(graphiql: true, port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 100}, httpCache: true) @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { +schema @server(graphiql: true, port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 100}, httpCache: true) @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/snapshots/execution_spec__test-add-link-to-empty-config.md_merged.snap b/tests/snapshots/execution_spec__test-add-link-to-empty-config.md_merged.snap index bba7c6f380..16ae75e5af 100644 --- a/tests/snapshots/execution_spec__test-add-link-to-empty-config.md_merged.snap +++ b/tests/snapshots/execution_spec__test-add-link-to-empty-config.md_merged.snap @@ -2,6 +2,6 @@ source: tests/execution_spec.rs expression: merged --- -schema @server @upstream @link(src: "../fixtures/link-const.graphql", type: Config) @link(src: "../fixtures/link-enum.graphql", type: Config) { +schema @server @upstream @link(src: "link-const.graphql", type: Config) @link(src: "link-enum.graphql", type: Config) { query: Query } diff --git a/tests/snapshots/execution_spec__test-expr-success.md_merged.snap b/tests/snapshots/execution_spec__test-expr-success.md_merged.snap index eeffd7646a..3af57c3604 100644 --- a/tests/snapshots/execution_spec__test-expr-success.md_merged.snap +++ b/tests/snapshots/execution_spec__test-expr-success.md_merged.snap @@ -2,7 +2,7 @@ source: tests/execution_spec.rs expression: merged --- -schema @server(port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 1000}) @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { +schema @server(port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 1000}) @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/snapshots/execution_spec__test-grpc.md_merged.snap b/tests/snapshots/execution_spec__test-grpc.md_merged.snap index 535426b2bd..312572fcb2 100644 --- a/tests/snapshots/execution_spec__test-grpc.md_merged.snap +++ b/tests/snapshots/execution_spec__test-grpc.md_merged.snap @@ -2,7 +2,7 @@ source: tests/execution_spec.rs expression: merged --- -schema @server(port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 1000}) @link(id: "news", src: "../../src/grpc/tests/news.proto", type: Protobuf) { +schema @server(port: 8000) @upstream(baseURL: "http://localhost:50051", batch: {delay: 10, headers: [], maxSize: 1000}) @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/snapshots/execution_spec__test-js-hello-world.md_merged.snap b/tests/snapshots/execution_spec__test-js-hello-world.md_merged.snap index 7b489a033e..3102d752cb 100644 --- a/tests/snapshots/execution_spec__test-js-hello-world.md_merged.snap +++ b/tests/snapshots/execution_spec__test-js-hello-world.md_merged.snap @@ -2,7 +2,7 @@ source: tests/execution_spec.rs expression: merged --- -schema @server @upstream @link(src: "../http/scripts/test.js", type: Script) { +schema @server @upstream @link(src: "test.js", type: Script) { query: Query } diff --git a/tests/snapshots/execution_spec__test-js-request-reponse.md_merged.snap b/tests/snapshots/execution_spec__test-js-request-reponse.md_merged.snap index 7b489a033e..3102d752cb 100644 --- a/tests/snapshots/execution_spec__test-js-request-reponse.md_merged.snap +++ b/tests/snapshots/execution_spec__test-js-request-reponse.md_merged.snap @@ -2,7 +2,7 @@ source: tests/execution_spec.rs expression: merged --- -schema @server @upstream @link(src: "../http/scripts/test.js", type: Script) { +schema @server @upstream @link(src: "test.js", type: Script) { query: Query } diff --git a/tests/snapshots/execution_spec__test-nested-link.md_merged.snap b/tests/snapshots/execution_spec__test-nested-link.md_merged.snap index aedb678df1..b7fd3efe85 100644 --- a/tests/snapshots/execution_spec__test-nested-link.md_merged.snap +++ b/tests/snapshots/execution_spec__test-nested-link.md_merged.snap @@ -2,6 +2,6 @@ source: tests/execution_spec.rs expression: merged --- -schema @server @upstream @link(src: "../fixtures/graphql-with-link.graphql", type: Config) { +schema @server @upstream @link(src: "graphql-with-link.graphql", type: Config) { query: Query } From 798d25bc49fc835840597b43ec598784a1124728 Mon Sep 17 00:00:00 2001 From: mogery Date: Wed, 7 Feb 2024 18:11:46 +0000 Subject: [PATCH 3/5] chore: remove unused fixtures --- tests/data/posts.json | 38 ------------- tests/data/user-posts.json | 44 --------------- tests/data/users.json | 71 ------------------------ tests/fixtures/graphql-with-link.graphql | 18 ------ tests/fixtures/link-const.graphql | 7 --- tests/fixtures/link-enum.graphql | 12 ---- tests/graphql/errors/proto/news.proto | 33 ----------- tests/http/scripts/test.js | 37 ------------ 8 files changed, 260 deletions(-) delete mode 100644 tests/data/posts.json delete mode 100644 tests/data/user-posts.json delete mode 100644 tests/data/users.json delete mode 100644 tests/fixtures/graphql-with-link.graphql delete mode 100644 tests/fixtures/link-const.graphql delete mode 100644 tests/fixtures/link-enum.graphql delete mode 100644 tests/graphql/errors/proto/news.proto delete mode 100644 tests/http/scripts/test.js diff --git a/tests/data/posts.json b/tests/data/posts.json deleted file mode 100644 index c5f3b640bb..0000000000 --- a/tests/data/posts.json +++ /dev/null @@ -1,38 +0,0 @@ -[ - { - "userId": 1, - "id": 11, - "title": "User 1 Post 1", - "body": "delectus reiciendis molestiae occaecati non minima eveniet qui voluptatibus\naccusamus in eum beatae sit\nvel qui neque voluptates ut commodi qui incidunt\nut animi commodi" - }, - { - "userId": 1, - "id": 12, - "title": "User 1 Post 2", - "body": "itaque id aut magnam\npraesentium quia et ea odit et ea voluptas et\nsapiente quia nihil amet occaecati quia id voluptatem\nincidunt ea est distinctio odio" - }, - { - "userId": 2, - "id": 13, - "title": "User 2 Post 1", - "body": "delectus reiciendis molestiae occaecati non minima eveniet qui voluptatibus\naccusamus in eum beatae sit\nvel qui neque voluptates ut commodi qui incidunt\nut animi commodi" - }, - { - "userId": 2, - "id": 14, - "title": "User 2 Post 2", - "body": "itaque id aut magnam\npraesentium quia et ea odit et ea voluptas et\nsapiente quia nihil amet occaecati quia id voluptatem\nincidunt ea est distinctio odio" - }, - { - "userId": 3, - "id": 15, - "title": "User 3 Post 1", - "body": "delectus reiciendis molestiae occaecati non minima eveniet qui voluptatibus\naccusamus in eum beatae sit\nvel qui neque voluptates ut commodi qui incidunt\nut animi commodi" - }, - { - "userId": 3, - "id": 16, - "title": "User 3 Post 2", - "body": "itaque id aut magnam\npraesentium quia et ea odit et ea voluptas et\nsapiente quia nihil amet occaecati quia id voluptatem\nincidunt ea est distinctio odio" - } -] diff --git a/tests/data/user-posts.json b/tests/data/user-posts.json deleted file mode 100644 index 7ef890049f..0000000000 --- a/tests/data/user-posts.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "1": [ - { - "userId": 1, - "id": 11, - "title": "User 1 Post 1", - "body": "delectus reiciendis molestiae occaecati non minima eveniet qui voluptatibus\naccusamus in eum beatae sit\nvel qui neque voluptates ut commodi qui incidunt\nut animi commodi" - }, - { - "userId": 1, - "id": 12, - "title": "User 1 Post 2", - "body": "itaque id aut magnam\npraesentium quia et ea odit et ea voluptas et\nsapiente quia nihil amet occaecati quia id voluptatem\nincidunt ea est distinctio odio" - } - ], - "2": [ - { - "userId": 2, - "id": 13, - "title": "User 2 Post 1", - "body": "delectus reiciendis molestiae occaecati non minima eveniet qui voluptatibus\naccusamus in eum beatae sit\nvel qui neque voluptates ut commodi qui incidunt\nut animi commodi" - }, - { - "userId": 2, - "id": 14, - "title": "User 2 Post 2", - "body": "itaque id aut magnam\npraesentium quia et ea odit et ea voluptas et\nsapiente quia nihil amet occaecati quia id voluptatem\nincidunt ea est distinctio odio" - } - ], - "3": [ - { - "userId": 3, - "id": 15, - "title": "User 3 Post 1", - "body": "delectus reiciendis molestiae occaecati non minima eveniet qui voluptatibus\naccusamus in eum beatae sit\nvel qui neque voluptates ut commodi qui incidunt\nut animi commodi" - }, - { - "userId": 3, - "id": 16, - "title": "User 3 Post 2", - "body": "itaque id aut magnam\npraesentium quia et ea odit et ea voluptas et\nsapiente quia nihil amet occaecati quia id voluptatem\nincidunt ea est distinctio odio" - } - ] -} diff --git a/tests/data/users.json b/tests/data/users.json deleted file mode 100644 index d5ad59bf34..0000000000 --- a/tests/data/users.json +++ /dev/null @@ -1,71 +0,0 @@ -[ - { - "id": 1, - "name": "Leanne Graham", - "username": "Bret", - "email": "Sincere@april.biz", - "address": { - "street": "Kulas Light", - "suite": "Apt. 556", - "city": "Gwenborough", - "zipcode": "92998-3874", - "geo": { - "lat": "-37.3159", - "lng": "81.1496" - } - }, - "phone": "1-770-736-8031 x56442", - "website": "hildegard.org", - "company": { - "name": "Romaguera-Crona", - "catchPhrase": "Multi-layered client-server neural-net", - "bs": "harness real-time e-markets" - } - }, - { - "id": 2, - "name": "Ervin Howell", - "username": "Antonette", - "email": "Shanna@melissa.tv", - "address": { - "street": "Victor Plains", - "suite": "Suite 879", - "city": "Wisokyburgh", - "zipcode": "90566-7771", - "geo": { - "lat": "-43.9509", - "lng": "-34.4618" - } - }, - "phone": "010-692-6593 x09125", - "website": "anastasia.net", - "company": { - "name": "Deckow-Crist", - "catchPhrase": "Proactive didactic contingency", - "bs": "synergize scalable supply-chains" - } - }, - { - "id": 3, - "name": "Clementine Bauch", - "username": "Samantha", - "email": "Nathan@yesenia.net", - "address": { - "street": "Douglas Extension", - "suite": "Suite 847", - "city": "McKenziehaven", - "zipcode": "59590-4157", - "geo": { - "lat": "-68.6102", - "lng": "-47.0653" - } - }, - "phone": "1-463-123-4447", - "website": "ramiro.info", - "company": { - "name": "Romaguera-Jacobson", - "catchPhrase": "Face to face bifurcated interface", - "bs": "e-enable strategic applications" - } - } -] diff --git a/tests/fixtures/graphql-with-link.graphql b/tests/fixtures/graphql-with-link.graphql deleted file mode 100644 index 31fba654ba..0000000000 --- a/tests/fixtures/graphql-with-link.graphql +++ /dev/null @@ -1,18 +0,0 @@ -schema @server @upstream(baseURL: "http://localhost:8000/graphql") @link(src: "./link-enum.graphql", type: Config) { - query: Query -} - -type Post { - id: Int! - userId: Int! - user: User @graphQL(args: [{key: "id", value: "{{value.userId}}"}], name: "user") -} - -type Query { - post(id: Int!): Post @http(baseURL: "http://jsonplaceholder.typicode.com", path: "/posts/{{args.id}}") -} - -type User { - id: Int - name: String -} diff --git a/tests/fixtures/link-const.graphql b/tests/fixtures/link-const.graphql deleted file mode 100644 index 3859dc933c..0000000000 --- a/tests/fixtures/link-const.graphql +++ /dev/null @@ -1,7 +0,0 @@ -schema @server @upstream { - query: Query -} - -type Query { - hello: String @const(data: "Hello from server") -} diff --git a/tests/fixtures/link-enum.graphql b/tests/fixtures/link-enum.graphql deleted file mode 100644 index 66d05c91ff..0000000000 --- a/tests/fixtures/link-enum.graphql +++ /dev/null @@ -1,12 +0,0 @@ -schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") { - query: Query -} - -enum Foo { - BAR - BAZ -} - -type Query { - foo: Foo @http(path: "/foo") -} diff --git a/tests/graphql/errors/proto/news.proto b/tests/graphql/errors/proto/news.proto deleted file mode 100644 index 600547a105..0000000000 --- a/tests/graphql/errors/proto/news.proto +++ /dev/null @@ -1,33 +0,0 @@ -syntax = "proto3"; - -import "google/protobuf/empty.proto"; - -package news; - -message News { - int32 id = 1; - string title = 2; - string body = 3; - string postImage = 4; -} - -service NewsService { - rpc GetAllNews (google.protobuf.Empty) returns (NewsList) {} - rpc GetNews (NewsId) returns (News) {} - rpc GetMultipleNews (MultipleNewsId) returns (NewsList) {} - rpc DeleteNews (NewsId) returns (google.protobuf.Empty) {} - rpc EditNews (News) returns (News) {} - rpc AddNews (News) returns (News) {} -} - -message NewsId { - int32 id = 1; -} - -message MultipleNewsId { - repeated NewsId ids = 1; -} - -message NewsList { - repeated News news = 1; -} diff --git a/tests/http/scripts/test.js b/tests/http/scripts/test.js deleted file mode 100644 index 9cf8b0a595..0000000000 --- a/tests/http/scripts/test.js +++ /dev/null @@ -1,37 +0,0 @@ -// TODO: get rid of this function and do it automatically -function str2ab(str) { - var buf = new ArrayBuffer(str.length) // 2 bytes for each char - var bufView = new Uint8Array(buf) - for (var i = 0, strLen = str.length; i < strLen; i++) { - bufView[i] = str.charCodeAt(i) - } - return buf -} -function onEvent(event) { - if (event.message.response) { - return event - } - if (event.message.request.method === "GET" && event.message.request.url === "http://localhost:3000/hello") { - return { - message: { - response: { - status: 200, - headers: { - "Content-Type": "application/json", - }, - body: str2ab(JSON.stringify("hello world")), - }, - }, - } - } else if (event.message.request.method === "GET" && event.message.request.url === "http://localhost:3000/hi") { - return { - message: { - request: { - url: "http://localhost:3000/bye", - headers: {}, - method: "GET", - }, - }, - } - } -} From c51ca398895a0c7cc2a848a38b428e5afe936ebf Mon Sep 17 00:00:00 2001 From: mogery Date: Wed, 7 Feb 2024 18:16:11 +0000 Subject: [PATCH 4/5] chore(execution_spec): update doc --- tests/execution_spec.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/execution_spec.md b/tests/execution_spec.md index 40f58da9a5..9deb620a03 100644 --- a/tests/execution_spec.md +++ b/tests/execution_spec.md @@ -11,6 +11,7 @@ A Markdown-based snapshot testing framework for Tailcall. - [`mock`](#mock) - [`env`](#env) - [`assert`](#assert) + - [`file`](#file) - [Instruction](#instruction) - [Test process](#test-process) - [Snapshots](#snapshots) @@ -145,6 +146,31 @@ Example: ``` ```` +#### `#### file:` + +A `file` block creates a file in the spec's virtual file system. The [`server` block](#server) will only be able to access files created in this way: the true filesystem is not available to it. + +Every `file` block has the filename declared in the header. The language of the code block is optional and does not matter. + +Example: +````md +#### file:enum.graphql +```graphql +schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") { + query: Query +} + +enum Foo { + BAR + BAZ +} + +type Query { + foo: Foo @http(path: "/foo") +} +``` +```` + ### Instruction A level 6 heading (`######`), with the text being one of the following: @@ -181,6 +207,7 @@ There must be exactly zero or one instruction in a test. 1. If the snapshot doesn't match the generated schema, the runner generates a new snapshot and throws an error. 1. If the test has an [`assert` block](#assert), the runner performs `assert` checks: 1. If there is a [`mock` block](#mock), the runner sets up the mock HTTP client based on it. + 1. If there is at least one [`file` block](#file), the runner sets up the mock filesystem based on them. 1. If there is an [`env` block](#env), the runner uses it for the app context. 1. Creates an app context based on the [`server` block](#server). 1. For each assertion in the block (0-based index `i`), the runner does the following: From dd1c07ec7208a837ac867e13300d1e27da346f4c Mon Sep 17 00:00:00 2001 From: mogery Date: Wed, 7 Feb 2024 18:21:35 +0000 Subject: [PATCH 5/5] chore: lint --- tests/execution/grpc-batch.md | 1 + .../grpc-override-url-from-upstream.md | 1 + tests/execution/grpc-simple.md | 1 + tests/execution/grpc-url-from-upstream.md | 1 + .../test-add-link-to-empty-config.md | 2 ++ tests/execution/test-duplicated-link.md | 1 + tests/execution/test-expr-success.md | 1 + tests/execution/test-grpc-group-by.md | 1 + tests/execution/test-grpc-missing-fields.md | 1 + tests/execution/test-grpc-nested-data.md | 1 + tests/execution/test-grpc-nested-optional.md | 1 + tests/execution/test-grpc-optional.md | 1 + tests/execution/test-grpc-service-method.md | 1 + tests/execution/test-grpc-service.md | 1 + tests/execution/test-grpc.md | 1 + tests/execution/test-js-hello-world.md | 1 + tests/execution/test-js-multiple-scripts.md | 1 + tests/execution/test-js-request-reponse.md | 1 + .../test-missing-argument-on-all-resolvers.md | 5 ++-- tests/execution/test-nested-link.md | 2 ++ tests/execution_spec.md | 2 ++ tests/execution_spec.rs | 24 ++++++++++++------- 22 files changed, 40 insertions(+), 12 deletions(-) diff --git a/tests/execution/grpc-batch.md b/tests/execution/grpc-batch.md index e5b15750e5..71e9d99d5d 100644 --- a/tests/execution/grpc-batch.md +++ b/tests/execution/grpc-batch.md @@ -1,6 +1,7 @@ # Grpc datasource with batching #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/grpc-override-url-from-upstream.md b/tests/execution/grpc-override-url-from-upstream.md index ea4a165843..c96c477e5c 100644 --- a/tests/execution/grpc-override-url-from-upstream.md +++ b/tests/execution/grpc-override-url-from-upstream.md @@ -1,6 +1,7 @@ # Grpc datasource #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/grpc-simple.md b/tests/execution/grpc-simple.md index 8a28d3a5e3..175db98ed9 100644 --- a/tests/execution/grpc-simple.md +++ b/tests/execution/grpc-simple.md @@ -1,6 +1,7 @@ # Grpc datasource #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/grpc-url-from-upstream.md b/tests/execution/grpc-url-from-upstream.md index c61e130223..5bd191b090 100644 --- a/tests/execution/grpc-url-from-upstream.md +++ b/tests/execution/grpc-url-from-upstream.md @@ -1,6 +1,7 @@ # Grpc datasource #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-add-link-to-empty-config.md b/tests/execution/test-add-link-to-empty-config.md index f46b66ba3d..ccf8657531 100644 --- a/tests/execution/test-add-link-to-empty-config.md +++ b/tests/execution/test-add-link-to-empty-config.md @@ -3,6 +3,7 @@ ###### check identity #### file:link-const.graphql + ```graphql schema @server @upstream { query: Query @@ -14,6 +15,7 @@ type Query { ``` #### file:link-enum.graphql + ```graphql schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") { query: Query diff --git a/tests/execution/test-duplicated-link.md b/tests/execution/test-duplicated-link.md index 074ec9c333..e2affc02d1 100644 --- a/tests/execution/test-duplicated-link.md +++ b/tests/execution/test-duplicated-link.md @@ -3,6 +3,7 @@ ###### sdl error #### file:jsonplaceholder.graphql + ```graphql schema @server(port: 8000, graphiql: true, hostname: "0.0.0.0") diff --git a/tests/execution/test-expr-success.md b/tests/execution/test-expr-success.md index 72d5674301..24257495c2 100644 --- a/tests/execution/test-expr-success.md +++ b/tests/execution/test-expr-success.md @@ -3,6 +3,7 @@ ###### check identity #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-grpc-group-by.md b/tests/execution/test-grpc-group-by.md index ddd5dcecd6..9b1432b0a0 100644 --- a/tests/execution/test-grpc-group-by.md +++ b/tests/execution/test-grpc-group-by.md @@ -3,6 +3,7 @@ ###### sdl error #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-grpc-missing-fields.md b/tests/execution/test-grpc-missing-fields.md index fe5632a9b3..006e82ea1a 100644 --- a/tests/execution/test-grpc-missing-fields.md +++ b/tests/execution/test-grpc-missing-fields.md @@ -3,6 +3,7 @@ ###### sdl error #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-grpc-nested-data.md b/tests/execution/test-grpc-nested-data.md index 3eeafaca7c..f88fd9c711 100644 --- a/tests/execution/test-grpc-nested-data.md +++ b/tests/execution/test-grpc-nested-data.md @@ -3,6 +3,7 @@ ###### sdl error #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-grpc-nested-optional.md b/tests/execution/test-grpc-nested-optional.md index bfa0bb7f5b..0ea7d1866b 100644 --- a/tests/execution/test-grpc-nested-optional.md +++ b/tests/execution/test-grpc-nested-optional.md @@ -3,6 +3,7 @@ ###### sdl error #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-grpc-optional.md b/tests/execution/test-grpc-optional.md index f910b9c5cc..eaec6bdc81 100644 --- a/tests/execution/test-grpc-optional.md +++ b/tests/execution/test-grpc-optional.md @@ -3,6 +3,7 @@ ###### sdl error #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-grpc-service-method.md b/tests/execution/test-grpc-service-method.md index 048cdb94dd..0480199f56 100644 --- a/tests/execution/test-grpc-service-method.md +++ b/tests/execution/test-grpc-service-method.md @@ -3,6 +3,7 @@ ###### sdl error #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-grpc-service.md b/tests/execution/test-grpc-service.md index 4a2a2dadb0..7d4b9a2c3b 100644 --- a/tests/execution/test-grpc-service.md +++ b/tests/execution/test-grpc-service.md @@ -3,6 +3,7 @@ ###### sdl error #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-grpc.md b/tests/execution/test-grpc.md index 7943437bb4..125c3c4718 100644 --- a/tests/execution/test-grpc.md +++ b/tests/execution/test-grpc.md @@ -3,6 +3,7 @@ ###### check identity #### file:news.proto + ```protobuf syntax = "proto3"; diff --git a/tests/execution/test-js-hello-world.md b/tests/execution/test-js-hello-world.md index a147237a36..2fba984f03 100644 --- a/tests/execution/test-js-hello-world.md +++ b/tests/execution/test-js-hello-world.md @@ -1,6 +1,7 @@ # Js Hello World #### file:test.js + ```js // TODO: get rid of this function and do it automatically function str2ab(str) { diff --git a/tests/execution/test-js-multiple-scripts.md b/tests/execution/test-js-multiple-scripts.md index 6b4abd380c..7525de066d 100644 --- a/tests/execution/test-js-multiple-scripts.md +++ b/tests/execution/test-js-multiple-scripts.md @@ -3,6 +3,7 @@ ###### sdl error #### file:test.js + ```js // TODO: get rid of this function and do it automatically function str2ab(str) { diff --git a/tests/execution/test-js-request-reponse.md b/tests/execution/test-js-request-reponse.md index 772dac51c9..e7ce9e7942 100644 --- a/tests/execution/test-js-request-reponse.md +++ b/tests/execution/test-js-request-reponse.md @@ -1,6 +1,7 @@ # Js Request Response Hello World #### file:test.js + ```js // TODO: get rid of this function and do it automatically function str2ab(str) { diff --git a/tests/execution/test-missing-argument-on-all-resolvers.md b/tests/execution/test-missing-argument-on-all-resolvers.md index ce666d907b..919fbc1c08 100644 --- a/tests/execution/test-missing-argument-on-all-resolvers.md +++ b/tests/execution/test-missing-argument-on-all-resolvers.md @@ -3,6 +3,7 @@ ###### sdl error #### file:news.proto + ```protobuf syntax = "proto3"; @@ -42,9 +43,7 @@ message NewsList { #### server: ```graphql -schema - @upstream(baseURL: "http://jsonplaceholder.typicode.com") - @link(id: "news", src: "news.proto", type: Protobuf) { +schema @upstream(baseURL: "http://jsonplaceholder.typicode.com") @link(id: "news", src: "news.proto", type: Protobuf) { query: Query } diff --git a/tests/execution/test-nested-link.md b/tests/execution/test-nested-link.md index dd1fb1b7d5..f672b8e33a 100644 --- a/tests/execution/test-nested-link.md +++ b/tests/execution/test-nested-link.md @@ -3,6 +3,7 @@ ###### check identity #### file:link-enum.graphql + ```graphql schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") { query: Query @@ -19,6 +20,7 @@ type Query { ``` #### file:graphql-with-link.graphql + ```graphql schema @server @upstream(baseURL: "http://localhost:8000/graphql") @link(src: "link-enum.graphql", type: Config) { query: Query diff --git a/tests/execution_spec.md b/tests/execution_spec.md index 9deb620a03..83eff4aa1f 100644 --- a/tests/execution_spec.md +++ b/tests/execution_spec.md @@ -153,8 +153,10 @@ A `file` block creates a file in the spec's virtual file system. The [`server` b Every `file` block has the filename declared in the header. The language of the code block is optional and does not matter. Example: + ````md #### file:enum.graphql + ```graphql schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") { query: Query diff --git a/tests/execution_spec.rs b/tests/execution_spec.rs index 0febaaf56c..6eaefe761c 100644 --- a/tests/execution_spec.rs +++ b/tests/execution_spec.rs @@ -314,19 +314,23 @@ impl ExecutionSpec { if let Some(Node::Text(text)) = heading.children.first() { let name = text.value.as_str(); - if name.starts_with("file:") { - let name = &name[5..]; + if let Some(name) = name.strip_prefix("file:") { if files.insert(name.to_string(), content).is_some() { - return Err(anyhow!("Double declaration of file {:?} in {:#?}", name, path)); + return Err(anyhow!( + "Double declaration of file {:?} in {:#?}", + name, + path + )); } } else { let lang = match lang { Some(x) => Ok(x), - None => { - Err(anyhow!("Unexpected languageless code block in {:?}", path)) - } + None => Err(anyhow!( + "Unexpected languageless code block in {:?}", + path + )), }?; - + let source = Source::from_str(&lang)?; match name { @@ -575,7 +579,7 @@ impl HttpIO for MockHttpClient { } struct MockFileSystem { - spec: ExecutionSpec + spec: ExecutionSpec, } impl MockFileSystem { @@ -592,7 +596,9 @@ impl FileIO for MockFileSystem { async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result { let base = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/execution/"); - let path = path.strip_prefix(&base.to_string_lossy().to_string()).unwrap_or(path); + let path = path + .strip_prefix(&base.to_string_lossy().to_string()) + .unwrap_or(path); match self.spec.files.get(path) { Some(x) => Ok(x.to_owned()),