"""
Unit tests of the Dashboard application
"""

from django.contrib.contenttypes import generic
from django.db import IntegrityError
from django.test import TestCase

from launch_control.utils.call_helper import ObjectFactoryMixIn
from launch_control.dashboard_app.models import (
        HardwareDevice,
        SoftwarePackage,
        )


class SoftwarePackageTestCase(TestCase, ObjectFactoryMixIn):

    class Dummy:
        class SoftwarePackage:
            name = 'libfoo'
            version = '1.2.0'

    def test_creation_1(self):
        dummy, sw_package = self.make_and_get_dummy(SoftwarePackage)
        sw_package.save()
        self.assertEqual(sw_package.name, dummy.name)
        self.assertEqual(sw_package.version, dummy.version)

    def test_uniqueness(self):
        pkg1 = self.make(SoftwarePackage)
        pkg1.save()
        pkg2 = self.make(SoftwarePackage)
        self.assertRaises(IntegrityError, pkg2.save)


class HardwarePackageTestCase(TestCase, ObjectFactoryMixIn):

    class Dummy:
        class HardwareDevice:
            device_type = 'device.cpu'
            description = 'some cpu'
    
    def test_creation_1(self):
        dummy, hw_device = self.make_and_get_dummy(HardwareDevice)
        hw_device.save()
        self.assertEqual(hw_device.device_type, dummy.device_type)
        self.assertEqual(hw_device.description, dummy.description)

    def test_attributes(self):
        hw_device = self.make(HardwareDevice)
        hw_device.save()
        hw_device.attributes.create(name="connection-bus", value="usb")
        self.assertEqual(hw_device.attributes.count(), 1)
        attr = hw_device.attributes.get()
        self.assertEqual(attr.name, "connection-bus")
        self.assertEqual(attr.value, "usb")

    def test_attributes_uniqueness(self):
        hw_device = self.make(HardwareDevice)
        hw_device.save()
        hw_device.attributes.create(name="name", value="value")
        self.assertRaises(IntegrityError, hw_device.attributes.create,
                name="name", value="value")


@@ -42,16 +42,69 @@
 on%0A%22%22%22%0A%0A
+from django.contrib.auth.models import (User, Group)%0A
 from dja
@@ -143,16 +143,16 @@
 generic%0A
-
 from dja
@@ -328,16 +328,38 @@
 mport (%0A
+        BundleStream,%0A
         
@@ -1200,13 +1200,9 @@
 pu'%0A
-    
 %0A
+
     
@@ -2117,12 +2117,1772 @@
 e=%22value%22)%0A%0A
+class BundleStreamTestsMixIn(ObjectFactoryMixIn):%0A%0A    def test_creation_1(self):%0A        dummy, bundle_stream = self.make_and_get_dummy(BundleStream)%0A        bundle_stream.save()%0A        self.assertEqual(bundle_stream.user, dummy.user)%0A        self.assertEqual(bundle_stream.group, dummy.group)%0A        self.assertEqual(bundle_stream.name, dummy.name)%0A        self.assertEqual(bundle_stream.slug, dummy.slug)%0A%0A    def test_uniqueness(self):%0A        bundle_stream1 = self.make(BundleStream)%0A        bundle_stream1.save()%0A        bundle_stream2 = self.make(BundleStream)%0A        self.assertEqual(bundle_stream1.user, bundle_stream2.user)%0A        self.assertEqual(bundle_stream1.group, bundle_stream2.group)%0A        self.assertEqual(bundle_stream1.slug, bundle_stream2.slug)%0A        self.assertRaises(IntegrityError, bundle_stream2.save)%0A%0A%0Aclass BundleStreamTests_1(TestCase, BundleStreamTestsMixIn):%0A%0A    class Dummy:%0A        class BundleStream:%0A            name = 'My stream'%0A            slug = 'my-stream'%0A            @property%0A            def user(self):%0A                user, created = User.objects.get_or_create(username='joe')%0A                return user%0A            group = None%0A%0A%0Aclass BundleStreamTests_2(TestCase, BundleStreamTestsMixIn):%0A%0A    class Dummy:%0A        class BundleStream:%0A            name = 'My stream'%0A            slug = 'my-stream'%0A            user = None%0A            @property%0A            def group(self):%0A                group, created = Group.objects.get_or_create(name='developers')%0A                return group%0A%0A%0Aclass BundleStreamTests_3(TestCase, BundleStreamTestsMixIn):%0A%0A    class Dummy:%0A        class BundleStream:%0A            name = 'My stream'%0A            slug = 'my-stream'%0A            user = None%0A            group = None%0A

Add BundleStream test cases (failing on sqlite)