These are chat archives for django/django

27th
Apr 2016
sumit sinha
@sumitsinha91
Apr 27 2016 07:50
hey
anyone have unittest rested documents for study and for better understand
because i ma new on python and unittest and i have work on unittest
Filip Figiel
@megapctr
Apr 27 2016 07:52
google is your friend
sumit sinha
@sumitsinha91
Apr 27 2016 07:52
hehehe
i know
Filip Figiel
@megapctr
Apr 27 2016 07:52
what do you need specifically?
help on concept of unit tests, help with a library or figuring out how to test code?
sumit sinha
@sumitsinha91
Apr 27 2016 07:53
some blog have very better for understand
ya excetally
how to test code that i want
Filip Figiel
@megapctr
Apr 27 2016 07:55
you may want to take a look at mock library
sumit sinha
@sumitsinha91
Apr 27 2016 07:55
from where??
Filip Figiel
@megapctr
Apr 27 2016 07:55
that way you can avoid triggering side effects your functions may try to execute
pip install mock
and google for the documentation
sumit sinha
@sumitsinha91
Apr 27 2016 07:55
ok
sure
Filip Figiel
@megapctr
Apr 27 2016 07:56
however, it's best to write as many pure functions as you can
sumit sinha
@sumitsinha91
Apr 27 2016 07:56
here i am testing django test
Filip Figiel
@megapctr
Apr 27 2016 07:56
a pure function does not have any state, nor does it trigger side effects - that way you don't need to patch or mock as much
sumit sinha
@sumitsinha91
Apr 27 2016 07:57
here my project has been developed in django with python
and my work is api testing
Filip Figiel
@megapctr
Apr 27 2016 07:58
testing APIs is rather simple
do you use pytest?
pytest along with pytest-django are great for testing django apps and apis
sumit sinha
@sumitsinha91
Apr 27 2016 07:59
no i did't use this one pytest
you have any link for pytest and mock
related best tutorial
or any blogs
sumit sinha
@sumitsinha91
Apr 27 2016 08:02
thanks
man
Filip Figiel
@megapctr
Apr 27 2016 08:04
the best thing about pytest is fixtures
sumit sinha
@sumitsinha91
Apr 27 2016 08:05
ok
now i have work to test api with url
Filip Figiel
@megapctr
Apr 27 2016 08:05
make sure to check out how fixtures work, and the various behaviors of fixtures (parameterize, yield, etc)
sumit sinha
@sumitsinha91
Apr 27 2016 08:06
after hiting url whats url getting
Filip Figiel
@megapctr
Apr 27 2016 08:06
in pytest, you use python's assert statement for everything
e.g. assert 2 + 2 == 4
if something breaks, pytest will try to trace back the values
e.g. if assert foo == bar fails, pytest may show you that foo was 2 + 2 and bar was 5
sumit sinha
@sumitsinha91
Apr 27 2016 08:07
ohhh
good
here i have done test in different way like simple python
i am sending you code as example
Filip Figiel
@megapctr
Apr 27 2016 08:08
for API tests you will want to assert response codes
sumit sinha
@sumitsinha91
Apr 27 2016 08:09
class PartyVerifyTestCase(TestCase):

    def setUp(self):
        self._token = self._get_auth_token()
        self._headers = {'Authorization': "Token %s" % self._token}

    def _get_auth_token(self):

        return '9b9756d8a35f4082ded6e8e9fec20d005bc7a67d'

    def test_party_verify_without_authentication(self):
        url = 'http://dev.oms.fetchr.us/party/verify/12456'
        resp = self.client.get(url)
        self.assertEqual(resp.status_code, 403)

    def test_party_verify_with_authentication(self):
        url = 'http://dev.oms.fetchr.us/party/verify/12456'
        resp = requests.get(url, headers=self._headers)
        self.assertEqual(resp.status_code, 200)
seee my code for examle
example
how is my code concept is good or not
??
Filip Figiel
@megapctr
Apr 27 2016 08:11
looks ok, but with pytest you'll do it in a different way
token and headers would be fixtures
and you would take them as function arguments for your tests
def test_foo(self, token, headers)
sumit sinha
@sumitsinha91
Apr 27 2016 08:12
ohhh
i think i have to read first pytest documents then i will able to understand
Filip Figiel
@megapctr
Apr 27 2016 08:12
good luck :)
sumit sinha
@sumitsinha91
Apr 27 2016 08:12
what is fixture ??
Mir Nazim
@mnazim
Apr 27 2016 08:22
@sumitsinha91 Fixture is a data in json format that can be imported using manage.py loaddata command
sumit sinha
@sumitsinha91
Apr 27 2016 08:36
ohhh
thanks
Filip Figiel
@megapctr
Apr 27 2016 08:41
nooo
not in this sense
pytest fixtures are functions which work like setUp for your tests
Mir Nazim
@mnazim
Apr 27 2016 08:42
My bad, missed the context from previous messages.
Filip Figiel
@megapctr
Apr 27 2016 08:42
@pytest.fixture
def foo():
  return 2

def test_foo(foo):
  assert foo + foo == 4
Eric
@lroolle
Apr 27 2016 11:26
Halohalo all。。
Alex
@ajbeach2
Apr 27 2016 14:36
what is the 'r' in the following code?
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
is this a regex?
Mir Nazim
@mnazim
Apr 27 2016 14:36
yes. it means a regex.
Alex
@ajbeach2
Apr 27 2016 14:36
cool
sheepsy90
@sheepsy90
Apr 27 2016 15:16
Is there any way in django1.8.5 or higher to define the Schema that should be used. We are using a pgbouncer to handle connections to our database. But we experience problems with setting the search path because of side effects.
Wie have a postgres DB
Eric
@lroolle
Apr 27 2016 16:12
Lroolle.com build by django. Leave me any messages if you like
Alex
@ajbeach2
Apr 27 2016 16:39
is there an advantage of doing nested routes vs dynamic routes?
Ezumezu Anselm
@aezumezu
Apr 27 2016 17:03
Hi
Which of these frameworks is easier and better to use for page layout and formating with flask.
Bootstrap or Materialize?
Mir Nazim
@mnazim
Apr 27 2016 17:47
@aezumezu Bootstrap definitely has the weight of a large community behind it. No idea about materialize.
Ezumezu Anselm
@aezumezu
Apr 27 2016 17:48
Thank you much.
Mir Nazim
@mnazim
Apr 27 2016 17:48
@ajbeach2 provide more details. Are we talking about Django urls? What do you mean by nested routes?
Alex
@ajbeach2
Apr 27 2016 20:16
@mnazim never mind actually
Mir Nazim
@mnazim
Apr 27 2016 20:17
:thumbsup: @ajbeach2
Alex
@ajbeach2
Apr 27 2016 20:17
right now, im trying to figure out how to do dynamic routes based on usernames
so for example, mywebsite.com/username
how do i do that?
so that the username is the root of the path, and that maps to the users-detail
im using django rest framework
Mir Nazim
@mnazim
Apr 27 2016 20:23

Assuming your users app is called users, In your project urls.py, you can add following, at the bottom of the url list.

    url(r'^', include('users.urls', namespace='users')),

and then in users/urls.py,

url(r'^(?P<username>[-\w.]+)/$', UserProfileView.as_view(), name='profile')
It needs to be as last one because if put on top other urls might not be reachable.
Alex
@ajbeach2
Apr 27 2016 20:24
right
that is one way, thanks
Mir Nazim
@mnazim
Apr 27 2016 20:24
you can then use it in templates as {% url 'users:profile' user_object.username %}
:thumbsup:
Alex
@ajbeach2
Apr 27 2016 20:24
im using django rest though, i was trying to use: http://www.django-rest-framework.org/api-guide/routers/
Mir Nazim
@mnazim
Apr 27 2016 20:29
I haven't used DRF routers in a lot because I don't like them, may be you could try something like
router.register(r'', UserViewSet) to make users urls without a prefix. Not sure if it will work.
Alex
@ajbeach2
Apr 27 2016 20:56
yeah that didn't work
Mir Nazim
@mnazim
Apr 27 2016 20:58
Do you absolutely need to have user API endpoint at the root of the url. You can have the frontend profile url like that and have /user/username as the API endpoint.
Alex
@ajbeach2
Apr 27 2016 20:58
yeah that is probably a better solution
good call
Mir Nazim
@mnazim
Apr 27 2016 20:59
I usually make two endpoints for users
/user/me for the currently loggedin user.
/user/username for the other users.
Alex
@ajbeach2
Apr 27 2016 20:59
yeah, i will do something similar
Mir Nazim
@mnazim
Apr 27 2016 20:59
currently loggedin user in DRF context would be the one with whose access token the endpoint is accessed.
:thumbsup:
Alex
@ajbeach2
Apr 27 2016 21:00
im using jwt
or whatever the jwt plugin for djanog rest is
Mir Nazim
@mnazim
Apr 27 2016 21:00
even in jwt's case, idea remains the same
Alex
@ajbeach2
Apr 27 2016 21:01
true
Alex
@ajbeach2
Apr 27 2016 21:09
@mnazim have you used webpack with django rest? i haven't figured out how to setup hot reloading to work with django
in rails, you can tell rails to serve all assets from a different location
ie, if you were runnign your webpack server at 8080, you can tell rails to server all assets form localhost:8080, is there somethign like that for django?
Mir Nazim
@mnazim
Apr 27 2016 21:11
A friend of mine wrote this https://github.com/owais/django-webpack-loader/
You might like this. It's quite popular
Alex
@ajbeach2
Apr 27 2016 21:12
sweeeeet