How to use __enter__ method in gabbi

Best Python code snippet using gabbi_python

test_database_interaction.py

Source:test_database_interaction.py Github

copy

Full Screen

...24 Test Purpose: Tests that an error is thrown if a bad query is passed.25 """26 query_string = 'bad QuEry 5$3'27 mock_connect = mocker.patch('psycopg2.connect')28 mock_connect().__enter__().cursor().__enter__().fetchall.side_effect = ProgrammingError()29 with pytest.raises(ProgrammingError):30 self.database.select(query_string)31 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)32 mock_connect().__enter__().cursor().__enter__().fetchall.assert_called()33 mock_connect().__enter__().close.assert_not_called()34 def test_select_no_rows_found(self, mocker):35 """36 Test Type: Unit37 Test Purpose: Tests that an empty list is returned if a query results with no rows38 """39 query_string = 'SELECT * FROM table;'40 expected = []41 mock_connect = mocker.patch('psycopg2.connect')42 mock_connect().__enter__().cursor().__enter__().fetchall.return_value = expected43 assert self.database.select(query_string) == expected44 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)45 mock_connect().__enter__().cursor().__enter__().fetchall.assert_called()46 mock_connect().__enter__().close.assert_called()47 def test_select_singleton(self, mocker):48 """49 Test Type: Unit50 Test Purpose: Tests that a singleton list is returned if a query results with 1 row51 """52 query_string = 'SELECT * FROM table;'53 expected = [('test1', 'test2', 'test3')]54 mock_connect = mocker.patch('psycopg2.connect')55 mock_connect().__enter__().cursor().__enter__().fetchall.return_value = expected56 assert self.database.select(query_string) == expected57 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)58 mock_connect().__enter__().cursor().__enter__().fetchall.assert_called()59 mock_connect().__enter__().close.assert_called()60 def test_select_multiple_rows(self, mocker):61 """62 Test Type: Unit63 Test Purpose: Tests that the number of rows that the query finds has is returned by select64 """65 query_string = 'SELECT * FROM table;'66 expected = [('test1', 'test2', 'test3'), ('test1', 'test2', 'test3'), ('test1', 'test2', 'test3')]67 mock_connect = mocker.patch('psycopg2.connect')68 mock_connect().__enter__().cursor().__enter__().fetchall.return_value = expected69 assert self.database.select(query_string) == expected70 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)71 mock_connect().__enter__().cursor().__enter__().fetchall.assert_called()72 mock_connect().__enter__().close.assert_called()73 def test_insert_connection_error(self, mocker):74 """75 Test Type: Unit76 Test Purpose: Tests that an error is thrown if a the database connection fails.77 """78 query_string = 'bad QuEry 5$3'79 mock_connect = mocker.patch('psycopg2.connect')80 mock_connect.side_effect = DatabaseError81 with pytest.raises(DatabaseError):82 self.database.insert(query_string)83 def test_insert_error_query(self, mocker):84 """85 Test Type: Unit86 Test Purpose: Tests that an error is thrown if a bad query is passed.87 """88 query_string = 'bad QuEry 5$3'89 mock_connect = mocker.patch('psycopg2.connect')90 mock_connect().__enter__().cursor().__enter__().execute.side_effect = ProgrammingError()91 with pytest.raises(ProgrammingError):92 self.database.insert(query_string)93 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)94 mock_connect().__enter__().commit.assert_not_called()95 mock_connect().__enter__().close.assert_not_called()96 def test_insert_success(self, mocker):97 """98 Test Type: Unit99 Test Purpose: Tests a successful insert statement.100 """101 query_string = "INSERT INTO table ('test') VALUES ('test');"102 mock_connect = mocker.patch('psycopg2.connect')103 self.database.insert(query_string)104 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)105 mock_connect().__enter__().commit.assert_called()106 mock_connect().__enter__().close.assert_called()107 def test_update_connection_error(self, mocker):108 """109 Test Type: Unit110 Test Purpose: Tests that an error is thrown if a the database connection fails.111 """112 query_string = 'bad QuEry 5$3'113 mock_connect = mocker.patch('psycopg2.connect')114 mock_connect.side_effect = DatabaseError115 with pytest.raises(DatabaseError):116 self.database.update(query_string)117 def test_update_error_query(self, mocker):118 """119 Test Type: Unit120 Test Purpose: Tests that an error is thrown if a bad query is passed.121 """122 query_string = 'bad QuEry 5$3'123 mock_connect = mocker.patch('psycopg2.connect')124 mock_connect().__enter__().cursor().__enter__().execute.side_effect = ProgrammingError()125 with pytest.raises(ProgrammingError):126 self.database.update(query_string)127 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)128 mock_connect().__enter__().commit.assert_not_called()129 mock_connect().__enter__().close.assert_not_called()130 def test_update_success(self, mocker):131 """132 Test Type: Unit133 Test Purpose: Tests a successful update statement.134 """135 query_string = "UPDATE table SET 'test'='this_will_work' WHERE table.testing = 'true';"136 mock_connect = mocker.patch('psycopg2.connect')137 self.database.update(query_string)138 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)139 mock_connect().__enter__().commit.assert_called()140 mock_connect().__enter__().close.assert_called()141 def test_modify_data_connection_error(self, mocker):142 """143 Test Type: Unit144 Test Purpose: Tests that an error is thrown if a the database connection fails.145 """146 query_string = 'bad QuEry 5$3'147 mock_connect = mocker.patch('psycopg2.connect')148 mock_connect.side_effect = DatabaseError149 with pytest.raises(DatabaseError):150 self.database._modify_data(query_string)151 def test_modify_data_error_query(self, mocker):152 """153 Test Type: Unit154 Test Purpose: Tests that an error is thrown if a bad query is passed.155 """156 query_string = 'bad QuEry 5$3'157 mock_connect = mocker.patch('psycopg2.connect')158 mock_connect().__enter__().cursor().__enter__().execute.side_effect = ProgrammingError()159 with pytest.raises(ProgrammingError):160 self.database._modify_data(query_string)161 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)162 mock_connect().__enter__().cursor().__enter__().fetchone.assert_not_called()163 mock_connect().__enter__().commit.assert_not_called()164 mock_connect().__enter__().close.assert_not_called()165 def test_modify_data_success_no_return(self, mocker):166 """167 Test Type: Unit168 Test Purpose: Tests a successful update statement without returning value.169 """170 query_string = "UPDATE table SET 'test'='this_will_work' WHERE table.testing = 'true';"171 mock_connect = mocker.patch('psycopg2.connect')172 result = self.database._modify_data(query_string)173 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)174 mock_connect().__enter__().cursor().__enter__().fetchone.assert_not_called()175 mock_connect().__enter__().commit.assert_called()176 mock_connect().__enter__().close.assert_called()177 assert result is None178 def test_modify_data_success_return(self, mocker):179 """180 Test Type: Unit181 Test Purpose: Tests a successful modify data statement with returning value.182 """183 expected = ('update', 'results')184 query_string = "UPDATE table SET 'test'='this_will_work' WHERE table.testing = 'true';"185 mock_connect = mocker.patch('psycopg2.connect')186 mock_connect().__enter__().cursor().__enter__().fetchone.return_value = [expected]187 result = self.database._modify_data(query_string, True)188 mock_connect().__enter__().cursor().__enter__().execute.assert_called_with(query_string)189 mock_connect().__enter__().cursor().__enter__().fetchone.assert_called()190 mock_connect().__enter__().commit.assert_called()191 mock_connect().__enter__().close.assert_called()192 assert result == expected193 def test_insert_data_from_file_connection_error(self, mocker):194 """195 Test Type: Unit196 Test Purpose: Tests that an error is thrown if a the database connection fails.197 """198 mock_connect = mocker.patch('psycopg2.connect')199 mock_connect.side_effect = DatabaseError200 with pytest.raises(DatabaseError):201 self.database.insert_data_from_file("table", ("column_name"), BytesIO(b"test\nfile"), ",")202 def test_insert_data_from_file_bad_table(self, mocker):203 """204 Test Type: Unit205 Test Purpose: Tests that an error is thrown if a bad table is passed.206 """207 mock_file = BytesIO(b"test\nfile")208 mock_connect = mocker.patch('psycopg2.connect')209 mock_connect().__enter__().cursor().__enter__().copy_from.side_effect = ProgrammingError()210 with pytest.raises(ProgrammingError):211 self.database.insert_data_from_file("bad_table", ("col"), mock_file, ",")212 mock_connect().__enter__().cursor().__enter__().copy_from.assert_called_with(213 mock_file, "bad_table", sep=",", columns=("col")214 )215 mock_connect().__enter__().close.assert_not_called()216 def test_insert_data_from_file_bad_columns(self, mocker):217 """218 Test Type: Unit219 Test Purpose: Tests that an error is thrown if a bad column is passed.220 """221 mock_file = BytesIO(b"test\nfile")222 mock_connect = mocker.patch('psycopg2.connect')223 mock_connect().__enter__().cursor().__enter__().copy_from.side_effect = ProgrammingError()224 with pytest.raises(ProgrammingError):225 self.database.insert_data_from_file("table", ("col", "bad_column"), mock_file, ",")226 mock_connect().__enter__().cursor().__enter__().copy_from.assert_called_with(227 mock_file, "table", sep=",", columns=("col", "bad_column")228 )229 mock_connect().__enter__().close.assert_not_called()230 def test_insert_data_from_file_bad_file(self, mocker):231 """232 Test Type: Unit233 Test Purpose: Tests that an error is thrown if a bad file is passed.234 """235 mock_file = BytesIO(b"test, test2\nfile")236 mock_connect = mocker.patch('psycopg2.connect')237 mock_connect().__enter__().cursor().__enter__().copy_from.side_effect = ProgrammingError()238 with pytest.raises(ProgrammingError):239 self.database.insert_data_from_file("table", ("col"), mock_file, ",")240 mock_connect().__enter__().cursor().__enter__().copy_from.assert_called_with(241 mock_file, "table", sep=",", columns=("col")242 )243 mock_connect().__enter__().close.assert_not_called()244 def test_insert_data_from_file_success(self, mocker):245 """246 Test Type: Unit247 Test Purpose: Tests a successful data insertion from a file statement.248 """249 mock_connect = mocker.patch('psycopg2.connect')250 mock_file = BytesIO(b"test\nfile")251 self.database.insert_data_from_file("table", ("col"), mock_file, ",")252 mock_connect().__enter__().cursor().__enter__().copy_from.assert_called_with(253 mock_file, "table", sep=",", columns=("col")254 )...

Full Screen

Full Screen

With.py

Source:With.py Github

copy

Full Screen

...7 operations = 208 rounds = 80000910 class ContextManager(object):11 def __enter__(self):12 pass13 def __exit__(self, exc, val, tb):14 pass1516 def test(self):1718 cm = self.ContextManager()1920 for i in xrange(self.rounds):21 with cm: pass22 with cm: pass23 with cm: pass24 with cm: pass25 with cm: pass26 with cm: pass27 with cm: pass28 with cm: pass29 with cm: pass30 with cm: pass31 with cm: pass32 with cm: pass33 with cm: pass34 with cm: pass35 with cm: pass36 with cm: pass37 with cm: pass38 with cm: pass39 with cm: pass40 with cm: pass4142 def calibrate(self):4344 cm = self.ContextManager()4546 for i in xrange(self.rounds):47 pass484950class TryFinally(Test):5152 version = 2.053 operations = 2054 rounds = 800005556 class ContextManager(object):57 def __enter__(self):58 pass59 def __exit__(self):60 # "Context manager" objects used just for their cleanup61 # actions in finally blocks usually don't have parameters.62 pass6364 def test(self):6566 cm = self.ContextManager()6768 for i in xrange(self.rounds):69 cm.__enter__()70 try: pass71 finally: cm.__exit__()7273 cm.__enter__()74 try: pass75 finally: cm.__exit__()7677 cm.__enter__()78 try: pass79 finally: cm.__exit__()8081 cm.__enter__()82 try: pass83 finally: cm.__exit__()8485 cm.__enter__()86 try: pass87 finally: cm.__exit__()8889 cm.__enter__()90 try: pass91 finally: cm.__exit__()9293 cm.__enter__()94 try: pass95 finally: cm.__exit__()9697 cm.__enter__()98 try: pass99 finally: cm.__exit__()100101 cm.__enter__()102 try: pass103 finally: cm.__exit__()104105 cm.__enter__()106 try: pass107 finally: cm.__exit__()108109 cm.__enter__()110 try: pass111 finally: cm.__exit__()112113 cm.__enter__()114 try: pass115 finally: cm.__exit__()116117 cm.__enter__()118 try: pass119 finally: cm.__exit__()120121 cm.__enter__()122 try: pass123 finally: cm.__exit__()124125 cm.__enter__()126 try: pass127 finally: cm.__exit__()128129 cm.__enter__()130 try: pass131 finally: cm.__exit__()132133 cm.__enter__()134 try: pass135 finally: cm.__exit__()136137 cm.__enter__()138 try: pass139 finally: cm.__exit__()140141 cm.__enter__()142 try: pass143 finally: cm.__exit__()144145 cm.__enter__()146 try: pass147 finally: cm.__exit__()148149 def calibrate(self):150151 cm = self.ContextManager()152153 for i in xrange(self.rounds):154 pass155156157class WithRaiseExcept(Test):158159 version = 2.0160 operations = 2 + 3 + 3161 rounds = 100000162163 class BlockExceptions(object):164 def __enter__(self):165 pass166 def __exit__(self, exc, val, tb):167 return True168169 def test(self):170171 error = ValueError172 be = self.BlockExceptions()173174 for i in xrange(self.rounds):175 with be: raise error176 with be: raise error177 with be: raise error,"something"178 with be: raise error,"something" ...

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 gabbi 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