mcfly
mcfly

Reputation: 834

Generated query from memory database is not accurate

I have an entity in containing :

@Entity
@Table(name = "pictures")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
public class PictureEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", insertable = false, updatable = false, nullable = false)
private UUID id;

@Column
private String path;

@Column(name = "thumb_path")
private String thumbPath;

@Column
@Enumerated(EnumType.STRING)
private Status status;

@Column(name = "creation_utc")
@Temporal(TemporalType.TIMESTAMP)
private Date creationTimeUtc;

@Column(name = "creation_local")
@Temporal(TemporalType.TIMESTAMP)
private Date creationTimeLocal;

@ManyToOne
@JoinColumn(name = "project_id", updatable = true, insertable = true)
private ProjectEntity project;

@ManyToOne
@JoinColumn(name = "user_id", updatable = true, insertable = true)
private UserEntity user;

@OneToOne(mappedBy = "picture", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private ProcessedPictureEntity processedPicture;

public enum Status {
    VALIDATED,
    PROCESSED,
    REJECTED,
    WAITING_VALIDATION
}

}

When I call a save with H2 database, it saves the "project_id" field too. But if I use mysql, the generated query isn't the same, project is not saved (which I think is the correct behavior). I want the test with H2 to crash if updatable/insertable on project_id are false.

How can I correct this ?

Upvotes: 0

Views: 35

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81988

If I understand you correctly you have two problems:

  1. H2 and MySQL behave differently causing bugs to slip through your tests.

  2. You want to test if a certain field got updated.

For 1.: I recommend Testcontainers. It allows you to run tests with an actual MySQL database (or any other database that you can get a docker image for). This will make your integration tests way more valuable.

For 2.: Execute whatever code you suspect does the update in question and then check if the field got changed. Make sure the changes get flushed which is a common cause of tests not behaving as on things. For checking for changes I recommend Springs JdbcTemplate for easily executing queries.

Upvotes: 1

Related Questions