Best Python code snippet using gabbi_python
__init__.py
Source:__init__.py
1from __future__ import unicode_literals2import io3import os4import unittest5from django.core.management import call_command6from django.test import TestCase, modify_settings7settings_module = os.environ["DJANGO_SETTINGS_MODULE"]8class MigrateMixin:9 @unittest.skipIf(10 settings_module != "test_project.settings_pgsql",11 "Executing DDL statements while in a transaction on databases that can't perform a "12 "rollback is prohibited.",13 )14 def test_migrate(self):15 """Make sure migrations actually work"""16 # with open(os.devnull, "w") as nothing:17 # self.assertIsNone(call_command("migrate", "dadv", stdout=nothing))18 try:19 call_command("migrate")20 except Exception:21 self.assertTrue(False, "Migrations failed")22 else:23 self.assertTrue(True, "Migrations succeded")24class CommandOutputMixin:25 def get_command_output(self, cmd, *cmd_args, **cmd_options):26 file_obj = io.StringIO()27 cmd_options.update(stdout=file_obj)28 call_command(cmd, *cmd_args, **cmd_options)29 output = file_obj.getvalue()30 file_obj.close()31 return output32class MigrationsTesterBase(MigrateMixin, CommandOutputMixin):33 bool_match = "ALTER COLUMN \"is_functional\" SET DEFAULT 'False';"34 text_match = (35 'ALTER TABLE "dadv_testtextdefault" ALTER COLUMN "description" '36 "SET DEFAULT 'No description provided';"37 )38 charfield_match = (39 'ALTER TABLE "dadv_testhappypath" ALTER COLUMN "name" '40 "SET DEFAULT 'Happy path'"41 )42 date_match = 'ALTER TABLE "dadv_testhappypath" ALTER COLUMN "dob" SET DEFAULT \'1970-01-01\';'43 current_timestamp_match = (44 'ALTER TABLE "dadv_testhappypath" ALTER COLUMN "rebirth" SET DEFAULT now();'45 )46 current_date_match = (47 'ALTER TABLE "dadv_testhappypath" ALTER COLUMN "married" SET DEFAULT now();'48 )49 custom_column_match = 'ALTER TABLE "dadv_testcustomcolumnname" ALTER COLUMN "custom_field" SET DEFAULT \'False\';'50 def test_bool_default(self):51 actual = self.get_command_output("sqlmigrate", "dadv", "0001")52 self.assertIn(self.bool_match, actual)53 def test_text_default(self):54 """Make sure we can add defaults for text fields"""55 actual = self.get_command_output("sqlmigrate", "dadv", "0002")56 self.assertIn(self.text_match, actual)57 def test_charfield_default(self):58 """Make sure we can add defaults for char fields"""59 actual = self.get_command_output("sqlmigrate", "dadv", "0003")60 self.assertIn(self.charfield_match, actual)61 def test_default_date(self):62 """Make sure temporal values work"""63 actual = self.get_command_output("sqlmigrate", "dadv", "0004")64 self.assertIn(self.date_match, actual)65 def test_current_timestamp(self):66 """Make sure we can provide current timestamps as default"""67 actual = self.get_command_output("sqlmigrate", "dadv", "0004")68 self.assertIn(self.current_timestamp_match, actual)69 def test_current_date(self):70 """Make sure we can provide current dates as default"""71 actual = self.get_command_output("sqlmigrate", "dadv", "0004")72 self.assertIn(self.current_date_match, actual)73 def test_custom_column_name(self):74 """Make sure we can provide current dates as default"""75 actual = self.get_command_output("sqlmigrate", "dadv", "0005")76 self.assertIn(self.custom_column_match, actual)77@unittest.skipUnless(78 settings_module == "test_project.settings_pgsql",79 "PostgreSQL settings file not selected",80)81@modify_settings(INSTALLED_APPS={"append": "dadv.apps.DadvConfig"})82class MigrationsTesterPgSQL(TestCase, MigrationsTesterBase):83 pass84@unittest.skipUnless(85 settings_module == "test_project.settings_crdb",86 "CockroachDB settings file not selected",87)88@modify_settings(INSTALLED_APPS={"append": "dadv.apps.DadvConfig"})89class MigrationsTesterCRDB(TestCase, MigrationsTesterBase):90 pass91@unittest.skipUnless(92 settings_module == "test_project.settings_mysql", "MySQL settings file not selected"93)94@modify_settings(INSTALLED_APPS={"append": "dadv.apps.DadvConfig"})95class MigrationsTesterMySQL(TestCase, MigrationsTesterBase):96 bool_match = "ALTER COLUMN `is_functional` SET DEFAULT '0';"97 charfield_match = (98 "ALTER TABLE `dadv_testhappypath` ALTER COLUMN `name` SET DEFAULT 'Happy path';"99 )100 date_match = (101 "ALTER TABLE `dadv_testhappypath` ALTER COLUMN `dob` SET DEFAULT '1970-01-01';"102 )103 current_timestamp_match = (104 "ALTER TABLE `dadv_testhappypath` ALTER COLUMN `rebirth` SET DEFAULT "105 "CURRENT_TIMESTAMP;"106 )107 custom_column_match = "ALTER TABLE `dadv_testcustomcolumnname` ALTER COLUMN `custom_field` SET DEFAULT '0';"108 @unittest.expectedFailure109 def test_text_default(self):110 super(MigrationsTesterMySQL, self).test_text_default()111 @unittest.expectedFailure112 def test_current_date(self):113 super(MigrationsTesterMySQL, self).test_current_date()114@unittest.skipUnless(115 settings_module == "test_project.settings_mssql",116 "Microsoft SQL Server settings file not selected",117)118@modify_settings(INSTALLED_APPS={"append": "dadv.apps.DadvConfig"})119class MigrationsTesterMicrosoftSQL(TestCase, MigrationsTesterBase):120 bool_match = (121 "ALTER TABLE [dadv_testbooldefault] "122 "ADD CONSTRAINT [DADV_TestBoolDefault_is_functional_DEFAULT] "123 "DEFAULT '0' FOR [is_functional];"124 )125 charfield_match = (126 "ALTER TABLE [dadv_testhappypath] ADD CONSTRAINT [DADV_TestHappyPath_name_DEFAULT] "127 "DEFAULT 'Happy path' FOR [name];"128 )129 text_match = (130 "ALTER TABLE [dadv_testtextdefault] ADD CONSTRAINT ["131 "DADV_TestTextDefault_description_DEFAULT] DEFAULT 'No description provided' FOR ["132 "description];"133 )134 date_match = (135 "ALTER TABLE [dadv_testhappypath] "136 "ADD CONSTRAINT [DADV_testhappypath_dob_DEFAULT] DEFAULT '1970-01-01' FOR [dob];"137 )138 current_date_match = (139 "ALTER TABLE [dadv_testhappypath] "140 "ADD CONSTRAINT [DADV_testhappypath_married_DEFAULT] "141 "DEFAULT GETDATE() "142 "FOR [married];"143 )144 current_timestamp_match = (145 "ALTER TABLE [dadv_testhappypath] ADD CONSTRAINT [DADV_testhappypath_rebirth_DEFAULT] "146 "DEFAULT GETDATE() FOR [rebirth];"...
0004_time_related_fields.py
Source:0004_time_related_fields.py
1# Generated by Django 2.0.8 on 2018-08-12 18:232import datetime3from django.db import migrations, models4import django.utils.timezone5from django_add_default_value import AddDefaultValue, NOW, TODAY6class Migration(migrations.Migration):7 dependencies = [("dadv", "0003_testhappypath")]8 operations = [9 migrations.AddField(10 model_name="testhappypath",11 name="dob",12 field=models.DateField(default=datetime.date(1970, 1, 1)),13 ),14 migrations.AddField(15 model_name="testhappypath",16 name="rebirth",17 field=models.DateTimeField(default=django.utils.timezone.now),18 ),19 migrations.AddField(20 model_name="testhappypath",21 name="married",22 field=models.DateField(default=datetime.date.today),23 ),24 AddDefaultValue(25 model_name="testhappypath", name="dob", value=datetime.date(1970, 1, 1)26 ),27 AddDefaultValue(model_name="testhappypath", name="rebirth", value=NOW),28 AddDefaultValue(model_name="testhappypath", name="married", value=TODAY),...
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!!