def test_seed_editor_commit(self): loc = Location(name=u'name', code=u'code') plant = Plant(accession=self.accession, location=loc, code=u'1', quantity=1) propagation = Propagation() plant.propagations.append(propagation) editor = PropagationEditor(model=propagation) widgets = editor.presenter.view.widgets seed_presenter = editor.presenter._seed_presenter view = editor.presenter.view # set default values in editor widgets view.set_widget_value('prop_type_combo', u'Seed') view.set_widget_value('prop_date_entry', default_propagation_values['date']) view.set_widget_value('notes_textview', default_propagation_values['notes']) for widget, attr in seed_presenter.widget_to_field_map.iteritems(): w = widgets[widget] if isinstance(w, gtk.ComboBoxEntry) and not w.get_model(): widgets[widget].child.props.text = default_seed_values[attr] view.set_widget_value(widget, default_seed_values[attr]) # update the editor, send the RESPONSE_OK signal and commit the changes update_gui() editor.handle_response(gtk.RESPONSE_OK) editor.presenter.cleanup() model_id = editor.model.id editor.commit_changes() editor.session.close() s = db.Session() propagation = s.query(Propagation).get(model_id) self.assert_(propagation.prop_type == u'Seed') # make sure the each value in default_seed_values matches the model for attr, expected in default_seed_values.iteritems(): v = getattr(propagation._seed, attr) if isinstance(v, datetime.date): format = prefs.prefs[prefs.date_format_pref] v = v.strftime(format) if isinstance(expected, datetime.date): expected = expected.strftime(format) self.assert_(v==expected, '%s = %s(%s)' % (attr, expected, v)) for attr, expected in default_propagation_values.iteritems(): v = getattr(propagation, attr) self.assert_(v==expected, '%s = %s(%s)' % (attr, expected, v)) s.close()
class PropagationTests(GardenTestCase): def __init__(self, *args): super(PropagationTests, self).__init__(*args) def setUp(self): super(PropagationTests, self).setUp() self.accession = self.create(Accession, species=self.species,code=u'1') # self.location = self.create(Location, name=u'name', code=u'code') # self.plant = self.create(Plant, accession=self.accession, # location=self.location, code=u'2') self.session.commit() def tearDown(self): #self.session.delete(self.location) #self.session.delete(self.plant) #self.session.commit() #self.session.begin() super(PropagationTests, self).tearDown() def test_plant_prop(self): """ Test the Accession->AccessionPropagation->Propagation relation """ loc = Location(name=u'name', code=u'code') plant = Plant(accession=self.accession, location=loc, code=u'1', quantity=1) prop = Propagation() prop.plant = plant prop.prop_type = u'UnrootedCutting' cutting = PropCutting(**default_cutting_values) cutting.propagation = prop self.session.commit() self.assert_(prop in self.accession.propagations) self.assert_(prop.accession == self.accession) def test_plant_prop(self): """ Test the Plant->PlantPropagation->Propagation relation """ prop = Propagation() loc = self.create(Location, name=u'site1', code=u'1') plant = self.create(Plant, accession=self.accession, location=loc, code=u'1', quantity=1) prop.prop_type = u'UnrootedCutting' cutting = PropCutting(**default_cutting_values) cutting.propagation = prop plant.propagations.append(prop) self.session.commit() self.assert_(prop in plant.propagations) self.assert_(prop.plant == plant) def test_get_summary(self): loc = Location(name=u'name', code=u'code') plant = Plant(accession=self.accession, location=loc, code=u'1', quantity=1) prop = Propagation() prop.plant = plant prop.prop_type = u'UnrootedCutting' cutting = PropCutting(**default_cutting_values) cutting.propagation = prop rooted = PropRooted() rooted.cutting = cutting self.session.commit() summary = prop.get_summary() #debug(summary) self.assert_(summary) prop = Propagation() prop.prop_type = u'Seed' prop.plant = plant seed = PropSeed(**default_seed_values) seed.propagation = prop self.session.commit() summary = prop.get_summary() #debug(summary) self.assert_(summary) def test_cutting_property(self): loc = Location(name=u'name', code=u'code') plant = Plant(accession=self.accession, location=loc, code=u'1', quantity=1) prop = Propagation() prop.plant = plant prop.prop_type = u'UnrootedCutting' prop.accession = self.accession cutting = PropCutting(**default_cutting_values) cutting.propagation = prop rooted = PropRooted() rooted.cutting = cutting self.session.add(rooted) self.session.commit() self.assert_(rooted in prop._cutting.rooted) rooted_id = rooted.id cutting_id = cutting.id self.assert_(rooted_id, 'no prop_rooted.id') # setting the _cutting property on Propagation should cause # the cutting and its rooted children to be deleted prop._cutting = None self.session.commit() self.assert_(not self.session.query(PropCutting).get(cutting_id)) self.assert_(not self.session.query(PropRooted).get(rooted_id)) def test_seed_property(self): loc = Location(name=u'name', code=u'code') plant = Plant(accession=self.accession, location=loc, code=u'1', quantity=1) prop = Propagation() plant.propagations.append(prop) prop.prop_type = u'Seed' prop.accession = self.accession seed = PropSeed(**default_seed_values) self.session.add(seed) seed.propagation = prop self.session.commit() self.assert_(seed == prop._seed) seed_id = seed.id # this should cause the cutting and its rooted children to be deleted prop._seed = None self.session.commit() self.assert_(not self.session.query(PropSeed).get(seed_id)) def test_cutting_editor(self): loc = Location(name=u'name', code=u'code') plant = Plant(accession=self.accession, location=loc, code=u'1', quantity=1) propagation = Propagation() plant.propagations.append(propagation) self.editor = PropagationEditor(model=propagation) widgets = self.editor.presenter.view.widgets view = self.editor.presenter.view view.set_widget_value('prop_type_combo', u'UnrootedCutting') view.set_widget_value('prop_date_entry', utils.today_str()) cutting_presenter = self.editor.presenter._cutting_presenter for widget, attr in cutting_presenter.widget_to_field_map.iteritems(): #debug('%s=%s' % (widget, default_cutting_values[attr])) view.set_widget_value(widget, default_cutting_values[attr]) update_gui() self.editor.handle_response(gtk.RESPONSE_OK) self.editor.commit_changes() model = self.editor.model s = object_session(model) s.expire(model) self.assert_(model.prop_type == u'UnrootedCutting') for attr, value in default_cutting_values.iteritems(): v = getattr(model._cutting, attr) self.assert_(v==value, '%s = %s(%s)' % (attr, value, v)) self.editor.session.close() def test_seed_editor_commit(self): loc = Location(name=u'name', code=u'code') plant = Plant(accession=self.accession, location=loc, code=u'1', quantity=1) propagation = Propagation() plant.propagations.append(propagation) editor = PropagationEditor(model=propagation) widgets = editor.presenter.view.widgets seed_presenter = editor.presenter._seed_presenter view = editor.presenter.view # set default values in editor widgets view.set_widget_value('prop_type_combo', u'Seed') view.set_widget_value('prop_date_entry', default_propagation_values['date']) view.set_widget_value('notes_textview', default_propagation_values['notes']) for widget, attr in seed_presenter.widget_to_field_map.iteritems(): w = widgets[widget] if isinstance(w, gtk.ComboBoxEntry) and not w.get_model(): widgets[widget].child.props.text = default_seed_values[attr] view.set_widget_value(widget, default_seed_values[attr]) # update the editor, send the RESPONSE_OK signal and commit the changes update_gui() editor.handle_response(gtk.RESPONSE_OK) editor.presenter.cleanup() model_id = editor.model.id editor.commit_changes() editor.session.close() s = db.Session() propagation = s.query(Propagation).get(model_id) self.assert_(propagation.prop_type == u'Seed') # make sure the each value in default_seed_values matches the model for attr, expected in default_seed_values.iteritems(): v = getattr(propagation._seed, attr) if isinstance(v, datetime.date): format = prefs.prefs[prefs.date_format_pref] v = v.strftime(format) if isinstance(expected, datetime.date): expected = expected.strftime(format) self.assert_(v==expected, '%s = %s(%s)' % (attr, expected, v)) for attr, expected in default_propagation_values.iteritems(): v = getattr(propagation, attr) self.assert_(v==expected, '%s = %s(%s)' % (attr, expected, v)) s.close() def test_seed_editor_load(self): loc = Location(name=u'name', code=u'code') plant = Plant(accession=self.accession, location=loc, code=u'1', quantity=1) propagation = Propagation(**default_propagation_values) propagation.prop_type = u'Seed' propagation._seed = PropSeed(**default_seed_values) plant.propagations.append(propagation) editor = PropagationEditor(model=propagation) widgets = editor.presenter.view.widgets seed_presenter = editor.presenter._seed_presenter view = editor.presenter.view update_gui() # check that the values loaded correctly from the model in the # editor widget def get_widget_text(w): if isinstance(w, gtk.TextView): return w.get_buffer().props.text elif isinstance(w, gtk.Entry): return w.props.text elif isinstance(w, gtk.ComboBoxEntry): return w.get_active_text() else: raise ValueError('%s not supported' % type(w)) # make sure the default values match the values in the widgets date_format = prefs.prefs[prefs.date_format_pref] for widget, attr in editor.presenter.widget_to_field_map.iteritems(): if not attr in default_propagation_values: continue default = default_propagation_values[attr] if isinstance(default, datetime.date): default = default.strftime(date_format) value = get_widget_text(widgets[widget]) self.assert_(value == default, '%s = %s (%s)' % (attr, value, default)) # check the default for the PropSeed and SeedPresenter for widget, attr in seed_presenter.widget_to_field_map.iteritems(): if not attr in default_seed_values: continue default = default_seed_values[attr] if isinstance(default, datetime.date): default = default.strftime(date_format) if isinstance(default, int): default = str(default) value = get_widget_text(widgets[widget]) self.assert_(value == default, '%s = %s (%s)' % (attr, value, default)) def itest_editor(self): """ Interactively test the PropagationEditor """ from bauble.plugins.garden.propagation import PropagationEditor propagation = Propagation() #propagation.prop_type = u'UnrootedCutting' propagation.accession = self.accession editor = PropagationEditor(model=propagation) propagation = editor.start() debug(propagation) self.assert_(propagation.accession)