Skip to content

Commit

Permalink
add more data to lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavomm19 committed Oct 3, 2023
1 parent 8b5fc6d commit 4b00676
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 72 deletions.
71 changes: 35 additions & 36 deletions breathecode/registry/tests/urls/tests_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,58 +74,57 @@ def get_serializer_technology(technology, data={}):
}


class TestRegistryAsset(LegacyAPITestCase):
def test_with_no_assets(bc: Breathecode, client):

def test_with_no_assets(self, bc: Breathecode):
url = reverse_lazy('registry:assets')
response = client.get(url)
json = response.json()

Asset = bc.database.get_model('registry.Asset')
assert json == []
assert bc.database.list_of('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(bc: Breathecode, client):

def test_one_asset(self, bc: Breathecode):
model = bc.database.create(asset=1)

model = bc.database.create(asset=1)
url = reverse_lazy('registry:assets')
response = client.get(url)
json = response.json()

url = reverse_lazy('registry:assets')
response = self.client.get(url)
json = response.json()
expected = [get_serializer(model.asset)]

expected = [get_serializer(model.asset)]
assert json == expected
assert bc.database.list_of('registry.Asset') == [bc.format.to_dict(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):
def test_many_assets(bc: Breathecode, client):

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

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

expected = [get_serializer(asset) for asset in model.asset]
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)
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}))
def test_assets_technologies_expand(bc: Breathecode, client):

url = reverse_lazy('registry:assets') + f'?expand=technologies'
response = self.client.get(url)
json = response.json()
technology = {'slug': 'learn-react', 'title': 'Learn React'}
model = bc.database.create(asset_technology=(1, technology), asset=(3, {'technologies': 1}))

expected = [
get_serializer(asset, data={'technologies': [get_serializer_technology(model.asset_technology)]})
for asset in model.asset
]
url = reverse_lazy('registry:assets') + f'?expand=technologies'
response = client.get(url)
json = response.json()

assert json == expected
assert bc.database.list_of('registry.Asset') == bc.format.to_dict(model.asset)
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)
49 changes: 14 additions & 35 deletions breathecode/registry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,27 +501,28 @@ def get(self, request, asset_slug=None):
lookup = {}
query = handler.lookup.build(
lang,
strings={'iexact': [
'test_status',
'sync_status',
]},
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',
})

like = request.GET.get('like', None)
if like is not None:
items = items.filter(
Q(slug__icontains=like) | Q(title__icontains=like)
| Q(assetalias__slug__icontains=like))

if 'asset_type' in self.request.GET:
param = self.request.GET.get('asset_type')
lookup['asset_type__in'] = [p.upper() for p in param.split(',') if p]

if 'category' in self.request.GET:
param = self.request.GET.get('category')
lookup['category__slug__in'] = [p for p in param.split(',')]

if 'slug' in self.request.GET:
asset_type = self.request.GET.get('asset_type', None)
param = self.request.GET.get('slug')
Expand All @@ -543,22 +544,10 @@ def get(self, request, asset_slug=None):
else:
lookup['visibility'] = 'PUBLIC'

if 'technologies' in self.request.GET:
param = self.request.GET.get('technologies')
lookup['technologies__slug__in'] = [p.lower() for p in param.split(',')]

if 'difficulty' in self.request.GET:
param = self.request.GET.get('difficulty')
lookup['difficulty__in'] = [p.upper() for p in param.split(',')]

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

if 'status' in self.request.GET:
param = self.request.GET.get('status')
lookup['status__in'] = [p.upper() for p 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 All @@ -575,16 +564,6 @@ def get(self, request, asset_slug=None):
if param == 'true':
lookup['with_video'] = True

if 'interactive' in self.request.GET:
param = self.request.GET.get('interactive')
if param == 'true':
lookup['interactive'] = True

if 'graded' in self.request.GET:
param = self.request.GET.get('graded')
if param == 'true':
lookup['graded'] = True

lookup['external'] = False
if 'external' in self.request.GET:
param = self.request.GET.get('external')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ def slug(lang: str, key: str, value: str, alias=None) -> Q:
def string(mode: str) -> Callable[[str, str, str], Q]:

def handler(lang: str, key: str, value: str, alias=None) -> str:
return Q(**{f'{key}__{mode}': value})
param = value
if (mode == 'in'):
param = param.split(',') if param is not None else []
return Q(**{f'{key}__{mode}': param})

return handler

Expand Down
15 changes: 15 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ def bc(request):
return Breathecode(request.instance)


@pytest.fixture
def client():
from django.test.client import Client

return Client()


from django.core.cache import cache


@pytest.fixture(autouse=True)
def clear_cache():
cache.clear()


@pytest.fixture(autouse=True)
def enable_hook_manager(monkeypatch):
"""
Expand Down

0 comments on commit 4b00676

Please sign in to comment.