Changeset 2338 in products


Ignore:
Timestamp:
May 18, 2010 8:29:24 PM (14 years ago)
Author:
mylan
Message:

Added test for link, uid, title for the field

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.referencedatagridfield/branches/plone4/quintagroup/referencedatagridfield/tests/testField.py

    r2337 r2338  
    66 
    77 
    8 class TestField(TestCase): 
    9     """ Field unit tests """ 
     8class TestReferenceDataGridField(TestCase): 
     9    """ ReferenceDataGridField unit tests """ 
    1010 
    1111    def afterSetUp(self): 
     
    1414        self.refcat = self.portal.reference_catalog 
    1515        self.field = self.demo.getField('demo_rdgf') 
    16         self.data = [{"uid": self.doc.UID(), 
    17                       "link": "http://test.link", 
    18                       "title": "test"},] 
    1916 
    2017    def testColumnProperties(self): 
     
    3330 
    3431    def testGet(self): 
     32        data = [{"uid": self.doc.UID(), "link": "http://test.link", "title": "test"},] 
    3533        # if data set: 
    3634        # result is list with dictionary items, as in DataGridField 
    37         self.field.set(self.demo, self.data) 
     35        self.field.set(self.demo, data) 
    3836 
    3937        field_data = self.field.get(self.demo) 
     
    4947        self.assertEqual(item_data.has_key("title"), True) 
    5048 
     49    def getData(self, key, index=0): 
     50        data = self.field.get(self.demo) 
     51        return data and data[index].get(key, None) or None 
     52 
     53    def getRefsCount(self): 
     54        return len(self.refcat.getReferences(self.demo, self.field.relationship)) 
     55 
     56    def testSetUID(self): 
     57        # link always must present in the data 
     58        row = {"uid": "", "link": "/"} 
     59        data = [row,] 
     60        # If set unexistent UID - UID - not set 
     61        row['uid'] = "123" 
     62        self.field.set(self.demo, data) 
     63        self.assertEqual(self.getData("uid"), None) 
     64        # No references to the object 
     65        self.assertEqual(self.getRefsCount(), 0) 
     66 
     67        # If link is not remote url and passed uid of existent object  - uid is saved 
     68        row["uid"] = self.doc.UID() 
     69        self.field.set(self.demo, data) 
     70        self.assertEqual(self.getData("uid"), self.doc.UID()) 
     71        # Also reference added to the object catalog 
     72        self.assertEqual(self.getRefsCount(), 1) 
     73 
     74    def testSetTitleForLink(self): 
     75        row = {"link": "http://google.com"} 
     76        data = [row,] 
     77        # If there is title data with external link - it is stored in the field 
     78        row["title"] = "google" 
     79        self.field.set(self.demo, data) 
     80        self.assertEqual(self.getData("title"), "google") 
     81 
     82        # If No title specified for the external link title will be  equals to link 
     83        row["title"] = "" 
     84        self.field.set(self.demo, data) 
     85        self.assertEqual(self.getData("title"), "http://google.com") 
     86         
     87        self.assertEqual(self.getRefsCount(), 0) 
     88         
     89    def testSetTitleForUID(self): 
     90        row = {"uid": self.doc.UID(), "link": "/"} 
     91        data = [row,] 
     92        # If there is title data with correct uid - it is stored in the field 
     93        row["title"] = "Custom Title" 
     94        self.field.set(self.demo, data) 
     95        self.assertEqual(self.getData("title"), "Custom Title") 
     96 
     97        # If No title specified with correct portal UID object -  
     98        # title will be get from the object 
     99        row["title"] = "" 
     100        self.field.set(self.demo, data) 
     101        self.assertEqual(self.getData("title"), "Test Document") 
     102 
     103    def testNoLink(self): 
     104        # Link is key data for the field. 
     105        # If no link present in the data - no data will be saved 
     106        # even with correct uid. 
     107        data = [{"uid": self.doc.UID(), "title": "Title"},] 
     108        self.field.set(self.demo, data) 
     109        self.assertEqual(self.field.get(self.demo), []) 
     110        # If a external link present in the data - it will be saved     
     111        data = [{"link": "http://google.com"},] 
     112        self.field.set(self.demo, data) 
     113        self.assertEqual(self.getData("link"), "http://google.com") 
     114        
     115         
    51116 
    52117def test_suite(): 
    53118    return unittest.TestSuite([ 
    54         unittest.makeSuite(TestDataGridRelatedField), 
     119        unittest.makeSuite(TestReferenceDataGridField), 
    55120        ]) 
Note: See TracChangeset for help on using the changeset viewer.