GithubHelp home page GithubHelp logo

apollographql / otj-pg-embedded Goto Github PK

View Code? Open in Web Editor NEW

This project forked from opentable/otj-pg-embedded

0.0 2.0 2.0 350 KB

Java embedded PostgreSQL component for testing

License: Apache License 2.0

Shell 2.03% Java 97.97%

otj-pg-embedded's Introduction

Archival

This repo was archived by the Apollo Security team on 2023-05-26

This repo is a fork of opentable/otj-pg-embedded. Please see the upstream repo with issues or for new versions.

OpenTable Embedded PostgreSQL Component

Allows embedding PostgreSQL into Java application code with no external dependencies. Excellent for allowing you to unit test with a "real" Postgres without requiring end users to install and set up a database cluster.

Build Status

Basic Usage

In your JUnit test just add:

@Rule
public SingleInstancePostgresRule pg = EmbeddedPostgresRules.singleInstance();

This simply has JUnit manage an instance of EmbeddedPostgres (start, stop). You can then use this to get a DataSource with: pg.getEmbeddedPostgres().getPostgresDatabase();

Additionally you may use the EmbeddedPostgres class directly by manually starting and stopping the instance; see EmbeddedPostgresTest for an example.

Default username/password is: postgres/postgres and the default database is 'postgres'

Migrators (Flyway or Liquibase)

You can easily integrate Flyway or Liquibase database schema migration:

Flyway
@Rule 
public PreparedDbRule db =
    EmbeddedPostgresRules.preparedDatabase(
        FlywayPreparer.forClasspathLocation("db/my-db-schema"));
Liquibase
@Rule
public PreparedDbRule db = 
    EmbeddedPostgresRules.preparedDatabase(
            LiquibasePreparer.forClasspathLocation("liqui/master.xml"));

This will create an independent database for every test with the given schema loaded from the classpath. Database templates are used so the time cost is relatively small, given the superior isolation truly independent databases gives you.

Postgres version

The JAR file contains bundled version of Postgres. You can pass different Postgres version by implementing PgBinaryResolver.

Example:

class ClasspathBinaryResolver implements PgBinaryResolver {
    public InputStream getPgBinary(String system, String machineHardware) throws IOException {
        ClassPathResource resource = new ClassPathResource(format("postgresql-%s-%s.txz", system, machineHardware));
        return resource.getInputStream();
    }
}

EmbeddedPostgreSQL
            .builder()
            .setPgBinaryResolver(new ClasspathBinaryResolver())
            .start();

Windows

If you experience difficulty running otj-pg-embedded tests on Windows, make sure you've installed the appropriate MFC redistributables.


Copyright (C) 2017 OpenTable, Inc

otj-pg-embedded's People

Contributors

airwallex-dan-bell avatar amr avatar amumurst avatar arteam avatar benhardy avatar cannibalcow avatar cgc avatar corydolphin avatar danielcompton avatar dbunina avatar epaul avatar flozano avatar gokceyucel avatar hgschmie avatar jond3k avatar kevcodez avatar malltshik avatar marekmalevic avatar meredrica avatar mikebell90 avatar motofix avatar msval avatar nedtwigg avatar peakematt avatar pennello avatar poeschl avatar stevenschlansker avatar tiagosimao avatar tomix26 avatar whostolebenfrog avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.