annotate tests/couchdb/ListDateTimeFieldTests.py @ 215:b7caf487bc54

run make prior to starting the worm
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 13 May 2013 17:35:11 +0200
parents 460a3062c5e6
children bb3c851b18b1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
170
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
1
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
2 from backend.couchdb.ListDateTimeField import ListDateTimeField
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
3 from couchdb import Server
198
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
4 from couchdb.http import ResourceNotFound
170
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
5 from couchdb.mapping import Document, TextField
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
6 from datetime import datetime
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
7 import unittest
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
8
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
9 class ListDateTimeFieldTests(unittest.TestCase):
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
10 def testToJsonValid(self):
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
11 value = datetime(2011, 9, 7, 15, 21, 0)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
12 result = ListDateTimeField()._to_json(value)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
13 self.assertEqual([2011, 9, 7, 15, 21, 0], result)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
14
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
15 def testToJsonInvalid(self):
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
16 value = "this is invalid input"
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
17 result = ListDateTimeField()._to_json(value)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
18 self.assertEqual(value, result)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
19
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
20 def testToPythonValid(self):
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
21 value = [2011, 9, 7, 15, 21, 0]
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
22 result = ListDateTimeField()._to_python(value)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
23 expected = datetime(2011, 9, 7, 15, 21, 0)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
24 self.assertEqual(expected, result)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
25
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
26 def testToPythonInvalid(self):
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
27 value = "this is invalid input"
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
28 result = ListDateTimeField()._to_python(value)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
29 self.assertEqual(value, result)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
30
198
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
31 def testRoundTrip(self):
170
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
32 class Dummy(Document):
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
33 title = TextField()
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
34 date = ListDateTimeField()
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
35
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
36 server = Server()
198
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
37 database = self._createDatabase(server)
170
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
38
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
39 dummy = Dummy()
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
40 dummy.title = "created from unit test"
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
41 date = datetime.now().replace(microsecond=0)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
42 dummy.date = date
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
43 dummy.store(database)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
44 self.assertNotEqual(None, dummy.id)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
45
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
46 dummy2 = Dummy.load(database, dummy.id)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
47 self.assertNotEqual(None, dummy2)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
48 self.assertFalse(dummy == dummy2)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
49 self.assertEqual(date, dummy2.date)
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
50
198
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
51 del server[database.name]
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
52
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
53 def _createDatabase(self, server):
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
54 databaseName = "feedworm-unit-test"
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
55 database = None
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
56 try:
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
57 database = server[databaseName]
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
58 except (ResourceNotFound):
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
59 database = server.create(databaseName)
460a3062c5e6 create the database if it doesn't exist and drop it after test
dirk
parents: 170
diff changeset
60 return database
170
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
61
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
62 if __name__ == "__main__":
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
63 #import sys;sys.argv = ['', 'Test.testName']
f0afcd1c5656 implement a couchdb mapping field that stores a datetime instance as a JSON array
dirk
parents:
diff changeset
64 unittest.main()