unit testing controllers

Mads Kiilerich mads at kiilerich.com
Tue Feb 17 21:50:21 EST 2015


On 02/14/2015 10:29 PM, Thomas De Schampheleire wrote:
> Hi,
>
> I would like to add some tests for the pullrequest controller method
> _get_repo_refs(), which returns the list of
> revisions/branches/tags/... to populate the select boxes on the pull
> request creation page.
>
> Most of the current tests are actually parsing the HTTP response data,
> but this is an indirect way of testing. I would prefer testing the
> function directly.
>
> I'm a bit stuck on setting up the data correctly. I added the
> following in tests/functional/test_pullrequests.py:
>
>
>      def test_repo_refs(self):
>          main = fixture.create_repo('main')
>          Session.add(main)
>          Session.commit()
>          #fork = fixture.create_fork(main, 'fork')
>
>          controller = PullrequestsController()
>
>          print controller._get_repo_refs(main)
>
>          fixture.destroy_repo('main')
>
>
> but when _get_repo_refs is invoked, an error is thrown:
>
> test_repo_refs (kallithea.tests.functional.test_pullrequests.TestPullrequestsController)
> ... ERROR
>
> ======================================================================
> ERROR: test_repo_refs
> (kallithea.tests.functional.test_pullrequests.TestPullrequestsController)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>    File "/home/tdescham/repo/contrib/kallithea-typos/kallithea/tests/functional/test_pullrequests.py",
> line 25, in test_repo_refs
>      print controller._get_repo_refs(main)
>    File "/home/tdescham/repo/contrib/kallithea-typos/kallithea/controllers/pullrequests.py",
> line 108, in _get_repo_refs
>      tiprev = repo.tags.get('tip')
> AttributeError: 'Repository' object has no attribute 'tags'
>
>
> I'm guessing that the repository I created is not yet fully created,
> or some other setup action is missing.
>
> Any help or feedback is appreciated.

That must be because fixture.py create_repo returns the raw db.py 
Repository database record (see fixture.py).
_get_repo_refs expects a kallithea/lib/vcs/backends/hg/repository.py 
MercurialRepository .
You can get that by calling .scm_instance on the Repository record, like 
it is done in PullrequestsController.index before it calls _get_repo_refs .

/Mads


More information about the kallithea-general mailing list