# Path: voeventdb/server/database/convenience.py
# def ivorn_present(session, ivorn):
#     """
#     Predicate, returns whether the IVORN is in the database.
#     """
#     return bool(
#         session.query(Voevent.id).filter(Voevent.ivorn == ivorn).count())
# 
# def ivorn_prefix_present(session, ivorn_prefix):
#     """
#     Predicate, returns whether there is an entry in the database with matching
#     IVORN prefix.
#     """
#     n_matches = session.query(Voevent.ivorn).filter(
#             Voevent.ivorn.like('{}%'.format(ivorn_prefix))).count()
#     return bool(n_matches)
# 
# def safe_insert_voevent(session, etree):
#     """
#     Insert a VOEvent, or skip with a warning if it's a duplicate.
# 
#     NB XML contents are checked to confirm duplication - if there's a mismatch,
#     we raise a ValueError.
#     """
#     new_row = Voevent.from_etree(etree)
#     if not ivorn_present(session, new_row.ivorn):
#         session.add(new_row)
#     else:
#         old_xml = session.query(Voevent.xml).filter(
#             Voevent.ivorn == new_row.ivorn).scalar()
#         if old_xml != new_row.xml:
#             raise ValueError('Tried to load a VOEvent with duplicate IVORN,'
#                              'but XML contents differ - not clear what to do.')
#         else:
#             logger.warning('Skipping insert for packet with duplicate IVORN, '
#                         'XML matches OK.')


from voeventdb.server.database.convenience import (
    ivorn_present,
    ivorn_prefix_present,
    safe_insert_voevent,
)
import voeventdb.server.database.convenience as convenience
import voeventdb.server.database.query as query
import copy
import pytest
import logging


class TestConvenienceFuncs:
    def test_ivorn_present(self, fixture_db_session, simple_populated_db):
        s = fixture_db_session
        dbinf = simple_populated_db
        assert ivorn_present(s, dbinf.inserted_ivorns[0]) == True
        assert ivorn_present(s, dbinf.absent_ivorn) == False

    def test_ivorn_prefix_present(self, fixture_db_session, simple_populated_db):
        s = fixture_db_session
        dbinf = simple_populated_db
        assert ivorn_prefix_present(s, dbinf.inserted_ivorns[0]) == True
        assert ivorn_prefix_present(s, dbinf.inserted_ivorns[0][:-3]) == True
        assert ivorn_prefix_present(s, dbinf.absent_ivorn) == False

    def test_safe_insert(self, fixture_db_session, simple_populated_db,
                         caplog
                         ):
        s = fixture_db_session
        dbinf = simple_populated_db
        nlogs = len(caplog.records)
        with caplog.at_level(logging.WARNING):
            safe_insert_voevent(s, dbinf.insert_packets[0])
        assert len(caplog.records) == nlogs + 1
        assert caplog.records[-1].levelname == 'WARNING'

        bad_packet = copy.copy(dbinf.insert_packets[0])
        bad_packet.Who.AuthorIVORN = 'ivo://foo.bar'
        with pytest.raises(ValueError):

# Path: voeventdb/server/database/convenience.py
# def ivorn_present(session, ivorn):
#     """
#     Predicate, returns whether the IVORN is in the database.
#     """
#     return bool(
#         session.query(Voevent.id).filter(Voevent.ivorn == ivorn).count())
# 
# def ivorn_prefix_present(session, ivorn_prefix):
#     """
#     Predicate, returns whether there is an entry in the database with matching
#     IVORN prefix.
#     """
#     n_matches = session.query(Voevent.ivorn).filter(
#             Voevent.ivorn.like('{}%'.format(ivorn_prefix))).count()
#     return bool(n_matches)
# 
# def safe_insert_voevent(session, etree):
#     """
#     Insert a VOEvent, or skip with a warning if it's a duplicate.
# 
#     NB XML contents are checked to confirm duplication - if there's a mismatch,
#     we raise a ValueError.
#     """
#     new_row = Voevent.from_etree(etree)
#     if not ivorn_present(session, new_row.ivorn):
#         session.add(new_row)
#     else:
#         old_xml = session.query(Voevent.xml).filter(
#             Voevent.ivorn == new_row.ivorn).scalar()
#         if old_xml != new_row.xml:
#             raise ValueError('Tried to load a VOEvent with duplicate IVORN,'
#                              'but XML contents differ - not clear what to do.')
#         else:
#             logger.warning('Skipping insert for packet with duplicate IVORN, '
#                         'XML matches OK.')


# Path: voeventdb/server/tests/test_database/test_db_complex_queries.py
from voeventdb.server.database.convenience import (
    ivorn_present,
    ivorn_prefix_present,
    safe_insert_voevent,
)
import voeventdb.server.database.convenience as convenience
import voeventdb.server.database.query as query
import copy
import pytest
import logging



class TestConvenienceFuncs:
    def test_ivorn_present(self, fixture_db_session, simple_populated_db):
        s = fixture_db_session
        dbinf = simple_populated_db
        assert ivorn_present(s, dbinf.inserted_ivorns[0]) == True
        assert ivorn_present(s, dbinf.absent_ivorn) == False

    def test_ivorn_prefix_present(self, fixture_db_session, simple_populated_db):
        s = fixture_db_session
        dbinf = simple_populated_db
        assert ivorn_prefix_present(s, dbinf.inserted_ivorns[0]) == True
        assert ivorn_prefix_present(s, dbinf.inserted_ivorns[0][:-3]) == True
        assert ivorn_prefix_present(s, dbinf.absent_ivorn) == False

    def test_safe_insert(self, fixture_db_session, simple_populated_db,
                         caplog
                         ):
        s = fixture_db_session
        dbinf = simple_populated_db
        nlogs = len(caplog.records)
        with caplog.at_level(logging.WARNING):
            safe_insert_voevent(s, dbinf.insert_packets[0])
        assert len(caplog.records) == nlogs + 1
        assert caplog.records[-1].levelname == 'WARNING'

        bad_packet = copy.copy(dbinf.insert_packets[0])
        bad_packet.Who.AuthorIVORN = 'ivo://foo.bar'
        with pytest.raises(ValueError):

            safe_insert_voevent(s, bad_packet)