Skip to content

Commit

Permalink
Add additional constructors to the Constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
mfvanek committed Dec 8, 2024
1 parent 1db5995 commit ff072fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void onDatabaseWithThem(final String schemaName) {
Assertions.assertThat(notValidConstraints)
.hasSize(2)
.containsExactly(
Constraint.ofType(ctx.enrichWithSchema("accounts"), "c_accounts_chk_client_id_not_validated_yet", ConstraintType.CHECK),
Constraint.ofType(ctx.enrichWithSchema("accounts"), "c_accounts_fk_client_id_not_validated_yet", ConstraintType.FOREIGN_KEY));
Constraint.ofType(ctx, "accounts", "c_accounts_chk_client_id_not_validated_yet", ConstraintType.CHECK),
Constraint.ofType(ctx, "accounts", "c_accounts_fk_client_id_not_validated_yet", ConstraintType.FOREIGN_KEY));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "accounts"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package io.github.mfvanek.pg.model.constraint;

import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.dbobject.DbObject;
import io.github.mfvanek.pg.model.dbobject.PgObjectType;
import io.github.mfvanek.pg.model.table.TableNameAware;
Expand Down Expand Up @@ -170,4 +171,22 @@ public static Constraint ofType(@Nonnull final String tableName,
@Nonnull final ConstraintType constraintType) {
return new Constraint(tableName, constraintName, constraintType);
}

/**
* Constructs a {@code Constraint} object with given {@code ConstraintType} and context.
*
* @param pgContext the schema context to enrich table name; must be non-null.
* @param tableName table name; should be non-blank.
* @param constraintName constraint name; should be non-blank.
* @param constraintType constraint type; should be non-null.
* @return {@code Constraint}
* @since 0.14.3
*/
@Nonnull
public static Constraint ofType(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
@Nonnull final String constraintName,
@Nonnull final ConstraintType constraintType) {
return ofType(PgContext.enrichWith(tableName, pgContext), constraintName, constraintType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package io.github.mfvanek.pg.model.constraint;

import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.dbobject.PgObjectType;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;
Expand All @@ -21,13 +22,16 @@ class ConstraintTest {

@Test
void testToString() {
final Constraint constraintWithCheck = Constraint.ofType("t", "not_valid_id", ConstraintType.CHECK);
assertThat(constraintWithCheck)
final PgContext ctx = PgContext.of("tst");
assertThat(Constraint.ofType("t", "not_valid_id", ConstraintType.CHECK))
.hasToString("Constraint{tableName='t', constraintName='not_valid_id', constraintType=CHECK}");
assertThat(Constraint.ofType(ctx, "t", "not_valid_id", ConstraintType.CHECK))
.hasToString("Constraint{tableName='tst.t', constraintName='not_valid_id', constraintType=CHECK}");

final Constraint constraintWithForeignKey = Constraint.ofType("t", "not_valid_id", ConstraintType.FOREIGN_KEY);
assertThat(constraintWithForeignKey)
assertThat(Constraint.ofType("t", "not_valid_id", ConstraintType.FOREIGN_KEY))
.hasToString("Constraint{tableName='t', constraintName='not_valid_id', constraintType=FOREIGN_KEY}");
assertThat(Constraint.ofType(ctx, "t1", "not_valid_id", ConstraintType.FOREIGN_KEY))
.hasToString("Constraint{tableName='tst.t1', constraintName='not_valid_id', constraintType=FOREIGN_KEY}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ void onDatabaseWithThem(final String schemaName) {
Assertions.assertThat(notValidConstraints)
.hasSize(2)
.containsExactly(
Constraint.ofType(ctx.enrichWithSchema("accounts"), "c_accounts_chk_client_id_not_validated_yet", ConstraintType.CHECK),
Constraint.ofType(ctx.enrichWithSchema("accounts"), "c_accounts_fk_client_id_not_validated_yet", ConstraintType.FOREIGN_KEY));
Constraint.ofType(ctx, "accounts", "c_accounts_chk_client_id_not_validated_yet", ConstraintType.CHECK),
Constraint.ofType(ctx, "accounts", "c_accounts_fk_client_id_not_validated_yet", ConstraintType.FOREIGN_KEY));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "accounts"))
Expand Down

0 comments on commit ff072fb

Please sign in to comment.