How to use database method in hypothesis

Best Python code snippet using hypothesis

models.py

Source:models.py Github

copy

Full Screen

1'''2Each class in this file serves as an object relational map for the tables 3in the underlying database. This is done through the use of Flask-SQLAlchemy.4In general, each class function serves only to initialize the object (analogous5to an entry into or already existing in the database) and to represent the object6as a string. The exceptions to this rule can be found in the Account class which7contains a number of getter and setter methods.8'''9from handlers.Database import database10 11class Account (database.db.Model):12 id = database.db.Column(database.db.Integer, primary_key=True)13 pword = database.db.Column(database.db.String(255), nullable=False)14 q1_id = database.db.Column(database.db.Integer, database.db.ForeignKey('question.id',ondelete='RESTRICT',onupdate='RESTRICT'), nullable=False)15 q2_id = database.db.Column(database.db.Integer, database.db.ForeignKey('question.id',ondelete='RESTRICT',onupdate='RESTRICT'), nullable=False)16 answer1 = database.db.Column(database.db.Text, nullable=False)17 answer2 = database.db.Column(database.db.Text, nullable=False)18 created = database.db.Column(database.db.DateTime, nullable=False)19 authenticated = database.db.Column(database.db.Boolean, default=False)20 code = database.db.Column(database.db.Integer,default=None,nullable=True)21 22 admin = database.db.relationship('Admin', backref='account',uselist=False,lazy='joined',cascade='all, delete-orphan')23 manager = database.db.relationship('Manager', backref='account', uselist=False, lazy='joined',cascade='all, delete-orphan')24 q1 = database.db.relationship('Question',foreign_keys=[q1_id])25 q2 = database.db.relationship('Question',foreign_keys=[q2_id])26 27 def __init__(self,pword,q1,q2,a1,a2,created):28 self.pword = pword29 self.q1_id = q130 self.q2_id = q231 self.answer1 = a132 self.answer2 = a233 self.created = created34 def __repr__(self):35 return '<Account {0}>'.format(self.id)36 37 def is_active(self):38 return True39 def get_id(self):40 return self.id41 def is_authenticated(self):42 return self.authenticated43 def is_anonymous(self):44 return False45class Question(database.db.Model):46 id = database.db.Column(database.db.Integer, primary_key=True)47 prompt = database.db.Column(database.db.String(255), nullable=False)48 49 def __init__(self,prompt):50 self.prompt = prompt51 52 def __repr__(self):53 return '{0}'.format(self.prompt) 54 55class Admin (database.db.Model):56 id = database.db.Column(database.db.Integer, primary_key=True)57 account_id = database.db.Column(database.db.Integer, database.db.ForeignKey('account.id',ondelete='CASCADE',onupdate='RESTRICT'), nullable=False)58 email = database.db.Column(database.db.String(32), nullable=False, unique=True)59 fname = database.db.Column(database.db.String(32), nullable=False)60 lname = database.db.Column(database.db.String(32), nullable=False)61 62 def __init__(self,account,email,fname,lname):63 self.account = account64 self.email = email65 self.fname = fname66 self.lname = lname67 68 def __repr__(self):69 return '<Admin {0} {1}>'.format(self.fname,self.lname)70class Manager (database.db.Model):71 id = database.db.Column(database.db.Integer, primary_key=True)72 account_id = database.db.Column(database.db.Integer, database.db.ForeignKey('account.id',ondelete='CASCADE',onupdate='RESTRICT'), nullable=False)73 title = database.db.Column(database.db.String(32), nullable=False)74 fname = database.db.Column(database.db.String(32), nullable=False)75 lname = database.db.Column(database.db.String(32), nullable=False)76 signature = database.db.Column(database.db.Text, nullable=False)77 email = database.db.Column(database.db.String(32), nullable=False, unique=True)78 79 createdBy = database.db.relationship('Award', backref='manager',lazy='dynamic')80 81 def __init__(self,account,title,fname,lname,signature,email):82 self.account = account83 self.title = title84 self.fname = fname85 self.lname = lname86 self.signature = signature87 self.email = email88 def __repr__(self):89 return '<Manager {0}>'.format(self.fname + ' ' + self.lname)90class AwardType (database.db.Model):91 id = database.db.Column(database.db.Integer, primary_key=True)92 name = database.db.Column(database.db.String(32),nullable=False)93 type = database.db.relationship('Award', backref='award_type', lazy='dynamic')94 def __init__(self,name):95 self.name = name96 def __repr__(self):97 return '<AwardType {0}>'.format(self.name)98class Award (database.db.Model):99 id = database.db.Column(database.db.Integer, primary_key=True)100 creator = database.db.Column(database.db.Integer, database.db.ForeignKey('manager.id',ondelete='SET NULL',onupdate='RESTRICT'),nullable=True)101 type_id = database.db.Column(database.db.Integer, database.db.ForeignKey('award_type.id',ondelete='RESTRICT',onupdate='RESTRICT'),nullable=False)102 message = database.db.Column(database.db.String(255),nullable=False)103 issuedOn = database.db.Column(database.db.Date,nullable=False)104 recipient = database.db.Column(database.db.Integer,database.db.ForeignKey('employee.id',ondelete='SET NULL',onupdate='RESTRICT'),nullable=True)105 background_id = database.db.Column(database.db.Integer,database.db.ForeignKey('award_background.id',ondelete='RESTRICT',onupdate='RESTRICT'),nullable=False)106 theme_id = database.db.Column(database.db.Integer,database.db.ForeignKey('award_theme.id',ondelete='RESTRICT',onupdate='RESTRICT'),nullable=False)107 border_id = database.db.Column(database.db.Integer,database.db.ForeignKey('award_border.id',ondelete='RESTRICT',onupdate='RESTRICT'),nullable=False)108 109 def __init__(self,creator,typeId,message,issuedOn,recipient,background,theme,border):110 self.creator = creator111 self.type_id = typeId112 self.message = message113 self.issuedOn = issuedOn114 self.recipient = recipient115 self.background_id = background116 self.theme_id = theme117 self.border_id = border118 def __repr__(self):119 return '<Award {0} {1} {2}>'.format(self.creator,self.type_id,self.message)120 def check_row(self):121 if self.creator is None and self.recipient is None:122 return True123 124class AwardBackground(database.db.Model):125 id = database.db.Column(database.db.Integer, primary_key=True)126 filename = database.db.Column(database.db.String(32), nullable=False)127 128 background = database.db.relationship('Award', backref='award_background', lazy='dynamic')129 130 def __init__(self,filename):131 self.filename = filename132 133 def __repr__(self):134 return '<AwardBackground {0}>'.format(self.filename)135class AwardTheme(database.db.Model):136 id = database.db.Column(database.db.Integer, primary_key=True)137 theme = database.db.Column(database.db.String(32), nullable=False)138 139 color = database.db.relationship('Award', backref='award_theme', lazy='dynamic')140 141 def __init__(self,theme):142 self.theme = theme143 144 def __repr__(self):145 return '<AwardTheme {0}>'.format(self.theme)146class AwardBorder(database.db.Model):147 id = database.db.Column(database.db.Integer, primary_key=True)148 filename = database.db.Column(database.db.String(32), nullable=False)149 150 border = database.db.relationship('Award', backref='award_border', lazy='dynamic')151 152 def __init__(self,filename):153 self.filename = filename154 155 def __repr__(self):156 return '<AwardBorder {0}>'.format(self.filename)157 158class Employee(database.db.Model):159 id = database.db.Column(database.db.Integer,primary_key=True)160 fname = database.db.Column(database.db.String(32),nullable=False)161 lname = database.db.Column(database.db.String(32),nullable=False)162 email = database.db.Column(database.db.String(32),nullable=False,unique=True)163 164 award = database.db.relationship('Award', backref='employee', lazy='dynamic')165 166 def __init__(self,fname,lname,email):167 self.fname = fname168 self.lname = lname169 self.email = email170 171 def __repr__(self):172 return '<Employee {0} {1} {2}>'.format(self.fname,self.lname,self.email)173 174 ...

Full Screen

Full Screen

creation.py

Source:creation.py Github

copy

Full Screen

1import os2import shutil3import sys4from pathlib import Path5from django.db.backends.base.creation import BaseDatabaseCreation6class DatabaseCreation(BaseDatabaseCreation):7 @staticmethod8 def is_in_memory_db(database_name):9 return not isinstance(database_name, Path) and (10 database_name == ':memory:' or 'mode=memory' in database_name11 )12 def _get_test_db_name(self):13 test_database_name = self.connection.settings_dict['TEST']['NAME'] or ':memory:'14 if test_database_name == ':memory:':15 return 'file:memorydb_%s?mode=memory&cache=shared' % self.connection.alias16 return test_database_name17 def _create_test_db(self, verbosity, autoclobber, keepdb=False):18 test_database_name = self._get_test_db_name()19 if keepdb:20 return test_database_name21 if not self.is_in_memory_db(test_database_name):22 # Erase the old test database23 if verbosity >= 1:24 self.log('Destroying old test database for alias %s...' % (25 self._get_database_display_str(verbosity, test_database_name),26 ))27 if os.access(test_database_name, os.F_OK):28 if not autoclobber:29 confirm = input(30 "Type 'yes' if you would like to try deleting the test "31 "database '%s', or 'no' to cancel: " % test_database_name32 )33 if autoclobber or confirm == 'yes':34 try:35 os.remove(test_database_name)36 except Exception as e:37 self.log('Got an error deleting the old test database: %s' % e)38 sys.exit(2)39 else:40 self.log('Tests cancelled.')41 sys.exit(1)42 return test_database_name43 def get_test_db_clone_settings(self, suffix):44 orig_settings_dict = self.connection.settings_dict45 source_database_name = orig_settings_dict['NAME']46 if self.is_in_memory_db(source_database_name):47 return orig_settings_dict48 else:49 root, ext = os.path.splitext(orig_settings_dict['NAME'])50 return {**orig_settings_dict, 'NAME': '{}_{}.{}'.format(root, suffix, ext)}51 def _clone_test_db(self, suffix, verbosity, keepdb=False):52 source_database_name = self.connection.settings_dict['NAME']53 target_database_name = self.get_test_db_clone_settings(suffix)['NAME']54 # Forking automatically makes a copy of an in-memory database.55 if not self.is_in_memory_db(source_database_name):56 # Erase the old test database57 if os.access(target_database_name, os.F_OK):58 if keepdb:59 return60 if verbosity >= 1:61 self.log('Destroying old test database for alias %s...' % (62 self._get_database_display_str(verbosity, target_database_name),63 ))64 try:65 os.remove(target_database_name)66 except Exception as e:67 self.log('Got an error deleting the old test database: %s' % e)68 sys.exit(2)69 try:70 shutil.copy(source_database_name, target_database_name)71 except Exception as e:72 self.log('Got an error cloning the test database: %s' % e)73 sys.exit(2)74 def _destroy_test_db(self, test_database_name, verbosity):75 if test_database_name and not self.is_in_memory_db(test_database_name):76 # Remove the SQLite database file77 os.remove(test_database_name)78 def test_db_signature(self):79 """80 Return a tuple that uniquely identifies a test database.81 This takes into account the special cases of ":memory:" and "" for82 SQLite since the databases will be distinct despite having the same83 TEST NAME. See https://www.sqlite.org/inmemorydb.html84 """85 test_database_name = self._get_test_db_name()86 sig = [self.connection.settings_dict['NAME']]87 if self.is_in_memory_db(test_database_name):88 sig.append(self.connection.alias)89 else:90 sig.append(test_database_name)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful