import os
import shutil
import tarfile

from pdcupdater.tests.handler_tests import (
    BaseHandlerTest, mock_pdc
)


class TestModuleStateChange(BaseHandlerTest):
    handler_path = 'pdcupdater.handlers.modules:ModuleStateChangeHandler'

    test_data_dir = os.path.join(os.path.dirname(__file__), "test_modules_data")
    repo_dir = os.path.join(test_data_dir, "repos")
    foo_repo_tar = os.path.join(repo_dir, "foo.tar")
    foo_repo_dir = os.path.join(repo_dir, "foo")
    foo_repo_url = foo_repo_dir + '?#286f3a32f3e034508012fcbba63ed40092e4129b'
    test_tree_dir = os.path.join(test_data_dir, "trees/Test-0-20160712.0")

    def setUp(self):
        super(TestModuleStateChange, self).setUp()

        # Remove example repo that may be left over from previous calls.
        try:
            shutil.rmtree(self.foo_repo_dir)
        except OSError:
            pass

        # Untar example repository
        with tarfile.open(self.foo_repo_tar, "r") as tar:
            tar.extractall(self.repo_dir)

    def tearDown(self):
        super(TestModuleStateChange, self).tearDown()
        shutil.rmtree(self.foo_repo_dir)

    @mock_pdc
    def test_handle_module(self, pdc):
        msg = {
            'topic': 'buildsys.module.state.change',
            'msg': {
                'state': 'done',
                'koji_tag': 'module-core-24',
                'module_uid': 'Core-24-0',
                'module_version': '24',
                'module_release': '0',
                'scmurl': self.foo_repo_url,
                'topdir': self.test_tree_dir,
            }
        }
        self.handler.handle(pdc, msg)

@@ -31,16 +31,28 @@
  tarfile
+%0Aimport copy
 %0A%0Afrom p
@@ -635,16 +635,524 @@
 12.0%22)%0A%0A
+    state_init_msg = %7B%0A        'topic': 'org.fedoraproject.stg.buildsys.module.state.change',%0A        'msg': %7B%0A            'state': 'init',%0A            'name': %22Core%22,%0A            'version': '24',%0A            'release': '0',%0A            'scmurl': foo_repo_url,%0A        %7D%0A    %7D%0A%0A    state_done_msg = copy.deepcopy(state_init_msg)%0A    state_done_msg%5B'topic'%5D = 'org.fedoraproject.stg.rida.module.state.change'%0A    state_done_msg%5B'msg'%5D.update(%7B%0A        'state': 'done',%0A        'topdir': test_tree_dir%0A    %7D)%0A%0A
     def 
@@ -1676,439 +1676,159 @@
 est_
-handle_module(self, pdc):%0A        msg = %7B%0A            'topic': 'buildsys.module.state.change',%0A            'msg': %7B%0A                'state': 'done',%0A                'koji_tag': 'module-core-24',%0A                'module_uid': 'Core-24-0',%0A                'module_version': '24',%0A                'module_release': '0',%0A                'scmurl': self.foo_repo_url,%0A                'topdir': self.test_tree_dir,%0A            %7D%0A        %7D
+create_unreleased_variant(self, pdc):%0A        self.handler.handle(pdc, self.state_init_msg)%0A%0A    @mock_pdc%0A    def test_handle_created_tree(self, pdc):
 %0A   
@@ -1857,13 +1857,29 @@
 le(pdc, 
+self.state_done_
 msg)%0A

Update unit tests for modules for recent changes.