def test_it_should_expand_when_expansion_is_specified(client): menu_item = f.MenuItemFactory() r = client.get('/menu-items/%s/?expand=menu' % menu_item.pk) assert r.data['menu'] == { 'id': menu_item.menu.pk, 'restaurant': menu_item.menu.restaurant.pk, 'title': menu_item.menu.title }
def test_it_should_be_able_to_expand_multiple_fields(client): menu_item = f.MenuItemFactory() r = client.get('/menu-items/%s/?expand=menu,price_bracket' % menu_item.pk) assert r.data['menu'] == { 'id': menu_item.menu.pk, 'restaurant': menu_item.menu.restaurant.pk, 'title': menu_item.menu.title } assert r.data['price_bracket'] == { 'id': menu_item.price_bracket.pk, 'title': menu_item.price_bracket.title }
def test_kwargs_reused_across_requests(client): meta = test_views.MenuItemSerializer.Meta price_bracket_spec = meta.expandable_fields['price_bracket'] assert (test_views.PriceBracketSerializer, (), {}) == price_bracket_spec menu_item = f.MenuItemFactory() r = client.get('/menu-items/%s/?expand=menu.restaurant,menu.chef' % (menu_item.pk, )) # Spec has not been altered assert (test_views.PriceBracketSerializer, (), {}) == price_bracket_spec
def test_it_should_be_able_to_expand_multiple_nested_fields(client): menu_item = f.MenuItemFactory() r = client.get('/menu-items/%s/?expand=menu.restaurant,menu.chef' % (menu_item.pk, )) assert r.data['menu'] == { 'id': menu_item.menu.pk, 'restaurant': { 'id': menu_item.menu.restaurant.pk, 'title': menu_item.menu.restaurant.title, }, 'chef': { 'id': menu_item.menu.chef.pk, 'name': menu_item.menu.chef.name, 'stars': menu_item.menu.chef.stars, }, 'title': menu_item.menu.title }
def test_when_expand_is_not_in_expandable_fields(client): menu_item = f.MenuItemFactory() r = client.get('/menu-items/%s/?expand=blub' % menu_item.pk) assert r.status_code == status.HTTP_200_OK
def test_it_should_be_collapsed_when_no_expansion_is_specified(client): menu_item = f.MenuItemFactory() r = client.get('/menu-items/%s/' % menu_item.pk) assert r.data['menu'] == menu_item.menu.pk