Commits

Frank Becker committed c43dde7

first basic saving of hosts to CouchDB

  • Participants

Comments (0)

Files changed (3)

File couchdb/__init__.py

+#!/usr/bin/env python
+
+# -*- coding: utf-8 -*-
+
+"""
+Title
+"""
+__author__ = "Frank Becker <fb@alien8.de>"
+__version__ = "0.1"
+__date__ = "Date: 4/28/11"
+__copyright__ = "Copyright (c) 2011 Frank Becker"
+__license__ = "Python"
+
+  

File couchdb/fill_couch.py

+#!/usr/bin/env python
+
+# -*- coding: utf-8 -*-
+
+"""
+Python source code - replace this with a description of the code and write
+the code below this text.
+"""
+from datetime import datetime
+
+__author__ = "Frank Becker <fb@alien8.de>"
+__version__ = "$Revision: 0.0 $"
+__date__ = "$Date: YDATE $"
+__copyright__ = "Copyright (c) 2010 Frank Becker"
+__license__ = "Python"
+
+import couchdbkit
+
+class Host(couchdbkit.Document):
+    """CouchDB doc for Host"""
+    name = couchdbkit.StringProperty()
+    date_updated = couchdbkit.DateTimeProperty()
+    date_created = couchdbkit.DateTimeProperty()
+
+
+def db_connect():
+    """connect to db"""
+
+    try:
+        server = couchdbkit.Server(uri="http://alzey.osrc.amd.com:5984")
+    except:
+        return False
+
+    db = server.get_or_create_db("myhost_hosts")
+
+    return db
+
+def db_register(doc_class, db):
+    """Register Document class to DB and return it
+
+    FIXME (a8): This doesn't work, I got no fucking clue why
+    """
+
+    try:
+        setattr(doc_class, 'set_db', db)
+    except:
+        return False
+
+    return doc_class
+
+def save_or_create_host(cls, name=None, **kwargs):
+    """
+
+    Args:
+        cls registered Host() class
+        name=None, **kwargs
+
+    Returns:
+
+        host obj
+
+    """
+    if not name:
+        return False
+
+    host = cls(name=name, date_created=datetime.utcnow())
+    for key, value in kwargs.items():
+        setattr(host, key, value)
+    try:
+        host.save()
+    except:
+        return False
+
+    return host
+
+
+def main():
+    """ main
+    """
+    db_connect()
+
+if __name__ == '__main__':
+    main()
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8 :
+

File couchdb/tests.py

+#!/usr/bin/env python
+
+# -*- coding: utf-8 -*-
+import couchdbkit
+
+__author__ = 'a8'
+
+import unittest
+import fill_couch
+
+class CouchDBConnect(unittest.TestCase):
+
+    def test_db_connect(self):
+        self.assertNotEqual(fill_couch.db_connect(), False)
+
+class CouchDBRegister(unittest.TestCase):
+
+    def setUp(self):
+        """
+        init db
+        """
+        self.db = fill_couch.db_connect()
+
+    def disabled_db_register(self):
+        self.assertTrue(issubclass(
+            fill_couch.db_register(fill_couch.Host, self.db),
+            couchdbkit.Document))
+
+    def tearDown(self):
+        del(self.db)
+
+
+class SaveHost(unittest.TestCase):
+    """"""
+
+    def setUp(self):
+        """
+        init db
+        """
+        self.db = fill_couch.db_connect()
+        fill_couch.Host.set_db(self.db)
+
+    def test_simple_save(self):
+        """"test to save a simple host"""
+        self.host1 = fill_couch.save_or_create_host(
+            fill_couch.Host,
+            name="eris")
+        self.assertTrue(isinstance(
+            self.host1,
+            fill_couch.Host))
+        self.host1.delete()
+
+    def test_complex_host_save(self):
+        """"test to save a simple host"""
+        self.host2 = fill_couch.save_or_create_host(
+            fill_couch.Host,
+            name="fnord",
+            cpu_family="fam10",
+            )
+        self.assertTrue(isinstance(
+            self.host2,
+            fill_couch.Host))
+        self.assertEqual(self.host2.name, "fnord")
+        self.assertEqual(self.host2.cpu_family, "fam10")
+        self.host2.delete()
+
+    def tearDown(self):
+        del(self.db)
+
+if __name__ == '__main__':
+    unittest.main()