Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavomm19 committed Oct 2, 2023
1 parent 987bcfa commit 954d9bd
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 20 deletions.
132 changes: 132 additions & 0 deletions breathecode/registry/tests/urls/tests_asset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import pytest
from unittest.mock import MagicMock, call
from django.urls.base import reverse_lazy
from breathecode.tests.mixins.legacy import LegacyAPITestCase
from django.utils import timezone
from breathecode.tests.mixins.breathecode_mixin import Breathecode

UTC_NOW = timezone.now()

# enable this file to use the database
pytestmark = pytest.mark.usefixtures('db')


def get_serializer(asset, data={}):
return {
'id':
asset.id,
'slug':
asset.slug,
'title':
asset.title,
'asset_type':
asset.asset_type,
'category': {
'id': asset.category.id,
'slug': asset.category.slug,
'title': asset.category.title,
},
'description':
asset.description,
'difficulty':
asset.difficulty,
'duration':
None,
'external':
False,
'gitpod':
False,
'graded':
False,
'intro_video_url':
None,
'lang':
asset.lang,
'preview':
None,
'published_at':
None,
'readme_url':
None,
'solution_video_url':
None,
'status':
'NOT_STARTED',
'url':
None,
'translations': {},
'technologies': [tech.slug for tech in asset.technologies.all()] if asset.technologies else [],
'seo_keywords':
[seo_keyword.slug for seo_keyword in asset.seo_keywords.all()] if asset.seo_keywords else [],
'visibility':
asset.visibility,
**data,
}


def get_serializer_technology(technology, data={}):
return {
'slug': technology.slug,
'title': technology.title,
'description': technology.description,
'icon_url': technology.icon_url,
'is_deprecated': technology.is_deprecated,
**data,
}


class TestRegistryAsset(LegacyAPITestCase):

def test_with_no_assets(self, bc: Breathecode):

Asset = bc.database.get_model('registry.Asset')

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

assert bc.database.list_of('registry.Asset') == []
assert json == []

def test_one_asset(self, bc: Breathecode):

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

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

expected = [get_serializer(model.asset)]

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

def test_many_assets(self, bc: Breathecode):

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

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

expected = [get_serializer(asset) for asset in model.asset]

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

def test_assets_technologies_expand(self, bc: Breathecode):

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'
response = self.client.get(url)
json = response.json()

expected = [
get_serializer(asset, data={'technologies': [get_serializer_technology(model.asset_technology)]})
for asset in model.asset
]

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()),
path('asset', AssetView.as_view(), name='assets'),
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
28 changes: 10 additions & 18 deletions breathecode/registry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,14 @@ def get(self, request, asset_slug=None):

items = Asset.objects.all()
lookup = {}

if 'author' in self.request.GET:
param = self.request.GET.get('author')
lookup['author__id'] = param

if 'owner' in self.request.GET:
param = self.request.GET.get('owner')
lookup['owner__id'] = param
query = handler.lookup.build(
lang,
strings={'iexact': [
'test_status',
'sync_status',
]},
ids=['author', 'owner'],
)

like = request.GET.get('like', None)
if like is not None:
Expand All @@ -522,14 +522,6 @@ def get(self, request, asset_slug=None):
param = self.request.GET.get('category')
lookup['category__slug__in'] = [p for p in param.split(',')]

if 'test_status' in self.request.GET:
param = self.request.GET.get('test_status')
lookup['test_status__iexact'] = param

if 'sync_status' in self.request.GET:
param = self.request.GET.get('sync_status')
lookup['sync_status__iexact'] = param

if 'slug' in self.request.GET:
asset_type = self.request.GET.get('asset_type', None)
param = self.request.GET.get('slug')
Expand Down Expand Up @@ -609,12 +601,12 @@ def get(self, request, asset_slug=None):
param = self.request.GET.get('exclude_category')
items = items.exclude(category__slug__in=[p for p in param.split(',') if p])

items = items.filter(**lookup)
items = items.filter(query, **lookup)
items = handler.queryset(items)

if 'big' in self.request.GET:
serializer = AssetMidSerializer(items, many=True)
elif 'with_techs' in self.request.GET:
elif 'expand' in self.request.GET and self.request.GET.get('expand') == 'technologies':
serializer = AssetAndTechnologySerializer(items, many=True)
else:
serializer = AssetSerializer(items, many=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def generate_registry_models(self,
models['asset'] = create_models(asset, 'registry.Asset', **kargs)

if 'asset_technology' in models and 'asset' in models:
for instance in models['asset_technology']:
technologies = models['asset_technology']
if not isinstance(technologies, list):
technologies = [models['asset_technology']]
for instance in technologies:
instance.featured_asset = just_one(models['asset'])
instance.save()

Expand Down

0 comments on commit 954d9bd

Please sign in to comment.