samedi 28 mars 2020

Using db VIEW in tests

In my spring-boot web app i have data model described follow next sql code (two entities and view)

create table ref_street (
  id bigint not null default nextval('ref_street_seq') primary key,
  name character varying(255)
);

create table op_client (
    id bigint not null default nextval('op_client_seq') primary key,
    fio character varying(255),
    street_id bigint not null
    ...
);

alter table op_client add constraint fk_op_client_ref_street foreign key(street_id) references ref_street(id);

create or replace view v_view_client as
select
    oc.id id,
    oc.fio fio,
    rs.id street_id,
    rs."name" street_name,
from
    op_client oc
left join ref_street rs on oc.street_id = rs.id;

In my application domain model i have OpClient entity with spring-jpa repository looks like this

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "op_client")
@SequenceGenerator(name = "default_gen", sequenceName = "op_client_seq", allocationSize = 1)
public class OpClient extends AbstractEntity<Long> {
  @Column(name = "user_id")
  private Long userId;

  @Column(name = "fio")
  private String fio;

  @Column(name = "street_id")
  private Long streetId;

...

  @CreatedDate
  @Column(name = "create_dt")
  private LocalDate createDate;
}

@Repository
public interface OpClientRepository extends JpaRepository<OpClient, Long>

}

And the ViewCient entity and repository looks like this


@Entity
@Data
@EqualsAndHashCode(callSuper = true)
@Table(name = "v_view_client")
public class ViewClient extends AbstractEntityWithManualId<Long> {

  @Column(name = "fio")
  private String fio;

  @Column(name = "street_id")
  private Long streetId;

  @Column(name = "street_name")
  private String streetName;
}

@Repository
public interface ViewClientRepository extends JpaRepository<ViewClient, Long> {

}

In my test case i am using Postgresql Embedded Engine com.opentable.components.otj-pg-embedded

The question is why if a'm using OpClientRepository for successfuly inserting data into op_client table my viewClientRepository return empty list? My test case code looks like this:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {DomofonContextMemoryDb.class})
@ActiveProfiles("test")
public class ClientServiceTest {

  @Autowired
  private ClientService clientService;

  @Autowired
  private ViewClientRepository viewClientRepository;

  @Test
  public void addClientTest() {
    OpClient client = new OpClient();
    client.setFio("Test Test Test");
    client.setStreetId(1L);
    clientService.addClient(client);

    assertThat(viewClientRepository.findAll()).hasSizeGreaterThan(0);
  }

than throw assert exception.

Aucun commentaire:

Enregistrer un commentaire