from mike.lib.objects.switches import Switches

from nose.tools import eq_, ok_, raises
from django.db.utils import IntegrityError
from uuid import uuid4


def test_switch():
    host_id = uuid4()

    uuids = []
    uuids.append(Switches.add(name="test_sw1",
                              host_id=host_id,
                              datapath_id=1))
    r = Switches.get_from_uuids(uuids)[0]
    eq_(r.name, "test_sw1", msg=None)
    eq_(r.host_id, host_id, msg=None)
    ok_(r.internal, msg=None)
    eq_(r.datapath_id, 1, msg=None)

    uuids.append(Switches.add(name="test_sw2",
                              host_id=host_id,
                              internal=False,
                              datapath_id=2))
    eq_(len(Switches.get_from_uuids(uuids)), 2, msg=None)
    Switches.delete([uuids[0]])

    r = Switches.get_from_uuids(uuids)[0]
    eq_(r.name, "test_sw2", msg=None)
    eq_(r.host_id, host_id, msg=None)
    ok_(not r.internal, msg=None)
    eq_(r.datapath_id, 2, msg=None)


@raises(IntegrityError)
def test_add_exists_switch():
    host_id = uuid4()

    uuids = []
    uuids.append(Switches.add(name="test_sw1",
                              host_id=host_id,
                              datapath_id=1))
    uuids.append(Switches.add(name="test_sw1",
                              host_id=host_id,
                              datapath_id=1))

@@ -160,24 +160,26 @@
  test_switch
+es
 ():%0A    host
@@ -416,26 +416,16 @@
 est_sw1%22
-, msg=None
 )%0A    eq
@@ -440,34 +440,24 @@
 _id, host_id
-, msg=None
 )%0A    ok_(r.
@@ -460,34 +460,24 @@
 _(r.internal
-, msg=None
 )%0A    eq_(r.
@@ -490,26 +490,16 @@
 th_id, 1
-, msg=None
 )%0A%0A    u
@@ -725,26 +725,16 @@
 ids)), 2
-, msg=None
 )%0A    Sw
@@ -828,26 +828,16 @@
 est_sw2%22
-, msg=None
 )%0A    eq
@@ -856,26 +856,16 @@
  host_id
-, msg=None
 )%0A    ok
@@ -880,26 +880,16 @@
 internal
-, msg=None
 )%0A    eq
@@ -910,18 +910,8 @@
 d, 2
-, msg=None
 )%0A%0A%0A
@@ -960,16 +960,18 @@
 s_switch
+es
 ():%0A    

remove msg from nose test