Best Python code snippet using pyresttest_python
test_tagger.py
Source:test_tagger.py  
...3import pkg_resources4import zeit.cms.testing5import zeit.intrafind.testing6class TagTestHelpers(object):7    def get_content(self):8        from zeit.cms.testcontenttype.testcontenttype import ExampleContentType9        return ExampleContentType()10    def set_tags(self, content, xml):11        from zeit.connector.interfaces import IWebDAVProperties12        dav = IWebDAVProperties(content)13        name, ns = dav_key = (14            'rankedTags', 'http://namespaces.zeit.de/CMS/tagging')15        dav[dav_key] = """<ns:{tag} xmlns:ns="{ns}">16        <rankedTags>{0}</rankedTags></ns:{tag}>""".format(17            xml, ns=ns, tag=name)18    def get_tagger(self, content):19        from zeit.intrafind.tagger import Tagger20        return Tagger(content)21class TestTagger(zeit.cms.testing.FunctionalTestCase, TagTestHelpers):22    layer = zeit.intrafind.testing.ZCML_LAYER23    def test_tagger_should_provide_interface(self):24        import zope.interface.verify25        from zeit.cms.tagging.interfaces import ITagger26        self.assertTrue(27            zope.interface.verify.verifyObject(28                ITagger, self.get_tagger(self.get_content())))29    def test_tagger_should_be_empty_if_not_tagged(self):30        content = self.get_content()31        tagger = self.get_tagger(content)32        self.assertEqual([], list(tagger))33    def test_tagger_should_get_tags_from_content(self):34        content = self.get_content()35        self.set_tags(content, """36<tag uuid="uid-karenduve">Karen Duve</tag>37<tag uuid="uid-berlin">Berlin</tag>38""")39        tagger = self.get_tagger(content)40        self.assertEqual(set(['uid-berlin', 'uid-karenduve']), set(tagger))41    def test_len_should_return_amount_of_tags(self):42        content = self.get_content()43        self.set_tags(content, """44<tag uuid="uid-karenduve">Karen Duve</tag>45<tag uuid="uid-berlin">Berlin</tag>46""")47        tagger = self.get_tagger(content)48        self.assertEqual(2, len(tagger))49        self.set_tags(content, """50<tag uuid="uid-karenduve">Karen Duve</tag>51<tag uuid="uid-berlin">Berlin</tag>52<tag uuid="uid-fleisch">Fleisch</tag>53""")54        self.assertEqual(3, len(tagger))55    def test_tags_should_be_accessible_by_id(self):56        content = self.get_content()57        self.set_tags(content, """58<tag uuid="uid-karenduve">Karen Duve</tag>59<tag uuid="uid-berlin">Berlin</tag>60""")61        tagger = self.get_tagger(content)62        tag = tagger['uid-karenduve']63        self.assertEqual(tagger, tag.__parent__)64        self.assertEqual('uid-karenduve', tag.__name__)65        self.assertEqual('Karen Duve', tag.label)66    def test_tag_should_have_entity_type(self):67        content = self.get_content()68        self.set_tags(content, """69<tag uuid="uid-karenduve">Karen Duve</tag>70<tag uuid="uid-berlin" type="Location">Berlin</tag>71""")72        tagger = self.get_tagger(content)73        self.assertEqual('Location', tagger['uid-berlin'].entity_type)74    def test_tag_should_have_url_value(self):75        content = self.get_content()76        self.set_tags(content, """77<tag uuid="uid-karenduve">Karen Duve</tag>78<tag uuid="uid-berlin" url_value="dickesb">Berlin</tag>79""")80        tagger = self.get_tagger(content)81        self.assertEqual('dickesb', tagger['uid-berlin'].url_value)82    def test_getitem_should_raise_keyerror_if_tag_does_not_exist(self):83        content = self.get_content()84        tagger = self.get_tagger(content)85        self.assertRaises(KeyError, lambda: tagger['foo'])86    def test_iter_ignores_tags_without_uuids(self):87        content = self.get_content()88        self.set_tags(content, """89<tag>Karen Duve</tag>90<tag uuid="uid-berlin">Berlin</tag>91""")92        tagger = self.get_tagger(content)93        self.assertEqual(['uid-berlin'], list(tagger))94    def test_setitem_should_add_tag(self):95        from zeit.intrafind.tag import Tag96        content = self.get_content()97        tagger = self.get_tagger(content)98        tagger['uid-berlin'] = Tag('uid-berlin', 'Berlin')99        self.assertEqual(['uid-berlin'], list(tagger))100        self.assertEqual('Berlin', tagger['uid-berlin'].label)101    def test_setitem_should_set_entity_type(self):102        from zeit.intrafind.tag import Tag103        content = self.get_content()104        tagger = self.get_tagger(content)105        tagger['uid-berlin'] = Tag(106            'uid-berlin', 'Berlin', entity_type='Location')107        self.assertEqual('Location', tagger['uid-berlin'].entity_type)108    def test_iter_should_be_sorted_by_document_order(self):109        content = self.get_content()110        self.set_tags(content, """111<tag uuid="uid-berlin">Berlin</tag>112<tag uuid="uid-karenduve">Karen Duve</tag>113<tag uuid="uid-fleisch">Fleisch</tag>114""")115        tagger = self.get_tagger(content)116        self.assertEqual(117            ['uid-berlin', 'uid-karenduve', 'uid-fleisch'], list(tagger))118    def test_updateOrder_should_sort_tags(self):119        content = self.get_content()120        self.set_tags(content, """121<tag uuid="uid-berlin">Berlin</tag>122<tag uuid="uid-karenduve">Karen Duve</tag>123<tag uuid="uid-fleisch">Fleisch</tag>124""")125        tagger = self.get_tagger(content)126        tagger.updateOrder(['uid-fleisch', 'uid-berlin', 'uid-karenduve'])127        self.assertEqual(128            ['uid-fleisch', 'uid-berlin', 'uid-karenduve'], list(tagger))129    def test_updateOrder_should_sort_tags_even_when_keys_are_generator(self):130        content = self.get_content()131        self.set_tags(content, """132<tag uuid="uid-berlin">Berlin</tag>133<tag uuid="uid-karenduve">Karen Duve</tag>134<tag uuid="uid-fleisch">Fleisch</tag>135""")136        tagger = self.get_tagger(content)137        tagger.updateOrder(138            iter(['uid-fleisch', 'uid-berlin', 'uid-karenduve']))139        self.assertEqual(140            ['uid-fleisch', 'uid-berlin', 'uid-karenduve'], list(tagger))141    def test_updateOrder_should_do_nothing_if_tags_are_empty(self):142        content = self.get_content()143        tagger = self.get_tagger(content)144        with self.assertNothingRaised():145            tagger.updateOrder([])146    def test_given_keys_differ_from_existing_keys_should_raise(self):147        content = self.get_content()148        self.set_tags(content, """149<tag uuid="uid-berlin">Berlin</tag>150<tag uuid="uid-karenduve">Karen Duve</tag>151<tag uuid="uid-fleisch">Fleisch</tag>152""")153        tagger = self.get_tagger(content)154        self.assertRaises(155            ValueError,156            lambda: tagger.updateOrder(['uid-berlin', 'uid-karenduve']))157    def test_contains_should_return_true_for_existing_tag(self):158        content = self.get_content()159        self.set_tags(content, """160<tag uuid="uid-karenduve">Karen Duve</tag>161""")162        tagger = self.get_tagger(content)163        self.assertIn('uid-karenduve', tagger)164    def test_contains_should_return_false_for_noneexisting_tag(self):165        content = self.get_content()166        tagger = self.get_tagger(content)167        self.assertNotIn('uid-karenduve', tagger)168    def test_get_should_return_existing_tag(self):169        content = self.get_content()170        self.set_tags(content, """171<tag uuid="uid-karenduve">Karen Duve</tag>172""")173        tagger = self.get_tagger(content)174        self.assertEqual('Karen Duve', tagger.get('uid-karenduve').label)175    def test_get_should_return_default_if_tag_does_not_exist(self):176        content = self.get_content()177        tagger = self.get_tagger(content)178        self.assertEqual(mock.sentinel.default,179                         tagger.get('uid-karenduve', mock.sentinel.default))180    def test_delitem_should_remove_tag(self):181        content = self.get_content()182        # use an umlaut to exercise serialization183        self.set_tags(content, """184<tag uuid="uid-karenduve">Karen Düve</tag>185""")186        tagger = self.get_tagger(content)187        del tagger['uid-karenduve']188        self.assertNotIn('uid-karenduve', tagger)189    def test_delitem_should_add_tag_to_disabled_list_in_dav(self):190        from zeit.connector.interfaces import IWebDAVProperties191        content = self.get_content()192        self.set_tags(content, """193<tag uuid="uid-karenduve">Karen Duve</tag>194""")195        tagger = self.get_tagger(content)196        del tagger['uid-karenduve']197        dav = IWebDAVProperties(content)198        dav_key = ('disabled', 'http://namespaces.zeit.de/CMS/tagging')199        self.assertEqual('uid-karenduve', dav[dav_key])200    def test_disabled_tags_should_be_separated_by_tab(self):201        from zeit.connector.interfaces import IWebDAVProperties202        content = self.get_content()203        self.set_tags(content, """204<tag uuid="uid-karenduve">Karen Duve</tag>205<tag uuid="uid-berlin">Berlin</tag>206""")207        tagger = self.get_tagger(content)208        del tagger['uid-karenduve']209        dav = IWebDAVProperties(content)210        dav_key = ('disabled', 'http://namespaces.zeit.de/CMS/tagging')211        self.assertEqual('uid-karenduve', dav[dav_key])212    def test_parse_should_return_inner_rankedTags(self):213        content = self.get_content()214        self.set_tags(content, """215<tag uuid="uid-karenduve">Karen Duve</tag>216<tag uuid="uid-berlin">Berlin</tag>217""")218        tagger = self.get_tagger(content)219        node = tagger._parse()220        self.assertEqual('rankedTags', node.tag)221    def test_rankedKeys_dav_property_should_not_be_added_to_xml(self):222        import zeit.cms.content.interfaces223        import zope.interface224        content = self.get_content()225        self.set_tags(content, """226<tag uuid="uid-karenduve">Karen Duve</tag>227<tag uuid="uid-berlin">Berlin</tag>228""")229        zope.interface.alsoProvides(230            content, zeit.cms.content.interfaces.IDAVPropertiesInXML)231        sync = zeit.cms.content.interfaces.IDAVPropertyXMLSynchroniser(content)232        sync.sync()233        dav_attribs = u'\n'.join(234            unicode(a) for a in content.xml.head.attribute[:])235        self.assertNotIn('rankedTags', dav_attribs)236    def test_existing_tags_should_cause_rankedTags_to_be_added_to_xml(self):237        import zope.component238        import zeit.cms.repository.interfaces239        import zeit.cms.checkout.helper240        repository = zope.component.getUtility(241            zeit.cms.repository.interfaces.IRepository)242        repository['content'] = self.get_content()243        with zeit.cms.checkout.helper.checked_out(repository['content']) as \244                content:245            self.set_tags(content, """246    <tag uuid="uid-karenduve">Karen Duve</tag>247    <tag uuid="uid-berlin">Berlin</tag>248    """)249        self.assertEqual(250            ['Karen Duve', 'Berlin'],251            repository['content'].xml.head.rankedTags.getchildren())252    def test_no_tags_cause_rankedTags_element_to_be_removed_from_xml(self):253        import zope.component254        import zeit.cms.repository.interfaces255        import zeit.cms.checkout.helper256        content = self.get_content()257        content.xml.head.rankedTags = 'bla bla bla'258        repository = zope.component.getUtility(259            zeit.cms.repository.interfaces.IRepository)260        repository['content'] = content261        with zeit.cms.checkout.helper.checked_out(repository['content']):262            # cycle263            pass264        self.assertNotIn('rankedTags', repository['content'].xml.head.keys())265    def test_checkin_should_not_fail_with_no_tags_and_no_rankedTags_element(266        self):267        import zope.component268        import zeit.cms.repository.interfaces269        import zeit.cms.checkout.helper270        repository = zope.component.getUtility(271            zeit.cms.repository.interfaces.IRepository)272        repository['content'] = self.get_content()273        with zeit.cms.checkout.helper.checked_out(repository['content']):274            # cycle275            pass276    def test_disabled_tags_should_be_removed_from_xml(self):277        import zope.component278        import zeit.cms.repository.interfaces279        import zeit.cms.checkout.helper280        repository = zope.component.getUtility(281            zeit.cms.repository.interfaces.IRepository)282        repository['content'] = self.get_content()283        with zeit.cms.checkout.helper.checked_out(repository['content']) as \284                content:285            self.set_tags(content, """286    <tag uuid="uid-karenduve">Karen Duve</tag>287    <tag uuid="uid-berlin">Berlin</tag>288    """)289            tagger = self.get_tagger(content)290            del tagger['uid-berlin']291        self.assertEqual(292            ['Karen Duve'],293            repository['content'].xml.head.rankedTags.getchildren())294    def test_rankedTags_in_xml_should_be_updated_on_modified_event(self):295        import zeit.cms.checkout.helper296        import zeit.cms.repository.interfaces297        import zope.component298        import zope.lifecycleevent299        repository = zope.component.getUtility(300            zeit.cms.repository.interfaces.IRepository)301        repository['content'] = self.get_content()302        with zeit.cms.checkout.helper.checked_out(repository['content']) as \303                content:304            self.set_tags(content, """305    <tag uuid="uid-karenduve">Karen Duve</tag>306    <tag uuid="uid-berlin">Berlin</tag>307    """)308            zope.lifecycleevent.modified(content)309            self.assertEqual(310                ['Karen Duve', 'Berlin'],311                content.xml.head.rankedTags.getchildren())312    def test_modified_event_should_leave_non_content_alone(self):313        # regression #12394314        import zeit.cms.content.interfaces315        import zope.interface316        import zope.lifecycleevent317        dummy = type('Dummy', (object,), {})318        zope.interface.alsoProvides(319            dummy, zeit.cms.content.interfaces.IXMLRepresentation)320        with mock.patch(321                'zeit.cms.tagging.tag.add_ranked_tags_to_head') as handler:322            zope.lifecycleevent.modified(dummy)323            self.assertFalse(handler.called)324    def test_set_pinned_should_update_tab_separated_property(self):325        from zeit.connector.interfaces import IWebDAVProperties326        content = self.get_content()327        self.set_tags(content, """328<tag uuid="uid-karenduve">Karen Duve</tag>329<tag uuid="uid-berlin">Berlin</tag>330""")331        tagger = self.get_tagger(content)332        tagger.set_pinned(['uid-berlin', 'uid-karenduve'])333        self.assertEqual(('uid-berlin', 'uid-karenduve'), tagger.pinned)334        dav = IWebDAVProperties(content)335        dav_key = ('pinned', 'http://namespaces.zeit.de/CMS/tagging')336        self.assertEqual('uid-berlin\tuid-karenduve', dav[dav_key])337        self.assertTrue(tagger['uid-berlin'].pinned)338class TaggerUpdateTest(zeit.cms.testing.FunctionalTestCase, TagTestHelpers):339    layer = zeit.intrafind.testing.ZCML_LAYER340    def test_update_should_post_xml_urlencoded_to_intrafind(self):341        handler = zeit.intrafind.testing.RequestHandler342        content = self.get_content()343        tagger = self.get_tagger(content)344        tagger.update()345        self.assertEqual(1, len(handler.posts_received))346        self.assertTrue(handler.posts_received[0]['data'].startswith(347            'xml=%3C%3Fxml+version'))348    def test_update_should_extract_tags_from_response(self):349        handler = zeit.intrafind.testing.RequestHandler350        handler.response_body = pkg_resources.resource_string(351            __name__, 'tagger_response.xml')352        content = self.get_content()353        tagger = self.get_tagger(content)354        tagger.update()355        self.assertEqual(6, len(tagger))356    def test_update_should_clear_disabled_tags(self):357        from zeit.connector.interfaces import IWebDAVProperties358        content = self.get_content()359        self.set_tags(content, """360<tag uuid="uid-karenduve">Karen Duve</tag>""")361        tagger = self.get_tagger(content)362        del tagger['uid-karenduve']363        tagger.update()364        dav = IWebDAVProperties(content)365        dav_key = ('disabled', 'http://namespaces.zeit.de/CMS/tagging')366        self.assertEqual('', dav[dav_key])367    def test_update_should_keep_pinned_tags(self):368        content = self.get_content()369        self.set_tags(content, """370<tag uuid="uid-karenduve">Karen Duve</tag>""")371        tagger = self.get_tagger(content)372        tagger.set_pinned(['uid-karenduve'])373        tagger.update()374        self.assertEqual(['uid-karenduve'], list(tagger))375    def test_update_should_not_duplicate_pinned_tags(self):376        # this is a rather tricky edge case:377        # when we pin a manual tag first, and then also pin a tag that378        # comes in via update() again, we used to screw it up,379        # since we compared against a generator multiple times380        from zeit.intrafind.tag import Tag381        handler = zeit.intrafind.testing.RequestHandler382        handler.response_body = pkg_resources.resource_string(383            __name__, 'tagger_response.xml')384        content = self.get_content()385        tagger = self.get_tagger(content)386        tagger.update()387        self.assertEqual(6, len(tagger))388        tagger['uid-karenduve'] = Tag('uid-karenduve', 'Karen Duve')389        tagger.set_pinned([390            'uid-karenduve', 'aa23f170-65a2-4a00-8fec-006a3b4b73d2'])391        tagger.update()392        self.assertEqual(393            [u'Feiertag', u'USA', u'Post',394             u'Verlag', u'New York', u'Reims', u'Karen Duve'],...repec2bibtex.py
Source:repec2bibtex.py  
...6from bs4 import BeautifulSoup7import re8# Util functions9# %%10def get_content(x, soup):11    try:12         return soup.find("meta", {"name" : x})["content"]13    except:14        return ""15def print_authors(x):16    text = ""17    for (i, author) in enumerate(x):18        if i>0:19            text = text + " and "+author20        else:21            text = author22    return text23def trim_name(x):24    return re.sub('[^a-zA-Z]+', '', x.lower())25def last_names(x):26    text = ""27    for (i, author) in enumerate(x):28        ln = trim_name(author.split(',')[0])29        if i>0:30            text = text + "&"+ln31        else:32            text = ln33    return text.replace(" ", "")34def last_clean(x):35    return x.replace("&", "\&")36def get_bibtex(link):37    page = requests.get(link)38    soup = BeautifulSoup(page.content, 'html.parser')39    # To get the data40    each_author = [s["content"] for s in soup.find_all("meta", {"name" : "citation_author"})]41    citation_title              = get_content("citation_title", soup)42    citation_authors            = get_content("citation_authors", soup)43    citation_date               = get_content("citation_date", soup)44    citation_year               = get_content("citation_year", soup)45    citation_publication_date   = get_content("citation_publication_date", soup)46    citation_publisher          = ""47    publisher                   = get_content("citation_publisher", soup)48    if publisher=="":49        publisher               = get_content("citation_technical_report_institution", soup)50    51    citation_journal_title      = get_content("citation_journal_title", soup)52    citation_issue              = get_content("citation_issue", soup)53    citation_firstpage          = get_content("citation_firstpage", soup)54    citation_lastpage           = get_content("citation_lastpage", soup)55    is_edited                   = get_content("is_edited", soup)56    series                      = get_content("series", soup)57    keywords                    = get_content("key_words", soup)58    paper_url                   = get_content("citation_abstract_html_url", soup)59    abstract                    = get_content("citation_abstract", soup)60    volume                      = get_content("citation_volume", soup)61    number                      = get_content("wp_number", soup)62    page_start                  = get_content("citation_firstpage", soup)63    page_end                    = get_content("citation_lastpage", soup)64    edition                     = get_content("book_edition", soup)65    book_title                  = get_content("book_title", soup)66    book_editor                 = get_content("book_editor", soup)67    chapter                     = get_content("chapter", soup)68    type_ref                    = get_content("redif-type", soup)69    #Small changes after testing70    # i) avoid the & character71    if type_ref == "article":72        handle = f"""73                    @ARTICLE{{{last_names(each_author)+citation_year},74                    title = {{ {last_clean(citation_title)} }},75                    author = {{ {last_clean(print_authors(each_author))} }},76                    year = {{ {last_clean(citation_year)} }},77                    journal = {{ {last_clean(citation_journal_title)} }},78                    volume = {{ {last_clean(volume)} }},79                    number = {{ {last_clean(series)} }},80                    pages = {{ {page_start}-{page_end} }},81                    abstract = {{ {last_clean(abstract)} }},82                    url = {{ {paper_url} }}...jingdong_detail.py
Source:jingdong_detail.py  
...54        hxs = Selector(response)55        i = ProductDetail()56        i['link_hash'] = response.meta['link_hash']57        i['product_url'] = response.url58        i['product_name'] = get_content(hxs.xpath(self._xpath_product_name).extract())59        i['product_code'] = response.meta['product_code']60        i['product_description'] = ''61        amount = get_content(hxs.xpath(self._xpath_investment_amount).extract())62        i['investment_amount'] = get_money(amount)63        amount_min = get_content(hxs.xpath(self._xpath_investment_amount_min).extract())64        i['investment_amount_min'] = get_money(amount_min)65        amount_max = get_content(hxs.xpath(self._xpath_investment_amount_max).extract())66        i['investment_amount_max'] = get_money(amount_max)67        i['investment_period'] = get_content(hxs.xpath(self._xpath_investment_period).extract())68        i['annualized_return_rate_min'] = get_content(hxs.xpath(self._xpath_annualized_return_rate_min).extract())69        i['annualized_return_rate_max'] = get_content(hxs.xpath(self._xpath_annualized_return_rate_max).extract())70        i['safeguard_mode'] = 071        i['safeguard_mode_text'] = get_content(hxs.xpath(self._xpath_safeguard_mode_text).extract())72        i['publish_period'] = get_content(hxs.xpath(self._xpath_publish_period).extract())73        i['start_date'] = get_content(hxs.xpath(self._xpath_start_date).extract())74        i['end_date'] = get_content(hxs.xpath(self._xpath_end_date).extract())75        remain_amount = get_content(hxs.xpath(self._xpath_remain_amount).extract())76        i['remain_amount'] = get_money(remain_amount)77        i['remain_time'] = ''78        i['progress'] = get_content(hxs.xpath(self._xpath_progress).extract())79        i['promotion'] = ''80        i['created_at'] = now()81        i['created_by'] = 'autoload'82        i['updated_at'] = now()83        i['updated_by'] = 'autoload'84        #i['failed_times'] = 085        i['invest_num'] = get_content(hxs.xpath(self._xpath_num).extract())86        i['product_status'] = '0'87        #i['version'] = 0...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!!
