Boog Slayer / tests.py

import os
import pdb
import time
import mock
import unittest
import contextlib

import slayer

from datetime import datetime

class TaskTest(unittest.TestCase):
    def test_whenTaskIsCreatedWithDescItShouldBeSet(self):
        task = slayer.Task(desc="Nothing happened...")
        self.assertEqual("Nothing happened...", task.desc)

    def test_whenTaskIsCreatedWithNoIdItShouldBeNone(self):
        task = slayer.Task()
        self.assertEqual(None, task.id)

    def test_whenTaskIsCreatedWithNoDescItShouldBeNone(self):
        task = slayer.Task()
        self.assertEqual(None, task.desc)

class IntervalTests(unittest.TestCase):
    def test_whenIntervalIsCreatedWithNoStartItShouldBeNow(self):
        self.skipTest("Yeah, like that")
        interval = slayer.Interval()
        self.assertAlmostEqual(datetime.now(), interval.start, places=0)

    def test_whenIntervalIsCreatedWithStartItShouldBeTheProvidedOne(self):
        self.skipTest("Yeah, like that")
        expected_start = time.time()-200
        interval = slayer.Interval(start=expected_start)
        self.assertAlmostEqual(expected_start, interval.start)

    def test_whenIntervalIsCreatedWithNoEndItShouldBeNone(self):
        self.skipTest("Yeah, like that")
        interval = slayer.Interval()
        self.assertEqual(None, interval.end)

    def test_whenIntervalIsCreatedWithEndItShouldBeTheProvidedOne(self):
        self.skipTest("Yeah, like that")
        expected_end = time.time()-42
        interval = slayer.Interval(end=expected_end)
        self.assertEqual(expected_end, interval.end)

class SlayerTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        slayer.DB_PATH = os.path.join(slayer.BASE_DIR, 'test.sq3')
        slayer.make_db()

    @classmethod
    def tearDownClass(cls):
        os.remove(slayer.DB_PATH)

    def setUp(self):
        slayer.add_project(project_name='default')

    def tearDown(self):
        engine = slayer.make_engine()
        with contextlib.closing(engine.connect()) as con:
            trans = con.begin()
            for table in reversed(slayer.Base.metadata.sorted_tables):
                con.execute(table.delete())
            trans.commit()

    def test_whenParseIsCalledWithNoArgsItShouldRaiseSystemExit(self):
        with self.assertRaises(SystemExit):
            slayer._parse([])

    def test_whenParseIsCalledWithTaskStartAndNoDescShouldSystemExit(self):
        with self.assertRaises(SystemExit):
            slayer._parse(['task', 'start'])

    def test_whenParseIsCalledWithTaskStartAndDescShouldReturnResults(self):
        results = slayer._parse(['task', 'start', 'Rock me amadaeus'])
        expected_command = 'task'
        expected_action = 'start'
        expected_data = 'Rock me amadaeus'

        self.assertEqual(expected_command, results.command)
        self.assertEqual(expected_action, results.action)
        self.assertEqual(expected_data, results.data)

    ##################
    # Interval Tests #
    ##################
    def test_whenStartIntervalIsCalledTaskDoesNotExistItShouldRaiseValueError(self):
        with self.assertRaises(ValueError):
            slayer.start_interval(task_id=4, user_id='wango')

    def test_whenStartIntervalIsCalledAndUserDNEThenItShouldValueError(self):
        task_id = slayer.add_task(project='default').id
        with self.assertRaises(ValueError):
            slayer.start_interval(task_id=task_id, user_id='wango')

    def test_whenStartIntervalIsCalledAndTaskAndUserExistItShouldBeAddedToUser(self):
        user_id = slayer.add_user().id
        task_id = slayer.add_task(project='default').id
        session = slayer.make_session()
        interval = slayer.start_interval(task_id=task_id, user_id=user_id,
                                         session=session)
        user = slayer.get_user(user_id, session)
        self.assertEqual(interval, user.timesheet[0])

    def test_whenStartIntervalIsCalledOkItShouldReturnTheInterval(self):
        user_id = slayer.add_user().id
        task_id = slayer.add_task(project='default').id
        interval = slayer.start_interval(task_id=task_id, user_id=user_id)
        self.assertTrue(isinstance(interval, slayer.Interval))

    ##############
    # Task Tests #
    ##############
    def test_whenAddTaskIsCalledItShouldReturnTheTaskWithNewId(self):
        task = slayer.add_task(project='default')
        self.assertLess(0, task.id)

    def test_whenAddTaskIsCalledWithDescItShouldBeSet(self):
        expected_desc = 'No real project thing'
        id = slayer.add_task(desc=expected_desc, project='default').id
        task = slayer.get_task(id)
        self.assertEqual(expected_desc, task.desc)

    def test_whenAddTaskIsCalledWithProjectThatExistsItShouldBeSet(self):
        expected_proj = 'Boog Slayer'
        slayer.add_project(expected_proj)
        id = slayer.add_task(project=expected_proj).id
        task = slayer.get_task(id)
        self.assertEqual(expected_proj, task.project)

    def test_whenAddTaskIsCalledWithProjectAndProjectDoesNotExistShouldRaiseValueError(self):
        expected_proj = 'Boog Slayer'
        with self.assertRaises(ValueError):
            slayer.add_task(project=expected_proj)

    def test_whenAddTaskIsCalledWithUserAndUserDoesNotExistShouldRaiseValueError(self):
        imaginary_user = 'some super cool id'
        with self.assertRaises(ValueError):
            slayer.add_task(assigned_to=imaginary_user)

    def test_whenAddTaskIsCalledWithParentCaseThatDoesntExistItShouldRaiseValueError(self):
        imaginary_task_id = 42
        with self.assertRaises(ValueError):
            slayer.add_task(parent_id=imaginary_task_id)

    def test_whenGetTaskIsCalledWithIdItShouldReturnTask(self):
        task_id = slayer.add_task(project='default').id
        task = slayer.get_task(task_id=task_id)
        self.assertEqual(task_id, task.id)

    def test_whenEditTaskIsCalledWithInvalidIdItShouldRaiseValueError(self):
        with self.assertRaises(ValueError):
            slayer.edit_task(id=4)

    def test_whenEditTaskIsCalledWithDescItShouldBeSet(self):
        expected_desc = 'No real project thing'
        id = slayer.add_task(project='default').id
        slayer.edit_task(id, desc=expected_desc)
        task = slayer.get_task(id)
        self.assertEqual(expected_desc, task.desc)

    def test_whenEditTaskIsCalledWithProjectItShouldBeSet(self):
        expected_proj = 'XTF-1234'
        id = slayer.add_task(project='default').id
        slayer.edit_task(id, project=expected_proj)
        task = slayer.get_task(id)
        self.assertEqual(expected_proj, task.project)

    def test_whenEditTaskIsCalledWithTypeItShouldBeUpdated(self):
        expected_type = 'Fnord'
        id = slayer.add_task().id
        slayer.edit_task(id, type=expected_type)
        task = slayer.get_task(id)
        self.assertEqual(expected_type, task.type)

    def test_whenEditTaskIsCalledWithStatusItShouldBeUpdated(self):
        expected_status = 'Newed'
        id = slayer.add_task().id
        slayer.edit_task(id, status=expected_status)
        task = slayer.get_task(id)
        self.assertEqual(expected_status, task.status)

    def test_whenEditTaskIsCalledWithAssignedToItShouldBeUpdated(self):
        expected_assigned_to = 'Puff, the magic dragon'
        id = slayer.add_task().id
        slayer.edit_task(id, assigned_to=expected_assigned_to)
        task = slayer.get_task(id)
        self.assertEqual(expected_assigned_to, task.assigned_to)

    def test_whenEditTaskIsCalledWithContactItShouldBeUpdated(self):
        expected_contact = '3-2-1'
        id = slayer.add_task().id
        slayer.edit_task(id, contact=expected_contact)
        task = slayer.get_task(id)
        self.assertEqual(expected_contact, task.contact)

    def test_whenEditTaskIsCalledWithPriorityItShouldBeUpdated(self):
        expected_priority = 1
        id = slayer.add_task().id
        slayer.edit_task(id, priority=expected_priority)
        task = slayer.get_task(id)
        self.assertEqual(expected_priority, task.priority)

    def test_whenEditTaskIsCalledWithDueItShouldBeUpdated(self):
        expected_due = datetime(1984, 8, 26)
        id = slayer.add_task().id
        slayer.edit_task(id, due=expected_due)
        task = slayer.get_task(id)
        self.assertEqual(expected_due, task.due)

    ##############
    # User Tests #
    ##############
    def test_whenUserIsAddedWithoutProvidingIdItShouldDefaultToBlankString(self):
        user = slayer.add_user()
        self.assertEqual('', user.id)

    def test_whenUserIsAddedGetUserWithIdShouldReturnUser(self):
        user_id = slayer.add_user().id
        user = slayer.get_user(user_id)
        self.assertEqual(user_id, user.id)

    def test_whenUserIsAddedWithIdAndNameItShouldSetBoth(self):
        user = slayer.add_user(user_id="uwwerne", name="Wahoo Tango")
        got_user = slayer.get_user(user.id)
        self.assertEqual(user.id, got_user.id)
        self.assertEqual(user.name, got_user.name)

    def test_whenEditUserIsCalledWithWrongIdItShouldValueError(self):
        with self.assertRaises(ValueError):
            slayer.edit_user(user_id='4135')

    def test_usersShouldBeEqualWhenIdAndNameAreTheSame(self):
        user_a = slayer.User(id='wango', name='tango')
        user_b = slayer.User(id='wango', name='tango')
        self.assertEqual(user_a, user_b)

    def test_whenEditUserIsCalledAndUserExistsItShouldReturnUser(self):
        session = slayer.make_session()
        expected_user = slayer.add_user(user_id='roscivs',
                                        name='Ross Werner',
                                        session=session)
        actual_user = slayer.edit_user('roscivs', session=session)
        self.assertEqual(expected_user, actual_user)

    def test_whenEditUserIsCalledWithNoneNewIdAndNoneNameItShouldStaySame(self):
        user_id = slayer.add_user(user_id='waynerd', name="Wango Tango").id
        slayer.edit_user(user_id, new_id=None, new_name=None)
        user = slayer.get_user(user_id=user_id)
        self.assertEqual('waynerd', user.id)
        self.assertEqual('Wango Tango', user.name)

    def test_whenEditUserIsCalledWithNewIdThenOldIdShouldBeGone(self):
        old_id = slayer.add_user(user_id='bob').id
        slayer.edit_user(old_id, new_id='fnord')
        user = slayer.get_user(user_id=old_id)
        self.assertEqual(None, user)

    def test_whenEditUserIsCalledAndNewIdExistsThenShouldRaiseValueError(self):
        taken_id = 'wango'
        slayer.add_user(user_id=taken_id)
        good_id = slayer.add_user(user_id='good').id
        with self.assertRaises(ValueError):
            slayer.edit_user(good_id, new_id=taken_id)

    def test_whenEditUserIsCalledAndNewNameProvidedThenNameShouldBeNewOne(self):
        user_id = slayer.add_user(user_id='wango', name='').id
        new_name = 'Tango'
        slayer.edit_user(user_id, new_name=new_name)
        user = slayer.get_user(user_id)
        self.assertEqual(new_name, user.name)

    def test_whenDeleteUserIsCalledWithInvalidIdThenShouldRaiseValueError(self):
        with self.assertRaises(ValueError):
            slayer.delete_user(user_id='fnord')

    def test_whenDeleteUserIsCalledItShouldReallyDeleteUser(self):
        user_id = 'roscivs'
        slayer.add_user(user_id=user_id)
        slayer.delete_user(user_id)
        actual_user = slayer.get_user(user_id)
        self.assertEqual(None, actual_user)


    #################
    # Project Tests #
    #################
    def test_whenGetProjectIsCalledItShouldReturnTheRightOne(self):
        session = slayer.make_session()
        expected_project = slayer.Project(name='SRP-014')
        session.add(expected_project)
        session.commit()
        project = slayer.get_project(expected_project.name, session=session)
        self.assertEqual(expected_project, project)

    def test_whenAddProjectIsCalledWithNoArgumentsTheNameShouldBeEmptyString(self):
        project = slayer.add_project()
        self.assertEqual('', project.name)

    def test_whenDuplicateProjectNamesAreAddedValueErrorShouldBeRaised(self):
        slayer.add_project('whatever')
        with self.assertRaises(ValueError):
            slayer.add_project('whatever')


    def test_whenArchiveProjectIsCalledItShouldSetFlagOnProject(self):
        project_name = 'Boog Slayer'
        slayer.add_project(project_name=project_name)
        slayer.archive_project(project_name)
        project = slayer.get_project(project_name)

        self.assertTrue(project.archived)

    def test_whenEditProjectIsCalledWithNewNameThatExistsShouldRaiseValueError(self):
        taken_name = 'Boog Slayer'
        project_name = 'Boog Player'
        slayer.add_project(taken_name)
        slayer.add_project(project_name)
        with self.assertRaises(ValueError):
            slayer.edit_project(project_name, new_name=taken_name)

    def test_whenEditProjectIsCalledAndNoProjectHasThatNameShouldRaiseValueError(self):
        with self.assertRaises(ValueError):
            slayer.edit_project(project_name='whatever, not like you care')

    def test_whenEditProjectIsCalledWithGoodNewNameItShouldSucceed(self):
        new_name = 'F Cancer'
        project_name = 'Screw Cancer'
        slayer.add_project(project_name)
        slayer.edit_project(project_name, new_name=new_name)
        self.assertTrue(slayer.get_project(new_name) is not None)

    def test_whenEditProjectIsCalledWithNewDescItShouldSucceed(self):
        project_name = 'Symphony in C'
        desc = 'A Cake song'
        slayer.add_project(project_name)
        slayer.edit_project(project_name, new_desc=desc)
        project = slayer.get_project(project_name)

        self.assertEqual(desc, project.desc)

    def test_whenProjectIsRestoredItShouldSetArchivedToFalse(self):
        project_name = 'Model P Fnord'
        slayer.add_project(project_name)
        slayer.archive_project(project_name)
        slayer.restore_project(project_name)
        project = slayer.get_project(project_name)
        self.assertFalse(project.archived)

    def test_whenArchiveProjectIsCalledAndProjectDoesNotExistItShouldValueError(self): 
        with self.assertRaises(ValueError):
            slayer.archive_project('Doesn\'t matter really')

    def test_whenRestoreProjectIsCalledAndProjectDoesNotExistItShouldValueError(self): 
        with self.assertRaises(ValueError):
            slayer.restore_project('Doesn\'t matter really')

unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.