def test_resource_uri(self): self.assertEqual( Pin(resource=Organization(uri='/orgs/foo/')).resource_uri, '/orgs/foo/') self.assertEqual( Pin(resource=Source(uri='/orgs/foo/sources/bar/')).resource_uri, '/orgs/foo/sources/bar/')
def test_uri(self): org = OrganizationFactory(mnemonic='org-1') user = UserProfileFactory(username='******') self.assertEqual(Pin().uri, None) self.assertEqual( Pin(id=1, organization=org).uri, '/orgs/org-1/pins/1/') self.assertEqual(Pin(id=2, user=user).uri, '/users/user-1/pins/2/')
def test_parent(self): org = OrganizationFactory() user = UserProfileFactory() self.assertEqual(Pin().parent, None) self.assertEqual(Pin(organization=org).parent, org) self.assertEqual(Pin(user=user).parent, user) self.assertEqual(Pin(user=user, organization=org).parent, org)
def test_clean(self): org = OrganizationFactory() pin = Pin(resource=org) with self.assertRaises(ValidationError) as ex: pin.full_clean() self.assertEqual( ex.exception.message_dict, dict( parent=['Pin needs to be owned by a user or an organization.' ])) pin.organization = org pin.full_clean()
def create(self, validated_data): resource_type = validated_data.get('resource_type', '') resource_id = validated_data.get('resource_id', '') resource = Pin.get_resource(resource_type, resource_id) item = Pin( user_id=validated_data.get('user_id', None), organization_id=validated_data.get('organization_id', None), resource=resource, ) if not resource: self._errors[ 'resource'] = 'Resource type %s with id %s does not exists.' % ( resource_type, resource_id) return item try: item.full_clean() item.save() except (ValidationError, IntegrityError): self._errors.update(dict(__all__='This pin already exists.')) return item
def test_get_resource(self): org = OrganizationFactory() source = OrganizationSourceFactory(organization=org) collection = OrganizationCollectionFactory(organization=org) self.assertEqual( Pin.get_resource(resource_type='Source', resource_id=source.id), source) self.assertEqual( Pin.get_resource(resource_type='collection', resource_id=collection.id), collection) self.assertEqual( Pin.get_resource(resource_type='org', resource_id=org.id), org) self.assertEqual( Pin.get_resource(resource_type='organization', resource_id=org.id), org) self.assertEqual( Pin.get_resource(resource_type='organization', resource_id=123), None) self.assertEqual( Pin.get_resource(resource_type='foobar', resource_id=123), None)
def test_default_order(self): org = OrganizationFactory(mnemonic='org-1') source = OrganizationSourceFactory(organization=org) user = UserProfileFactory(username='******') org_pin1 = Pin(organization=org, resource=source) org_pin1.save() self.assertEqual(org_pin1.order, 0) org_pin2 = Pin( organization=org, resource=OrganizationCollectionFactory(organization=org)) org_pin2.save() self.assertEqual(org_pin2.order, 1) org_pin2.delete() org_pin3 = Pin( organization=org, resource=OrganizationCollectionFactory(organization=org)) org_pin3.save() self.assertEqual(org_pin3.order, 1) user_pin1 = Pin(user=user, resource=source) user_pin1.save() self.assertEqual(user_pin1.order, 0) user_pin2 = Pin( user=user, resource=OrganizationCollectionFactory(organization=org)) user_pin2.save() self.assertEqual(user_pin2.order, 1) user_pin2.delete() user_pin3 = Pin( user=user, resource=OrganizationCollectionFactory(organization=org)) user_pin3.save() self.assertEqual(user_pin3.order, 1)
def test_soft_delete(self): pin = Pin() pin.delete = Mock(return_value=True) self.assertEqual(pin.soft_delete(), True) pin.delete.assert_called_once()
def test_update_order(self): org = OrganizationFactory(mnemonic='org-1') org_pin1 = Pin(organization=org, resource=OrganizationSourceFactory(organization=org)) org_pin1.save() self.assertEqual(org_pin1.order, 0) org_pin2 = Pin( organization=org, resource=OrganizationCollectionFactory(organization=org)) org_pin2.save() self.assertEqual(org_pin2.order, 1) org_pin3 = Pin( organization=org, resource=OrganizationCollectionFactory(organization=org)) org_pin3.save() self.assertEqual(org_pin3.order, 2) org_pin3.to(0) org_pin1.refresh_from_db() org_pin2.refresh_from_db() org_pin3.refresh_from_db() self.assertEqual(org_pin3.order, 0) self.assertEqual(org_pin1.order, 1) self.assertEqual(org_pin2.order, 2) org_pin3.to(2) org_pin1.refresh_from_db() org_pin2.refresh_from_db() org_pin3.refresh_from_db() self.assertEqual(org_pin1.order, 0) self.assertEqual(org_pin2.order, 1) self.assertEqual(org_pin3.order, 2) org_pin3.to(1) org_pin1.refresh_from_db() org_pin2.refresh_from_db() org_pin3.refresh_from_db() self.assertEqual(org_pin1.order, 0) self.assertEqual(org_pin3.order, 1) self.assertEqual(org_pin2.order, 2)