Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavomm19 committed Oct 3, 2023
1 parent 4b00676 commit bcb6ad8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 26 deletions.
105 changes: 101 additions & 4 deletions breathecode/registry/tests/urls/tests_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_serializer_technology(technology, data={}):

def test_with_no_assets(bc: Breathecode, client):

url = reverse_lazy('registry:assets')
url = reverse_lazy('registry:asset')
response = client.get(url)
json = response.json()

Expand All @@ -88,7 +88,7 @@ def test_one_asset(bc: Breathecode, client):

model = bc.database.create(asset=1)

url = reverse_lazy('registry:assets')
url = reverse_lazy('registry:asset')
response = client.get(url)
json = response.json()

Expand All @@ -102,7 +102,7 @@ def test_many_assets(bc: Breathecode, client):

model = bc.database.create(asset=3)

url = reverse_lazy('registry:assets')
url = reverse_lazy('registry:asset')
response = client.get(url)
json = response.json()

Expand All @@ -117,7 +117,7 @@ def test_assets_technologies_expand(bc: Breathecode, client):
technology = {'slug': 'learn-react', 'title': 'Learn React'}
model = bc.database.create(asset_technology=(1, technology), asset=(3, {'technologies': 1}))

url = reverse_lazy('registry:assets') + f'?expand=technologies'
url = reverse_lazy('registry:asset') + f'?expand=technologies'
response = client.get(url)
json = response.json()

Expand All @@ -128,3 +128,100 @@ def test_assets_technologies_expand(bc: Breathecode, client):

assert json == expected
assert bc.database.list_of('registry.Asset') == bc.format.to_dict(model.asset)


def test_assets_with_slug(bc: Breathecode, client):

assets = [{'slug': 'randy'}, {'slug': 'jackson'}]
model = bc.database.create(asset=assets)

url = reverse_lazy('registry:asset') + '?slug=randy'
response = client.get(url)
json = response.json()

expected = [get_serializer(model.asset[0])]

assert json == expected
assert bc.database.list_of('registry.Asset') == bc.format.to_dict(model.asset)


def test_assets_with_lang(bc: Breathecode, client):

assets = [{'lang': 'us'}, {'lang': 'es'}]
model = bc.database.create(asset=assets)

url = reverse_lazy('registry:asset') + '?language=en'
response = client.get(url)
json = response.json()

expected = [get_serializer(model.asset[0])]

assert json == expected
assert bc.database.list_of('registry.Asset') == bc.format.to_dict(model.asset)


def test_assets_with_visibility(bc: Breathecode, client):

assets = [{'visibility': 'PUBLIC'}, {'visibility': 'PRIVATE'}]
model = bc.database.create(asset=assets)

url = reverse_lazy('registry:asset') + '?visibility=PRIVATE'
response = client.get(url)
json = response.json()

expected = [get_serializer(model.asset[1])]

assert json == expected
assert bc.database.list_of('registry.Asset') == bc.format.to_dict(model.asset)


def test_assets_with_bad_academy(bc: Breathecode, client):

model = bc.database.create(asset=2)

url = reverse_lazy('registry:asset') + '?academy=banana'
response = client.get(url)
json = response.json()

expected = {'detail': 'academy-id-must-be-integer', 'status_code': 400}

assert json == expected
assert response.status_code == 400
assert bc.database.list_of('registry.Asset') == bc.format.to_dict(model.asset)


def test_assets_with_academy(bc: Breathecode, client):

academies = bc.database.create(academy=2)
assets = [{'academy': academies.academy[0]}, {'academy': academies.academy[1]}]
model = bc.database.create(asset=assets)

url = reverse_lazy('registry:asset') + '?academy=2'
response = client.get(url)
json = response.json()

expected = [get_serializer(model.asset[1])]

assert json == expected
assert bc.database.list_of('registry.Asset') == bc.format.to_dict(model.asset)


def test_assets_with_category(bc: Breathecode, client):

categories = [{'slug': 'how-to'}, {'slug': 'como'}]
model_categories = bc.database.create(asset_category=categories)
assets = [{
'category': model_categories.asset_category[0]
}, {
'category': model_categories.asset_category[1]
}]
model = bc.database.create(asset=assets)

url = reverse_lazy('registry:asset') + '?category=how-to'
response = client.get(url)
json = response.json()

expected = [get_serializer(model.asset[0])]

assert json == expected
assert bc.database.list_of('registry.Asset') == bc.format.to_dict(model.asset)
2 changes: 1 addition & 1 deletion breathecode/registry/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

app_name = 'registry'
urlpatterns = [
path('asset', AssetView.as_view(), name='assets'),
path('asset', AssetView.as_view(), name='asset'),
path('asset/test', handle_test_asset),
path('asset/thumbnail/<str:asset_slug>', AssetThumbnailView.as_view(), name='asset_thumbnail_slug'),
path('asset/preview/<str:asset_slug>', render_preview_html),
Expand Down
41 changes: 20 additions & 21 deletions breathecode/registry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,23 +499,26 @@ def get(self, request, asset_slug=None):

items = Asset.objects.all()
lookup = {}
query = handler.lookup.build(
lang,
strings={
'iexact': [
'test_status',
'sync_status',
],
'in': ['difficulty', 'status', 'asset_type', 'category__slug', 'technologies__slug']
},
ids=['author', 'owner'],
bools={
'exact': ['with_video', 'interactive', 'graded'],
},
overwrite={
'category': 'category__slug',
'technologies': 'technologies__slug',
})
query = handler.lookup.build(lang,
strings={
'iexact': [
'test_status',
'sync_status',
],
'in': [
'difficulty', 'status', 'asset_type', 'category__slug',
'technologies__slug', 'seo_keywords__slug'
]
},
ids=['author', 'owner'],
bools={
'exact': ['with_video', 'interactive', 'graded'],
},
overwrite={
'category': 'category__slug',
'technologies': 'technologies__slug',
'seo_keywords': 'seo_keywords__slug'
})

like = request.GET.get('like', None)
if like is not None:
Expand Down Expand Up @@ -544,10 +547,6 @@ def get(self, request, asset_slug=None):
else:
lookup['visibility'] = 'PUBLIC'

if 'keywords' in self.request.GET:
param = self.request.GET.get('keywords')
items = items.filter(seo_keywords__slug__in=param.split(','))

try:
if 'academy' in self.request.GET and self.request.GET.get('academy') not in ['null', '']:
param = self.request.GET.get('academy')
Expand Down

0 comments on commit bcb6ad8

Please sign in to comment.