Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertions for database structure #12

Open
aartiPl opened this issue Jul 9, 2015 · 4 comments
Open

Add assertions for database structure #12

aartiPl opened this issue Jul 9, 2015 · 4 comments
Assignees

Comments

@aartiPl
Copy link

aartiPl commented Jul 9, 2015

I need assertions for checking database structure.
E.g. I want to check if specific table exists in database.

Proposed assertions:
assertThat(table).exists();
or
assertThat(source).tableExists("name");

Additionally it would be nice to have convinience method isEmpty() for
assertThat(table).hasNumberOfRows(0);

JDBC code to check if table exists ()

public static boolean hasTable(DataSource ds, String name) {
        boolean exists = false;

        try(Connection c = ds.getConnection();
            //table names usually stored in database uppercased
            ResultSet rs = c.getMetaData().getTables(null, null, name.toUpperCase(), null)) {

            if (rs.next()) {
                exists = true; // table exists
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return exists;
    }

Or just do some operation and catch jdbc exception: table not exists. Below assertion partially does the trick, but is not elegant.
assertThat(table).hasNumberOfRows(0);

@regis1512 regis1512 self-assigned this Jul 9, 2015
@regis1512
Copy link
Member

It is a good idea.
But i prefer to plan for a next release.
For the moment, the goal is to make the first release (it will be done in a couple of day).
Thanks

fiery-phoenix added a commit to fiery-phoenix/assertj-db that referenced this issue Jul 6, 2016
@Avinash-Bhat
Copy link
Contributor

@regis1512 is anyone working on this? I'd like to take a crack on this as I have started some work on this out of necessity.

@Avinash-Bhat
Copy link
Contributor

This is my proposal for the APIs (not exhaustive list).

  1. TableAssert.exist()
  2. AbstractColumnAssert.exist()
  3. AbstractColumnAssert.hasType(String)
  4. AbstractColumnAssert.isPrimaryKey()
  5. AbstractColumnAssert.hasDefaultValue(Object)

For completion, the inverse of the isPrimaryKey and, exist should also be provided.

there should be additional methods for additional checks , like foreign key, uniqueness, non-nullability, auto-increment,...

Avinash-Bhat added a commit to Avinash-Bhat/assertj-db that referenced this issue Aug 5, 2016
VanRoy pushed a commit that referenced this issue Dec 24, 2019
@VanRoy
Copy link
Member

VanRoy commented Dec 26, 2019

@Avinash-Bhat Checking existence of table is implemented in release 1.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants