Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.SchemaExtractor.checkForeignKeyToAutoIncrementPresent
Source:SchemaExtractor.java
...15 */16 Objects.requireNonNull(schema);17 for (TableDto table : schema.tables) {18 for (ColumnDto column : table.columns) {19 checkForeignKeyToAutoIncrementPresent(schema, table, column);20 checkForeignKeyToAutoIncrementMissing(schema, table, column);21 }22 }23 return true;24 }25 private static void checkForeignKeyToAutoIncrementMissing(DbSchemaDto schema, TableDto table, ColumnDto column) {26 if (column.foreignKeyToAutoIncrement) {27 return;28 }29 Optional<ForeignKeyDto> fk = table.foreignKeys.stream()30 .filter(it -> it.sourceColumns.contains(column.name))31 .findFirst();32 if (!fk.isPresent()) {33 //not a foreign key34 return;35 }36 //TODO proper handling of multi-column PKs/FKs37 Optional<TableDto> targetTable = schema.tables.stream()38 .filter(t -> t.name.equals(fk.get().targetTable))39 .findFirst();40 if (!targetTable.isPresent()) {41 throw new IllegalArgumentException("Foreign key in table " + table.name +42 " pointing to non-existent table " + fk.get().targetTable);43 }44 List<ColumnDto> pks = targetTable.get().columns.stream()45 .filter(c -> c.primaryKey)46 .collect(Collectors.toList());47 if (pks.isEmpty()) {48 throw new IllegalArgumentException("No PK in table " + targetTable.get().name + " that has FKs pointing to it");49 }50 for (ColumnDto pk : pks) {51 if (pk.autoIncrement || pk.foreignKeyToAutoIncrement) {52 throw new IllegalArgumentException("Column " + pk.name + " in table " +53 pk.table + " is auto-increment, although FK pointing to it does not mark it " +54 "as autoincrement in " + column.name + " in " + table.name55 );56 }57 }58 }59 private static void checkForeignKeyToAutoIncrementPresent(DbSchemaDto schema, TableDto table, ColumnDto column) {60 if (!column.foreignKeyToAutoIncrement) {61 return;62 }63 Optional<ForeignKeyDto> fk = table.foreignKeys.stream()64 .filter(it -> it.sourceColumns.contains(column.name))65 .findFirst();66 if (!fk.isPresent()) {67 throw new IllegalArgumentException("No foreign key constraint for marked column " +68 column.name + " in table " + table.name);69 }70 //TODO proper handling of multi-column PKs/FKs71 Optional<TableDto> targetTable = schema.tables.stream()72 .filter(t -> t.name.equals(fk.get().targetTable))73 .findFirst();...
checkForeignKeyToAutoIncrementPresent
Using AI Code Generation
1package org.evomaster.client.java.controller.api.dto.database.schema;2import java.util.Objects;3public class ColumnDto {4 public String name;5 public String type;6 public boolean isPrimaryKey;7 public boolean isForeignKey;8 public boolean isNullable;9 public boolean isAutoIncrement;10 public String foreignKeyTable;11 public String foreignKeyColumn;12 public ColumnDto() {13 }14 public ColumnDto(String name, String type, boolean isPrimaryKey, boolean isForeignKey, boolean isNullable, boolean isAutoIncrement, String foreignKeyTable, String foreignKeyColumn) {15 this.name = name;16 this.type = type;17 this.isPrimaryKey = isPrimaryKey;18 this.isForeignKey = isForeignKey;19 this.isNullable = isNullable;20 this.isAutoIncrement = isAutoIncrement;21 this.foreignKeyTable = foreignKeyTable;22 this.foreignKeyColumn = foreignKeyColumn;23 }24 public String getName() {25 return name;26 }27 public void setName(String name) {28 this.name = name;29 }30 public String getType() {31 return type;32 }33 public void setType(String type) {34 this.type = type;35 }36 public boolean isPrimaryKey() {37 return isPrimaryKey;38 }39 public void setPrimaryKey(boolean primaryKey) {40 isPrimaryKey = primaryKey;41 }42 public boolean isForeignKey() {43 return isForeignKey;44 }45 public void setForeignKey(boolean foreignKey) {46 isForeignKey = foreignKey;47 }48 public boolean isNullable() {49 return isNullable;50 }51 public void setNullable(boolean nullable) {52 isNullable = nullable;53 }54 public boolean isAutoIncrement() {55 return isAutoIncrement;56 }57 public void setAutoIncrement(boolean autoIncrement) {58 isAutoIncrement = autoIncrement;59 }60 public String getForeignKeyTable() {61 return foreignKeyTable;62 }63 public void setForeignKeyTable(String foreignKeyTable) {64 this.foreignKeyTable = foreignKeyTable;65 }66 public String getForeignKeyColumn() {67 return foreignKeyColumn;68 }69 public void setForeignKeyColumn(String foreignKeyColumn) {70 this.foreignKeyColumn = foreignKeyColumn;71 }72 public boolean equals(Object o) {73 if (this == o) return true;74 if (o == null || getClass() != o.getClass()) return false;
checkForeignKeyToAutoIncrementPresent
Using AI Code Generation
1import org.evomaster.client.java.controller.api.dto.database.schema.ForeignKeyDto2import org.evomaster.client.java.controller.api.dto.database.schema.SchemaDto3import org.evomaster.client.java.controller.api.dto.database.schema.TableDto4import org.evomaster.client.java.controller.internal.db.SchemaExtractor5import org.junit.jupiter.api.Assertions6import org.junit.jupiter.api.Test7import org.junit.jupiter.api.extension.ExtendWith8import org.springframework.boot.test.context.SpringBootTest9import org.springframework.test.context.junit.jupiter.SpringExtension10@ExtendWith(SpringExtension::class)11@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)12class FooTest {13 fun test(){14 val schema = SchemaDto()15 val table = TableDto()16 val fk = ForeignKeyDto()17 table.foreignKeys = mutableListOf(fk)18 schema.tables = mutableListOf(table)19 val result = SchemaExtractor.checkForeignKeyToAutoIncrementPresent(schema)20 Assertions.assertTrue(result)21 }22}23CREATE TABLE table1 (24 PRIMARY KEY (id)25);26CREATE TABLE table2 (27 FOREIGN KEY (id) REFERENCES table1(id)28);
checkForeignKeyToAutoIncrementPresent
Using AI Code Generation
1public class FooController {2 private FooRepository repository;3 private FooService service;4 @GetMapping("/")5 public List<FooDto> getAllFoos() {6 return service.getAllFoos();7 }8 @GetMapping("/{id}")9 public FooDto getFooById(@PathVariable("id") Long id) {10 return service.getFooById(id);11 }12 @PostMapping("/")13 public FooDto createFoo(@RequestBody FooDto foo) {14 return service.createFoo(foo);15 }16 @PutMapping("/{id}")17 public FooDto updateFoo(@PathVariable("id") Long id, @RequestBody FooDto foo) {18 return service.updateFoo(id, foo);19 }20 @DeleteMapping("/{id}")21 public void deleteFoo(@PathVariable("id") Long id) {22 service.deleteFoo(id);23 }24}25public class FooDto {26 private Long id;27 private String name;28 public Long getId() {29 return id;30 }31 public void setId(Long id) {32 this.id = id;33 }34 public String getName() {35 return name;36 }37 public void setName(String name) {38 this.name = name;39 }40}41public class FooEntity {42 @GeneratedValue(strategy = GenerationType.IDENTITY)43 private Long id;44 private String name;45 public FooEntity() {46 }47 public FooEntity(Long id, String name) {48 this.id = id;49 this.name = name;50 }51 public Long getId() {52 return id;53 }54 public void setId(Long id) {55 this.id = id;56 }57 public String getName() {58 return name;59 }60 public void setName(String name) {61 this.name = name;62 }63}64public class FooRepository extends JpaRepository<FooEntity, Long> {65}66public class FooService {67 private FooRepository repository;68 public List<FooDto> getAllFoos() {69 return repository.findAll().stream()70 .map(this::convertToDto)71 .collect(Collectors.toList());72 }73 public FooDto getFooById(Long id) {74 return convertToDto(repository.findById(id)75 .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)));76 }
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!