Best Python code snippet using autotest_python
UTWaitingTimer.py
Source:UTWaitingTimer.py
...16 self.session.set_max_retry(for_url='http://haijia.bjxueche.net/', max_retries=10)17 self.dut = Role.get('WaitingTimer')18 self.dut.load_properties(session=self.session)19 @httpretty.activate20 def test_get_server_time(self):21 httpretty.register_uri(httpretty.GET, 'http://haijia.bjxueche.net/',22 body='Mock, Post succeed',23 forcing_headers={'date': 'Fri, 26 Dec 2014 14:59:41 GMT'})24 loc_time = self.dut.get_server_time()25 self.assertEqual('2014-12-26 22:59:41+08:00', str(loc_time))26 @httpretty.activate27 def test_get_server_time_2(self):28 httpretty.register_uri(httpretty.GET, "http://haijia.bjxueche.net/",29 Date='Mon, 15 Dec 2014 16:58:19 GMT')30 loc_time = self.dut.get_server_time()31 self.assertEqual(loc_time.day, 16)32 self.assertEqual(loc_time.hour, 0)33 self.assertEqual(loc_time.minute, 58)34 httpretty.register_uri(httpretty.GET, "http://haijia.bjxueche.net/",35 Date='Mon, 15 Dec 2014 17:08:20 GMT')36 loc_time = self.dut.get_server_time()37 self.assertEqual(loc_time.day, 16)38 self.assertEqual(loc_time.hour, 1)39 self.assertEqual(loc_time.minute, 8)40 @httpretty.activate41 def test_get_server_time_with_retry(self):42 self.retry_cnt = 043 def _retry(request, uri, headers):44 if self.retry_cnt == 9:45 print('Round:{0}, Response'.format(self.retry_cnt))46 return 200, headers, 'Mock, Connection succeed'47 else:48 print('Round:{0}, Timeout'.format(self.retry_cnt))49 self.retry_cnt += 150 raise socket.timeout()51 httpretty.register_uri(httpretty.GET, 'http://haijia.bjxueche.net/',52 body=_retry,53 forcing_headers={'date': 'Fri, 26 Dec 2014 14:59:41 GMT'})54 loc_time = self.dut.get_server_time()55 self.assertEqual('2014-12-26 22:59:41+08:00', str(loc_time))56 self.assertEqual(self.retry_cnt, 9)57 def test_localize_date(self):58 fmt01 = self.dut.localize_date('Jan 01 2017')59 fmt02 = self.dut.localize_date('2017-01-01')60 fmt03 = self.dut.localize_date('20170101')61 self.assertEquals(fmt01, fmt02)62 self.assertEquals(fmt02, fmt03)63 def test_get_server_time_timeout_after_retry(self):64 URLsForHJ.connect = 'http://haijia.bjxueche.net:81'65 self.session.set_max_retry(for_url='http://haijia.bjxueche.net:81/', max_retries=5)66 def _test():67 self.dut.get_server_time()68 start_time = time.time()69 self.assertRaises(ConnectionError, _test)70 delta = time.time() - start_time71 print('Return after {0} seconds because of timeout '.format(delta))72 self.assertGreaterEqual(delta, 20*5)73 def offset_n_minutes(self, current_time, func_get_offset_minute):74 self.dut.debut_hour = current_time.hour + func_get_offset_minute(current_time.minute)//6075 self.dut.debut_minute = func_get_offset_minute(current_time.minute) % 6076 def test_get_book_time_today(self):77 """78 Don't set book date, DUT set it 7 days later automatically, and gives the wake-up time as 3 minutes later79 """80 loc_time = datetime.now()81 self.offset_n_minutes(loc_time, func_get_offset_minute=lambda e: e + 3)82 book_time = self.dut.get_debut_time(loc_time)83 self.assertEqual(book_time.day, loc_time.day)84 self.assertEqual(book_time.hour, self.dut.debut_hour)85 self.assertEqual(book_time.minute, self.dut.debut_minute)86 def test_get_book_time_today_set(self):87 """88 Set book date to 7 days later , and DUT gives the wake-up time as 3 minutes later89 """90 seven_days = timedelta(days=7)91 loc_time = datetime.now()92 self.offset_n_minutes(loc_time, func_get_offset_minute=lambda e: e + 3)93 self.dut.lesson_date = loc_time + seven_days94 book_time = self.dut.get_debut_time(loc_time)95 self.assertEqual(book_time.day, loc_time.day)96 self.assertEqual(book_time.hour, self.dut.debut_hour)97 self.assertEqual(book_time.minute, self.dut.debut_minute)98 def test_get_book_time_later_equal_tomorrow_set(self):99 """100 Set book date to 8 and 9 days later , and DUT gives the wake-up time as 3 minutes later at some day101 """102 for i in range(2):103 print('Round {0}'.format(i))104 eight_days = timedelta(days=8+i)105 loc_time = datetime.now()106 self.offset_n_minutes(loc_time, func_get_offset_minute=lambda e: e + 3)107 self.dut.lesson_date = loc_time + eight_days108 book_time = self.dut.get_debut_time(loc_time)109 wake_up_date = loc_time + timedelta(days=1+i)110 self.assertEqual(book_time.day, wake_up_date.day)111 self.assertEqual(book_time.hour, self.dut.debut_hour)112 self.assertEqual(book_time.minute, self.dut.debut_minute)113 def test_get_book_time_tomorrow(self):114 """115 Don't set book date, DUT set it 8 days later automatically,116 and gives the wake-up time at tomorrow117 """118 loc_time = datetime.now()119 self.offset_n_minutes(loc_time, func_get_offset_minute=lambda e: e - 3)120 book_time = self.dut.get_debut_time(loc_time)121 print('Wake up at {0}'.format(str(book_time)))122 wake_up_date = loc_time + timedelta(days=1)123 self.assertEqual(book_time.day, wake_up_date.day)124 self.assertEqual(book_time.hour, self.dut.debut_hour)125 self.assertEqual(book_time.minute, self.dut.debut_minute)126 def test_get_book_time_tomorrow_set(self):127 """128 Set book date to 1~8 days later , and DUT gives the wake-up time as tomorrow129 """130 for i in range(8):131 _7_8_days = timedelta(days=1+i)132 loc_time = datetime.now()133 self.offset_n_minutes(loc_time, func_get_offset_minute=lambda e: e - 3)134 self.dut.lesson_date = loc_time + _7_8_days135 print('Round {0}: want {1} in {2}'.format(i, str(self.dut.lesson_date), str(_7_8_days)))136 book_time = self.dut.get_debut_time(loc_time)137 print('Wake up at {0}'.format(str(book_time)))138 wake_up_date = loc_time + timedelta(days=1)139 self.assertEqual(book_time.day, wake_up_date.day)140 self.assertEqual(book_time.hour, self.dut.debut_hour)141 self.assertEqual(book_time.minute, self.dut.debut_minute)142 def test_get_book_time_later_than_tomorrow_set(self):143 """144 Set book date to 9~11 days later , and DUT gives the wake-up time as tomorrow145 """146 for i in range(3):147 _7_8_days = timedelta(days=9+i)148 loc_time = datetime.now()149 self.offset_n_minutes(loc_time, func_get_offset_minute=lambda e: e - 3)150 self.dut.lesson_date = loc_time + _7_8_days151 print('Round {0}: want {1} in {2}'.format(i, str(self.dut.lesson_date), str(_7_8_days)))152 book_time = self.dut.get_debut_time(loc_time)153 print('Wake up at {0}'.format(str(book_time)))154 wake_up_date = loc_time + timedelta(days=2+i)155 self.assertEqual(book_time.day, wake_up_date.day)156 self.assertEqual(book_time.hour, self.dut.debut_hour)157 self.assertEqual(book_time.minute, self.dut.debut_minute)158 def test_get_sleep_span_0(self):159 loc_time = self.dut.get_server_time()160 book_time = loc_time.replace(second=(loc_time.second + 30) % 60,161 minute=loc_time.minute + (loc_time.second + 30) // 60)162 self.assertEqual(0, self.dut.get_sleep_span(book_time, loc_time))163 def test_get_sleep_span_50s_tc0(self):164 loc_time = self.dut.get_server_time()165 book_time = loc_time.replace(minute=(loc_time.minute + 1) % 60,166 hour=loc_time.hour + (loc_time.minute + 1) // 60)167 self.assertLessEqual(self.dut.get_sleep_span(book_time, loc_time), 50)168 def test_get_sleep_span_50s_tc1(self):169 loc_time = self.dut.get_server_time()170 book_time = loc_time.replace(minute=(loc_time.minute + 3) % 60,171 hour=loc_time.hour + (loc_time.minute + 3) // 60)172 self.assertEqual(50, self.dut.get_sleep_span(book_time, loc_time))173 def test_get_sleep_span_10m_tc0(self):174 loc_time = self.dut.get_server_time()175 book_time = loc_time.replace(minute=(loc_time.minute + 10) % 60,176 hour=loc_time.hour + (loc_time.minute + 10) // 60)177 self.assertEqual(50, self.dut.get_sleep_span(book_time, loc_time))178 def test_get_sleep_span_10m_tc1(self):179 loc_time = self.dut.get_server_time()180 book_time = loc_time.replace(minute=(loc_time.minute + 15) % 60,181 hour=loc_time.hour + (loc_time.minute + 15) // 60)182 self.assertEqual(600, self.dut.get_sleep_span(book_time, loc_time))183 def test_get_sleep_span_10m_tc2(self):184 loc_time = self.dut.get_server_time()185 book_time = loc_time.replace(minute=(loc_time.minute + 11) % 60,186 hour=loc_time.hour + (loc_time.minute + 11) // 60)187 self.assertEqual(50, self.dut.get_sleep_span(book_time, loc_time))188 def test_get_book_date_auto(self):189 _7_days = timedelta(days=7)190 loc_time = datetime.now()191 book_date = self.dut.get_book_date(loc_time)192 expt_date = loc_time + _7_days193 self.assertEqual(expt_date.year, book_date.year)194 self.assertEqual(expt_date.month, book_date.month)195 self.assertEqual(expt_date.day, book_date.day)196 def test_get_book_date_set(self):197 _9_days = timedelta(days=9)198 loc_time = datetime.now()199 self.offset_n_minutes(loc_time, func_get_offset_minute=lambda e: e - 3)200 expt_date = loc_time + _9_days201 self.dut.lesson_date = expt_date202 book_date = self.dut.get_book_date(loc_time)203 self.assertEqual(expt_date.year, book_date.year)204 self.assertEqual(expt_date.month, book_date.month)205 self.assertEqual(expt_date.day, book_date.day)206 def to_test_run_common(self, func_get_book_minute, delay=None):207 server_time = self.dut.get_server_time()208 self.offset_n_minutes(server_time, func_get_book_minute)209 if delay is not None:210 time.sleep(delay)211 self.dut.run()212 return self.dut, server_time213 def test_run_tc0(self):214 dut, server_time = self.to_test_run_common(lambda e: e + 1)215 wait_end_time = dut.get_server_time()216 self.assertTrue(wait_end_time - server_time < timedelta(seconds=50))217 def test_run_tc1(self):218 dut, server_time = self.to_test_run_common(lambda e: e + 1, delay=10)219 wait_end_time = dut.get_server_time()220 self.assertTrue(wait_end_time - server_time < timedelta(seconds=50))221 def test_run_tc2(self):222 dut, server_time = self.to_test_run_common(lambda e: e + 3)223 wait_end_time = dut.get_server_time()224 self.assertTrue(wait_end_time - server_time > timedelta(seconds=100))225 def test_run_tc3(self):226 dut, server_time = self.to_test_run_common(lambda e: e + 4)227 wait_end_time = dut.get_server_time()228 self.assertGreaterEqual(wait_end_time - server_time, timedelta(minutes=2))229 def test_book_now_0(self):230 dut_book_now = BookNowTimer()231 dut_book_now.load_properties(session=self.session, lesson_date='Jan 1 2020')232 loc_time = dut_book_now.get_server_time()233 book_time = dut_book_now.run()234 wait_end_time = dut_book_now.get_server_time()...
gc_libt0.7000000000000001_pp1.0_fp1.0_0.py
Source:gc_libt0.7000000000000001_pp1.0_fp1.0_0.py
1import gc, weakref2from twisted.internet import defer, reactor, task3from twisted.python import log4from zope.interface import implements5from txampext.multiplex import MultiplexServerProtocol, MultiplexClientProtocol,\6 IAmpproxyMultiplexFactory7from txampext.sanity import AMPCommandFactory, AMPSanitizer8from txampext import amp9from twisted.internet.protocol import connectionDone10class SimplestProtocol(amp.AMP):11 def connectionMade(self):12 print "Called connectionMade"13 self.sendCommand('get_server_time', {})14 self.sendCommand('get_server_time', {})15 self.sendCommand('get_server_time', {})16 self.sendCommand('get_server_time', {})17 self.sendCommand('get_server_time', {})18 self.sendCommand('get_server_time', {})19 self.sendCommand('get_server_time', {})20 self.sendCommand('get_server_time', {})...
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!!