Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DR-2942 - Validate Column data types match on relationship create #1414

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from

Conversation

snf2ye
Copy link
Contributor

@snf2ye snf2ye commented Feb 22, 2023

When creating a relationship between two columns, the data type should match. This PR add validation on relationship creation to verify this constraint is met.
Example error message:
RelationshipDatatypeMismatch -> Column data types in relationship must match: Column person.hasCat has data type boolean and Column car.ownerId has data type integer

if (fromColumn.isPresent() && toColumn.isPresent()) {
TableDataType fromColumnDataType = fromColumn.get().getDatatype();
TableDataType toColumnDataType = toColumn.get().getDatatype();
if (!fromColumnDataType.equals(toColumnDataType)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we may want this to be a little more lenient (e.g. text = string). Might be a bit of a pain to have to put together the list but I think we'll need it


LinkedHashMap errors =
ValidationUtils.validateMatchingColumnDataTypes(fromTerm, toTerm, tables);
assertThat("There should be one error.", errors.size(), equalTo(1));
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a good place to use the hasSize matcher:

assertThat("There should be one error.", errors, hasSize(1));

It would be nice if there was an easy way to assert that the error itself is what we'd expect. The way I think of it, if this test failed we might have a hard time debugging why (though that will be easier if using hasSize matcher, since we'll see errors emitted in output in the event the assertion fails).

Sometimes I'll put the error message in a @VisibleForTesting function and assert that the error matches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same case as below, I don't think "hasSize" works on Maps without converting to a list.

I've added another check that checks for the exact expected error message.


LinkedHashMap errors =
ValidationUtils.validateMatchingColumnDataTypes(fromTerm, toTerm, tables);
assertThat("The data types match so there should not be an error.", errors.size(), equalTo(0));
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a good place to use the empty matcher:

assertThat("The data types match so there should not be an error.", errors, empty());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh nice, I didn't know about that matcher option. However, it looks like it doesn't work with a map without converting to a List. This seems like unnecessary overhead in this case.


@Test
public void testRelationshipValidationDifferentDataType() {
RelationshipTermModel fromTerm = new RelationshipTermModel().column("hasCat").table("person");
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm having a little difficulty following the tests' intentions -- that is, it's not obvious that this relationship has invalid types.

What do you think about defining a few static String variables for the sample table column names that make the types clear, and using them in relationship construction along with table definition? Maybe something like:

private static final String PERSON_BOOLEAN_COLUMN = "hasCat";
private static final String PERSON_INTEGER_COLUMN = "id";
private static final String CAR_INTEGER_COLUMN = "ownerId";

…

    RelationshipTermModel fromTerm = new RelationshipTermModel().column(PERSON_BOOLEAN_COLUMN).table("person");
    RelationshipTermModel toTerm = new RelationshipTermModel().column(CAR_INTEGER_COLUMN).table("car");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that makes this a lot clearer, thanks for the idea!

@snf2ye snf2ye marked this pull request as draft February 24, 2023 15:59
@snf2ye snf2ye force-pushed the sh-dr-2942-column-validation branch from b945f89 to 2f301da Compare March 13, 2023 18:56
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles({"google", "integrationtest"})
@Category(Integration.class)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I'll make this an OnDemand test.

@snf2ye snf2ye force-pushed the sh-dr-2942-column-validation branch from 0db4187 to 2671415 Compare March 16, 2023 12:24
@sonarcloud
Copy link

sonarcloud bot commented Aug 8, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 3 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

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.

4 participants