Ignore:
Timestamp:
21 Mar 2012, 10:41:15 (13 years ago)
Author:
uli
Message:

Make import converters more modular (support special converters for special zope.schema field types) and support lists and result entries as import input.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_converters.py

    r7819 r7932  
    3333from waeup.kofa.app import University
    3434from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
     35from waeup.kofa.interfaces import (
     36    SimpleKofaVocabulary, SubjectSource, GradeSource)
     37from waeup.kofa.schoolgrades import ResultEntryField
    3538from waeup.kofa.university import Faculty
    3639from waeup.kofa.utils.converters import IObjectConverter
    3740from waeup.kofa.utils.helpers import attrs_to_fields
    38 from waeup.kofa.interfaces import SimpleKofaVocabulary
    3941
    4042colors = SimpleKofaVocabulary(
     
    8587        vocabulary = car_nums,
    8688        )
     89    grades = schema.List(
     90        title = u'School Grades',
     91        value_type = ResultEntryField(),
     92        required = True,
     93        default = [],
     94        )
     95    friends = schema.List(
     96        title = u'Friends',
     97        value_type = schema.TextLine(
     98            title = u'Name',
     99            )
     100        )
     101
    87102    @invariant
    88103    def kevinIsYoung(contact):
     
    330345        assert data['num_cars'] == 1
    331346        return
     347
     348    def test_list_of_textlines(self):
     349        # We can convert lists of text lines
     350        converter = IObjectConverter(IContact)
     351        err, inv_err, data = converter.fromStringDict(
     352            {"friends": "['Fred', 'Wilma']"}, 'contact')
     353        self.assertEqual(
     354            data, {'friends': ['Fred', 'Wilma']})
     355        return
     356
     357    def test_list_of_resultentries(self):
     358        # We can handle lists of result entries
     359        converter = IObjectConverter(IContact)
     360        # get currently valid values
     361        s_src, g_src = SubjectSource(), GradeSource()
     362        s_val1, s_val2 = list(s_src.factory.getValues())[0:2]
     363        g_val1, g_val2 = list(g_src.factory.getValues())[0:2]
     364        req_string = u"[('%s', '%s'), ('%s', '%s')]" % (
     365                s_val1, g_val1, s_val2, g_val2)
     366        err, inv_err, data = converter.fromStringDict(
     367            {"grades": req_string,
     368             },
     369            'contact')
     370        result_grades = data['grades']
     371        self.assertTrue(isinstance(result_grades, list))
     372        self.assertEqual(len(result_grades), 2)
     373        self.assertEqual(result_grades[0].subject, s_val1)
     374        self.assertEqual(result_grades[0].grade, g_val1)
     375        return
Note: See TracChangeset for help on using the changeset viewer.