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

Initial tests for SitesManager Model class. #5999

Closed
wants to merge 1 commit into from

Conversation

czolnowski
Copy link
Contributor

It's my initial tests for Sites Manager Model class. I would like to add possibility to offsetting limit and filtering sites fetch query result.

@mattab
Copy link
Member

mattab commented Aug 14, 2014

Good start! Nice to see you add Model tests.

Feedback:

Have a look at: https://github.com/piwik/piwik/blob/master/plugins/API/tests/CsvRendererTest.php#L101-101 - I like how Thomas writes unit tests. the naming of methods is this way:

test_$methodBeingTested_should$Condition

So for example shouldReturnNullIfSiteDoesNotExists() would be: test_getSite_shouldReturnFalseWhenSiteDoesNotExist

This way we know by looking at test name function which method it is testing.

@tsteur
Copy link
Member

tsteur commented Aug 15, 2014

Awesome to see those kinda tests :)

Actually, I'm usually trying to use test_$methodBeingTested_should$ExpectedBehavior_If$Condition so test_getSite_shouldNotDeleteSite_IfSiteIdIsInvalid. But it is not always possible and sometimes I forget an underscore ;)

Maybe you also want to create the Model in the constructor to remove duplicated code.

To make tests further easier to maintain, to read and to remove more duplicated code creating your own asserts and helper methods could be nice and reusable. Ideally a test consists only of 2 or 3 lines like this:

public function shouldReturnSiteById()
{
    $id = $this->createSite('1');

    $this->assertGetSitesFromSiteIds(array('test1'), array($id));
}

private function createSite($appendix)
{
    return $this->siteCreator->createSite(array(
        'name' => 'test' . $appendix,
        'main_url' => 'http://example' . $appendix . '.com'
    ));
}

private function assertGetSitesFromSiteIds($expectedSiteNames, $siteIds)
{
    $sites = $this->model->getSitesFromIds($siteIds);

    foreach ($sites as $index => $site) {
        $this->assertEquals($expectedSiteNames[$index], $site['name']);
    }
}

I've also learned the hard way that is good practice to compare the whole returned array since you never know what happens with the other values otherwise. For instance when testing only the name of a site it is hard to tell what happens with the other site parameters. Especially when we think of a database where we don't know whether it'll store all values, whether it'll convert/truncate any values, ... But it'd be ok for now to do it like this and we would maybe rather create those kinda tests in test_createSite maybe ;)

@mattab mattab added the Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review. label Sep 17, 2014
@mattab mattab added this to the Piwik 2.9.0 milestone Sep 17, 2014
@mattab mattab added the c: Tests & QA For issues related to automated tests or making it easier to QA & test issues. label Oct 12, 2014
@mattab mattab modified the milestones: Piwik 2.9.0, Piwik 2.10.0 Oct 23, 2014
@mattab
Copy link
Member

mattab commented Nov 4, 2014

Thank you for this proposed pull request.

Because it was last updated more than one month ago, it is our policy to close pull requests opened for a long time without updates. If you would like to continue work on the pull request, please simply ping us to have it re-opened (after you have pushed a new commit).

We hope you understand this and we look forward to seeing an update from you on this pull request or another one!

Thanks.

@mattab mattab closed this Nov 4, 2014
@mattab mattab added the answered For when a question was asked and we referred to forum or answered it. label Nov 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered For when a question was asked and we referred to forum or answered it. c: Tests & QA For issues related to automated tests or making it easier to QA & test issues. Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants