Skip to content

[Cleanup] Remove unused to_postgres_type function from codegen - #224

Merged
JasoonS merged 2 commits into
mainfrom
remove-unused-converter
Sep 25, 2024
Merged

[Cleanup] Remove unused to_postgres_type function from codegen#224
JasoonS merged 2 commits into
mainfrom
remove-unused-converter

Conversation

@JasoonS

@JasoonS JasoonS commented Sep 24, 2024

Copy link
Copy Markdown
Contributor

No description provided.

@JasoonS
JasoonS requested a review from JonoPrest September 24, 2024 06:43
name => GqlScalar::Custom(name.to_string()),
}
}
fn to_postgres_type(&self, schema: &Schema) -> anyhow::Result<String> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is completely unused, so I removed all references to it and everything is still working correctly.

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);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were generated by ChatGPT but they check out and work pretty well 😊

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");
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to update these old tests to work on the relevant code.

pub field_name: CapitalizedOptions,
pub res_type: RescriptTypeIdent,
pub res_schema_code: String,
pub type_pg: String,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not being used in the Handlebars templates.

@DZakh DZakh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🚀

@JasoonS
JasoonS enabled auto-merge (squash) September 25, 2024 06:01
@JasoonS
JasoonS merged commit 6ca6c16 into main Sep 25, 2024
@JasoonS
JasoonS deleted the remove-unused-converter branch September 25, 2024 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants