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

[TESTS] Refactor to support database tests via sqlite #289

Merged
merged 11 commits into from
May 31, 2019
Merged

Conversation

mfn
Copy link
Collaborator

@mfn mfn commented May 30, 2019

Summary

Only "tests" related code has been touched in this PR!

I've already started moving tests into a dedicated namespace

  • create new namespace/directory layout for tests and supporting infrastructure
    image
  • Add a new TestCaseDatabase class which will setup an in-memory SQLITE database and see it with migrations/factories from tests/Support/database
    mainly, we will split tests between pure unit tests (no database required) and ones with

TODO

  • Add actual tests using the database, e.g. SelectFields tests
  • When endpoint test fails under certain error conditions, they return a regular 404 error page with the exception included in the HTML => this makes debugging harder than it should be
    Will tackle later.

This should pave the way for #277 and #283 so we we'll have proper tests for them.

@mfn mfn self-assigned this May 30, 2019
@zjbarg
Copy link
Contributor

zjbarg commented May 30, 2019

Eventually we need integration tests right?

@mfn
Copy link
Collaborator Author

mfn commented May 30, 2019

Eventually we need integration tests right?

Ack, see the TODO ;-)

@mfn mfn force-pushed the mp-tests-database branch from dc85a2d to c8aca6a Compare May 30, 2019 14:46
@mfn mfn force-pushed the mp-tests-database branch from 80a364a to 4531f37 Compare May 30, 2019 14:48
]);

$this->assertSqlQueries(<<<'SQL'
select "posts"."id", "posts"."title" from "posts" where "posts"."id" = ? limit 1;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test shows the functionality with SelectFields \o/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@mfn mfn force-pushed the mp-tests-database branch 2 times, most recently from 4c5d25a to 0940aad Compare May 30, 2019 19:30
@mfn mfn force-pushed the mp-tests-database branch from c93c633 to d7989a1 Compare May 30, 2019 20:05
@mfn mfn changed the title WiP: [TESTS] Refactor to support database tests via sqlite [TESTS] Refactor to support database tests via sqlite May 30, 2019
@mfn mfn marked this pull request as ready for review May 30, 2019 20:20
@mfn mfn requested a review from rebing May 30, 2019 20:20

public function type()
{
return GraphQL::type('Post');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this (and the other Queries as well) return Type::nonNull(GraphQL::type('Post')) instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok nvm, I just saw the issue you posted. It's strange though, because I thought I've used nonNull with custom types

description
}
}
GRAQPHQL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the issue with indenting this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean specifically?

]);

$this->assertSqlQueries(<<<'SQL'
select "posts"."id", "posts"."title" from "posts" where "posts"."id" = ? limit 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the issue with indenting this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its kinda painful to work this way! My editor went mad!
Image

img

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, works correctly for me => using PhpStorm 2019 here; what IDE are you using?

collapse_expand

The issue I'm seeing:

  • it doesn't matter for the GraphQL queries (whitespace is insignificant)
  • but for the SQL assertion/comparison, they are significant

Another reason I like to keep it this way is, especially for the SQL assertions, they can get very long. Inevitably horizontal scrolling for them will be necessary often, but the less that is required, the better.

Copy link
Owner

@rebing rebing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! The assertSqlQueries solution is cool.

@mfn mfn force-pushed the mp-tests-database branch from d7989a1 to eac57e9 Compare May 31, 2019 06:21
@mfn
Copy link
Collaborator Author

mfn commented May 31, 2019

The assertSqlQueries solution is cool.

Thanks!

I don't see this approach used very often. I've been working on commercial projects where, under complex circumstances, it's necessary to understand the SQL code being executed (selected columns, n+1 problems, redundant queries, …).
n+1 problems can sometimes also easily be spotted via just assertion the counts by seeing the actual statements it's very insightful.

Especially in this case where the entire goal of SelectFields is about custom queries, I though the only way to assert the correct is about the SQL statements.

@mfn mfn merged commit 01daf55 into master May 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants