# coding=utf-8

import sys
import struct
import unittest
from test_000_config import TestConfig
from test_001_connection import TestConnection

from gi.repository import Midgard
from gi.repository import GObject

class TestObjectCrud(unittest.TestCase):
  mgd = None

  def setUp(self):
    if self.mgd == None:
      self.mgd = TestConnection.openConnection()

  def getNewBook(self):
    obj = Midgard.Object.factory(self.mgd, "gir_test_book_crud", None)
    title = "The Holly Grail"
    author = "Sir Lancelot"
    price = 999.999
    serial = Midgard.TypeGuid.new(self.mgd)
    edition = 1
    sold = False
    description = "The true story of white rabbit"

    obj.set_property("title", title)
    obj.set_property("author", author)
    obj.set_property("price", price)
    obj.set_property("serial", serial)
    obj.set_property("edition", edition)
    obj.set_property("sold", sold)
    obj.set_property("description", description)

    return obj

  def testCreate(self):
    obj = self.getNewBook()
   
    title = "The Holly Grail"
    author = "Sir Lancelot"
    price = 999.999
    serial = Midgard.TypeGuid.new(self.mgd)
    edition = 1
    sold = False
    description = "The true story of white rabbit"

    obj.set_property("title", title)
    obj.set_property("author", author)
    obj.set_property("price", price)
    obj.set_property("serial", serial)
    obj.set_property("edition", edition)
    obj.set_property("sold", sold)
    obj.set_property("description", description)

    self.assertTrue(obj.create())
    self.assertTrue(Midgard.is_guid(obj.get_property("guid")))
    new_obj = Midgard.Object.factory(self.mgd, "gir_test_book_crud", obj.get_property("guid"))
    self.assertIsInstance(new_obj, Midgard.Object)
    self.assertEqual(new_obj.get_property("title"), title)
    self.assertEqual(new_obj.get_property("author"), author)
    self.assertEqual(round(new_obj.get_property("price"), 3), price) # FIXME , it should be fixed in core
    self.assertEqual(new_obj.get_property("serial"), serial)
    self.assertEqual(new_obj.get_property("edition"), edition)
    self.assertEqual(new_obj.get_property("sold"), sold)
    self.assertEqual(new_obj.get_property("description"), description)

    # Cleanup 
    obj.purge(False)

  def testUpdate(self):
    obj = self.getNewBook()

    title = "The Holly Grail 2"
    author = "Sir Lancelot"
    price = 09.099
    serial = Midgard.TypeGuid.new(self.mgd)
    edition = 2
    sold = True
    description = "The true story of white rabbit. Part 2"

    obj.set_property("title", title)
    obj.set_property("author", author)
    obj.set_property("price", price)
    obj.set_property("serial", serial)
    obj.set_property("edition", edition)
    obj.set_property("sold", sold)
    obj.set_property("description", description)

    self.assertTrue(obj.create())
    self.assertTrue(Midgard.is_guid(obj.get_property("guid")))

    self.assertTrue(obj.update())

    new_obj = Midgard.Object.factory(self.mgd, "gir_test_book_crud", obj.get_property("guid"))
    self.assertIsInstance(new_obj, Midgard.Object)
    self.assertEqual(new_obj.get_property("title"), title)
    self.assertEqual(new_obj.get_property("author"), author)
    self.assertEqual(round(new_obj.get_property("price"), 3), price) # FIXME , it should be fixed in core
    self.assertEqual(new_obj.get_property("serial"), serial)
    self.assertEqual(new_obj.get_property("edition"), edition)
    self.assertEqual(new_obj.get_property("sold"), sold)
    self.assertEqual(new_obj.get_property("description"), description)

    # Cleanup 
    obj.purge(False)

  def testInheritance(self):
    obj = Midgard.Object.factory(self.mgd, "gir_test_book_crud", None)
    self.assertIsInstance(obj, Midgard.Object)
    self.assertIsInstance(obj, Midgard.DBObject)

if __name__ == "__main__":
    unittest.main()

@@ -2206,32 +2206,54 @@
 , description)%0A%0A
+    # TODO, metadata%0A%0A
     # Cleanup %0A 
@@ -3573,24 +3573,915 @@
 scription)%0A%0A
+    # TODO, metadata%0A%0A    # Cleanup %0A    obj.purge(False)%0A%0A  def testDelete(self):%0A    obj = self.getNewBook()%0A%0A    title = %22The Holly Grail 2%22%0A    author = %22Sir Lancelot%22%0A    price = 09.099%0A    serial = Midgard.TypeGuid.new(self.mgd)%0A    edition = 2%0A    sold = True%0A    description = %22The true story of white rabbit. Part 2%22%0A%0A    obj.set_property(%22title%22, title)%0A    obj.set_property(%22author%22, author)%0A    obj.set_property(%22price%22, price)%0A    obj.set_property(%22serial%22, serial)%0A    obj.set_property(%22edition%22, edition)%0A    obj.set_property(%22sold%22, sold)%0A    obj.set_property(%22description%22, description)%0A%0A    self.assertTrue(obj.create())%0A    self.assertTrue(Midgard.is_guid(obj.get_property(%22guid%22)))%0A%0A    self.assertTrue(obj.delete(False))%0A%0A    # TODO metadata%0A%0A    new_obj = Midgard.Object.factory(self.mgd, %22gir_test_book_crud%22, obj.get_property(%22guid%22))%0A    self.assertIsNone(new_obj)%0A%0A
     # Cleanu

Test delete