# Path: suma/core/models/link.py
# class Link(Base, TimestampColumns):
#     hashid = Column(String, index=True, unique=True, nullable=False)
#     _url = Column('url', Unicode, index=True, nullable=False)
#     user_id = Column(BigInteger)
#     _hash = Column('hash', String, index=True, nullable=False)
#     _hash_netloc = Column('hash_netloc', String, nullable=False)
#     _hash_path = Column('hash_path', String, nullable=False)
#     meta = Column(MutableDict.as_mutable(JSONB), default={})
#     __table_args__ = (
#         Index('unique_url_and_user_id', _url, user_id, unique=True,
#               postgresql_where=(user_id != None)),
#         Index('unique_url_and_user_id_is_null', 'url', unique=True,
#               postgresql_where=(user_id == None))
#     )
# 
#     is_banned = column_property(
#         exists().where(
#             or_(
#                 BlockedLink.hash.in_([_hash, _hash_netloc, _hash_path])
#             )
#         )
#     )
# 
#     @property
#     def clicks(self):
#         if self._clicks:
#             return self._clicks.counter
#         return 0
#     
#     @hybrid_property
#     def url(self):
#         return self._url
# 
#     @url.setter
#     def url(self, value):
#         self._url = value
#         self._hash, self._hash_netloc, self._hash_path = create_url_hashes(value)
# 
# Path: suma/core/models/stat.py
# class Stat(Base, CreatedColumn):
#     link_id = Column(ForeignKey(Link.id), nullable=False)
#     link = relationship(Link, backref=backref('stats'))
#     referer = Column(Unicode)
#     ip = Column(INET)
# 
# class ClickCounter(Base):
#     id = Column(ForeignKey(Link.id), primary_key=True, nullable=False)
#     link = relationship(Link, backref=backref('_clicks', uselist=False, lazy='joined'))
#     counter = Column(BigInteger, default=1)


from suma.core.models import Link, ClickCounter, Stat


def test_add_click(db_session, stat_svc):
    link = Link(url='http://google.com/test', hashid='1234')
    db_session.add(link)
    db_session.flush()
    stat_svc.add_click(link.id)
    assert db_session.query(Stat).count() == 1
    assert db_session.query(ClickCounter).count() == 1
    assert db_session.query(ClickCounter).get(link.id).counter == 1
    stat_svc.add_click(link.id)
    assert db_session.query(ClickCounter).count() == 1

# Path: suma/core/models/link.py
# class Link(Base, TimestampColumns):
#     hashid = Column(String, index=True, unique=True, nullable=False)
#     _url = Column('url', Unicode, index=True, nullable=False)
#     user_id = Column(BigInteger)
#     _hash = Column('hash', String, index=True, nullable=False)
#     _hash_netloc = Column('hash_netloc', String, nullable=False)
#     _hash_path = Column('hash_path', String, nullable=False)
#     meta = Column(MutableDict.as_mutable(JSONB), default={})
#     __table_args__ = (
#         Index('unique_url_and_user_id', _url, user_id, unique=True,
#               postgresql_where=(user_id != None)),
#         Index('unique_url_and_user_id_is_null', 'url', unique=True,
#               postgresql_where=(user_id == None))
#     )
# 
#     is_banned = column_property(
#         exists().where(
#             or_(
#                 BlockedLink.hash.in_([_hash, _hash_netloc, _hash_path])
#             )
#         )
#     )
# 
#     @property
#     def clicks(self):
#         if self._clicks:
#             return self._clicks.counter
#         return 0
#     
#     @hybrid_property
#     def url(self):
#         return self._url
# 
#     @url.setter
#     def url(self, value):
#         self._url = value
#         self._hash, self._hash_netloc, self._hash_path = create_url_hashes(value)
# 
# Path: suma/core/models/stat.py
# class Stat(Base, CreatedColumn):
#     link_id = Column(ForeignKey(Link.id), nullable=False)
#     link = relationship(Link, backref=backref('stats'))
#     referer = Column(Unicode)
#     ip = Column(INET)
# 
# class ClickCounter(Base):
#     id = Column(ForeignKey(Link.id), primary_key=True, nullable=False)
#     link = relationship(Link, backref=backref('_clicks', uselist=False, lazy='joined'))
#     counter = Column(BigInteger, default=1)


# Path: tests/core/services/test_stat_service.py
from suma.core.models import Link, ClickCounter, Stat



def test_add_click(db_session, stat_svc):
    link = Link(url='http://google.com/test', hashid='1234')
    db_session.add(link)
    db_session.flush()
    stat_svc.add_click(link.id)
    assert db_session.query(Stat).count() == 1
    assert db_session.query(ClickCounter).count() == 1
    assert db_session.query(ClickCounter).get(link.id).counter == 1
    stat_svc.add_click(link.id)
    assert db_session.query(ClickCounter).count() == 1

    assert db_session.query(ClickCounter).get(link.id).counter == 2