def test_refresh( self ): from camelot.core.orm import Session from camelot.model.party import Person refresh_action = application_action.Refresh() session = Session() # # create objects in various states # p1 = Person(first_name = u'p1', last_name = u'persistent' ) p2 = Person(first_name = u'p2', last_name = u'dirty' ) p3 = Person(first_name = u'p3', last_name = u'deleted' ) p4 = Person(first_name = u'p4', last_name = u'to be deleted' ) p5 = Person(first_name = u'p5', last_name = u'detached' ) p6 = Person(first_name = u'p6', last_name = u'deleted outside session' ) session.flush() p3.delete() session.flush() p4.delete() p2.last_name = u'clean' # # delete p6 without the session being aware # person_table = Person.table session.execute( person_table.delete().where( person_table.c.party_id == p6.id ) ) # # refresh the session through the action # list( refresh_action.model_run( self.context ) ) self.assertEqual( p2.last_name, u'dirty' )
def test_person_contact_mechanism( self ): from camelot.model.party import Person person = Person( first_name = u'Robin', last_name = u'The brave' ) self.assertEqual( person.contact_mechanisms_email, None ) mechanism = ('email', '*****@*****.**') person.contact_mechanisms_email = mechanism self.person_admin.flush( person ) self.assertEqual( person.contact_mechanisms_email, mechanism ) self.person_admin.delete( person ) person = Person( first_name = u'Robin', last_name = u'The brave' ) self.person_admin.flush( person ) self.assertEqual( person.contact_mechanisms_email[1], u'' )
def test_fixture(self): from camelot.model.party import Person from camelot.model.fixture import Fixture session = Session() self.assertEqual(Fixture.find_fixture_key(Person, -1), None) p1 = Person() self.assertEqual(Fixture.find_fixture_key_and_class(p1), (None, None)) session.expunge(p1) # insert a new Fixture p2 = Fixture.insert_or_update_fixture(Person, 'test', { 'first_name': 'Peter', 'last_name': 'Principle' }, fixture_class='test') # see if we can find it back self.assertEqual(Fixture.find_fixture_key(Person, p2.id), 'test') self.assertEqual(Fixture.find_fixture_key_and_class(p2), ('test', 'test')) self.assertEqual( Fixture.find_fixture_keys_and_classes(Person)[p2.id], ('test', 'test')) # delete the person, and insert it back in the same fixture session.delete(p2) session.flush() p3 = Fixture.insert_or_update_fixture(Person, 'test', { 'first_name': 'Peter', 'last_name': 'Principle' }, fixture_class='test') self.assertNotEqual(p2, p3) # remove all fixtures Fixture.remove_all_fixtures(Person)
def test_person_contact_mechanism(self): from camelot.model.party import Person mechanism_1 = (u"email", u"*****@*****.**") mechanism_2 = (u"email", u"*****@*****.**") person = Person(first_name=u"Robin", last_name=u"The brave") self.assertEqual(person.email, None) person.email = mechanism_1 self.person_admin.flush(person) self.assertEqual(person.email, mechanism_1) person.email = mechanism_2 self.assertEqual(person.email, mechanism_2) self.person_admin.delete(person) person = Person(first_name=u"Robin", last_name=u"The brave") self.person_admin.flush(person) self.assertEqual(person.email, None) person.email = mechanism_2 person.email = None self.assertEqual(person.email, None) self.person_admin.flush(person) self.assertEqual(person.email, None)
def test_insert_after_sort(self): from camelot.view.proxy.queryproxy import QueryTableProxy from camelot.model.party import Person self.proxy.sort(1, Qt.AscendingOrder) # check the query self.assertTrue(self.proxy.columnCount() > 0) rowcount = self.proxy.rowCount() self.assertTrue(rowcount > 0) # check the sorting self._load_data() data0 = self._data(0, 1) data1 = self._data(1, 1) self.assertTrue(data1 > data0) self.proxy.sort(1, Qt.DescendingOrder) self._load_data() data0 = self._data(0, 1) data1 = self._data(1, 1) self.assertTrue(data0 > data1) # insert a new object person = Person() self.proxy.append_object(person) new_rowcount = self.proxy.rowCount() self.assertTrue(new_rowcount > rowcount) new_row = new_rowcount - 1 self.assertEqual(person, self.proxy._get_object(new_row)) # fill in the required fields self.assertFalse(self.person_admin.is_persistent(person)) self._set_data(new_row, 0, 'Foo') self._set_data(new_row, 1, 'Bar') self.assertEqual(person.first_name, 'Foo') self.assertEqual(person.last_name, 'Bar') self._load_data() self.assertEqual(self._data(new_row, 0), 'Foo') self.assertEqual(self._data(new_row, 1), 'Bar') self.assertTrue(self.person_admin.is_persistent(person)) # create a related proxy (eg, to display a form view) related_proxy = QueryTableProxy(self.person_admin, self.proxy.get_query_getter(), self.person_admin.get_columns, max_number_of_rows=1, cache_collection_proxy=self.proxy) self.assertEqual(new_rowcount, related_proxy.rowCount()) self._load_data(related_proxy) self.assertEqual(self._data(new_row, 0, related_proxy), 'Foo')
def test_fixture_version(self): from camelot.model.party import Person from camelot.model.fixture import FixtureVersion self.assertEqual(FixtureVersion.get_current_version(u'unexisting'), 0) FixtureVersion.set_current_version(u'demo_data', 0) self.assertEqual(FixtureVersion.get_current_version(u'demo_data'), 0) example_file = os.path.join(os.path.dirname(__file__), '..', 'camelot_example', 'import_example.csv') person_count_before_import = Person.query.count() # begin load csv if fixture version import csv if FixtureVersion.get_current_version(u'demo_data') == 0: reader = csv.reader(open(example_file)) for line in reader: Person(first_name=line[0], last_name=line[1]) FixtureVersion.set_current_version(u'demo_data', 1) Person.query.session.flush() # end load csv if fixture version self.assertTrue(Person.query.count() > person_count_before_import) self.assertEqual(FixtureVersion.get_current_version(u'demo_data'), 1)
def test_refresh( self ): from camelot.core.orm import Session from camelot.model.party import Person refresh_action = application_action.Refresh() session = Session() session.expunge_all() # create objects in various states # p1 = Person(first_name = u'p1', last_name = u'persistent' ) p2 = Person(first_name = u'p2', last_name = u'dirty' ) p3 = Person(first_name = u'p3', last_name = u'deleted' ) p4 = Person(first_name = u'p4', last_name = u'to be deleted' ) p5 = Person(first_name = u'p5', last_name = u'detached' ) p6 = Person(first_name = u'p6', last_name = u'deleted outside session' ) session.flush() p3.delete() session.flush() p4.delete() p2.last_name = u'clean' # # delete p6 without the session being aware # person_table = Person.table session.execute( person_table.delete().where( person_table.c.party_id == p6.id ) ) # # refresh the session through the action # list( refresh_action.model_run( self.context ) ) self.assertEqual( p2.last_name, u'dirty' )