# -*- coding: utf-8 -*-
#

import mox
import pytest

import sqlalchemy.exc

import avalon.models
import avalon.tags.insert


class DummySession(object):

    def query(self, cls):
        pass

    def commit(self):
        pass

    def close(self):
        pass


class DummyQuery(object):

    def delete(self):
        pass


class TestCleaner(object):

    def setup_method(self, method):
        self.mox = mox.Mox()

    def teardown_method(self, method):
        self.mox.UnsetStubs()

    def test_clean_without_error(self):
        session_handler = self.mox.CreateMock(avalon.models.SessionHandler)
        session = self.mox.CreateMock(DummySession)
        query = self.mox.CreateMock(DummyQuery)

        session_handler.get_session().AndReturn(session)
        session.query(avalon.models.Album).AndReturn(query)
        query.delete()
        session.commit()
        session_handler.close(session)
        self.mox.ReplayAll()

        clean = avalon.tags.insert.Cleaner(session_handler)
        clean.clean_type(avalon.models.Album)
        self.mox.VerifyAll()

    def test_clean_with_error(self):
        session_handler = self.mox.CreateMock(avalon.models.SessionHandler)
        session = self.mox.CreateMock(DummySession)
        err = sqlalchemy.exc.SQLAlchemyError()

        session_handler.get_session().AndReturn(session)
        session.query(avalon.models.Album).AndRaise(err)
        session_handler.close(session)
        self.mox.ReplayAll()

        clean = avalon.tags.insert.Cleaner(session_handler)
        with pytest.raises(sqlalchemy.exc.SQLAlchemyError):
            clean.clean_type(avalon.models.Album)
        self.mox.VerifyAll()


class TestTrackFieldLoader(object):

    def test_insert_without_error(self):
        pass

    def test_insert_with_error(self):
        pass


class TestTrackLoader(object):
    pass




@@ -315,32 +315,330 @@
 %0A        pass%0A%0A%0A
+class MockTag(object):%0A%0A    def __init__(self):%0A        self.path = None%0A        self.album = None%0A        self.artist = None%0A        self.genre = None%0A        self.title = None%0A        self.track = None%0A        self.year = None%0A        self.length = None%0A%0A%0Adef id_gen_mock(val):%0A    return None%0A%0A%0A
 class TestCleane
@@ -2018,33 +2018,924 @@
 def 
-test_insert_witho
+setup_method(self, method):%0A        self.mox = mox.Mox()%0A%0A    def teardown_method(self, method):%0A        self.mox.UnsetStubs()%0A%0A    def test_insert_invalid_attribute_error(self):%0A        tag = MockTag()%0A        tag.path = unicode('/home/something/music/song.flac')%0A        tag.album = unicode('True North')%0A        tag.artist = unicode('Bad Religion')%0A        tag.genre = unicode('Punk')%0A        tag.length = 115%0A        tag.title = unicode('True North')%0A        tag.track = 1%0A        tag.year = 2013%0A%0A        session_handler = self.mox.CreateMock(avalon.models.SessionHandler)%0A        model_cls = self.mox.CreateMockAnything(avalon.models.Album)%0A%0A        inserter = avalon.tags.insert.TrackFieldLoader(session_handler, %5Btag%5D)%0A%0A        with pytest.raises(AttributeError):%0A            inserter.insert(model_cls, id_gen_mock, 'blah')%0A        self.mox.VerifyAll()%0A%0A%0A    def test_insert_invalid_attrib
 ut
+e
 _error
+_utf8_path
 (sel
@@ -2972,20 +2972,77 @@
 _insert_
-with
+duplicate_ids(self):%0A        pass%0A%0A    def test_insert_commit
 _error(s

Insert AttributeError test case