def test_manager_safe_multi_save01(self): create_ptype = CremePropertyType.create ptype1 = create_ptype(str_pk='test-prop_delicious', text='is delicious') ptype2 = create_ptype(str_pk='test-prop_happy', text='is happy') entity1 = CremeEntity.objects.create(user=self.user) entity2 = CremeEntity.objects.create(user=self.user) count = CremeProperty.objects.safe_multi_save([ CremeProperty(type=ptype1, creme_entity=entity1), CremeProperty(type=ptype2, creme_entity=entity1), CremeProperty(type=ptype2, creme_entity=entity2), ]) self.assertEqual(3, count) self.get_object_or_fail(CremeProperty, type=ptype1.id, creme_entity=entity1.id) self.get_object_or_fail(CremeProperty, type=ptype2.id, creme_entity=entity1.id) self.get_object_or_fail(CremeProperty, type=ptype2.id, creme_entity=entity2.id)
def test_field_info_get_value01(self): FieldInfo = meta.FieldInfo user = get_user_model().objects.create(username='******') al = FakeContact.objects.create(user=user, first_name='Alphonse', last_name='Elric') self.assertEqual(al.first_name, FieldInfo(FakeContact, 'first_name').value_from(al)) self.assertEqual(user, FieldInfo(FakeContact, 'user').value_from(al)) self.assertEqual( user.username, FieldInfo(FakeContact, 'user__username').value_from(al)) # Other model ptype = CremePropertyType.objects.create(text='Is the hero', is_custom=True) prop = CremeProperty(type=ptype, creme_entity=al) self.assertEqual( ptype.text, FieldInfo(CremeProperty, 'type__text').value_from(prop)) self.assertEqual( al.entity_type.model, FieldInfo(CremeProperty, 'creme_entity__entity_type__model').value_from(prop)) with self.assertRaises(ValueError): FieldInfo(CremeProperty, 'type__text').value_from( al) # 'al' is not a CremeProperty self.assertIsNone(FieldInfo(FakeContact, 'sector').value_from(al)) self.assertIsNone( FieldInfo(FakeContact, 'sector__title').value_from(al))
def test_manager_safe_multi_save03(self): "Avoid creating existing properties" create_ptype = CremePropertyType.create ptype1 = create_ptype(str_pk='test-prop_delicious', text='is delicious') ptype2 = create_ptype(str_pk='test-prop_happy', text='is happy') entity = CremeEntity.objects.create(user=self.user) def build_prop1(): return CremeProperty(type=ptype1, creme_entity=entity) prop1 = build_prop1() prop1.save() count = CremeProperty.objects.safe_multi_save([ build_prop1(), CremeProperty(type=ptype2, creme_entity=entity), ]) self.assertEqual(1, count) self.assertStillExists(prop1) self.get_object_or_fail(CremeProperty, type=ptype2.id, creme_entity=entity.id)
def test_manager_safe_multi_save02(self): "De-duplicates arguments." create_ptype = CremePropertyType.create ptype1 = create_ptype(str_pk='test-prop_delicious', text='is delicious') ptype2 = create_ptype(str_pk='test-prop_happy', text='is happy') entity = CremeEntity.objects.create(user=self.user) count = CremeProperty.objects.safe_multi_save([ CremeProperty(type=ptype1, creme_entity=entity), CremeProperty(type=ptype2, creme_entity=entity), CremeProperty(type=ptype1, creme_entity=entity), # <=== duplicate ]) self.assertEqual(2, count) self.get_object_or_fail(CremeProperty, type=ptype1.id, creme_entity=entity.id) self.get_object_or_fail(CremeProperty, type=ptype2.id, creme_entity=entity.id)
def test_manager_safe_multi_save05(self): "Argument <check_existing>." create_ptype = CremePropertyType.create ptype1 = create_ptype(str_pk='test-prop_delicious', text='is delicious') ptype2 = create_ptype(str_pk='test-prop_happy', text='is happy') entity = CremeEntity.objects.create(user=self.user) with CaptureQueriesContext() as ctxt1: CremeProperty.objects.safe_multi_save( [CremeProperty(type=ptype1, creme_entity=entity)], check_existing=True, ) with CaptureQueriesContext() as ctxt2: CremeProperty.objects.safe_multi_save( [CremeProperty(type=ptype2, creme_entity=entity)], check_existing=False, ) self.get_object_or_fail(CremeProperty, type=ptype1.id, creme_entity=entity.id) self.get_object_or_fail(CremeProperty, type=ptype2.id, creme_entity=entity.id) self.assertEqual(len(ctxt1), len(ctxt2) + 1)
def test_disable03(self): "Property creation & deletion" user = self.user hayao = FakeContact.objects.create(user=user, first_name='Hayao', last_name='Miyazaki') ptype = CremePropertyType.create(str_pk='test-prop_make_animes', text='Make animes') old_count = HistoryLine.objects.count() prop = CremeProperty(type=ptype, creme_entity=hayao) HistoryLine.disable(prop) prop.save() self.assertEqual(old_count, HistoryLine.objects.count()) # ----------------------- prop = self.refresh(prop) HistoryLine.disable(prop) prop.delete() self.assertEqual(old_count, HistoryLine.objects.count())
def build_prop1(): return CremeProperty(type=ptype1, creme_entity=entity)