Revision: 2626
Author: janne.t.harkonen
Date: Mon Mar 15 06:45:32 2010
Log: Initial implementation and test library for atdd example
http://code.google.com/p/robotframework/source/detail?r=2626

Added:
 /trunk/proto/atdd_example/VacalcLibrary.py
 /trunk/proto/atdd_example/test_vacalc.py
 /trunk/proto/atdd_example/vacalc.py

=======================================
--- /dev/null
+++ /trunk/proto/atdd_example/VacalcLibrary.py  Mon Mar 15 06:45:32 2010
@@ -0,0 +1,36 @@
+import os
+import sys
+import subprocess
+
+from vacalc import User, UserStore
+
+
+class VacalcLibrary(object):
+
+    def __init__(self, db_file='db.csv'):
+        self._db_file = db_file
+
+    def count_vacation(self, startdate, year):
+        return User('Foo', startdate).count_vacation(year)
+
+    def clear_database(self):
+        if os.path.isfile(self._db_file):
+            os.remove(self._db_file)
+
+    def add_user(self, name, startdate):
+        self._run('add_user', name, startdate)
+
+    def _run(self, command, *args):
+        cmd = [sys.executable, 'vacalc.py', command] + list(args)
+        print subprocess.list2cmdline(cmd)
+        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                                stderr=subprocess.STDOUT)
+        self._status = proc.stdout.read().strip()
+        print self._status
+
+    def status_should_be(self, status):
+        if self._status != status:
+ raise AssertionError("Expected status to be '%s' but it was '%s'"
+                                % (status, self._status))
+
+
=======================================
--- /dev/null
+++ /trunk/proto/atdd_example/test_vacalc.py    Mon Mar 15 06:45:32 2010
@@ -0,0 +1,31 @@
+import datetime
+import unittest
+
+from vacalc import User, UserStore, DuplicateUser
+
+
+class TestUser(unittest.TestCase):
+
+    def test_creating_user(self):
+        user = User('Juan von Rantanen', '2010-3-15')
+        self.assertEquals(user.name, 'Juan von Rantanen')
+        self.assertEquals(user.startdate, datetime.date(2010, 3, 15))
+
+
+class TestUserStore(unittest.TestCase):
+
+    def test_adding_user(self):
+        store = UserStore()
+        user = store.add_user('Test User Store', '2000-12-24')
+        self.assertEquals(user.name, 'Test User Store')
+        self.assertEquals(user.startdate, datetime.date(2000, 12, 24))
+        self.assertEquals(store._users, {user.name: user})
+
+    def test_adding_duplicate_user(self):
+        store = UserStore()
+        user = store.add_user('test', '2000-12-24')
+ self.assertRaises(DuplicateUser, store.add_user, 'test', '2001-01-24')
+
+
+if __name__ == '__main__':
+    unittest.main()
=======================================
--- /dev/null
+++ /trunk/proto/atdd_example/vacalc.py Mon Mar 15 06:45:32 2010
@@ -0,0 +1,89 @@
+from __future__ import with_statement
+import os
+import sys
+import csv
+import datetime
+
+
+class VacalcError(Exception): pass
+class DuplicateUser(VacalcError): pass
+
+
+class UserStore(object):
+
+    def __init__(self, db_file='db.csv'):
+        self._db_file = db_file
+        if db_file and os.path.isfile(db_file):
+            self._users = self._read_users(db_file)
+        else:
+            self._users = {}
+
+    def _read_users(self, path):
+        users = {}
+        with open(path) as db:
+            for row in csv.reader(db):
+                user = User(row[0], row[1])
+                users[user.name] = user
+        return users
+
+    def get_user(self, name):
+        for row in csv.reader(open(self._db_file)):
+            if row[0] == name:
+                return User(row[0], row[1])
+
+    def add_user(self, name, startdate):
+        if name in self._users:
+            raise DuplicateUser(name)
+        user = User(name, startdate)
+        self._users[user.name] = user
+        self._serialize(user)
+        return user
+
+    def _serialize(self, user):
+        with open(self._db_file, 'a') as db:
+            writer = csv.writer(db, lineterminator='\n')
+            writer.writerow([user.name, user.startdate.isoformat()])
+
+
+class VacationCalculator(object):
+
+    def __init__(self, userstore):
+        self._userstore = userstore
+
+    def vacation(self, name, year):
+        user = self._userstore.get_user(name, year)
+        return user.count_vacation(year)
+
+    def add_user(self, name, startdate):
+        user = self._userstore.add_user(name, startdate)
+        return "Successfully added user '%s'." % user.name
+
+
+class User(object):
+
+    def __init__(self, name, startdate):
+        self.name = name
+        self.startdate = self._parse_date(startdate)
+
+    def _parse_date(self, datestring):
+        year, month, day = datestring.split('-')
+        return datetime.date(int(year), int(month), int(day))
+
+    def count_vacation(self, year):
+        fail
+
+
+def main(args):
+    try:
+        return getattr(VacationCalculator(UserStore()), args[0])(*args[1:])
+    except (AttributeError, TypeError):
+        raise VacalcError('invalid command or arguments')
+
+
+if __name__ == '__main__':
+    try:
+        print main(sys.argv[1:])
+        sys.exit(0)
+    except VacalcError, err:
+        print err
+        sys.exit(1)

Reply via email to