#  testresources: extensions to python unittest to allow declaritive use
#  of resources by test cases.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#  Copyright (C) 2008  Jonathan M. Lange <jml@mumak.net>

"""Example of how you might use testresources in "the real world"."""


import shutil
import tempfile
import unittest

from storm.locals import *

from testresources import OptimisingTestSuite, TestResource, ResourcedTestCase


def create_schema(store):
    store.execute(
        """CREATE TABLE Person
           (id INTEGER PRIMARY KEY, name VARCHAR, nick VARCHAR)""")


class Person(object):
    __storm_table__ = 'Person'

    id = Int(primary=True)
    name = Unicode()
    nick = Unicode()

    def __init__(self, nick, name):
        self.nick = nick
        self.name = name

    @classmethod
    def get_nicks(self, store):
        for person in store.find(Person):
            yield person.nick


class DatabaseService(object):

    def _load_sample_data(self, store):
        people = dict(jml="Jonathan Lange",
                      lifeless="Robert Collins",
                      jamesh="James Henstridge")
        for nick, name in people.iteritems():
            person = Person(nick=nick.decode(), name=name.decode())
            store.add(person)

    def __init__(self, test_resource):
        self._test_resource = test_resource

    def setUp(self):
        print "CREATING DATABASE"
        self._temp_dir = tempfile.mkdtemp()
        self._database = create_database('sqlite:%s/test.db' % self._temp_dir)
        self._store = Store(self._database)
        create_schema(self._store)
        self._load_sample_data(self._store)

    def tearDown(self):
        shutil.rmtree(self._temp_dir)
        print "DESTROYING DATABASE"

    def dirtied(self):
        self._test_resource.dirtied(self)

    def get_store(self):
        return self._store


class _DatabaseResource(TestResource):

    def make(self):
        database = DatabaseService(self)
        database.setUp()
        return database

    def clean(self, database):
        database.tearDown()


DatabaseResource = _DatabaseResource()


class TestPerson(ResourcedTestCase):
    """Test the Person object."""

    resources = [('database', DatabaseResource)]

    def test_default_nicks(self):
        # The nicknames in our initial sample data are jml, lifeless and
        # jamesh.
        store = self.database.get_store()
        self.assertEqual(
            set([u'jml', u'lifeless', u'jamesh']),
            set(Person.get_nicks(store)))

    def test_the_real_jml(self):
        # There is only one.
        store = self.database.get_store()
        jml = store.find(Person, nick=u'jml').one()
        self.assertEqual(u'Jonathan Lange', jml.name)

    def test_add_person(self):
        # If we add a person, get_nicks() will include that person's nick
        # name.
        store = self.database.get_store()
        dude = Person(u'dude', u'Jeffrey Lebowski')
        store.add(dude)
        self.database.dirtied()
        self.assertEqual(
            set([u'dude', u'jml', u'lifeless', u'jamesh']),
            set(Person.get_nicks(store)))


def test_suite():
    suite = unittest.TestLoader().loadTestsFromName(__name__)
    optimising_suite = OptimisingTestSuite()
    optimising_suite.adsorbSuite(suite)
    return optimising_suite


if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(test_suite())

