في عام 2018 ، عمل في شركة Akvelon Inc. قابلت شخص واحد. قبل المقابلة ، أعطوني مهمة الاختبار الخاصة به للتحقق: تطبيق ويب صغير مثل دفتر ملاحظات أو قائمة المهام - React \ TypeScript ، C # على الطرف الخلفي و MS SQL Server كمخزن دائم. كان التطبيق من المألوف: مع وفرة من اختبارات الوحدة على السخرية ، معبأة في صورة عامل ميناء - من الواضح أن الشخص حاول. وكان لهذا الحل عيب واحد فقط - لم ينجح. إطلاقا. فشل أثناء محاولة حفظ صف جديد في قاعدة البيانات.

تذكرت هذه الحالة جيدًا ، حيث سلطت الضوء على العديد من المشاكل الشائعة في وقت واحد.
أولها الثقة الزائفة من اختبارات الوحدة. حتى تغطية 100٪ للشفرة مع الاختبارات لا تضمن عدم وجود أخطاء فيها.
– . , . : , production’. , Oracle, H2\HSQLDB, , , production (boolean, group by ).
PostgreSQL 10- 11-. ( ) , .
, : Embedded PostgreSQL Yandex QATools. . :
TestContainers , Docker’ .
otj-pg-embedded. , , HeadHunter DZone. Yandex QATools , otj-pg-embedded Windows MacOS , - . Liquibase Flyway « »:
abstract class DatabaseAwareTestBase {
@RegisterExtension
static final PreparedDbExtension embeddedPostgres =
EmbeddedPostgresExtension.preparedDatabase(
LiquibasePreparer.forClasspathLocation("changelogs/changelog.xml"));
}
GitHub.
. PostgreSQL : , .
, , , , , . , .
. , delete from <table>
. truncate. , . truncate , cascade:
truncate table tableA;
truncate table tableB cascade;
truncate table tableE;
, : , :
truncate table tableA, tableB, tableC, tableD, tableE;
cascade, ; . , .
, , , . , 11- Postgres ( 11- , 12-). otj-pg-embedded Zonky, , :
testImplementation enforcedPlatform('io.zonky.test.postgres:embedded-postgres-binaries-bom:11.6.0')
testImplementation 'io.zonky.test:embedded-postgres:1.2.6'
, 100% , .
@Test
void checkPostgresVersion() {
final String pgVersion = jdbcTemplate.queryForObject("select version();", String.class);
assertThat(pgVersion, startsWith("PostgreSQL 11.6"));
}
, – . Java-, Linux, . , (, ), CI- , .
SQL
: plain SQL . xml, yml , , . , - \ . xml- psql\pgAdmin – . C plain SQL .
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
<changeSet id="orders_table_create_2020-05-09" author="ivan.vakhrushev">
<createTable tableName="orders">
<column name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="shop_id" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="buyer_id" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="status" type="VARCHAR(20)">
<column name="creation_time" type="TIMESTAMP">
<constraints nullable="false"/>
</column>
<column name="update_time" type="TIMESTAMP"/>
</createTable>
</changeSet>
</databaseChangeLog>
create table if not exists orders
(
id bigint not null primary key,
shop_id bigint not null,
buyer_id bigint not null,
status varchar(20),
creation_time timestamp not null,
update_time timestamp
);
. Java 8 Java 11 . Instant\LocalDateTime. PostgreSQL Timestamp , «» . Java . , «» . , - :
Timestamp.valueOf(localDateTime.truncatedTo(ChronoUnit.MICROS));
Timestamp.from(instant.truncatedTo(ChronoUnit.MICROS));
. , - .
.