def test_from_dict_prm_tamp_mto(self): # When updating a DBTestCls2 object, # it should only be possible to modify # a DBTestCls1 object that is related to that object. prev_name = self.DBTestCls1.query.get(1).name twsu.from_dict(self.DBTestCls2.query.get(2), { 'other': {'id': 1, 'name': prev_name + '_fred'}}) assert(self.DBTestCls1.query.get(1).name == prev_name)
def test_from_dict_prm_tamp_otm(self): # When updating a DBTestCls1 object, it should only be possible to modify # a DBTestCls2 object that is related to that object. prev_nick = self.DBTestCls2.query.get(1).nick prev_id = self.DBTestCls2.query.get(1).id prev_count = self.DBTestCls2.query.count() twsu.from_dict(self.DBTestCls1(), {'others': [ {'id':prev_id, 'nick':prev_nick+'_fred'}]}) obj = self.DBTestCls2.query.get(1) count = self.DBTestCls2.query.count() assert(prev_nick == obj.nick) assert(prev_id == obj.id) assert(count == prev_count+1)
def test_query_from_dict_empty(self): d = {} x = self.DBTestCls1() self.session.add(x) e = twsu.from_dict(x, d) self.session.flush() assert(e.id == 2) assert(e.name == None) assert(e.others == [])
def test_from_dict_new_many_to_many_by_id(self): d = { 'id': None, 'surname': 'user1', 'roles': [self.admin_role], } u = twsu.from_dict(self.DBTestCls3(), d) self.session.add(u) self.session.flush() assert(u.id == 2) assert(u.surname == 'user1') assert(u.roles == [self.admin_role])
def test_query_from_dict_simple(self): d = { 'id': 1, } e = twsu.from_dict(self.DBTestCls1.query.filter_by(**d).first(), d) self.session.flush() assert(e.id == 1) assert(e.name == 'foo') assert(len(e.others) == 1) assert(e.others[0].id == 1) assert(e.others[0].nick == 'bob') assert(e.others[0].other == e)
def test_from_dict_new_many_to_one_by_id(self): d = { 'id': None, 'nick': 'bazaar', 'other_id': 1, } e = twsu.from_dict(self.DBTestCls2(), d) self.session.add(e) self.session.flush() assert(e.id == 3) assert(e.nick == 'bazaar') assert(len(e.other.others) == 2) assert(e in e.other.others)
def test_from_dict_new_many_to_one_by_id(self): d = { 'id': None, 'nick': 'bazaar', 'other_id': 1, } e = twsu.from_dict(self.DBTestCls2(), d) self.session.add(e) self.session.flush() assert (e.id == 3) assert (e.nick == 'bazaar') assert (len(e.other.others) == 2) assert (e in e.other.others)
def test_from_dict_old_many_to_one_by_dict_recall(self): assert(self.DBTestCls2.query.first().nick == 'bob') d = { 'nick': 'updated', 'other': { 'id': 1 } } e = twsu.from_dict(self.DBTestCls2.query.first(), d) self.session.flush() assert(self.DBTestCls2.query.first().nick == 'updated') assert(self.DBTestCls1.query.first().others[0].nick == 'updated')
def test_from_dict_modify_one_to_one_to_none(self): d = { 'id': None, 'name': 'user1', 'account': None } u = twsu.from_dict(self.session.query(self.DBTestCls6).one(), d) self.session.flush() assert(u.id == 1) assert(u.name == 'user1') assert(u.account == None) assert(self.session.query(self.DBTestCls5).count() == 0)
def test_from_dict_new_one_to_one_by_id(self): d = { 'id': None, 'name': 'user1', 'account': self.DBTestCls5(account_name='account2'), } u = twsu.from_dict(self.DBTestCls6(), d) self.session.add(u) self.session.flush() assert (u.id == 2) assert (u.name == 'user1') assert (u.account.account_name == 'account2') assert (self.session.query(self.DBTestCls5).count() == 2)
def test_from_dict_new_one_to_one_by_id_no_account(self): d = { 'id': None, 'name': 'user1', 'account': None, } u = twsu.from_dict(self.DBTestCls6(), d) self.session.add(u) self.session.flush() assert(u.id == 2) assert(u.name == 'user1') assert(u.account == None) assert(self.session.query(self.DBTestCls5).count() == 1)
def test_from_dict_new(self): d = { 'id': None, 'name': 'bazaar', } x = self.DBTestCls1() self.session.add(x) e = twsu.from_dict(x, d) if hasattr(self, 'session'): self.session.flush() assert(e.id == 2) assert(e.name == 'bazaar') assert(len(e.others) == 0)
def test_from_dict_new(self): d = { 'id': None, 'name': 'bazaar', } x = self.DBTestCls1() self.session.add(x) e = twsu.from_dict(x, d) if hasattr(self, 'session'): self.session.flush() assert (e.id == 2) assert (e.name == 'bazaar') assert (len(e.others) == 0)
def test_from_dict_modify_many_to_many(self): d = { 'id': 1, 'surname': 'user1', 'roles': [], } u = twsu.from_dict(self.session.query(self.DBTestCls3).one(), d) self.session.add(u) self.session.flush() assert (self.session.query(self.DBTestCls3).count() == 1) assert (self.session.query(self.DBTestCls4).count() == 1) assert (u.id == 1) assert (u.surname == 'user1') assert (u.roles == [])
def test_from_dict_modify_many_to_many(self): d = { 'id': 1, 'surname': 'user1', 'roles': [], } u = twsu.from_dict(self.session.query(self.DBTestCls3).one(), d) self.session.add(u) self.session.flush() assert(self.session.query(self.DBTestCls3).count() == 1) assert(self.session.query(self.DBTestCls4).count() == 1) assert(u.id == 1) assert(u.surname == 'user1') assert(u.roles == [])
def test_from_dict_modify_to_none(self): # Do this to set up an object with name => 'bazaar' self.test_from_dict_new() # Now try to modify that object and set its name to None d = { 'id': 2, 'name': None, } x = self.DBTestCls1.query.filter_by(id=2).first() e = twsu.from_dict(x, d) self.session.flush() eq_(e.id, 2) eq_(e.name, None) eq_(len(e.others), 0)
def test_from_dict_modify_one_to_one(self): d = { 'id': None, 'name': 'user1', 'account': { 'account_name': 'account2', } } u = twsu.from_dict(self.session.query(self.DBTestCls6).one(), d) self.session.add(u) self.session.flush() assert (u.id == 1) assert (u.name == 'user1') assert (u.account.account_name == 'account2') assert (self.session.query(self.DBTestCls5).count() == 1)
def test_from_dict_mixed_list(self): d = { 'id': None, 'name': 'qatar', 'others': [ {'nick': 'blang'}, 'foo', ] } try: e = twsu.from_dict(self.DBTestCls1(), d) assert(False) except Exception, e: assert(str(e) == 'Cannot send mixed (dict/non dict) data ' + 'to list relationships in from_dict data.')
def test_from_dict_old_many_to_one_by_dict(self): d = { 'id': None, 'nick': 'bazaar', 'other': { 'name': 'foo' } } x = self.DBTestCls2() self.session.add(x) e = twsu.from_dict(x, d) self.session.flush() assert(e.id == 3) assert(e.nick == 'bazaar') assert(e.other.name == 'foo')
def test_from_dict_mixed_list(self): d = { 'id': None, 'name': 'qatar', 'others': [ { 'nick': 'blang' }, 'foo', ] } try: e = twsu.from_dict(self.DBTestCls1(), d) assert (False) except Exception, e: assert (str(e) == 'Cannot send mixed (dict/non dict) data ' + 'to list relationships in from_dict data.')
def test_from_dict_new_one_to_many_by_dict(self): d = { 'id': None, 'name': 'qatar', 'others': [ {'nick': 'blang'}, {'nick': 'blong'}, ] } x = self.DBTestCls1() self.session.add(x) e = twsu.from_dict(x, d) self.session.flush() assert(e.id == 2) assert(e.name == 'qatar') assert(e.others[0].nick == 'blang') assert(e.others[0].id == 3) assert(e.others[1].nick == 'blong') assert(e.others[1].id == 4)
def test_from_dict_new_many_to_one_by_dict(self): d = { 'id': None, 'nick': 'bazaar', 'other': { 'name': 'blamaz' } } x = self.DBTestCls2() self.session.add(x) e = twsu.from_dict(x, d) if hasattr(self, 'session'): self.session.flush() print e.id print e.nick print e.other assert(e.id == 3) assert(e.nick == 'bazaar') assert(e in e.other.others) assert(e.other.name == 'blamaz') assert(e.other.id == 2)
def test_from_dict_new_one_to_many_by_dict(self): d = { 'id': None, 'name': 'qatar', 'others': [ { 'nick': 'blang' }, { 'nick': 'blong' }, ] } x = self.DBTestCls1() self.session.add(x) e = twsu.from_dict(x, d) self.session.flush() assert (e.id == 2) assert (e.name == 'qatar') assert (e.others[0].nick == 'blang') assert (e.others[0].id == 3) assert (e.others[1].nick == 'blong') assert (e.others[1].id == 4)