Beispiel #1
0
 def setUp(self):
     """
     Create a test Article instance
     """
     self.article = Article(
         bibcode="2013A&A...552A.143S",
         year=2013,
         first_author="Sudilovsky, V.",
         author=[
             "Sudilovsky, V",
             "Greiner, J",
             "Rau, A",
             "Salvato, M",
             "Savaglio, S",
             "Vergani, S",
             "Schady, P",
             "Elliott, J",
             "Kruehler, T",
             "Kann, D",
             "Klose, S",
             "Rossi, A",
             "Filgas, R",
             "Schmidl, S"
         ],
     )
Beispiel #2
0
 def test_equals(self):
     """
     the __eq__ method should compare bibcodes, and raise if bibcode isn't
     defined
     """
     self.assertNotEqual(Article(bibcode="Not the same"), self.article)
     self.assertEqual(Article(bibcode="2013A&A...552A.143S"), self.article)
     with self.assertRaises(TypeError):
         Article() == self.article
Beispiel #3
0
 def test_equals(self):
     """
     the __eq__ method should compare bibcodes, and raise if bibcode isn't
     defined or is None
     """
     self.assertNotEqual(Article(bibcode="Not the same"), self.article)
     self.assertEqual(Article(bibcode="2013A&A...552A.143S"), self.article)
     with self.assertRaisesRegexp(TypeError, "Cannot compare articles without bibcodes"):
         Article() == self.article
     with self.assertRaisesRegexp(TypeError, "Cannot compare articles without bibcodes"):
         Article(bibcode=None) == self.article
Beispiel #4
0
 def test_print_methods(self):
     """
     the class should return a user-friendly formatted identified when the
     __str__ or __unicode__ methods are called
     """
     self.assertEqual('<Sudilovsky, V. et al. 2013, 2013A&A...552A.143S>',
                      self.article.__str__())
     self.assertEqual(self.article.__unicode__(), self.article.__str__())
     self.assertEqual(Article().__str__(),
                      "<Unknown author Unknown year, Unknown bibcode>")
Beispiel #5
0
class TestArticle(unittest.TestCase):
    """
    Test the Article object
    """

    def setUp(self):
        """
        Create a test Article instance
        """
        self.article = Article(
            bibcode="2013A&A...552A.143S",
            year=2013,
            first_author="Sudilovsky, V.",
            author=[
                "Sudilovsky, V",
                "Greiner, J",
                "Rau, A",
                "Salvato, M",
                "Savaglio, S",
                "Vergani, S",
                "Schady, P",
                "Elliott, J",
                "Kruehler, T",
                "Kann, D",
                "Klose, S",
                "Rossi, A",
                "Filgas, R",
                "Schmidl, S"
            ],
        )

    def test_equals(self):
        """
        the __eq__ method should compare bibcodes, and raise if bibcode isn't
        defined
        """
        self.assertNotEqual(Article(bibcode="Not the same"), self.article)
        self.assertEqual(Article(bibcode="2013A&A...552A.143S"), self.article)
        with self.assertRaises(TypeError):
            Article() == self.article

    def test_init(self):
        """
        after init ._raw should be a dict containing a subset of all
        class attributes
        """
        for key, value in six.iteritems(self.article._raw):
            self.assertEqual(
                self.article.__getattribute__(key),
                value,
                msg="Instance attribute and _raw mismatch on {}".format(key)
            )

    def test_print_methods(self):
        """
        the class should return a user-friendly formatted identified when the
        __str__ or __unicode__ methods are called
        """
        self.assertEqual(
            '<Sudilovsky, V. et al. 2013, 2013A&A...552A.143S>',
            self.article.__str__()
        )
        self.assertEqual(self.article.__unicode__(), self.article.__str__())

    @patch('ads.search.Article._get_field')
    def test_cached_properties(self, patched):
        """
        The underlying computation responsible for filling in cached properties
        should be called once and only once per attribute.
        In addition, this computation should only be called if the instance
        attribute wasn't set elsewhere.
        """
        patched.return_value = "patched response"
        self.assertEqual(self.article.bibcode, '2013A&A...552A.143S')
        self.assertEqual(self.article.aff, 'patched response')
        self.assertEqual(self.article.aff, 'patched response')
        patched.assert_called_once_with('aff')

    def test_get_field(self):
        """
        The helper method _get_field() should return the value of a field
        based on its `id`
        """
        with self.assertRaisesRegexp(
                APIResponseError,
                "Cannot query an article without an id"):
            self.article._get_field('read_count')

        # Note that our mock solr server doesn't do anything with "q", and
        # therefore will just return the first article in the hardcoded
        # stubdata. We assume the live service will return the correct document.
        self.article.id = 9535116
        with MockSolrResponse(SEARCH_URL):
            self.assertEqual(self.article.pubdate, '1971-10-00')
            self.assertEqual(self.article.read_count, 0.0)
            self.assertIsNone(self.article.issue)
Beispiel #6
0
class TestArticle(unittest.TestCase):
    """
    Test the Article object
    """
    def setUp(self):
        """
        Create a test Article instance
        """
        self.article = Article(
            bibcode="2013A&A...552A.143S",
            year=2013,
            first_author="Sudilovsky, V.",
            author=[
                "Sudilovsky, V", "Greiner, J", "Rau, A", "Salvato, M",
                "Savaglio, S", "Vergani, S", "Schady, P", "Elliott, J",
                "Kruehler, T", "Kann, D", "Klose, S", "Rossi, A", "Filgas, R",
                "Schmidl, S"
            ],
        )

    def test_equals(self):
        """
        the __eq__ method should compare bibcodes, and raise if bibcode isn't
        defined or is None
        """
        self.assertNotEqual(Article(bibcode="Not the same"), self.article)
        self.assertEqual(Article(bibcode="2013A&A...552A.143S"), self.article)
        with six.assertRaisesRegex(self, TypeError,
                                   "Cannot compare articles without bibcodes"):
            Article() == self.article
        with six.assertRaisesRegex(self, TypeError,
                                   "Cannot compare articles without bibcodes"):
            Article(bibcode=None) == self.article

    def test_init(self):
        """
        after init ._raw should be a dict containing a subset of all
        class attributes
        """
        for key, value in six.iteritems(self.article._raw):
            self.assertEqual(
                self.article.__getattribute__(key),
                value,
                msg="Instance attribute and _raw mismatch on {}".format(key))

    def test_print_methods(self):
        """
        the class should return a user-friendly formatted identified when the
        __str__ or __unicode__ methods are called
        """
        self.assertEqual('<Sudilovsky, V. et al. 2013, 2013A&A...552A.143S>',
                         self.article.__str__())
        self.assertEqual(self.article.__unicode__(), self.article.__str__())
        self.article.first_author = None
        self.article.author = None
        self.article.bibcode = None
        self.article.year = None

        self.assertEqual(self.article.__str__(),
                         "<Unknown author Unknown year, Unknown bibcode>")

        # accessing print methods should use lazy loaded attributes: issue#98
        sq = SearchQuery(q="unittest", rows=1, fl=["bibcode"])
        with MockSolrResponse(sq.HTTP_ENDPOINT):
            article = list(sq)[0]
            self.assertEqual(
                article.__unicode__(),
                '<Sudilovsky, Oscar et al. 1971, 1971Sci...174..142S>')

    @patch('ads.search.Article._get_field')
    def test_cached_properties(self, patched):
        """
        The underlying computation responsible for filling in cached properties
        should be called once and only once per attribute.
        In addition, this computation should only be called if the instance
        attribute wasn't set elsewhere.
        """
        patched.return_value = "patched response"
        # explicitly set attributes shouldn't be managed by cached_property
        self.article.year
        self.assertEqual(patched.call_count, 0)

        self.assertEqual(self.article.aff, 'patched response')
        self.assertEqual(self.article.aff, 'patched response')
        patched.assert_called_once_with('aff')
        self.assertEqual(patched.call_count, 1)

    def test_get_field(self):
        """
        The helper method _get_field() should return the value of a field
        based on its `id`
        """
        with six.assertRaisesRegex(self, APIResponseError,
                                   "Cannot query an article without an id"):
            self.article._get_field('read_count')

        # Note that our mock solr server doesn't do anything with "q", and
        # therefore will just return the first article in the hardcoded
        # stubdata. We assume the live service will return the correct document.
        self.article.id = 9535116
        with MockSolrResponse(SEARCH_URL):
            self.assertEqual(self.article.pubdate, '1971-10-00')
            self.assertEqual(self.article.read_count, 0.0)
            self.assertIsNone(self.article.issue)

    def test_get_field_bibtex(self):
        """
        should emit a warning when calling _get_field with bibtex but otherwise work
        as expected both in the case of fl=bibtex and fl=None
        """
        for fl in [None, "bibtex"]:
            sq = SearchQuery(q="*", fl=fl)
            with warnings.catch_warnings(record=True) as w:
                with MockSolrResponse(SEARCH_URL):
                    with MockExportResponse(
                            "{base}bibtex".format(base=EXPORT_URL)):
                        article = next(sq)
                        article.bibtex
                if six.PY3:
                    msg = w[1].message.args[0]
                    self.assertEqual(
                        msg,
                        "bibtex should be queried with ads.ExportQuery(); "
                        "You will hit API ratelimits very quickly otherwise.")
                self.assertTrue(
                    article.bibtex.startswith("@ARTICLE{2013A&A...552A.143S"))
Beispiel #7
0
class TestArticle(unittest.TestCase):
    """
    Test the Article object
    """

    def setUp(self):
        """
        Create a test Article instance
        """
        self.article = Article(
            bibcode="2013A&A...552A.143S",
            year=2013,
            first_author="Sudilovsky, V.",
            author=[
                "Sudilovsky, V",
                "Greiner, J",
                "Rau, A",
                "Salvato, M",
                "Savaglio, S",
                "Vergani, S",
                "Schady, P",
                "Elliott, J",
                "Kruehler, T",
                "Kann, D",
                "Klose, S",
                "Rossi, A",
                "Filgas, R",
                "Schmidl, S"
            ],
        )

    def test_equals(self):
        """
        the __eq__ method should compare bibcodes, and raise if bibcode isn't
        defined or is None
        """
        self.assertNotEqual(Article(bibcode="Not the same"), self.article)
        self.assertEqual(Article(bibcode="2013A&A...552A.143S"), self.article)
        with self.assertRaisesRegexp(TypeError, "Cannot compare articles without bibcodes"):
            Article() == self.article
        with self.assertRaisesRegexp(TypeError, "Cannot compare articles without bibcodes"):
            Article(bibcode=None) == self.article

    def test_init(self):
        """
        after init ._raw should be a dict containing a subset of all
        class attributes
        """
        for key, value in six.iteritems(self.article._raw):
            self.assertEqual(
                self.article.__getattribute__(key),
                value,
                msg="Instance attribute and _raw mismatch on {}".format(key)
            )

    def test_print_methods(self):
        """
        the class should return a user-friendly formatted identified when the
        __str__ or __unicode__ methods are called
        """
        self.assertEqual(
            '<Sudilovsky, V. et al. 2013, 2013A&A...552A.143S>',
            self.article.__str__()
        )
        self.assertEqual(self.article.__unicode__(), self.article.__str__())
        self.assertEqual(
            Article().__str__(),
            "<Unknown author Unknown year, Unknown bibcode>"
        )

    @patch('ads.search.Article._get_field')
    def test_cached_properties(self, patched):
        """
        The underlying computation responsible for filling in cached properties
        should be called once and only once per attribute.
        In addition, this computation should only be called if the instance
        attribute wasn't set elsewhere.
        """
        patched.return_value = "patched response"
        # explicitly set attributes shouldn't be managed by cached_property
        self.article.year
        self.assertEqual(patched.call_count, 0)

        self.assertEqual(self.article.aff, 'patched response')
        self.assertEqual(self.article.aff, 'patched response')
        patched.assert_called_once_with('aff')
        self.assertEqual(patched.call_count, 1)

    def test_get_field(self):
        """
        The helper method _get_field() should return the value of a field
        based on its `id`
        """
        with self.assertRaisesRegexp(
                APIResponseError,
                "Cannot query an article without an id"):
            self.article._get_field('read_count')

        # Note that our mock solr server doesn't do anything with "q", and
        # therefore will just return the first article in the hardcoded
        # stubdata. We assume the live service will return the correct document.
        self.article.id = 9535116
        with MockSolrResponse(SEARCH_URL):
            self.assertEqual(self.article.pubdate, '1971-10-00')
            self.assertEqual(self.article.read_count, 0.0)
            self.assertIsNone(self.article.issue)

    def test_get_field_bibtex(self):
        """
        should emit a warning when calling _get_field with bibtex but otherwise work
        as expected both in the case of fl=bibtex and fl=None
        """
        for fl in [None, "bibtex"]:
            sq = SearchQuery(q="*", fl=fl)
            with warnings.catch_warnings(record=True) as w:
                with MockSolrResponse(SEARCH_URL):
                    with MockExportResponse("{base}bibtex".format(base=EXPORT_URL)):
                        article = next(sq)
                        article.bibtex
                if six.PY3:
                    msg = w[1].message.args[0]
                    self.assertEqual(msg, "bibtex should be queried with ads.ExportQuery(); "
                                          "You will hit API ratelimits very quickly otherwise.")
                self.assertTrue(article.bibtex.startswith("@ARTICLE{2013A&A...552A.143S"))