[Cleanup] Remove unused to_postgres_type function from codegen - #224
Merged
Conversation
JasoonS
commented
Sep 24, 2024
| name => GqlScalar::Custom(name.to_string()), | ||
| } | ||
| } | ||
| fn to_postgres_type(&self, schema: &Schema) -> anyhow::Result<String> { |
Contributor
Author
There was a problem hiding this comment.
This function is completely unused, so I removed all references to it and everything is still working correctly.
JasoonS
commented
Sep 24, 2024
Comment on lines
+1748
to
+1855
| #[test] | ||
| fn test_get_postgres_field_basic() { | ||
| let schema_str = r#" | ||
| type TestEntity { | ||
| id: ID! | ||
| name: String! @index | ||
| } | ||
| "#; | ||
| let gql_doc = setup_document(schema_str).unwrap(); | ||
| let schema = Schema::from_document(gql_doc).unwrap(); | ||
| let entity = schema.entities.get("TestEntity").unwrap(); | ||
| let field = entity.fields.get("name").unwrap(); | ||
| let pg_field = field | ||
| .get_postgres_field(&schema, entity) | ||
| .expect("Failed to get postgres field") | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(pg_field.field_name, "name"); | ||
| assert_eq!(pg_field.field_type, PGPrimitive::Text); | ||
| assert!(pg_field.is_index); | ||
| assert!(!pg_field.is_array); | ||
| assert!(!pg_field.is_nullable); | ||
| assert_eq!(pg_field.linked_entity, None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_postgres_field_with_linked_entity() { | ||
| let schema_str = r#" | ||
| type TestEntity { | ||
| id: ID! | ||
| relatedEntity: RelatedEntity! | ||
| } | ||
|
|
||
| type RelatedEntity { | ||
| id: ID! | ||
| } | ||
| "#; | ||
| let gql_doc = setup_document(schema_str).unwrap(); | ||
| let schema = Schema::from_document(gql_doc).unwrap(); | ||
| let entity = schema.entities.get("TestEntity").unwrap(); | ||
| let field = entity.fields.get("relatedEntity").unwrap(); | ||
| let pg_field = field | ||
| .get_postgres_field(&schema, entity) | ||
| .expect("Failed to get postgres field") | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(pg_field.field_name, "relatedEntity"); | ||
| assert_eq!(pg_field.field_type, PGPrimitive::Text); | ||
| assert!(!pg_field.is_index); | ||
| assert!(!pg_field.is_array); | ||
| assert!(!pg_field.is_nullable); | ||
| assert_eq!(pg_field.linked_entity, Some("RelatedEntity".to_string())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_postgres_field_array_type() { | ||
| let schema_str = r#" | ||
| type TestEntity { | ||
| id: ID! | ||
| tags: [String!]! | ||
| } | ||
| "#; | ||
| let gql_doc = setup_document(schema_str).unwrap(); | ||
| let schema = Schema::from_document(gql_doc).unwrap(); | ||
| let entity = schema.entities.get("TestEntity").unwrap(); | ||
| let field = entity.fields.get("tags").unwrap(); | ||
| let pg_field = field | ||
| .get_postgres_field(&schema, entity) | ||
| .expect("Failed to get postgres field") | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(pg_field.field_name, "tags"); | ||
| assert_eq!(pg_field.field_type, PGPrimitive::Text); | ||
| assert!(!pg_field.is_index); | ||
| assert!(pg_field.is_array); | ||
| assert!(!pg_field.is_nullable); | ||
| assert_eq!(pg_field.linked_entity, None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_postgres_field_enum_type() { | ||
| let schema_str = r#" | ||
| enum Status { | ||
| ACTIVE | ||
| INACTIVE | ||
| } | ||
|
|
||
| type TestEntity { | ||
| id: ID! | ||
| status: Status! | ||
| } | ||
| "#; | ||
| let gql_doc = setup_document(schema_str).unwrap(); | ||
| let schema = Schema::from_document(gql_doc).unwrap(); | ||
| let entity = schema.entities.get("TestEntity").unwrap(); | ||
| let field = entity.fields.get("status").unwrap(); | ||
| let pg_field = field | ||
| .get_postgres_field(&schema, entity) | ||
| .expect("Failed to get postgres field") | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(pg_field.field_name, "status"); | ||
| assert_eq!(pg_field.field_type, PGPrimitive::Enum("Status".to_string())); | ||
| assert!(!pg_field.is_index); | ||
| assert!(!pg_field.is_array); | ||
| assert!(!pg_field.is_nullable); | ||
| assert_eq!(pg_field.linked_entity, None); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
These were generated by ChatGPT but they check out and work pretty well 😊
JasoonS
commented
Sep 24, 2024
Comment on lines
1652
to
1714
| #[test] | ||
| fn gql_enum_type_to_postgres_type() { | ||
| fn gql_enum_type_to_pgprimitive() { | ||
| let name = String::from("TestEnum"); | ||
| let test_enum = GraphQLEnum::new(name.clone(), vec!["TEST_VALUE".to_string()]).unwrap(); | ||
| let field_type = | ||
| get_field_type_helper_with_additional("TestEnum!", vec![test_enum.clone()]); | ||
| let schema = Schema::new(vec![], vec![test_enum]).unwrap(); | ||
| let pg_type = field_type | ||
| .to_postgres_type(&schema) | ||
| .expect("unable to get postgres type"); | ||
| assert_eq!(pg_type, "TestEnum NOT NULL"); | ||
| let pg_primitive = field_type | ||
| .to_user_defined_field_type() | ||
| .to_underlying_postgres_primitive(&schema) | ||
| .expect("unable to get postgres primitive"); | ||
| assert_eq!(pg_primitive, PGPrimitive::Enum("TestEnum".to_string())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn gql_single_not_null_array_to_pg_type() { | ||
| fn gql_single_not_null_array_to_pgprimitive() { | ||
| let gql_type = "[String!]!"; | ||
| let pg_type = gql_type_to_postgres_type_test_helper(gql_type); | ||
| assert_eq!(pg_type, "text[] NOT NULL"); | ||
| let field_type = get_field_type_helper(gql_type); | ||
| let empty_schema = Schema::empty(); | ||
| let pg_primitive = field_type | ||
| .to_user_defined_field_type() | ||
| .to_underlying_postgres_primitive(&empty_schema) | ||
| .expect("unable to get postgres primitive"); | ||
| assert_eq!(pg_primitive, PGPrimitive::Text); | ||
| assert!(field_type.to_user_defined_field_type().is_array()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn gql_multi_not_null_array_to_pg_type() { | ||
| fn gql_multi_not_null_array_to_pgprimitive() { | ||
| let gql_type = "[[Int!]!]!"; | ||
| let pg_type = gql_type_to_postgres_type_test_helper(gql_type); | ||
| assert_eq!(pg_type, "integer[][] NOT NULL"); | ||
| let field_type = get_field_type_helper(gql_type); | ||
| let empty_schema = Schema::empty(); | ||
| let pg_primitive = field_type | ||
| .to_user_defined_field_type() | ||
| .to_underlying_postgres_primitive(&empty_schema) | ||
| .expect("unable to get postgres primitive"); | ||
| assert_eq!(pg_primitive, PGPrimitive::Integer); | ||
| assert!(field_type.to_user_defined_field_type().is_array()); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic] | ||
| fn gql_single_nullable_array_to_pg_type_should_panic() { | ||
| let gql_type = "[Int]!"; //Nested lists need to be not nullable | ||
| gql_type_to_postgres_type_test_helper(gql_type); | ||
| fn gql_single_nullable_array_to_pgprimitive_should_panic() { | ||
| let gql_type = "[Int]!"; // Nested lists need to be not nullable | ||
| let field_type = get_field_type_helper(gql_type); | ||
| let empty_schema = Schema::empty(); | ||
| let _pg_primitive = field_type | ||
| .to_user_defined_field_type() | ||
| .to_underlying_postgres_primitive(&empty_schema) | ||
| .expect("should panic due to validation error"); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic] | ||
| fn gql_multi_nullable_array_to_pg_type_should_panic() { | ||
| let gql_type = "[[Int!]]!"; //Nested lists need to be not nullable | ||
| gql_type_to_postgres_type_test_helper(gql_type); | ||
| fn gql_multi_nullable_array_to_pgprimitive_should_panic() { | ||
| let gql_type = "[[Int!]]!"; // Nested lists need to be not nullable | ||
| let field_type = get_field_type_helper(gql_type); | ||
| let empty_schema = Schema::empty(); | ||
| let _pg_primitive = field_type | ||
| .to_user_defined_field_type() | ||
| .to_underlying_postgres_primitive(&empty_schema) | ||
| .expect("should panic due to validation error"); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
I had to update these old tests to work on the relevant code.
JasoonS
commented
Sep 24, 2024
| pub field_name: CapitalizedOptions, | ||
| pub res_type: RescriptTypeIdent, | ||
| pub res_schema_code: String, | ||
| pub type_pg: String, |
Contributor
Author
There was a problem hiding this comment.
This was not being used in the Handlebars templates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.