def test_write(self):
        mocker = Mocker()
        db = mocker.mock()

        insert_book ='''
                    insert into book (title, isbn, publisher, list_price,
                    publish_date, class, sheet_numbers, folio, print_type,
                    author, barcode, comments) values
                    ('a', '1234', 'aph', '30', '2012', 'I.',
                    18, '4', 'mono', 'sb', 'a', 'blahblahblah')
                    '''
        db.query(insert_book)

        insert_tags = '''
                    '''
        db.query(insert_tags)
        db.commit()
        mocker.replay()

        writer = CatalogMySQLWriter(db)
        item = CatalogItem(
            'a', '1234', 'aph', '30', '2012', 'I.', 18, '4', 'mono', 'sb',
            'a', 'blahblahblah', { 'audience': 'ms', 'awards': 'annual' })
        writer.write(item)

        mocker.restore()
        mocker.verify()
    def test_write_duplicated_publisher(self):
        writer = CatalogMySQLWriter(self.db)
        item1 = CatalogItem(
            'a', '1234', 'aph', '30', '2012', 'I.', 18, '4', 'mono', 'sb',
            'a', 'blahblahblah', { 'audience': 'ms', 'awards': 'annual' })

        item2 = CatalogItem(
            'b', '1235', 'aph', '30', '2012', 'I.', 18, '4', 'mono', 'sb',
            'a', 'blahblahblah', { 'audience': 'ms', 'awards': 'annual' })

        job_id = writer.begin_write()
        self.helper.assert_unique('job', 'job_id', job_id)
        try:
            writer.write(item1)
            writer.write(item2)
            self.helper.assert_count('raw_book', 2)
            self.helper.assert_count('raw_tag', 4)
            self.helper.assert_count('raw_publisher', 1)
            writer.apply()
        finally:
            writer.undo()
            self.helper.assert_count('job', 0)
            self.helper.assert_count('raw_book', 0)
            self.helper.assert_count('raw_tag', 0)
            self.helper.assert_count('raw_publisher', 0)
    def test_write(self):
        writer = CatalogMySQLWriter(self.db)
        item = CatalogItem(
            'a', '1234', 'aph', '30', '2012', 'I.', 18, '4', 'mono', 'sb',
            'a', 'blahblahblah', { 'audience': 'ms', 'awards': 'annual' })

        job_id = writer.begin_write()
        self.helper.assert_unique('job', 'job_id', job_id)
        try:
            writer.write(item)
            self.helper.assert_count('raw_book', 1)
            self.helper.assert_count('raw_tag', 2)
            self.helper.assert_count('raw_publisher', 1)
            self.helper.assert_result(
                "select value from raw_code where type = 7 and name = '4'",
                ((1,),))
            self.helper.assert_result(
                "select value from raw_code where type = 8 and name = 'mono'",
                ((1,),))
            
            writer.apply()

            self.helper.assert_count('bookstore.book', 1)
            self.helper.assert_count('bookstore.book_tag', 2)
            self.helper.assert_count('bookstore.publisher', 1)
            self.helper.assert_count('bookstore.code', 2)
        finally:
            writer.undo()
            self.helper.assert_count('job', 0)
            self.helper.assert_count('raw_book', 0)
            self.helper.assert_count('raw_tag', 0)
            self.helper.assert_count('raw_publisher', 0)
            self.helper.assert_count('raw_code', 0)
    def test_import_catalog(self):

        catalog_stream = None
        field_map_stream = None
        
        try:
            catalog_stream = open(CATALOG_SAMPLE, 'rb')
            field_map_stream = open(FIELD_MAP_SAMPLE, 'rb')
            
            writer = CatalogMySQLWriter(self.db)
            import_catalog(catalog_stream, field_map_stream, writer)

            self.helper.assert_not_empty('raw_book')
            writer.undo()
            
        finally:
            if None <> catalog_stream: catalog_stream.close()
            if None <> field_map_stream: field_map_stream.close()
    def test_undo_write(self):
        writer = CatalogMySQLWriter(self.db)
        item = CatalogItem(
            'a', '1234', 'aph', '30', '2012', 'I.', 18, '4', 'mono', 'sb',
            'a', 'blahblahblah', { 'audience': 'ms', 'awards': 'annual' })

        job_id = writer.begin_write()
        self.helper.assert_unique('job', 'job_id', job_id)

        writer.write(item)
        writer.undo()
        self.helper.assert_count('job', 0)
        self.helper.assert_count('raw_book', 0)
        self.helper.assert_count('raw_tag', 0)
        self.helper.assert_count('raw_publisher', 0)
        self.helper.assert_count('raw_code', 0)