Skip to content

Instantly share code, notes, and snippets.

View harshvb7's full-sized avatar

Harsh harshvb7

  • Cologne, Germany
View GitHub Profile
{"SBALCONY":
[{
"t": "path",
"a": {"d": "M303.6,631.5c6.3,0,11.3,5.1,11.3,11.3s-5.1,11.3-11.3,11.3s-11.3-5.1-11.3-11.3S297.4,631.5,303.6,631.5z"}
},
{
"t": "path",
"a": {"d": "M332,642.8c6.3,0.6,10.8,6.1,10.3,12.2c-0.5,6.2-6.1,10.8-12.2,10.3c-6.2-0.5-10.8-6.1-10.3-12.2 C320.3,646.9,325.8,642.3,332,642.8z"}
},
{
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from django.contrib.messages.storage.fallback import FallbackStorage
from django.contrib.sessions.middleware import SessionMiddleware
from django.test import RequestFactory
class TestSomeView:
def test_view(self):
request = RequestFactory().get('/')
tests/views/test_project_detail_view.py
tests/views/.....
>> Views must ONLY test input and status code of the response. Mock the business logic to test the status code.
Ideally our views should be very SLIM and all the logic must go in services and helper functions
If we need to access some variables from request in the service, then better destructure it in the view and pass ONLY the variable in the service NEVER the request object.
In case of API view, call the service from the serializer after validating the input. Don't come back to the view with the validated input and then put the input again in the service.
tests/services/test_document_service.py
from elasticsearch_dsl import connections, UpdateByQuery
def bulk_update_docs(ids, instance, fields_changed=[]):
"""
Arguments:
ids: {list} -- list of ids of documents we want to update
instance {Model instance} -- The updated user model object
fields_changed {list} -- list of the model fields changed
"""
# we also index a keyword version of the field
name = Text(
analyzer=my_custom_analyzer,
fields={'keyword': es.Keyword()}
)
# and use it with facets like this
'city': TermsFacet(field='user.city.name.keyword'),
from elasticsearch_dsl import FacetedSearch, TermsFacet
class TweetSearch(FacetedSearch):
facets = {
# use bucket aggregations to define facets
'city': TermsFacet(field='user.city.name'),
}
# an example from elasticsearch-dsl docs
from elasticsearch_dsl import analyzer
html_strip = analyzer('html_strip',
tokenizer="standard",
filter=["standard", "lowercase", "stop", "snowball"],
char_filter=["html_strip"]
)
"""
Assume the structure of the document looks like this
{
"user": {
"first_name": "Foo",
"last_name": "Bar",
"age": "30",
"city": {
"id": 5,
for offer in basket.offer_set.published().exclude(ticket_price__state=13):
_voucher_ids = list(offer.ticket_price.ticket_vouchers_multiple.all().values_list('id', flat=True))
if (offer.ticket_price.ticket_voucher and offer.ticket_price.ticket_voucher != ticket_price) or (_voucher_ids and ticket_price.id not in _voucher_ids):
....