def setUp(self): self.api_view = UpdateInventoryLabelsAPIView() # Models can't be imported directly hence self self.PropertyLabels = self.api_view.models['property'] self.TaxlotLabels = self.api_view.models['taxlot'] self.mock_property_queryset = mock_queryset_factory( self.PropertyLabels, flatten=True, property_id=range(1, 11), statuslabel_id=[1] * 3 + [2] * 3 + [3] * 2 + [4] * 2 ) self.user_details = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } self.user = User.objects.create_superuser(**self.user_details) self.org = Organization.objects.create() self.org_user = OrganizationUser.objects.create( user=self.user, organization=self.org ) self.status_label = Label.objects.create( name='test', super_organization=self.org ) self.client.login(**self.user_details)
def setUp(self): self.api_view = UpdateInventoryLabelsAPIView() # Models can't be imported directly hence self self.PropertyViewLabels = self.api_view.models['property'] self.TaxlotViewLabels = self.api_view.models['taxlot'] self.user_details = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } self.user = User.objects.create_superuser(**self.user_details) self.org, _, _ = create_organization(self.user) self.status_label = Label.objects.create( name='test', super_organization=self.org ) self.status_label_2 = Label.objects.create( name='test_2', super_organization=self.org ) self.client.login(**self.user_details) self.label_1 = Label.objects.all()[0] self.label_2 = Label.objects.all()[1] self.label_3 = Label.objects.all()[2] self.label_4 = Label.objects.all()[3] # Create some real PropertyViews, Properties, PropertyStates, and StatusLabels since validations happen cycle_factory = FakeCycleFactory(organization=self.org, user=self.user) cycle = cycle_factory.get_cycle(start=datetime(2010, 10, 10, tzinfo=timezone.get_current_timezone())) property_state_factory = FakePropertyStateFactory(organization=self.org) for i in range(1, 11): ps = property_state_factory.get_property_state() p = Property.objects.create(organization=self.org) PropertyView.objects.create( cycle=cycle, state=ps, property=p ) self.propertyview_ids = PropertyView.objects.all().order_by('id').values_list('id', flat=True) self.mock_propertyview_label_qs = mock_queryset_factory( self.PropertyViewLabels, flatten=True, propertyview_id=self.propertyview_ids, statuslabel_id=[self.label_1.id] * 3 + [self.label_2.id] * 3 + [self.label_3.id] * 2 + [self.label_4.id] * 2 )
def setUp(self): self.api_view = UpdateInventoryLabelsAPIView() # Models can't be imported directly hence self self.PropertyLabels = self.api_view.models['property'] self.TaxlotLabels = self.api_view.models['taxlot'] self.mock_property_queryset = mock_queryset_factory( self.PropertyLabels, flatten=True, property_id=range(1, 11), statuslabel_id=[1] * 3 + [2] * 3 + [3] * 2 + [4] * 2 ) self.user_details = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } self.user = User.objects.create_superuser(**self.user_details) self.org = Organization.objects.create() self.org_user = OrganizationUser.objects.create( user=self.user, organization=self.org ) self.status_label = Label.objects.create( name='test', super_organization=self.org ) self.client.login(**self.user_details)
def setUp(self): self.api_view = UpdateInventoryLabelsAPIView() # Models can't be imported directly hence self self.PropertyViewLabels = self.api_view.models['property'] self.TaxlotViewLabels = self.api_view.models['taxlot'] self.user_details = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } self.user, self.org, _import_file, _import_record, self.cycle = self.set_up( ASSESSED_RAW) org_2, _, _ = create_organization(self.user) self.org_2_status_label = Label.objects.create( name='org_2_label', super_organization=org_2)
name='projects-add-inventory'), url(r'projects/(?P<pk>\w+)/remove/$', ProjectViewSet.as_view({'put': 'remove'}), name='projects-remove-inventory'), url(r'projects/(?P<pk>\w+)/update/$', ProjectViewSet.as_view({'put': 'update_details'}), name='projects-update'), url(r'projects/(?P<pk>\w+)/move/$', ProjectViewSet.as_view({'put': 'move'}), name='projects-move'), url(r'projects/(?P<pk>\w+)/copy/$', ProjectViewSet.as_view({'put': 'copy'}), name='projects-copy'), url( r'labels-property/$', UpdateInventoryLabelsAPIView.as_view(), {'inventory_type': 'property'}, name="property-labels", ), url( r'labels-taxlot/$', UpdateInventoryLabelsAPIView.as_view(), {'inventory_type': 'taxlot'}, name="taxlot-labels", ), url(r'^test_view_with_arg/([0-9]{1})/$', test_view_with_arg, name='testviewarg'), url(r'^export_reports_data/$', Report.as_view({'get': 'export_reports_data'}), name='export_reports_data'),
class TestLabelIntegrityChecks(DataMappingBaseTestCase): def setUp(self): self.api_view = UpdateInventoryLabelsAPIView() # Models can't be imported directly hence self self.PropertyViewLabels = self.api_view.models['property'] self.TaxlotViewLabels = self.api_view.models['taxlot'] self.user_details = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } self.user, self.org, _import_file, _import_record, self.cycle = self.set_up( ASSESSED_RAW) org_2, _, _ = create_organization(self.user) self.org_2_status_label = Label.objects.create( name='org_2_label', super_organization=org_2) def test_error_occurs_when_trying_to_apply_a_label_to_propertyview_from_a_different_org( self): org_1_property = Property.objects.create(organization=self.org) property_state_factory = FakePropertyStateFactory( organization=self.org) org_1_propertystate = property_state_factory.get_property_state() org_1_propertyview = PropertyView.objects.create( property=org_1_property, state=org_1_propertystate, cycle=self.cycle) # Via Label API View with transaction.atomic(): with self.assertRaises(IntegrityError): self.api_view.add_labels( self.api_view.models['property'].objects.none(), 'property', [org_1_propertyview.id], [self.org_2_status_label.id]) # Via PropertyView Model with transaction.atomic(): with self.assertRaises(IntegrityError): org_1_propertyview.labels.add(self.org_2_status_label) # Via PropertyState Rule with Label org_1_dq = DataQualityCheck.objects.get(organization=self.org) org_1_ps_rule = org_1_dq.rules.filter( table_name='PropertyState').first() # Purposely give an Org 1 Rule an Org 2 Label org_1_ps_rule.status_label = self.org_2_status_label org_1_ps_rule.save() with transaction.atomic(): with self.assertRaises(IntegrityError): org_1_dq.update_status_label( self.PropertyViewLabels, Rule.objects.get(pk=org_1_ps_rule.id), org_1_propertyview.id, ) self.assertFalse( PropertyView.objects.get( pk=org_1_propertyview.id).labels.all().exists()) def test_error_occurs_when_trying_to_apply_a_label_to_taxlotview_from_a_different_org( self): org_1_taxlot = TaxLot.objects.create(organization=self.org) taxlot_state_factory = FakeTaxLotStateFactory(organization=self.org) org_1_taxlotstate = taxlot_state_factory.get_taxlot_state() org_1_taxlotview = TaxLotView.objects.create(taxlot=org_1_taxlot, state=org_1_taxlotstate, cycle=self.cycle) with transaction.atomic(): with self.assertRaises(IntegrityError): self.api_view.add_labels( self.api_view.models['taxlot'].objects.none(), 'taxlot', [org_1_taxlot.id], [self.org_2_status_label.id]) with transaction.atomic(): with self.assertRaises(IntegrityError): org_1_taxlotview.labels.add(self.org_2_status_label) # Via TaxLotState Rule with Label org_1_dq = DataQualityCheck.objects.get(organization=self.org) org_1_tls_rule = org_1_dq.rules.filter( table_name='TaxLotState').first() # Purposely give an Org 1 Rule an Org 2 Label org_1_tls_rule.status_label = self.org_2_status_label org_1_tls_rule.save() with transaction.atomic(): with self.assertRaises(IntegrityError): org_1_dq.update_status_label( self.TaxlotViewLabels, Rule.objects.get(pk=org_1_tls_rule.id), org_1_taxlot.id, ) self.assertFalse( TaxLotView.objects.get( pk=org_1_taxlotview.id).labels.all().exists())
class TestUpdateInventoryLabelsAPIView(DeleteModelsTestCase): def setUp(self): self.api_view = UpdateInventoryLabelsAPIView() # Models can't be imported directly hence self self.PropertyViewLabels = self.api_view.models['property'] self.TaxlotViewLabels = self.api_view.models['taxlot'] self.user_details = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } self.user = User.objects.create_superuser(**self.user_details) self.org, _, _ = create_organization(self.user) self.status_label = Label.objects.create( name='test', super_organization=self.org ) self.status_label_2 = Label.objects.create( name='test_2', super_organization=self.org ) self.client.login(**self.user_details) self.label_1 = Label.objects.all()[0] self.label_2 = Label.objects.all()[1] self.label_3 = Label.objects.all()[2] self.label_4 = Label.objects.all()[3] # Create some real PropertyViews, Properties, PropertyStates, and StatusLabels since validations happen cycle_factory = FakeCycleFactory(organization=self.org, user=self.user) cycle = cycle_factory.get_cycle(start=datetime(2010, 10, 10, tzinfo=timezone.get_current_timezone())) property_state_factory = FakePropertyStateFactory(organization=self.org) for i in range(1, 11): ps = property_state_factory.get_property_state() p = Property.objects.create(organization=self.org) PropertyView.objects.create( cycle=cycle, state=ps, property=p ) self.propertyview_ids = PropertyView.objects.all().order_by('id').values_list('id', flat=True) self.mock_propertyview_label_qs = mock_queryset_factory( self.PropertyViewLabels, flatten=True, propertyview_id=self.propertyview_ids, statuslabel_id=[self.label_1.id] * 3 + [self.label_2.id] * 3 + [self.label_3.id] * 2 + [self.label_4.id] * 2 ) def test_get_label_desc(self): add_label_ids = [self.status_label.id] remove_label_ids = [] result = self.api_view.get_label_desc( add_label_ids, remove_label_ids )[0] expected = { 'id': self.status_label.id, 'name': 'test', 'color': 'green' } self.assertEqual(result, expected) def test_get_inventory_id(self): result = self.api_view.get_inventory_id( self.mock_propertyview_label_qs[0], 'property' ) self.assertEqual(result, self.propertyview_ids[0]) def test_exclude(self): result = self.api_view.exclude( self.mock_propertyview_label_qs, 'property', [self.label_3.id, self.label_4.id] ) pvid_7 = self.propertyview_ids[6] pvid_8 = self.propertyview_ids[7] pvid_9 = self.propertyview_ids[8] pvid_10 = self.propertyview_ids[9] expected = {self.label_3.id: [pvid_7, pvid_8], self.label_4.id: [pvid_9, pvid_10]} self.assertEqual(result, expected) def test_label_factory(self): result = self.api_view.label_factory('property', self.label_1.id, self.propertyview_ids[0]) self.assertEqual( result.__class__.__name__, self.PropertyViewLabels.__name__ ) self.assertEqual(result.propertyview_id, self.propertyview_ids[0]) self.assertEqual(result.statuslabel_id, self.label_1.id) def test_add_remove_labels(self): pvid_1 = self.propertyview_ids[0] pvid_2 = self.propertyview_ids[1] pvid_3 = self.propertyview_ids[2] result = self.api_view.add_labels( self.mock_propertyview_label_qs, 'property', [pvid_1, pvid_2, pvid_3], [self.label_2.id, self.label_3.id] ) self.assertEqual(result, [pvid_1, pvid_2, pvid_3] * 2) qs = self.PropertyViewLabels.objects.all() self.assertEqual(len(qs), 6) self.assertEqual(qs[0].propertyview_id, pvid_1) self.assertEqual(qs[0].statuslabel_id, self.label_2.id) result = self.api_view.remove_labels(qs, 'property', [self.label_2.id, self.label_3.id]) qs = self.PropertyViewLabels.objects.all() self.assertEqual(len(qs), 0) def test_put(self): client = APIClient() client.login( username=self.user_details['username'], password=self.user_details['password'] ) r = '/api/v3/labels_property/' url = "{}?organization_id={}".format( r, self.org.id ) pvid_1 = self.propertyview_ids[0] pvid_2 = self.propertyview_ids[1] pvid_3 = self.propertyview_ids[2] post_params = { 'add_label_ids': [self.status_label.id, self.status_label_2.id], 'remove_label_ids': [], 'inventory_ids': [pvid_1, pvid_2, pvid_3], } response = client.put( url, post_params, format='json' ) result = response.data self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(result['status'], 'success') self.assertEqual(result['num_updated'], 3) self.assertEqual(self.PropertyViewLabels.objects.count(), 6) label_assignments = defaultdict(list) for prop_label in self.PropertyViewLabels.objects.all(): label_assignments[prop_label.statuslabel_id].append(prop_label.propertyview_id) expected_label_assignments = { self.status_label.id: [pvid_1, pvid_2, pvid_3], self.status_label_2.id: [pvid_1, pvid_2, pvid_3], } self.assertEqual(label_assignments, expected_label_assignments) post_params = { 'add_label_ids': [], 'remove_label_ids': [self.status_label.id], 'inventory_ids': [pvid_1, pvid_2], } response = client.put( url, post_params, format='json' ) result = response.data self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(result['status'], 'success') self.assertEqual(result['num_updated'], 2) self.assertEqual(self.PropertyViewLabels.objects.count(), 4) label_assignments = defaultdict(list) for prop_label in self.PropertyViewLabels.objects.all(): label_assignments[prop_label.statuslabel_id].append(prop_label.propertyview_id) expected_label_assignments = { self.status_label.id: [pvid_3], self.status_label_2.id: [pvid_1, pvid_2, pvid_3], } self.assertEqual(label_assignments, expected_label_assignments)
), url( r'projects/(?P<pk>\w+)/move/$', ProjectViewSet.as_view({'put': 'transfer'}), {'action': 'move'}, name='projects-move' ), url( r'projects/(?P<pk>\w+)/copy/$', ProjectViewSet.as_view({'put': 'transfer'}), {'action': 'copy'}, name='projects-copy' ), url( r'labels-property/$', UpdateInventoryLabelsAPIView.as_view(), {'inventory_type': 'property'}, name="property-labels", ), url( r'labels-taxlot/$', UpdateInventoryLabelsAPIView.as_view(), {'inventory_type': 'taxlot'}, name="taxlot-labels", ), url( r'^test_view_with_arg/([0-9]{1})/$', test_view_with_arg, name='testviewarg' ), # url(
class TestUpdateInventoryLabelsAPIView(DeleteModelsTestCase): def setUp(self): self.api_view = UpdateInventoryLabelsAPIView() # Models can't be imported directly hence self self.PropertyLabels = self.api_view.models['property'] self.TaxlotLabels = self.api_view.models['taxlot'] self.mock_property_queryset = mock_queryset_factory( self.PropertyLabels, flatten=True, property_id=range(1, 11), statuslabel_id=[1] * 3 + [2] * 3 + [3] * 2 + [4] * 2) self.user_details = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } self.user = User.objects.create_superuser(**self.user_details) self.org, _, _ = create_organization(self.user) self.status_label = Label.objects.create(name='test', super_organization=self.org) self.client.login(**self.user_details) def test_get_label_desc(self): add_label_ids = [self.status_label.id] remove_label_ids = [] result = self.api_view.get_label_desc(add_label_ids, remove_label_ids)[0] expected = { 'id': self.status_label.id, 'name': 'test', 'color': 'green' } self.assertEqual(result, expected) def test_get_inventory_id(self): result = self.api_view.get_inventory_id(self.mock_property_queryset[0], 'property') self.assertEqual(result, 1) def test_exclude(self): result = self.api_view.exclude(self.mock_property_queryset, 'property', [3, 4]) expected = {3: [7, 8], 4: [9, 10]} self.assertEqual(result, expected) def test_label_factory(self): result = self.api_view.label_factory('property', 100, 100) self.assertEqual(result.__class__.__name__, self.PropertyLabels.__name__) self.assertEqual(result.property_id, 100) self.assertEqual(result.statuslabel_id, 100) def test_add_remove_labels(self): result = self.api_view.add_labels(self.mock_property_queryset, 'property', [1, 2, 3], [5, 6]) self.assertEqual(result, [1, 2, 3] * 2) qs = self.PropertyLabels.objects.all() self.assertEqual(len(qs), 6) self.assertEqual(qs[0].property_id, 1) self.assertEqual(qs[0].statuslabel_id, 5) result = self.api_view.remove_labels(qs, 'property', [5, 6]) qs = self.PropertyLabels.objects.all() self.assertEqual(len(qs), 0) def test_put(self): client = APIClient() client.login(username=self.user_details['username'], password=self.user_details['password']) r = reverse('api:v2:property-labels') url = "{}?organization_id={}".format(r, self.org.id) post_params = { 'add_label_ids': [self.status_label.id], 'remove_label_ids': [], 'inventory_ids': [1, 2, 3], } response = client.put(url, post_params, format='json') result = response.data self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(result['status'], 'success') self.assertEqual(result['num_updated'], 3) label = result['labels'][0] self.assertEqual(label['color'], self.status_label.color) self.assertEqual(label['id'], self.status_label.id) self.assertEqual(label['name'], self.status_label.name) post_params = { 'add_label_ids': [], 'remove_label_ids': [self.status_label.id], 'inventory_ids': [1, 2, 3], } response = client.put(url, post_params, format='json') result = response.data self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(result['status'], 'success') self.assertEqual(result['num_updated'], 3) label = result['labels'][0] self.assertEqual(label['color'], self.status_label.color) self.assertEqual(label['id'], self.status_label.id) self.assertEqual(label['name'], self.status_label.name)
class TestUpdateInventoryLabelsAPIView(TestCase): def setUp(self): self.api_view = UpdateInventoryLabelsAPIView() # Models can't be imported directly hence self self.PropertyLabels = self.api_view.models['property'] self.TaxlotLabels = self.api_view.models['taxlot'] self.mock_property_queryset = mock_queryset_factory( self.PropertyLabels, flatten=True, property_id=range(1, 11), statuslabel_id=[1] * 3 + [2] * 3 + [3] * 2 + [4] * 2 ) self.user_details = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } self.user = User.objects.create_superuser(**self.user_details) self.org = Organization.objects.create() self.org_user = OrganizationUser.objects.create( user=self.user, organization=self.org ) self.status_label = Label.objects.create( name='test', super_organization=self.org ) self.client.login(**self.user_details) def tearDown(self): self.user.delete() self.org.delete() self.org_user.delete() self.status_label.delete() # Models can't be imported directly hence self self.PropertyLabels.objects.all().delete() self.TaxlotLabels.objects.all().delete() def test_get_label_desc(self): add_label_ids = [self.status_label.id] remove_label_ids = [] result = self.api_view.get_label_desc( add_label_ids, remove_label_ids )[0] expected = { 'id': self.status_label.id, 'name': 'test', 'color': 'green' } self.assertEqual(result, expected) def test_get_inventory_id(self): result = self.api_view.get_inventory_id( self.mock_property_queryset[0], 'property' ) self.assertEqual(result, 1) def test_exclude(self): result = self.api_view.exclude( self.mock_property_queryset, 'property', [3, 4] ) expected = {3: [7, 8], 4: [9, 10]} self.assertEqual(result, expected) def test_label_factory(self): result = self.api_view.label_factory('property', 100, 100) self.assertEqual( result.__class__.__name__, self.PropertyLabels.__name__ ) self.assertEqual(result.property_id, 100) self.assertEqual(result.statuslabel_id, 100) def test_add_remove_labels(self): result = self.api_view.add_labels( self.mock_property_queryset, 'property', [1, 2, 3], [5, 6] ) self.assertEqual(result, [1, 2, 3] * 2) qs = self.PropertyLabels.objects.all() self.assertEqual(len(qs), 6) self.assertEqual(qs[0].property_id, 1) self.assertEqual(qs[0].statuslabel_id, 5) result = self.api_view.remove_labels(qs, 'property', [5, 6]) qs = self.PropertyLabels.objects.all() self.assertEqual(len(qs), 0) def test_put(self): client = APIClient() client.login( username=self.user_details['username'], password=self.user_details['password'] ) r = reverse('apiv2:property-labels') url = "{}?organization_id={}".format( r, self.org.id ) post_params = { 'add_label_ids': [self.status_label.id], 'remove_label_ids': [], 'inventory_ids': [1, 2, 3], } response = client.put( url, post_params, format='json' ) result = response.data self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(result['status'], 'success') self.assertEqual(result['num_updated'], 3) label = result['labels'][0] self.assertEqual(label['color'], self.status_label.color) self.assertEqual(label['id'], self.status_label.id) self.assertEqual(label['name'], self.status_label.name) post_params = { 'add_label_ids': [], 'remove_label_ids': [self.status_label.id], 'inventory_ids': [1, 2, 3], } response = client.put( url, post_params, format='json' ) result = response.data self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(result['status'], 'success') self.assertEqual(result['num_updated'], 3) label = result['labels'][0] self.assertEqual(label['color'], self.status_label.color) self.assertEqual(label['id'], self.status_label.id) self.assertEqual(label['name'], self.status_label.name)