def test_fl_many_many_are_included_from_l_side_with_sort_and_sort_will_not_work( self): singer1 = LinkedSinger(name='AQ') singer2 = LinkedSinger(name='BQ') singer3 = LinkedSinger(name='CC') singer4 = LinkedSinger(name='DD') song1 = LinkedSong(name='Lu Bo To Teo Yeo', singers=[singer2, singer1, singer3, singer4]).save() LinkedSong(name='Teo Sua Nang To Cim Tsung Ci', singers=[singer3, singer4]).save() result = LinkedSong.one(name='Lu Bo To Teo Yeo').include( 'singers', LinkedSinger.find(name={ '_suffix': 'Q' }).order('name', -1)).exec() self.assertEqual(len(result.singers), 2) self.assertEqual([singer.name for singer in result.singers], ['BQ', 'AQ']) result = LinkedSong.one(name='Lu Bo To Teo Yeo').include( 'singers', LinkedSinger.find(name={ '_suffix': 'Q' }).order('name', 1)).exec() self.assertEqual([singer.name for singer in result.singers], ['BQ', 'AQ']) self.assertEqual(len(result.singers), 2)
def test_initialized_object_list_add_local_key_syncs_local_object(self): song = LinkedSong(id=1) singer1 = LinkedSinger(id=1) singer2 = LinkedSinger(id=2) song.singers = [singer1, singer2] song.singer_ids.append(3) self.assertEqual(song.singers, [singer1, singer2])
def test_initialized_object_list_add_local_object_syncs_local_key(self): song = LinkedSong(id=1) singer1 = LinkedSinger(id=1) singer2 = LinkedSinger(id=2) song.singers = [singer1] self.assertEqual(song.singer_ids, [1]) song.singers.append(singer2) self.assertEqual(song.singer_ids, [1, 2])
def test_fl_many_many_set_with_ids_is_saved(self): s1 = LinkedSinger(name='s1').save() s2 = LinkedSinger(name='s2').save() song = LinkedSong(name='song', singer_ids=[s1._id, s2._id]) song.save() song = LinkedSong.one().include('singers').exec() names = [s.name for s in song.singers] self.assertEqual(names, ['s1', 's2'])
def test_initialized_object_list_del_local_key_syncs_local_object(self): song = LinkedSong(id=1) singer1 = LinkedSinger(id=1) singer2 = LinkedSinger(id=2) song.singers = [singer1, singer2] self.assertEqual(singer2.songs, [song]) song.singer_ids.remove(2) self.assertEqual(song.singers, [singer1]) self.assertEqual(singer2.songs, [])
def test_jobject_is_modified_if_local_key_list_is_appended(self): singer1 = LinkedSinger(id=1) singer2 = LinkedSinger(id=2) song = LinkedSong(id=1, singers=[singer1, singer2]) song._mark_not_new() self.assertEqual(song.is_modified, False) song.singer_ids.append(3) self.assertEqual(song.is_modified, True) self.assertEqual(song.modified_fields, ('singers', ))
def test_include_fetches_and_sets_foreign_list_value(self): singer = LinkedSinger(name='michael').save() LinkedSong(name='fairy tale', singers=[singer]).save() LinkedSong(name='heaven', singers=[singer]).save() singer = LinkedSinger.one().exec() result = singer.include('songs') self.assertEqual(result, singer) self.assertEqual(len(singer.songs), 2) self.assertEqual(singer.songs[0].name, 'fairy tale') self.assertEqual(singer.songs[1].name, 'heaven')
def test_include_fetches_and_sets_local_list_value(self): singer1 = LinkedSinger(name='michael').save() singer2 = LinkedSinger(name='victor').save() song = LinkedSong(name='song', singer_ids=[singer1.id, singer2.id]) song.save() result = song.include('singers') self.assertEqual(result, song) self.assertEqual(len(song.singers), 2) self.assertEqual(song.singers[0].name, 'michael') self.assertEqual(song.singers[1].name, 'victor')
def test_jobject_is_modified_if_local_key_list_is_assigned_nothing_changed_to_object_list( self): singer1 = LinkedSinger(id=1) singer2 = LinkedSinger(id=2) song = LinkedSong(id=1, singers=[singer1, singer2]) song._mark_not_new() self.assertEqual(song.is_modified, False) song.singer_ids = [1, 2, 3] self.assertEqual(song.is_modified, True) self.assertEqual(song.modified_fields, ('singers', ))
def test_jobject_is_not_modified_if_local_key_field_list_is_assigned_nothing_changed_at_all( self): singer1 = LinkedSinger(id=1) singer2 = LinkedSinger(id=2) song = LinkedSong(id=1, singers=[singer1, singer2]) song._mark_not_new() self.assertEqual(song.is_modified, False) song.singers = [singer1, singer2] self.assertEqual(song.is_modified, False) self.assertEqual(song.modified_fields, ())
def test_jobject_is_modified_if_local_key_field_list_is_truncated(self): singer1 = LinkedSinger(id=1) singer2 = LinkedSinger(id=2) singer3 = LinkedSinger(id=3) song = LinkedSong(id=1, singers=[singer1, singer2, singer3]) song._mark_not_new() self.assertEqual(song.is_modified, False) song.singers.remove(singer3) self.assertEqual(song.is_modified, True) self.assertEqual(song.modified_fields, ('singers', ))
def test_initialized_object_list_assign_local_key_syncs_local_object(self): song = LinkedSong(id=1) singer1 = LinkedSinger(id=1) singer2 = LinkedSinger(id=2) singer3 = LinkedSinger(id=3) singer4 = LinkedSinger(id=4) song.singers = [singer1] self.assertEqual(song.singer_ids, [1]) song.singer_ids = [7, 8, 1, 9, 10] self.assertEqual(song.singers, [singer1]) self.assertEqual(song.singer_ids, [7, 8, 1, 9, 10])
def test_fl_many_many_unset_with_atomic_operation_is_saved_on_l_side(self): s1 = LinkedSinger(name='s1').save() s2 = LinkedSinger(name='s2').save() song = LinkedSong(name='song') song.set(singers=[{'_add': s1.id}, {'_add': s2.id}]) song.save() song.set(singers=[{'_del': s1.id}]) song = LinkedSong.one().include('singers').exec()
def test_fl_many_many_are_included_from_f_side_with_filter(self): singer1 = LinkedSinger(name='Teh Khim Leng') singer2 = LinkedSinger(name='M Teh Khim Leng') song1 = LinkedSong(name='Lu Bo To Teo Yeo', singers=[singer1, singer2]).save() song2 = LinkedSong(name='Teo Sua Si Gueh Hai', singers=[singer1, singer2]).save() song3 = LinkedSong(name='Siao Thiang Go', singers=[singer1]).save() result = LinkedSinger.id(singer2.id).include( 'songs', LinkedSong.find(name={'_prefix': 'L'})).exec() self.assertEqual(len(result.songs), 1) self.assertEqual(result.songs[0].id, song1.id)
def test_fl_many_many_are_included_from_l_side_with_filter(self): singer1 = LinkedSinger(name='Teh Khim Leng') singer2 = LinkedSinger(name='M Teh Khim Leng') song1 = LinkedSong(name='Lu Bo To Teo Yeo', singers=[singer1, singer2]).save() singer3 = LinkedSinger(name='Phua Kheng Lim') singer4 = LinkedSinger(name='M Phua Kheng Lim') LinkedSong(name='Teo Sua Nang To Cim Tsung Ci', singers=[singer3, singer4]).save() result = LinkedSong.one(name='Lu Bo To Teo Yeo').include( 'singers', LinkedSinger.find(name={'_prefix': 'M'})).exec() self.assertEqual(len(result.singers), 1) self.assertEqual(result.singers[0].id, singer2.id)
def test_fl_many_many_are_included_from_l_side(self): singer1 = LinkedSinger(name='Teh Khim Leng') singer2 = LinkedSinger(name='M Teh Khim Leng') song1 = LinkedSong(name='Lu Bo To Teo Yeo', singers=[singer1, singer2]).save() singer3 = LinkedSinger(name='Phua Kheng Lim') singer4 = LinkedSinger(name='M Phua Kheng Lim') LinkedSong(name='Teo Sua Nang To Cim Tsung Ci', singers=[singer3, singer4]).save() result = LinkedSong.one( name='Lu Bo To Teo Yeo').include('singers').exec() self.assertEqual(result.singer_ids, [singer1.id, singer2.id]) self.assertEqual(result.singers[0].id, singer1.id) self.assertEqual(result.singers[1].id, singer2.id)
def test_fl_many_many_are_included_from_f_side_with_sort(self): singer1 = LinkedSinger(name='Teh Khim Leng') singer2 = LinkedSinger(name='M Teh Khim Leng') song1 = LinkedSong(name='Lu Bo To Teo Yeo', singers=[singer1, singer2]).save() song2 = LinkedSong(name='Lw Bo To Teo Yeo', singers=[singer1, singer2]).save() song3 = LinkedSong(name='Li Bo To Teo Yeo', singers=[singer1, singer2]).save() song4 = LinkedSong(name='Teo Sua Si Gueh Hai', singers=[singer1, singer2]).save() song5 = LinkedSong(name='Siao Thiang Go', singers=[singer1]).save() result = LinkedSinger.id(singer2.id).include( 'songs', LinkedSong.find(name={ '_prefix': 'L' }).order('name', -1)).exec() self.assertEqual(len(result.songs), 3) self.assertEqual( [song.name for song in result.songs], ['Lw Bo To Teo Yeo', 'Lu Bo To Teo Yeo', 'Li Bo To Teo Yeo']) result = LinkedSinger.id(singer2.id).include( 'songs', LinkedSong.find(name={ '_prefix': 'L' }).order('name', 1)).exec() self.assertEqual(len(result.songs), 3) self.assertEqual( [song.name for song in result.songs], ['Li Bo To Teo Yeo', 'Lu Bo To Teo Yeo', 'Lw Bo To Teo Yeo'])
def test_fl_many_many_set_with_atomic_operation_is_saved_on_l_side(self): s1 = LinkedSinger(name='s1').save() s2 = LinkedSinger(name='s2').save() song = LinkedSong(name='song') song.set(singers=[{'_add': s1.id}, {'_add': s2.id}]) song.save() song = LinkedSong.one().include('singers').exec() names = [s.name for s in song.singers] self.assertEqual(names, ['s1', 's2'])
def test_fl_many_many_is_saved(self): song1 = LinkedSong(name='song1') song2 = LinkedSong(name='song2') singer1 = LinkedSinger(name='singer1') singer2 = LinkedSinger(name='singer2') song1.singers = [singer1, singer2] song2.singers = [singer1, singer2] song1.save() collection = Connection.get_collection(LinkedSong) for item in collection.find({}): self.assertEqual(item['singerIds'], [ObjectId(singer1.id), ObjectId(singer2.id)])
def test_initialized_object_list_local_keys_can_be_accessed(self): song = LinkedSong() self.assertEqual(song.singers, []) self.assertEqual(song.singer_ids, [])