Best Python code snippet using autotest_python
test_views.py
Source:test_views.py
...22 self.assertEqual(response.status_code, 200)23 self.assertEqual(response.json()['relationship_status'], RELATIONSHIP_REQUEST_HAS_SENT)24 self.user1.refresh_from_db()25 self.user2.refresh_from_db()26 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT)27 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT)28 def test_view_cancel_own_send_request_for_relationship(self):29 client = Client()30 client.login(username=self.user1.username, password=self.password)31 response = client.post(reverse('friends:user_view', kwargs={'user_id': self.user2.id}), {'action': 'add'})32 self.assertEqual(response.status_code, 200)33 self.user1.refresh_from_db()34 self.user2.refresh_from_db()35 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT)36 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT)37 response = client.post(reverse('friends:my_friends'), {'action': 'cancel', 'user_id': self.user2.id})38 self.assertEqual(response.status_code, 200)39 self.assertEqual(response.json()['relationship_status'], NO_RELATIONSHIP)40 self.user1.refresh_from_db()41 self.user2.refresh_from_db()42 self.assertEqual(self.user1.check_relationship(self.user2), NO_RELATIONSHIP)43 self.assertEqual(self.user2.check_relationship(self.user1), NO_RELATIONSHIP)44 def test_view_cancel_foreign_send_request_for_relationship(self):45 client = Client()46 client.login(username=self.user1.username, password=self.password)47 response = client.post(reverse('friends:user_view', kwargs={'user_id': self.user2.id}), {'action': 'add'})48 self.assertEqual(response.status_code, 200)49 self.user1.refresh_from_db()50 self.user2.refresh_from_db()51 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT)52 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT)53 client.login(username=self.user2.username, password=self.password)54 response = client.post(reverse('friends:my_friends'), {'action': 'cancel', 'user_id': self.user1.id})55 self.assertEqual(response.status_code, 200)56 self.assertEqual(response.json()['relationship_status'], NO_RELATIONSHIP)57 self.user1.refresh_from_db()58 self.user2.refresh_from_db()59 self.assertEqual(self.user1.check_relationship(self.user2), NO_RELATIONSHIP)60 self.assertEqual(self.user2.check_relationship(self.user1), NO_RELATIONSHIP)61 def test_add_to_friends(self):62 client = Client()63 client.login(username=self.user1.username, password=self.password)64 response = client.post(reverse('friends:user_view', kwargs={'user_id': self.user2.id}), {'action': 'add'})65 self.assertEqual(response.status_code, 200)66 self.user1.refresh_from_db()67 self.user2.refresh_from_db()68 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT)69 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT)70 client.login(username=self.user2.username, password=self.password)71 response = client.post(reverse('friends:my_friends'), {'action': 'accept', 'user_id': self.user1.id})72 self.assertEqual(response.status_code, 200)73 self.assertEqual(response.json()['relationship_status'], RELATIONSHIP_FRIENDS)74 self.user1.refresh_from_db()75 self.user2.refresh_from_db()76 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_FRIENDS)77 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_FRIENDS)78 self.assertEqual(self.user1.get_friends()[0].username, self.user2.username)79 self.assertEqual(self.user2.get_friends()[0].username, self.user1.username)80 def test_remove_from_friends(self):81 client = Client()82 client.login(username=self.user1.username, password=self.password)83 response = client.post(reverse('friends:user_view', kwargs={'user_id': self.user2.id}), {'action': 'add'})84 self.assertEqual(response.status_code, 200)85 self.user1.refresh_from_db()86 self.user2.refresh_from_db()87 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT)88 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT)89 client.login(username=self.user2.username, password=self.password)90 response = client.post(reverse('friends:my_friends'), {'action': 'accept', 'user_id': self.user1.id})91 self.assertEqual(response.status_code, 200)92 self.assertEqual(response.json()['relationship_status'], RELATIONSHIP_FRIENDS)93 self.user1.refresh_from_db()94 self.user2.refresh_from_db()95 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_FRIENDS)96 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_FRIENDS)97 response = client.post(reverse('friends:my_friends'), {'action': 'cancel', 'user_id': self.user1.id})98 self.assertEqual(response.status_code, 200)99 self.assertEqual(response.json()['relationship_status'], NO_RELATIONSHIP)100 self.user1.refresh_from_db()101 self.user2.refresh_from_db()102 self.assertEqual(self.user1.check_relationship(self.user2), NO_RELATIONSHIP)...
metaprogramming.py
Source:metaprogramming.py
1from django.db import models2from copy import copy3from django.db.models.signals import post_save4class SerializerOptions(dict):5 """6 Options for API extensions7 """8 # Used in get_api_choices(), and _get_api_data()9 api_model = None10 # Each field named here is turned in to a @property, which calls the API11 # when accessed12 api_fields = []13 def __init__(self, klass_name, opts):14 """15 Set any options provided, replacing the default values16 """17 self._options_of = klass_name18 if opts:19 for key, value in opts.__dict__.iteritems():20 self[key] = value21 setattr(self, key, value)22 def __unicode__(self):23 return u"Options for %s" % self._options_of24class SerializerOptionsMetaclass(models.base.ModelBase):25 def __new__(mcs, name, bases, attrs):26 new = super(SerializerOptionsMetaclass, mcs).__new__(mcs, name, bases, attrs)27 opts = attrs.pop('SerializerMeta', None)28 setattr(new, '_ser_meta', SerializerOptions(name, opts))29 return new30class SerializableModel(models.Model):31 __metaclass__ = SerializerOptionsMetaclass32 class Meta:33 abstract = True34def is_db_expression(value):35 try:36 # django < 1.837 from django.db.models.expressions import ExpressionNode38 return isinstance(value, ExpressionNode)39 except ImportError:40 # django >= 1.8 (big refactoring in Lookup/Expressions/Transforms)41 from django.db.models.expressions import BaseExpression, Combinable42 return isinstance(value, (BaseExpression, Combinable))43# Adapted from http://stackoverflow.com/questions/110803/dirty-fields-in-django44class DirtyFieldsMixin(object):45 def __init__(self, *args, **kwargs):46 super(DirtyFieldsMixin, self).__init__(*args, **kwargs)47 post_save.connect(48 reset_state, sender=self.__class__,49 dispatch_uid='{name}-DirtyFieldsMixin-sweeper'.format(50 name=self.__class__.__name__))51 reset_state(sender=self.__class__, instance=self)52 def _as_dict(self, check_relationship):53 all_field = {}54 for field in self._meta.fields:55 if field.rel:56 if not check_relationship:57 continue58 field_value = getattr(self, field.attname)59 # If current field value is an expression, we are not evaluating it60 if is_db_expression(field_value):61 continue62 # Explanation of copy usage here :63 # https://github.com/smn/django-dirtyfields/commit/efd0286db8b874b5d6bd06c9e903b1a0c9cc6b0064 all_field[field.name] = copy(field.to_python(field_value))65 return all_field66 def get_dirty_fields(self, check_relationship=False):67 # check_relationship indicates whether we want to check for foreign keys68 # and one-to-one fields or ignore them69 new_state = self._as_dict(check_relationship)70 all_modify_field = {}71 for key, value in new_state.items():72 original_value = self._original_state[key]73 if value != original_value:74 all_modify_field[key] = original_value75 return all_modify_field76 def is_dirty(self, check_relationship=False):77 # in order to be dirty we need to have been saved at least once, so we78 # check for a primary key and we need our dirty fields to not be empty79 if not self.pk:80 return True81 return {} != self.get_dirty_fields(check_relationship=check_relationship)82def reset_state(sender, instance, **kwargs):83 # original state should hold all possible dirty fields to avoid84 # getting a `KeyError` when checking if a field is dirty or not...
test_models.py
Source:test_models.py
...15 self.user2.set_password('12345678')16 self.user2.save()17 def test_send_request_for_relationship(self):18 self.user1.accept(self.user2)19 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT)20 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT)21 def test_cancel_own_send_request_for_relationship(self):22 self.user1.accept(self.user2)23 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT)24 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT)25 self.user1.cancel(self.user2)26 self.assertEqual(self.user1.check_relationship(self.user2), NO_RELATIONSHIP)27 self.assertEqual(self.user2.check_relationship(self.user1), NO_RELATIONSHIP)28 def test_cancel_foreign_send_request_for_relationship(self):29 self.user1.accept(self.user2)30 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT)31 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT)32 self.user2.cancel(self.user1)33 self.assertEqual(self.user1.check_relationship(self.user2), NO_RELATIONSHIP)34 self.assertEqual(self.user2.check_relationship(self.user1), NO_RELATIONSHIP)35 def test_add_to_friends(self):36 self.user1.accept(self.user2)37 self.user2.accept(self.user1)38 self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_FRIENDS)39 self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_FRIENDS)40 self.assertEqual(self.user1.get_friends()[0].username, self.user2.username)41 self.assertEqual(self.user2.get_friends()[0].username, self.user1.username)42 def test_remove_from_friends(self):43 self.user1.accept(self.user2)44 self.user2.accept(self.user1)45 self.user1.cancel(self.user2)46 self.assertEqual(self.user1.check_relationship(self.user2), NO_RELATIONSHIP)47 self.assertEqual(self.user2.check_relationship(self.user1), NO_RELATIONSHIP)48class TestCaching(TestCase):49 def setUp(self):50 random_name = str(uuid1())51 self.user1 = User(username=random_name, email=random_name + '@m.ru')52 self.user1.set_password('12345678')53 self.user1.save()54 def test_get_size_caching_is_working(self):55 fake_size = 77756 cache.delete(self.user1.id)57 size = self.user1.get_size()58 cache.set(self.user1.id, fake_size)59 self.assertNotEqual(size, fake_size)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!