from tsdesktop.testing import TSDesktopTest
from tsdesktop import siteman, config_test

class Site(TSDesktopTest):

    def setUp(self):
        config_test.mock()

    def test_Site(self):
        s = siteman.Site('fake.test', '/var/www/html')
        self.assertEqual(s.name, 'fake.test')
        self.assertEqual(s.docroot, '/var/www/html')
        self.assertEqual(str(s), '<Site: fake.test>')

    def test_SiteNoDocroot(self):
        s = siteman.Site('fake.test', '/nonexistent')
        err = s.load()
        self.assertEqual(err, 'path not found')

    def test_SiteNotDir(self):
        s = siteman.Site('fake.test', '/usr/bin/env')
        err = s.load()
        self.assertEqual(err, 'not a dir')

    def test_siteAdd(self):
        err = siteman.siteAdd('fake2.test', '/var/tmp')
        self.assertIsNone(err)
        s = siteman.siteGet('fake2.test')
        self.assertIsInstance(s, siteman.Site)
        self.assertEqual(s.docroot, '/var/tmp')

    def test_siteAddExists(self):
        err = siteman.siteAdd('fake.test', '/ignored')
        self.assertEqual(err, 'site already exists')

    def test_siteAddDupDocroot(self):
        err = siteman.siteAdd('fake2.test', '/tmp')
        self.assertEqual(err, '/tmp registered by fake.test')

    def test_siteGetNoDocroot(self):
        config_test.mock({'site:fake.test2': {}})
        s = siteman.siteGet('fake.test2')
        self.assertEqual(s.docroot, 'docroot')

    def test_sitesAll(self):
        c = {
            'site:fake2.test': {'docroot': '/tmp'},
            'site:fake3.test': {'docroot': '/tmp'},
        }
        config_test.mock(c)
        l = sorted([str(i) for i in siteman.sitesAll()])
        self.assertListEqual(l, sorted([
            str(siteman.Site('fake.test', '/ignored')),
            str(siteman.Site('fake2.test', '/ignored')),
            str(siteman.Site('fake3.test', '/ignored')),
        ]))

    def test_sitesAllInvalidName(self):
        c = {'site:test;invalid': {'docroot': '/ignored'}}
        config_test.mock(c)
        l = siteman.sitesAll()
        self.assertIsNone(l)

@@ -67,16 +67,24 @@
 siteman,
+ config,
  config_
@@ -830,145 +830,8 @@
 err)
-%0A        s = siteman.siteGet('fake2.test')%0A        self.assertIsInstance(s, siteman.Site)%0A        self.assertEqual(s.docroot, '/var/tmp')
 %0A%0A  
@@ -1148,162 +1148,542 @@
 eGet
-NoDocroot(self):%0A        config_test.mock(%7B'site:fake.test2': %7B%7D%7D)%0A        s = siteman.siteGet('fake.test2')%0A        self.assertEqual(s.docroot, 'docroot'
+(self):%0A        s = siteman.siteGet('fake.test')%0A        self.assertIsInstance(s, siteman.Site)%0A        self.assertEqual(s.docroot, '/tmp')%0A%0A    def test_siteGetDefaultDocroot(self):%0A        config_test.mock(%7B'site:fake.test2': %7B%7D%7D)%0A        s = siteman.siteGet('fake.test2')%0A        self.assertEqual(s.docroot, 'docroot')%0A%0A    def test_siteGetNoDocroot(self):%0A        config_test.mock(%7B'site:fake.test2': %7B%7D%7D)%0A        config.cfg.remove_option('DEFAULT', 'docroot')%0A        s = siteman.siteGet('fake.test2')%0A        self.assertIsNone(s
 )%0A%0A 

improve siteGet tests