Best Python code snippet using tempest_python
settings.py
Source:settings.py  
1"""2Django settings for gui project.3Generated by 'django-admin startproject' using Django 1.8.4For more information on this file, see5https://docs.djangoproject.com/en/1.8/topics/settings/6For the full list of settings and their values, see7https://docs.djangoproject.com/en/1.8/ref/settings/8"""9# Build paths inside the project like this: os.path.join(BASE_DIR, ...)10import os11import sys12BASE_DIR = os.path.dirname(os.path.abspath(__file__))13sys.path.append(os.path.join(BASE_DIR, '..', '..', 'deploy'))14from sqs_ranking_spiders import QUEUES_LIST15from cache_layer import CACHE_QUEUES_LIST16# Quick-start development settings - unsuitable for production17# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/18# SECURITY WARNING: keep the secret key used in production secret!19SECRET_KEY = 'xjr=j=6i7_a4v$o!3sb043a2!m1j+zk#2b_f19va5q@zh5@s8s'20# SECURITY WARNING: don't run with debug turned on in production!21DEBUG = True22ALLOWED_HOSTS = ['localhost', '127.0.0.1']23LOGIN_URL = '/admin/'24SITE_ID = 125# Application definition26INSTALLED_APPS = (27    'django.contrib.admin',28    'django.contrib.auth',29    'django.contrib.contenttypes',30    'django.contrib.sessions',31    'django.contrib.messages',32    'django.contrib.staticfiles',33    'django.contrib.sites',34    'gui',35    'fcgi',36    'watchdog',37    'kill_servers',38    'sqs_stats',39    'reports',40)41MIDDLEWARE_CLASSES = (42    'django.contrib.sessions.middleware.SessionMiddleware',43    'django.middleware.common.CommonMiddleware',44    'django.middleware.csrf.CsrfViewMiddleware',45    'django.contrib.auth.middleware.AuthenticationMiddleware',46    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',47    'django.contrib.messages.middleware.MessageMiddleware',48    'django.middleware.clickjacking.XFrameOptionsMiddleware',49)50ROOT_URLCONF = 'urls'51TEMPLATES = [52    {53        'BACKEND': 'django.template.backends.django.DjangoTemplates',54        'DIRS': [os.path.join(BASE_DIR, 'templates'), ],55        'APP_DIRS': True,56        'OPTIONS': {57            'context_processors': [58                'django.template.context_processors.debug',59                'django.template.context_processors.request',60                'django.contrib.auth.context_processors.auth',61                'django.contrib.messages.context_processors.messages',62            ],63        },64    },65]66WSGI_APPLICATION = 'wsgi.application'67# Database68# https://docs.djangoproject.com/en/1.8/ref/settings/#databases69DATABASES = {70    'default': {71        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.72        'NAME': 'c38trillionmonkeys_com',                      # Or path to database file if using sqlite3.73        # The following settings are not used with sqlite3:74        'USER': 'root',75        'PASSWORD': 'L4f12v23Nh49IB8',76        'HOST': 'sqs-tools.cmuq9py90auz.us-east-1.rds.amazonaws.com',                      # Empty for localhost through domain sockets or           '127.0.0.1' for localhost through TCP.77        'PORT': '',                      # Set to empty string for default.78    }79}80# Internationalization81# https://docs.djangoproject.com/en/1.8/topics/i18n/82LANGUAGE_CODE = 'en-us'83TIME_ZONE = 'UTC'84USE_I18N = True85USE_L10N = True86USE_TZ = True87# Static files (CSS, JavaScript, Images)88# https://docs.djangoproject.com/en/1.8/howto/static-files/89STATIC_URL = '/static/'90STATIC_ROOT = os.path.join(BASE_DIR, 'static')91MEDIA_URL = '/media/'92MEDIA_ROOT = os.path.join(BASE_DIR, 'media')93REMOVE_FILES_OLDER_THAN = 60  # clear old data and log files, value in DAYS94# A sample logging configuration. The only tangible logging95# performed by this configuration is to send an email to96# the site admins on every HTTP 500 error when DEBUG=False.97# See http://docs.djangoproject.com/en/dev/topics/logging for98# more details on how to customize your logging configuration.99LOGGING = {100    'version': 1,101    'disable_existing_loggers': False,102    'handlers': {103        # Include the default Django email handler for errors104        # This is what you'd get without configuring logging at all.105        'mail_admins': {106            'class': 'django.utils.log.AdminEmailHandler',107            'level': 'ERROR',108             # But the emails are plain text by default - HTML is nicer109            'include_html': True,110        },111        # Log to a text file that can be rotated by logrotate112        'logfile': {113            'class': 'logging.handlers.WatchedFileHandler',114            'filename': os.path.join(BASE_DIR, 'django.errors'),115            'filters': []116        },117    },118    'loggers': {119        # Again, default Django configuration to email unhandled exceptions120        'django.request': {121            'handlers': ['logfile'],122            'level': 'ERROR',123            'propagate': True,124        },125        # Might as well log any errors anywhere else in Django126        'django': {127            'handlers': ['logfile'],128            'level': 'ERROR',129            'propagate': False,130        },131        # Your own app - this assumes all your logger names start with "myapp."132        'gui': {133            'handlers': ['logfile'],134            'level': 'WARNING', # Or maybe INFO or DEBUG135            'propagate': False136        },137    },138}139TEST_QUEUE = QUEUES_LIST['test']140TEST_CACHE_QUEUE = CACHE_QUEUES_LIST['test']141CACHE_MODELS_FILENAME = '/tmp/cache_models.pickle'142try:143    from local_settings import *  # noqa:F401144except ImportError:...models.py
Source:models.py  
1import os2import sys3from django.db import models4from django.core.urlresolvers import reverse_lazy5CWD = os.path.dirname(os.path.abspath(__file__))6sys.path.append(os.path.join(CWD,  '..', '..', '..',7                             'deploy'))8import settings9def get_data_filename(job):10    """ Returns local job filename relative to MEDIA """11    try:12        job = int(job)13    except TypeError:14        pass15    if not isinstance(job, int):16        job = job.pk17    return '/%s/data_file.csv' % job18def get_log_filename(job):19    """ Returns local job logs relative to MEDIA """20    try:21        job = int(job)22    except TypeError:23        pass24    if not isinstance(job, int):25        job = job.pk26    return '/%s/log.log' % job27def get_progress_filename(job):28    """ Returns local progress logs relative to MEDIA """29    try:30        job = int(job)31    except TypeError:32        pass33    if not isinstance(job, int):34        job = job.pk35    return '/%s/progress.progress' % job36class Job(models.Model):37    cache_choices = (38        ('no cache', 'no cache'), ('cache', 'cache')39    )40    _status_choices = [41        ('created', 'created'),42        ('pushed into sqs', 'pushed into sqs'),43        ('in progress', 'in progress'),44        ('finished', 'finished'),45        ('failed', 'failed')46    ]47    name = models.CharField(max_length=100, blank=True, null=True,48                            help_text='Optional, just for convenience')49    spider = models.CharField(max_length=100, choices=[])  # see gui/forms.py50    search_term = models.CharField(51        max_length=255, blank=True, null=True,52        help_text='Enter this OR product(s) URL below'53    )54    product_url = models.URLField(55        max_length=500, blank=True, null=True,56        help_text='Enter this OR search term above OR products URL below'57    )58    product_urls = models.CharField(59        max_length=1500, blank=True, null=True,60        help_text=('Enter this OR search term above OR product_url.'61                   ' Only for the CH+SC mode!')62    )63    quantity = models.IntegerField(64        blank=True, null=True, default=20,65        help_text='Leave blank for unlimited results (slow!)'66    )67    extra_cmd_args = models.TextField(68        max_length=300, blank=True, null=True,69        help_text="Extra command-line arguments, 1 per line. Example: enable_cache=1"70    )71    sc_ch_mode = models.BooleanField(72        default=False, help_text=('Run the spider in CH mode. Do not forget to'73                                  ' fill the Product UrlS field above.')74    )75    with_best_seller_ranking = models.BooleanField(76        default=False, help_text='For Walmart bestsellers matching')77    task_id = models.IntegerField(default=100000)78    server_name = models.CharField(max_length=100, default='test_server')79    branch_name = models.CharField(80        max_length=100, blank=True, null=True,81        help_text='Branch to use at the instance(s); leave blank for sc_production'82    )83    save_raw_pages = models.BooleanField(84        default=False, help_text='Upload raw cache to S3?')85    #load_raw_pages = models.DateField(  # DISABLED for now!86    #    blank=True, null=True, default=timezone.now().date(),87    #    help_text='Load raw cache from S3'88    #)89    mode = models.CharField(90        max_length=100, choices=cache_choices, default=cache_choices[0])91    created = models.DateTimeField(auto_now_add=True)92    finished = models.DateTimeField(blank=True, null=True)93    status = models.CharField(max_length=100, choices=_status_choices,94                              default='created')95    priority_choices = ['test', 'urgent', 'production', 'dev']96    priority = models.CharField(max_length=20, default='urgent', choices=[(c, c) for c in priority_choices])97    def searchterm_or_url(self):98        return ('SearchTerm [%s]' % self.search_term if self.search_term99                else 'URL')100    searchterm_or_url.short_description = 'Type'101    def view_as_image(self):102        # for url2screenshot spider103        if 'url2screenshot' in self.spider:104            return "<a href='%s' target='_blank'>Image</a>" % reverse_lazy(105                'view_base64_image', kwargs={'job': self.pk})106        return ''107    view_as_image.short_description = 'Image'108    view_as_image.allow_tags = True109    def get_input_queue(self):110        if self.priority not in self.priority_choices\111                or self.priority == 'test':  # default, test priority112            if self.mode == 'no cache':113                return settings.TEST_QUEUE114            elif self.mode == 'cache':115                return settings.TEST_CACHE_QUEUE116        if self.mode == 'no cache':117            return settings.QUEUES_LIST[self.priority]118        elif self.mode == 'cache':119            return settings.CACHE_QUEUES_LIST[self.priority]120class JobGrouperCache(models.Model):121    """ Group automatically incoming created jobs in a single piece122        (for products_url only). In other words, groups product_url into123        products_url124    """125    spider = models.CharField(max_length=100, db_index=True)126    product_url = models.URLField(max_length=500)127    extra_args = models.TextField(blank=True, null=True)  # for other args,JSON128    created = models.DateTimeField(auto_now_add=True, db_index=True,129                                   blank=True, null=True)130    def __unicode__(self):...test_image_cache_client.py
Source:test_image_cache_client.py  
...34            self.client.list_cache,35            'tempest.lib.common.rest_client.RestClient.get',36            fake_result,37            mock_args=['cache'])38    def test_cache_queue(self):39        self.check_service_client_function(40            self.client.cache_queue,41            'tempest.lib.common.rest_client.RestClient.put',42            {},43            status=202,44            image_id="e485aab9-0907-4973-921c-bb6da8a8fcf8")45    def test_cache_delete(self):46        fake_result = {}47        self.check_service_client_function(48            self.client.cache_delete,49            'tempest.lib.common.rest_client.RestClient.delete',50            fake_result, image_id="e485aab9-0907-4973-921c-bb6da8a8fcf8",51            status=204)52    def test_cache_clear_without_target(self):...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!!
