# Path: matl_online/public/models.py
# class DocumentationLink(Model):
#     """Model for storing hyperlinks to MATLAB's documentation."""
# 
#     __tablenamme__ = 'doclinks'
# 
#     id = Column(db.Integer, primary_key=True)
#     name = Column(db.String, unique=True, nullable=False)
#     link = Column(db.String, nullable=False)
# 
#     @classmethod
#     def refresh(cls):
#         """Fetch updated documentation from the Mathworks."""
#         # Flip the order of the links so that the first URL listed is the
#         # highest priority and will take precedence
#         for url in current_app.config['MATLAB_DOC_LINKS'][::-1]:
#             resp = requests.get(url)
#             soup = BeautifulSoup(resp.text, 'html.parser')
# 
#             terms = soup.findAll('td', {'class': 'term'})
#             links = [term.find('a') for term in terms]
# 
#             for link in links:
# 
#                 function = link.text.rstrip()
# 
#                 doc = cls.query.filter_by(name=function).first()
#                 doc_url = urljoin(url, link['href'])
# 
#                 # Create an entry if one doesn't already exist
#                 if doc is None:
#                     doc = cls(name=function)
# 
#                 doc.link = doc_url
#                 doc.save()
# 
#         # Make sure to remove i and j entries
#         toremove = cls.query.filter(or_(cls.name == 'i', cls.name == 'j')).all()
#         for item in toremove:
#             item.delete()
# 
#         return cls.query.all()
# 
# Path: tests/factories.py
# class DocumentationLinkFactory(BaseFactory):
#     """Factory for creating DocumentationLink objects on demand."""
# 
#     # The default link will simply append .html to the end of the name
#     link = LazyAttribute(lambda o: o.name + '.html')
# 
#     class Meta:
#         """Meta information for the factory."""
# 
#         model = DocumentationLink


import pytest
from matl_online.public.models import DocumentationLink
from .factories import DocumentationLinkFactory as DocLink
"""Unit tests for checking database model for links to external documentation."""




@pytest.mark.usefixtures('db')
class TestDocumentationLink:
    """Series of tests for the DocumentationLink database model."""

    def test_refresh_single(self, mocker, testapp):
        """Handle parsing a single doc entry."""
        req = mocker.patch('matl_online.matl.requests.get')
        testapp.app.config['MATLAB_DOC_LINKS'] = ['http://mathworks.com']

        funcname = 'func'
        funclink = 'http://link.html'

        req.return_value.text = _create_html([(funcname, funclink)])


# Path: matl_online/public/models.py
# class DocumentationLink(Model):
#     """Model for storing hyperlinks to MATLAB's documentation."""
# 
#     __tablenamme__ = 'doclinks'
# 
#     id = Column(db.Integer, primary_key=True)
#     name = Column(db.String, unique=True, nullable=False)
#     link = Column(db.String, nullable=False)
# 
#     @classmethod
#     def refresh(cls):
#         """Fetch updated documentation from the Mathworks."""
#         # Flip the order of the links so that the first URL listed is the
#         # highest priority and will take precedence
#         for url in current_app.config['MATLAB_DOC_LINKS'][::-1]:
#             resp = requests.get(url)
#             soup = BeautifulSoup(resp.text, 'html.parser')
# 
#             terms = soup.findAll('td', {'class': 'term'})
#             links = [term.find('a') for term in terms]
# 
#             for link in links:
# 
#                 function = link.text.rstrip()
# 
#                 doc = cls.query.filter_by(name=function).first()
#                 doc_url = urljoin(url, link['href'])
# 
#                 # Create an entry if one doesn't already exist
#                 if doc is None:
#                     doc = cls(name=function)
# 
#                 doc.link = doc_url
#                 doc.save()
# 
#         # Make sure to remove i and j entries
#         toremove = cls.query.filter(or_(cls.name == 'i', cls.name == 'j')).all()
#         for item in toremove:
#             item.delete()
# 
#         return cls.query.all()
# 
# Path: tests/factories.py
# class DocumentationLinkFactory(BaseFactory):
#     """Factory for creating DocumentationLink objects on demand."""
# 
#     # The default link will simply append .html to the end of the name
#     link = LazyAttribute(lambda o: o.name + '.html')
# 
#     class Meta:
#         """Meta information for the factory."""
# 
#         model = DocumentationLink


# Path: tests/test_doclinks.py
import pytest
from matl_online.public.models import DocumentationLink
from .factories import DocumentationLinkFactory as DocLink

"""Unit tests for checking database model for links to external documentation."""




@pytest.mark.usefixtures('db')
class TestDocumentationLink:
    """Series of tests for the DocumentationLink database model."""

    def test_refresh_single(self, mocker, testapp):
        """Handle parsing a single doc entry."""
        req = mocker.patch('matl_online.matl.requests.get')
        testapp.app.config['MATLAB_DOC_LINKS'] = ['http://mathworks.com']

        funcname = 'func'
        funclink = 'http://link.html'

        req.return_value.text = _create_html([(funcname, funclink)])


        out = DocumentationLink.refresh()