Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Change Route.level to be zero-indexed
Browse files Browse the repository at this point in the history
  • Loading branch information
meshy committed Jan 21, 2018
1 parent 9a9c0da commit d3963e4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions conman/routes/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ def move_branch(self, old_url, new_url):

def with_level(self, level=None):
"""
Annotate the queryset with the (1-indexed) level of each item.
Annotate the queryset with the (0-indexed) level of each item.
The level reflects the number of forward slashes in the path.
If "level" is passed in, the queryset will be filtered by the level.
"""
qs = self.annotate(level=CharCount('url', char='/'))
qs = self.annotate(level=CharCount('url', char='/') - 1)
if level is None:
return qs
return qs.filter(level=level)
4 changes: 2 additions & 2 deletions conman/routes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def handle(self, request, path):

@property
def level(self):
"""Fetch the one-indexed 'level' of this item in the URL tree."""
return self.url.count('/')
"""Fetch the 'level' of this item in the URL tree."""
return self.url.count('/') - 1 # 0-indexed.

@level.setter
def level(self, new_value):
Expand Down
5 changes: 3 additions & 2 deletions tests/routes/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def test_query(self):

def test_annotation(self):
"""Test the expression can be used for annotation."""
RouteFactory.create(url='/sixth/level/path/including/root/')
RouteFactory.create(url='/fifth/level/zero/indexed/path/')
route = Route.objects.annotate(level=CharCount('url', char='/')).get()
self.assertEqual(route.level, 6) # The number of "/" in the path.
# The number of "/" in the path minus one for zero-indexing.
self.assertEqual(route.level, 5)

def test_calling_format(self):
"""Ensure the 'char' argument is always a keyword-arg."""
Expand Down
8 changes: 4 additions & 4 deletions tests/routes/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ def test_no_level_passed(self):
RouteFactory.create(url='/branch/leaf/')
result = Route.objects.with_level().values_list('level', 'url')
expected = (
(1, '/'),
(2, '/branch/'),
(3, '/branch/leaf/'),
(0, '/'),
(1, '/branch/'),
(2, '/branch/leaf/'),
)
self.assertCountEqual(result, expected)

Expand All @@ -220,5 +220,5 @@ def test_level_passed(self):
RouteFactory.create(url='/') # Not in QS.
branch = RouteFactory.create(url='/branch/')
RouteFactory.create(url='/branch/leaf/') # Not in QS.
result = Route.objects.with_level(2)
result = Route.objects.with_level(1)
self.assertCountEqual(result, [branch])
8 changes: 4 additions & 4 deletions tests/routes/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def test_url_form_widget(self):
self.assertIsInstance(widget, forms.TextInput)

def test_level(self):
"""Route.level is based on the url field."""
"""Route.level is based on the url field, and is zero-indexed."""
urls = (
(1, '/'),
(2, '/branch/'),
(3, '/branch/leaf/'),
(0, '/'),
(1, '/branch/'),
(2, '/branch/leaf/'),
)
for level, url in urls:
with self.subTest(url=url):
Expand Down

0 comments on commit d3963e4

Please sign in to comment.