Source

MyHost / database / tests.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-
import time
import uuid
import couchdbkit

__author__ = 'a8'

import unittest
from couchdb import Host, get_all_hosts, get_hosts_by_filter, db_connect, \
    db_register, save_or_create_host

class CouchDBConnect(unittest.TestCase):

    def test_db_connect(self):
        self.assertNotEqual(db_connect(), False)

class CouchDBRegister(unittest.TestCase):

    def setUp(self):
        """
        init db
        """
        self.db = db_connect()

    def disabled_db_register(self):
        self.assertTrue(issubclass(
            db_register(Host, self.db),
            couchdbkit.Document))

    def tearDown(self):
        del(self.db)


class SaveHost(unittest.TestCase):
    """"""

    def setUp(self):
        """
        init db
        """
        self.db = db_connect()
        Host.set_db(self.db)

    def test_simple_save(self):
        """"test to save a simple host"""
        self.host1 = save_or_create_host(
            Host,
            name="eris")
        self.assertTrue(isinstance(
            self.host1,
            Host))
        self.host1.delete()

    def test_complex_host_save(self):
        """"test to save a simple host"""
        self.host2 = save_or_create_host(
            Host,
            name="fnord",
            cpu_family="fam10",
            )
        self.assertTrue(isinstance(
            self.host2,
            Host))
        self.assertEqual(self.host2.name, "fnord")
        self.assertEqual(self.host2.cpu_family, "fam10")
        self.host2.delete()

    def tearDown(self):
        del(self.db)


class GetHosts(unittest.TestCase):
    """"""

    def setUp(self):
        """
        init db
        """
        self.db = db_connect()
        Host.set_db(self.db)
        self.host1 = save_or_create_host(
            Host,
            name="eris")
        self.host2 = save_or_create_host(
            Host,
            name="fridolin")
        self.uuid3 = str(uuid.uuid4())
        self.host3 = save_or_create_host(
            Host,
            name="fridolin",
            uuid=self.uuid3,
        )

    def test_get_hosts(self):
        """"test to save a simple host"""
        self.assertTrue(len(Host.view("hosts/all").all()) >= 2)

    def test_get_hosts_by_name(self):
        """"test to save a simple host"""
        self.assertTrue(
            len(get_hosts_by_filter(name="fridolin")) >= 2
        )

    def test_get_hosts_by_name(self):
        """"test to save a simple host"""
        self.assertTrue(
            len(get_hosts_by_filter(name="fridolin", uuid=self.uuid3)) == 1
        )

    def test_get_hosts_by_name_sorted(self):
        """"test to save a simple host"""
        time.sleep(2)
        # set update time
        self.host3.save()
        host = get_hosts_by_filter(name="fridolin")[-1]
        self.assertTrue(getattr(host, 'uuid') == self.uuid3)

    def tearDown(self):
        self.host1.delete()
        self.host2.delete()
        self.host3.delete()
        del self.db

if __name__ == '__main__':
    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.