Best Python code snippet using fMBT_python
tests.py
Source:tests.py  
1from django.test import TestCase, Client, TransactionTestCase2from wechat.models import *3from django.template.loader import get_template4from .wrapper import WeChatView5import xmltodict6from WeChatTicket.settings import *7import json8from django.utils import timezone9from datetime import timedelta10class UnBindTest(TestCase):11    @classmethod12    def setUpTestData(cls):13        cls.open_id_1 = "asdfghjjl"14        cls.student_id_1 = "2016014234"15        cls.open_id_2 = "qwertyuiop"16        User.objects.create(open_id=cls.open_id_1, student_id=cls.student_id_1)17        User.objects.create(open_id=cls.open_id_2)18        cls.textMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'Content': 'è§£ç»'}19    def setUp(self):20        self.client = Client()21    def test_unbind(self):22        # test user who has bind23        msg = get_template('sendtext.xml').render(self.textMsg)24        response = self.client.post('/wechat', msg, content_type='application/xml')25        self.assertEqual(response.status_code, 200)26        content = xmltodict.parse(response.content)['xml']['Content'][0:9]27        answer = 'å¦å·ç»å®å·²ç»è§£é¤ã'28        self.assertEqual(content, answer)29        # test user unbind30        msg = get_template('sendtext.xml').render(self.textMsg)31        response = self.client.post('/wechat', msg, content_type='application/xml')32        self.assertEqual(response.status_code, 200)33        content = xmltodict.parse(response.content)['xml']['Content'][0:9]34        answer = 'å¦å·ç»å®å·²ç»è§£é¤ã'35        self.assertEqual(content, answer)36class BindTest(TestCase):37    @classmethod38    def setUpTestData(cls):39        cls.open_id_1 = "asdfghjjl"40        cls.student_id_1 = "2016014234"41        cls.open_id_2 = "qwertyuiop"42        User.objects.create(open_id=cls.open_id_1, student_id=cls.student_id_1)43        User.objects.create(open_id=cls.open_id_2)44        cls.clickMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'EventKey': 'SERVICE_BIND'}45        cls.textMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'Content': 'ç»å®'}46    def setUp(self):47        self.client = Client()48    def test_bind(self):49        # test user unbind with textMsg50        self.textMsg['FromUserName'] = self.open_id_251        msg = get_template('sendtext.xml').render(self.textMsg)52        response = self.client.post('/wechat', msg, content_type='application/xml')53        self.assertEqual(response.status_code, 200)54        content = xmltodict.parse(response.content)['xml']['Content'][0:17]55        answer = 'æ¢ç¥¨çåè½å¿
é¡»ç»å®å¦å·åæè½ä½¿ç¨ã'56        self.assertEqual(content, answer)57        # test user unbind with clickMsg58        self.clickMsg['FromUserName'] = self.open_id_259        msg = get_template('sendclick.xml').render(self.clickMsg)60        response = self.client.post('/wechat', msg, content_type='application/xml')61        self.assertEqual(response.status_code, 200)62        content = xmltodict.parse(response.content)['xml']['Content'][0:17]63        answer = 'æ¢ç¥¨çåè½å¿
é¡»ç»å®å¦å·åæè½ä½¿ç¨ã'64        self.assertEqual(content, answer)65        # test user binded with testMsg66        self.textMsg['FromUserName'] = self.open_id_167        msg = get_template('sendtext.xml').render(self.textMsg)68        response = self.client.post('/wechat', msg, content_type='application/xml')69        self.assertEqual(response.status_code, 200)70        content = xmltodict.parse(response.content)['xml']['Content'][0:8]71        answer = 'æ¨å·²ç»ç»å®äºå¦å·'72        self.assertEqual(content, answer)73        # test user binded with ClickEvent74        self.clickMsg['FromUserName'] = self.open_id_175        msg = get_template('sendclick.xml').render(self.clickMsg)76        response = self.client.post('/wechat', msg, content_type='application/xml')77        self.assertEqual(response.status_code, 200)78        content = xmltodict.parse(response.content)['xml']['Content'][0:8]79        answer = 'æ¨å·²ç»ç»å®äºå¦å·'80        self.assertEqual(content, answer)81class BookEmptyTest(TestCase):82    @classmethod83    def setUpTestData(cls):84        cls.open_id_1 = "asdfghjjl"85        cls.student_id_1 = "2016014234"86        cls.open_id_2 = "qwertyuiop"87        User.objects.create(open_id=cls.open_id_1, student_id=cls.student_id_1)88        User.objects.create(open_id=cls.open_id_2)89        cls.clickMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'EventKey': 'BOOKING_EMPTY'}90    def setUp(self):91        self.client = Client()92    def test_book(self):93        # test correct input94        msg = get_template('sendclick.xml').render(self.clickMsg)95        response = self.client.post('/wechat', msg, content_type='application/xml')96        self.assertEqual(response.status_code, 200)97        content = xmltodict.parse(response.content)['xml']['Content']98        answer = 'æ¨å¥½ï¼ç°å¨æ²¡ææ¨èçæ¢ç¥¨æ´»å¨å~'99        self.assertEqual(content, answer)100class BookWhatTest(TestCase):101    @classmethod102    def setUpTestData(cls):103        cls.open_id_1 = "asdfghjjl"104        cls.student_id_1 = "2016014234"105        cls.clickMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'EventKey': 'SERVICE_BOOK_WHAT'}106        cls.start_time1 = timezone.datetime(year=2018, month=11, day=1, hour=0, minute=0, second=0)107        cls.start_time2 = timezone.datetime(year=2018, month=12, day=1, hour=0, minute=0, second=0)108        cls.book_start = timezone.datetime(year=2018, month=10, day=1, hour=0, minute=0, second=0)109        cls.end_time1 = timezone.datetime(year=2018, month=12, day=1, hour=0, minute=0, second=0)110        cls.end_time2 = timezone.datetime(year=2018, month=12, day=30, hour=0, minute=0, second=0)111        cls.book_end = timezone.datetime(year=2018, month=10, day=25, hour=0, minute=0, second=0)112        act1 = Activity.objects.create(name="test1", key="test1", description="test1", start_time=cls.start_time1,113                                       end_time=cls.end_time1, book_start=cls.book_start, book_end=cls.book_end,114                                       total_tickets=100, remain_tickets=100, status=Activity.STATUS_PUBLISHED,115                                       pic_url="")116        act4 = Activity.objects.create(name="test1", key="test1", description="test1", start_time=cls.start_time2,117                                       end_time=cls.end_time2, book_start=cls.book_start, book_end=cls.book_end,118                                       total_tickets=100, remain_tickets=100, status=Activity.STATUS_PUBLISHED,119                                       pic_url="")120        act2 = Activity.objects.create(name="test1", key="test1", description="test1", start_time=cls.start_time1,121                                       end_time=cls.end_time1, book_start=cls.book_start, book_end=cls.book_end,122                                       total_tickets=100, remain_tickets=100, status=Activity.STATUS_DELETED,123                                       pic_url="")124        act3 = Activity.objects.create(name="test1", key="test1", description="test1", start_time=cls.start_time1,125                                       end_time=cls.end_time1, book_start=cls.book_start, book_end=cls.book_end,126                                       total_tickets=100, remain_tickets=100, status=Activity.STATUS_SAVED, pic_url="")127        User.objects.create(open_id=cls.open_id_1, student_id=cls.student_id_1)128    def setUp(self):129        self.client = Client()130    def test_bookWhat(self):131        # test activities with status != PUBLISHED132        msg = get_template('sendclick.xml').render(self.clickMsg)133        response = self.client.post('/wechat', msg, content_type='application/xml')134        self.assertEqual(response.status_code, 200)135        content = xmltodict.parse(response.content)['xml']["Articles"]['item']136        return_titles = []137        for article in content:138            return_titles.append(article['Title'])139        published = Activity.objects.filter(status=Activity.STATUS_PUBLISHED)140        published_titles = []141        for art in published:142            published_titles.append(art.name)143        self.assertEqual(return_titles, published_titles)144        # test with no activity145        Activity.objects.all().delete()146        msg = get_template('sendclick.xml').render(self.clickMsg)147        response = self.client.post('/wechat', msg, content_type='application/xml')148        self.assertEqual(response.status_code, 200)149        content = xmltodict.parse(response.content)['xml']['Content']150        self.assertEqual(content, 'å½åæ´»å¨å°ä¸å¯æ¢ç¥¨')151class CheckTicketTest(TestCase):152    @classmethod153    def setUpTestData(cls):154        pass155        cls.open_id_1 = "asdfghjjl"156        cls.student_id_1 = "2016014234"157        cls.open_id_2 = "zxcvbn"158        cls.student_id_2 = "2016014235"159        cls.open_id_3 = "dkfjghl"160        cls.clickMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'EventKey': 'SERVICE_GET_TICKET'}161        cls.start_time1 = timezone.datetime(year=2018, month=11, day=1, hour=0, minute=0, second=0)162        cls.start_time2 = timezone.datetime(year=2018, month=12, day=1, hour=0, minute=0, second=0)163        cls.book_start = timezone.datetime(year=2018, month=10, day=1, hour=0, minute=0, second=0)164        cls.end_time1 = timezone.datetime(year=2018, month=12, day=1, hour=0, minute=0, second=0)165        cls.end_time2 = timezone.datetime(year=2018, month=12, day=30, hour=0, minute=0, second=0)166        cls.book_end = timezone.datetime(year=2018, month=10, day=25, hour=0, minute=0, second=0)167        act1 = Activity.objects.create(name="test1", key="test1", description="test1", start_time=cls.start_time1,168                                       end_time=cls.end_time1, book_start=cls.book_start, book_end=cls.book_end,169                                       total_tickets=100, remain_tickets=100, status=Activity.STATUS_PUBLISHED,170                                       pic_url="")171        act4 = Activity.objects.create(name="test1", key="test1", description="test1", start_time=cls.start_time2,172                                       end_time=cls.end_time2, book_start=cls.book_start, book_end=cls.book_end,173                                       total_tickets=100, remain_tickets=100, status=Activity.STATUS_PUBLISHED,174                                       pic_url="")175        ticket1 = Ticket.objects.create(activity_id=act1.id, student_id=cls.student_id_1, status=Ticket.STATUS_VALID,176                                        unique_id="a")177        ticket2 = Ticket.objects.create(activity_id=act4.id, student_id=cls.student_id_1, status=Ticket.STATUS_VALID,178                                        unique_id="b")179        User.objects.create(open_id=cls.open_id_1, student_id=cls.student_id_1)180        User.objects.create(open_id=cls.open_id_2, student_id=cls.student_id_2)181        User.objects.create(open_id=cls.open_id_3)182    def setUp(self):183        self.client = Client()184    def test_check(self):185        # test unbind186        # test default handler187        self.clickMsg["FromUserName"] = self.open_id_3188        msg = get_template('sendclick.xml').render(self.clickMsg)189        response = self.client.post('/wechat', msg, content_type='application/xml')190        self.assertEqual(response.status_code, 200)191        content = xmltodict.parse(response.content)['xml']['Content']192        self.assertEqual(content, 'æ¨è¿æ²¡æç»å®ï¼ç»å®åæå¯ä½¿ç¨è¯¥åè½')193        # test no Ticket194        self.clickMsg["FromUserName"] = self.open_id_2195        msg = get_template('sendclick.xml').render(self.clickMsg)196        response = self.client.post('/wechat', msg, content_type='application/xml')197        self.assertEqual(response.status_code, 200)198        content = xmltodict.parse(response.content)['xml']['Content']199        self.assertEqual(content, 'å½å没æå·²è´ç票, æ¨å¯ç¹å»èåæ ä¸âæ¢å¥âæ¥çç°ææ´»å¨')200        # test user with tickets201        self.clickMsg["FromUserName"] = self.open_id_1202        msg = get_template('sendclick.xml').render(self.clickMsg)203        response = self.client.post('/wechat', msg, content_type='application/xml')204        self.assertEqual(response.status_code, 200)205        content = xmltodict.parse(response.content)['xml']["Articles"]['item']206        return_titles = []207        for article in content:208            return_titles.append(article['Title'])209        tickets = Ticket.objects.filter(status=Ticket.STATUS_VALID, student_id=self.student_id_1)210        an_titles = []211        for tic in tickets:212            an_titles.append(tic.activity.name)213        self.assertEqual(return_titles, an_titles)214class NonFunctionalHandlerTest(TestCase):215    # Test for help handler216    @classmethod217    def setUpTestData(cls):218        cls.open_id_1 = 'oxmsfsdfsaksfmksd'219        cls.open_id_2 = 'oxasfdsfdsafasdfd'220        cls.student_id_1 = '2000000000'221        cls.student_id_2 = '2016013240'222        User.objects.create(open_id=cls.open_id_1, student_id=cls.student_id_1)223        User.objects.create(open_id=cls.open_id_2, student_id=cls.student_id_2)224        cls.clickMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'EventKey': 'TEST'}225        cls.textMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'Content': 'TEST'}226    def setUp(self):227        self.client = Client()228    def test_click(self):229        # test default handler230        msg = get_template('sendclick.xml').render(self.clickMsg)231        response = self.client.post('/wechat', msg, content_type='application/xml')232        self.assertEqual(response.status_code, 200)233        content = xmltodict.parse(response.content)['xml']['Content']234        self.assertEqual(content, '对ä¸èµ·ï¼æ²¡ææ¾å°æ¨éè¦çä¿¡æ¯:(')235        # test help handler236        self.clickMsg['EventKey'] = 'SERVICE_HELP'237        msg = get_template('sendclick.xml').render(self.clickMsg)238        response = self.client.post('/wechat', msg, content_type='application/xml')239        self.assertEqual(response.status_code, 200)240        title = xmltodict.parse(response.content)['xml']['Articles']['item']['Title']241        self.assertEqual(title, 'âç´«èä¹å£°âä½¿ç¨æå')242    def test_text(self):243        # test default handler244        msg = get_template('sendtext.xml').render(self.textMsg)245        response = self.client.post('/wechat', msg, content_type='application/xml')246        self.assertEqual(response.status_code, 200)247        content = xmltodict.parse(response.content)['xml']['Content']248        self.assertEqual(content, '对ä¸èµ·ï¼æ²¡ææ¾å°æ¨éè¦çä¿¡æ¯:(')249        # test help handler250        self.textMsg['Content'] = '帮å©'251        msg = get_template('sendtext.xml').render(self.textMsg)252        response = self.client.post('/wechat', msg, content_type='application/xml')253        self.assertEqual(response.status_code, 200)254        title = xmltodict.parse(response.content)['xml']['Articles']['item']['Title']255        self.assertEqual(title, 'âç´«èä¹å£°âä½¿ç¨æå')256class TakeTicketHandlerTest(TestCase):257    # Test for take ticket handler258    @classmethod259    def setUpTestData(cls):260        cls.open_id_1 = 'oxmsfsdfsaksfmksd'261        cls.open_id_2 = 'oxasfdsfdsafasdfd'262        cls.student_id_1 = '2000000000'263        cls.student_id_2 = '2016013240'264        cls.unique_id_1 = 'dsafdasfefweksa'265        cls.unique_id_2 = 'kjknknkjnkkkkds'266        User.objects.create(open_id=cls.open_id_1, student_id=cls.student_id_1)267        User.objects.create(open_id=cls.open_id_2)268        cls.activity_1 = Activity.objects.create(name='test', key='test', description='myTest',269                                                 start_time='2018-12-02T08:00:00.000Z',270                                                 end_time=timezone.now() + timedelta(hours=2), place='github',271                                                 book_start='2018-10-02T08:00:00.000Z',272                                                 book_end=timezone.now() + timedelta(hours=1),273                                                 total_tickets=100, pic_url='xxx', remain_tickets=100, status=1)274        cls.activity_2 = Activity.objects.create(name='test2', key='test2', description='myTest2',275                                                 start_time='2018-12-02T08:00:00.000Z',276                                                 end_time=timezone.now() + timedelta(hours=2), place='github',277                                                 book_start='2018-10-02T08:00:00.000Z',278                                                 book_end=timezone.now() + timedelta(hours=1),279                                                 total_tickets=100, pic_url='xxx', remain_tickets=100, status=1)280        cls.ticket_1 = Ticket.objects.create(student_id=cls.student_id_1, unique_id=cls.unique_id_1,281                                             activity=Activity.objects.get(id=cls.activity_1.id),282                                             status=1)283        cls.ticket_2 = Ticket.objects.create(student_id=cls.student_id_2, unique_id=cls.unique_id_2,284                                             activity=Activity.objects.get(id=cls.activity_1.id),285                                             status=1)286        cls.textMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'Content': 'å票 ' + cls.activity_1.key}287    def setUp(self):288        self.client = Client()289    def test_text(self):290        # test unbound studentID and openid291        self.textMsg['FromUserName'] = self.open_id_2292        msg = get_template('sendtext.xml').render(self.textMsg)293        response = self.client.post('/wechat', msg, content_type='application/xml')294        self.assertEqual(response.status_code, 200)295        content = xmltodict.parse(response.content)['xml']['Content']296        self.assertEqual(content, '请å
ç»å®å§åå¦å·')297        # test nonexistent openid298        self.textMsg['FromUserName'] = 'testNonexistence'299        msg = get_template('sendtext.xml').render(self.textMsg)300        response = self.client.post('/wechat', msg, content_type='application/xml')301        self.assertEqual(response.status_code, 200)302        content = xmltodict.parse(response.content)['xml']['Content']303        self.assertEqual(content, '请å
ç»å®å§åå¦å·')304        # test nonexistent ticket305        self.textMsg['FromUserName'] = self.open_id_1306        self.textMsg['Content'] = 'å票 ' + self.activity_2.key307        msg = get_template('sendtext.xml').render(self.textMsg)308        response = self.client.post('/wechat', msg, content_type='application/xml')309        self.assertEqual(response.status_code, 200)310        content = xmltodict.parse(response.content)['xml']['Content']311        self.assertEqual(content, 'æ±æï¼æ¨å¹¶æ²¡æè¯¥æ´»å¨çé¨ç¥¨')312        # test ticket past due313        self.activity_1.status = Activity.STATUS_SAVED314        self.activity_1.save()315        self.textMsg['FromUserName'] = self.open_id_1316        self.textMsg['Content'] = 'å票 ' + self.activity_1.key317        msg = get_template('sendtext.xml').render(self.textMsg)318        response = self.client.post('/wechat', msg, content_type='application/xml')319        self.assertEqual(response.status_code, 200)320        content = xmltodict.parse(response.content)['xml']['Content']321        self.assertEqual(content, 'æ±æï¼æ¨å¹¶æ²¡æè¯¥æ´»å¨çé¨ç¥¨')322        self.activity_1.status = 1323        self.activity_1.save()324        # test ticket used325        self.ticket_1.status = Ticket.STATUS_USED326        self.ticket_1.save()327        self.textMsg['FromUserName'] = self.open_id_1328        self.textMsg['Content'] = 'å票 ' + self.activity_1.key329        msg = get_template('sendtext.xml').render(self.textMsg)330        response = self.client.post('/wechat', msg, content_type='application/xml')331        self.assertEqual(response.status_code, 200)332        content = xmltodict.parse(response.content)['xml']['Content']333        self.assertEqual(content, 'æ±æï¼æ¨å¹¶æ²¡æè¯¥æ´»å¨çé¨ç¥¨')334        self.ticket_1.status = Ticket.STATUS_VALID335        self.ticket_1.save()336        # test correct request337        self.textMsg['FromUserName'] = self.open_id_1338        self.textMsg['Content'] = 'å票 ' + self.activity_1.key339        msg = get_template('sendtext.xml').render(self.textMsg)340        response = self.client.post('/wechat', msg, content_type='application/xml')341        self.assertEqual(response.status_code, 200)342        url = xmltodict.parse(response.content)['xml']['Articles']['item']['Url']343        time = xmltodict.parse(response.content)['xml']['Articles']['item']['Description']344        self.assertEqual(time, 'å¼å§æ¶é´ï¼' + '2018-12-02 16:00:00' + '\nå°ç¹ï¼' + self.activity_1.place)345        response = self.client.get(url)346        self.assertEqual(response.status_code, 200)347class BookTicketHandlerTest(TestCase):348    @classmethod349    def setUpTestData(cls):350        cls.open_id_1 = 'oxmsfsdfsaksfmksd'351        cls.open_id_2 = 'oxasfdsfdsafasdfd'352        cls.student_id_1 = '2000000000'353        cls.student_id_2 = '2016013240'354        cls.unique_id_1 = 'dsafdasfefweksa'355        cls.unique_id_2 = 'kjknknkjnkkkkds'356        User.objects.create(open_id=cls.open_id_1, student_id=cls.student_id_1)357        User.objects.create(open_id=cls.open_id_2)358        cls.activity_1 = Activity.objects.create(name='test', key='test', description='myTest',359                                                 start_time='2018-12-02T08:00:00.000Z',360                                                 end_time=timezone.now() + timedelta(hours=2), place='github',361                                                 book_start='2018-10-02T08:00:00.000Z',362                                                 book_end=timezone.now() + timedelta(hours=1),363                                                 total_tickets=100, pic_url='xxx', remain_tickets=100, status=1)364        cls.activity_2 = Activity.objects.create(name='test2', key='test2', description='myTest2',365                                                 start_time='2018-12-02T08:00:00.000Z',366                                                 end_time=timezone.now() + timedelta(hours=2), place='github',367                                                 book_start='2018-10-02T08:00:00.000Z',368                                                 book_end=timezone.now() + timedelta(hours=1),369                                                 total_tickets=100, pic_url='xxx', remain_tickets=100, status=1)370        cls.ticket_1 = Ticket.objects.create(student_id=cls.student_id_1, unique_id=cls.unique_id_1,371                                             activity=Activity.objects.get(id=cls.activity_1.id),372                                             status=1)373        cls.ticket_2 = Ticket.objects.create(student_id=cls.student_id_2, unique_id=cls.unique_id_2,374                                             activity=Activity.objects.get(id=cls.activity_1.id),375                                             status=1)376        cls.textMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'Content': 'æ¢ç¥¨ ' + cls.activity_2.key}377        cls.clickMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1,378                        'EventKey': 'BOOKING_ACTIVITY_' + str(cls.activity_2.id)}379    def test_click(self):380        # test nonexistent activity381        self.clickMsg['EventKey'] = 'BOOKING_ACTIVITY_123'382        msg = get_template('sendclick.xml').render(self.clickMsg)383        response = self.client.post('/wechat', msg, content_type='application/xml')384        self.assertEqual(response.status_code, 200)385        content = xmltodict.parse(response.content)['xml']['Content']386        self.assertEqual(content, 'æªæ¥è¯¢å°ç¸å
³æ´»å¨')387        self.clickMsg['EventKey'] = 'BOOKING_ACTIVITY_' + str(self.activity_2.id)388        # test unbound user389        self.clickMsg['FromUserName'] = self.open_id_2390        msg = get_template('sendclick.xml').render(self.clickMsg)391        response = self.client.post('/wechat', msg, content_type='application/xml')392        self.assertEqual(response.status_code, 200)393        content = xmltodict.parse(response.content)['xml']['Content']394        self.assertEqual(content, 'æªç»å®å¦å·')395        self.clickMsg['FromUserName'] = self.open_id_1396        # test unpublished activity397        self.activity_2.status = Activity.STATUS_SAVED398        self.activity_2.save()399        msg = get_template('sendclick.xml').render(self.clickMsg)400        response = self.client.post('/wechat', msg, content_type='application/xml')401        self.assertEqual(response.status_code, 200)402        content = xmltodict.parse(response.content)['xml']['Content']403        self.assertEqual(content, 'è¯¥æ´»å¨æªåå¸')404        self.activity_2.status = Activity.STATUS_PUBLISHED405        self.activity_2.save()406        # test booking finished407        self.activity_2.book_end = timezone.now() - timedelta(hours=2)408        self.activity_2.save()409        msg = get_template('sendclick.xml').render(self.clickMsg)410        response = self.client.post('/wechat', msg, content_type='application/xml')411        self.assertEqual(response.status_code, 200)412        content = xmltodict.parse(response.content)['xml']['Content']413        self.assertEqual(content, 'è¯¥æ´»å¨æ¢ç¥¨å·²æªæ¢')414        self.activity_2.book_end = timezone.now() + timedelta(hours=1)415        self.activity_2.save()416        # test booking yet to begin417        self.activity_2.book_start = timezone.now() + timedelta(hours=2)418        self.activity_2.save()419        msg = get_template('sendclick.xml').render(self.clickMsg)420        response = self.client.post('/wechat', msg, content_type='application/xml')421        self.assertEqual(response.status_code, 200)422        content = xmltodict.parse(response.content)['xml']['Content']423        self.assertEqual(content, 'è¯¥æ´»å¨æªå¼æ¾æ¢ç¥¨')424        self.activity_2.book_start = '2018-10-02T08:00:00.000Z'425        self.activity_2.save()426        # test ticket depleted427        ticket = self.activity_2.remain_tickets428        self.activity_2.remain_tickets = 0429        self.activity_2.save()430        msg = get_template('sendclick.xml').render(self.clickMsg)431        response = self.client.post('/wechat', msg, content_type='application/xml')432        self.assertEqual(response.status_code, 200)433        content = xmltodict.parse(response.content)['xml']['Content']434        self.assertEqual(content, '该活å¨å·²æ ä½ç¥¨')435        self.activity_2.remain_tickets = ticket436        self.activity_2.save()437        # test already possessed ticket438        self.clickMsg['EventKey'] = 'BOOKING_ACTIVITY_' + str(self.activity_1.id)439        msg = get_template('sendclick.xml').render(self.clickMsg)440        response = self.client.post('/wechat', msg, content_type='application/xml')441        self.assertEqual(response.status_code, 200)442        content = xmltodict.parse(response.content)['xml']['Content']443        self.assertEqual(content, 'æ¨å¥½ï¼æ¨å·²ç»æè¯¥æ´»å¨ç票ï¼è¯·ç¹å»æ¥ç¥¨æ¥è¯¢çµå票详æ
')444        self.clickMsg['EventKey'] = 'BOOKING_ACTIVITY_' + str(self.activity_2.id)445        # test success booking446        msg = get_template('sendclick.xml').render(self.clickMsg)447        response = self.client.post('/wechat', msg, content_type='application/xml')448        self.assertEqual(response.status_code, 200)449        content = xmltodict.parse(response.content)['xml']['Articles']['item']['Title']450        self.assertEqual(content, self.activity_2.name)451    def test_text(self):452        # test illegal string pattern453        self.textMsg['Content'] = 'æ¢ç¥¨'454        msg = get_template('sendtext.xml').render(self.textMsg)455        response = self.client.post('/wechat', msg, content_type='application/xml')456        self.assertEqual(response.status_code, 200)457        content = xmltodict.parse(response.content)['xml']['Content']458        self.assertEqual(content, 'è¯·ææ ¼å¼è¾å
¥ï¼æ¢ç¥¨ æ´»å¨ä»£ç§°')459class RefundHandlerTest(TestCase):460    @classmethod461    def setUpTestData(cls):462        cls.open_id_1 = 'oxmsfsdfsaksfmksd'463        cls.open_id_2 = 'oxasfdsfdsafasdfd'464        cls.student_id_1 = '2000000000'465        cls.student_id_2 = '2016013240'466        cls.unique_id_1 = 'dsafdasfefweksa'467        cls.unique_id_2 = 'kjknknkjnkkkkds'468        User.objects.create(open_id=cls.open_id_1, student_id=cls.student_id_1)469        User.objects.create(open_id=cls.open_id_2)470        cls.activity_1 = Activity.objects.create(name='test', key='test', description='myTest',471                                                 start_time='2018-12-02T08:00:00.000Z',472                                                 end_time=timezone.now() + timedelta(hours=2), place='github',473                                                 book_start='2018-10-02T08:00:00.000Z',474                                                 book_end=timezone.now() + timedelta(hours=1),475                                                 total_tickets=100, pic_url='xxx', remain_tickets=100, status=1)476        cls.activity_2 = Activity.objects.create(name='test2', key='test2', description='myTest2',477                                                 start_time='2018-12-02T08:00:00.000Z',478                                                 end_time=timezone.now() + timedelta(hours=2), place='github',479                                                 book_start='2018-10-02T08:00:00.000Z',480                                                 book_end=timezone.now() + timedelta(hours=1),481                                                 total_tickets=100, pic_url='xxx', remain_tickets=100, status=1)482        cls.ticket_1 = Ticket.objects.create(student_id=cls.student_id_1, unique_id=cls.unique_id_1,483                                             activity=Activity.objects.get(id=cls.activity_1.id),484                                             status=1)485        cls.ticket_2 = Ticket.objects.create(student_id=cls.student_id_2, unique_id=cls.unique_id_2,486                                             activity=Activity.objects.get(id=cls.activity_1.id),487                                             status=1)488        cls.textMsg = {'ToUserName': 'TestName', 'FromUserName': cls.open_id_1, 'Content': 'é票 ' + cls.activity_1.key}489    def test_text(self):490        # test unbound openid491        self.textMsg['FromUserName'] = self.open_id_2492        msg = get_template('sendtext.xml').render(self.textMsg)493        response = self.client.post('/wechat', msg, content_type='application/xml')494        self.assertEqual(response.status_code, 200)495        content = xmltodict.parse(response.content)['xml']['Content']496        self.assertEqual(content, 'æªç»å®å¦å·')497        self.textMsg['FromUserName'] = self.open_id_1498        # test illegal string pattern499        self.textMsg['Content'] = 'é票'500        msg = get_template('sendtext.xml').render(self.textMsg)501        response = self.client.post('/wechat', msg, content_type='application/xml')502        self.assertEqual(response.status_code, 200)503        content = xmltodict.parse(response.content)['xml']['Content']504        self.assertEqual(content, 'è¯·ææ ¼å¼è¾å
¥ï¼é票 æ´»å¨ä»£ç§°')505        self.textMsg['Content'] = 'é票 ' + self.activity_1.key506        # test nonexistent activity507        self.textMsg['Content'] = 'é票 ' + self.activity_1.key + 'test'508        msg = get_template('sendtext.xml').render(self.textMsg)509        response = self.client.post('/wechat', msg, content_type='application/xml')510        self.assertEqual(response.status_code, 200)511        content = xmltodict.parse(response.content)['xml']['Content']512        self.assertEqual(content, 'æªæ¥è¯¢å°ç¸å
³æ´»å¨')513        self.textMsg['Content'] = 'é票 ' + self.activity_1.key514        # test no associated activity515        self.textMsg['Content'] = 'é票 ' + self.activity_2.key516        msg = get_template('sendtext.xml').render(self.textMsg)517        response = self.client.post('/wechat', msg, content_type='application/xml')518        self.assertEqual(response.status_code, 200)519        content = xmltodict.parse(response.content)['xml']['Content']520        self.assertEqual(content, 'æ¨æ²¡ææ¤æ´»å¨ç票')521        self.textMsg['Content'] = 'é票 ' + self.activity_1.key522        # test canceled ticket523        self.ticket_1.status = Ticket.STATUS_CANCELLED524        self.ticket_1.save()525        msg = get_template('sendtext.xml').render(self.textMsg)526        response = self.client.post('/wechat', msg, content_type='application/xml')527        self.assertEqual(response.status_code, 200)528        content = xmltodict.parse(response.content)['xml']['Content']529        self.assertEqual(content, 'æ¨ç票已ç»åæ¶ï¼ä¸å¯é票')530        self.ticket_1.status = Ticket.STATUS_VALID531        self.ticket_1.save()532        # test used ticket533        self.ticket_1.status = Ticket.STATUS_USED534        self.ticket_1.save()535        msg = get_template('sendtext.xml').render(self.textMsg)536        response = self.client.post('/wechat', msg, content_type='application/xml')537        self.assertEqual(response.status_code, 200)538        content = xmltodict.parse(response.content)['xml']['Content']539        self.assertEqual(content, 'æ¨ç票已ç»ä½¿ç¨ï¼ä¸å¯é票')540        self.ticket_1.status = Ticket.STATUS_VALID541        self.ticket_1.save()542        # test pass due543        self.activity_1.start_time = timezone.now()544        self.activity_1.save()545        msg = get_template('sendtext.xml').render(self.textMsg)546        response = self.client.post('/wechat', msg, content_type='application/xml')547        self.assertEqual(response.status_code, 200)548        content = xmltodict.parse(response.content)['xml']['Content']549        self.assertEqual(content, 'åªè½å¨æ´»å¨å¼å§45åéåé票')550        self.activity_1.start_time = '2018-12-02T08:00:00.000Z'551        self.activity_1.save()552        # test success refund553        msg = get_template('sendtext.xml').render(self.textMsg)554        response = self.client.post('/wechat', msg, content_type='application/xml')555        self.assertEqual(response.status_code, 200)556        content = xmltodict.parse(response.content)['xml']['Content']...craigslistSeach.py
Source:craigslistSeach.py  
1import sys, os2from os import link3from typing import Counter4from selenium import webdriver5from selenium.webdriver.common import service6from selenium.webdriver.firefox.options import Options7from selenium.webdriver.common.keys import Keys8from time import sleep9import csv10import tkinter as tk11import numpy as np12import Face.interface as interface13wantFree = input("Want free? Y/N ")14whatWant = interface.interface()15browser = webdriver.Firefox()16fileRead = open('F:\Documents\projects\Craigsearchbot\craigsheet.csv', 'r', newline='')17fileWrite = open('F:\Documents\projects\Craigsearchbot\craigsheet.csv', 'w', newline='')18mywriter = csv.writer(fileWrite, delimiter=',')19if wantFree == "Y":20    21        browser.get('https://losangeles.craigslist.org/search/sfv/zip?')22        23        searchForm = browser.find_element_by_id("query")24        searchForm.click()25        26        for i in whatWant:27                        28                searchForm.send_keys(i)29                sendClick = browser.find_element_by_class_name("searchbtn")30                sendClick.click()31                ulSelector = browser.find_element_by_class_name("rows")32                liClass = ulSelector.find_elements_by_tag_name("li")33                with open('F:\Documents\projects\Craigsearchbot\craigsheet.csv', 'w', newline='') as file:34                        for links in liClass:35                                linkyLinks = links.find_element_by_xpath('./a').get_attribute('href')36                                print(linkyLinks)37                                38                                mywriter = csv.writer(file, delimiter=' ')39                                mywriter.writerows([linkyLinks])40                41else:42        browser.get('https://losangeles.craigslist.org/search/sfv/sss?')43        linkyLinks = []44        linkDictionary = []45        for i in whatWant:46                47                searchForm = browser.find_element_by_id("query")48                searchForm.click()49                        50                searchForm.send_keys(i)51                sendClick = browser.find_element_by_class_name("searchbtn")52                sendClick.click()53                ulSelector = browser.find_element_by_class_name("rows")54                liClass = ulSelector.find_elements_by_tag_name("li")55                for links in liClass:56                        57                        linkyLinks.append(links.find_element_by_xpath('./a').get_attribute('href'))58                                59                for i in range(len(linkyLinks)):60                        linkDictionary.append(linkyLinks[i])61                print(linkDictionary)62                mywriter.writerow([linkDictionary])63                browser.get('https://losangeles.craigslist.org/search/sfv/sss?')64                65                ...install2.py
Source:install2.py  
1# -*- coding: utf-8 -*-2import xbmc3def printstar():4    print "***************************************************************************************"5    print "****************************************************************************************"6	7printstar()8print "test6.py has just been started"9printstar()10# xbmc.executebuiltin('Notification(test1.py, started)')11xbmc.executebuiltin('InstallAddon(skin.confluence)')12xbmc.sleep(1000)13xbmc.executebuiltin('SendClick(11)')14xbmc.sleep(300)15c = 016if not xbmc.getCondVisibility('System.HasAddon(skin.confluence)'):17    xbmc.sleep(1000)18    c = c + 119    if c > 120:20        printstar()21        print 'wibble'22        printstar()		23xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"Settings.SetSettingValue","id":1,"params":{"setting":"lookandfeel.skin","value":"skin.confluence"}}')24xbmc.executebuiltin('SendClick(11)')		25xbmc.sleep(1000)26xbmc.executebuiltin('InstallAddon(skin.amber)')27xbmc.sleep(1000)28xbmc.executebuiltin('SendClick(11)')29xbmc.sleep(300)30c = 031if not xbmc.getCondVisibility('System.HasAddon(skin.amber)'):32    xbmc.sleep(1000)33    c = c + 134    if c > 120:35        printstar()36        print 'wibble'37        printstar()		38xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"Settings.SetSettingValue","id":1,"params":{"setting":"lookandfeel.skin","value":"skin.amber"}}')39xbmc.executebuiltin('SendClick(11)')		40xbmc.sleep(1000)41		42xbmc.executebuiltin('InstallAddon(skin.titan)')43xbmc.sleep(1000)44xbmc.executebuiltin('SendClick(11)')45xbmc.sleep(300)46c = 047if not xbmc.getCondVisibility('System.HasAddon(skin.titan)'):48    xbmc.sleep(1000)49    c = c + 150    if c > 120:51        printstar()52        print 'wibble'53        printstar()	54xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"Settings.SetSettingValue","id":1,"params":{"setting":"lookandfeel.skin","value":"skin.titan"}}')55xbmc.executebuiltin('SendClick(11)')		56xbmc.sleep(5000)57xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"Settings.SetSettingValue","id":1,"params":{"setting":"lookandfeel.skin","value":"skin.confluence"}}')58xbmc.executebuiltin('SendClick(11)')		59xbmc.sleep(1000)		...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!!
