def test_taxlots_merge_without_losing_labels(self): # Create 3 Labels label_factory = FakeStatusLabelFactory(organization=self.org) label_1 = label_factory.get_statuslabel() label_2 = label_factory.get_statuslabel() label_3 = label_factory.get_statuslabel() self.view_1.labels.add(label_1, label_2) self.view_2.labels.add(label_2, label_3) # Merge the taxlots url = reverse('api:v2:taxlots-merge') + '?organization_id={}'.format( self.org.pk) post_params = json.dumps( {'state_ids': [self.state_2.pk, self.state_1.pk]}) self.client.post(url, post_params, content_type='application/json') # The resulting -View should have 3 notes view = TaxLotView.objects.first() self.assertEqual(view.labels.count(), 3) label_names = list(view.labels.values_list('name', flat=True)) self.assertCountEqual(label_names, [label_1.name, label_2.name, label_3.name])
def test_taxlots_unmerge_without_losing_labels(self): # Merge the taxlots url = reverse('api:v2:taxlots-merge') + '?organization_id={}'.format( self.org.pk) post_params = json.dumps({ 'state_ids': [self.state_2.pk, self.state_1.pk] # priority given to state_1 }) self.client.post(url, post_params, content_type='application/json') # Create 3 Labels - add 2 to view label_factory = FakeStatusLabelFactory(organization=self.org) label_1 = label_factory.get_statuslabel() label_2 = label_factory.get_statuslabel() view = TaxLotView.objects.first() # There's only one TaxLotView view.labels.add(label_1, label_2) # Unmerge the taxlots url = reverse('api:v2:taxlots-unmerge', args=[ view.id ]) + '?organization_id={}'.format(self.org.pk) self.client.post(url, content_type='application/json') for new_view in TaxLotView.objects.all(): self.assertEqual(new_view.labels.count(), 2) label_names = list(new_view.labels.values_list('name', flat=True)) self.assertCountEqual(label_names, [label_1.name, label_2.name])
def test_properties_unmerge_without_losing_labels(self): # Create 3 Labels - add 2 to view label_factory = FakeStatusLabelFactory(organization=self.org) label_1 = label_factory.get_statuslabel() label_2 = label_factory.get_statuslabel() view = PropertyView.objects.first() # There's only one PropertyView view.labels.add(label_1, label_2) # Unmerge the properties url = reverse('api:v2:properties-unmerge', args=[view.id]) + '?organization_id={}'.format(self.org.pk) self.client.post(url, content_type='application/json') for new_view in PropertyView.objects.all(): self.assertEqual(new_view.labels.count(), 2) label_names = list(new_view.labels.values_list('name', flat=True)) self.assertCountEqual(label_names, [label_1.name, label_2.name])
class TestPropertySerializers(DeleteModelsTestCase): def setUp(self): self.maxDiff = None user_details = { 'username': '******', 'password': '******', } self.user = User.objects.create_superuser(email='*****@*****.**', **user_details) self.org, _, _ = create_organization(self.user) self.audit_log_factory = FakePropertyAuditLogFactory( organization=self.org, user=self.user) self.property_factory = FakePropertyFactory(organization=self.org) self.property_state_factory = FakePropertyStateFactory( organization=self.org) self.property_view_factory = FakePropertyViewFactory( organization=self.org, user=self.user) self.ga_factory = FakeGreenAssessmentFactory(organization=self.org) self.gap_factory = FakeGreenAssessmentPropertyFactory( organization=self.org, user=self.user) self.label_factory = FakeStatusLabelFactory(organization=self.org) self.assessment = self.ga_factory.get_green_assessment() self.property_view = self.property_view_factory.get_property_view() self.gap_data = { 'source': 'test', 'status': 'complete', 'status_date': datetime.date(2017, 0o1, 0o1), 'metric': 5, 'version': '0.1', 'date': datetime.date(2016, 0o1, 0o1), 'eligibility': True, 'assessment': self.assessment, 'view': self.property_view, } self.urls = ['http://example.com', 'http://example.org'] def test_audit_log_serializer(self): """Test to_representation method.""" # test with AUDIT_USER_CREATE audit_log = self.audit_log_factory.get_property_audit_log() result = PropertyAuditLogReadOnlySerializer(audit_log).data self.assertEqual(result['description'], 'test audit log') self.assertEqual(result['date_edited'], audit_log.created.ctime()) self.assertEqual(result['source'], 'UserCreate') self.assertIsNone(result['changed_fields']) self.assertEqual(result['state']['city'], 'Boring') # test with AUDIT_USER_EDIT changed_fields = ['a', 'b', 'c'] audit_log = self.audit_log_factory.get_property_audit_log( record_type=AUDIT_USER_EDIT, description=json.dumps(changed_fields)) result = PropertyAuditLogReadOnlySerializer(audit_log).data self.assertEqual(result['description'], 'User edit') self.assertEqual(result['source'], 'UserEdit') self.assertEqual(result['changed_fields'], changed_fields) def test_property_view_list_serializer(self): """Test to_representation method.""" property_view_1 = self.property_view_factory.get_property_view() property_view_2 = self.property_view_factory.get_property_view() gap1_data = self.gap_data.copy() gap2_data = self.gap_data.copy() gap1_data['view'] = property_view_1 gap2_data['view'] = property_view_2 gap2_data['metric'] = 4 self.gap_factory.get_green_assessment_property(**gap1_data) self.gap_factory.get_green_assessment_property(**gap2_data) serializer = PropertyViewListSerializer(child=PropertyViewSerializer()) result = serializer.to_representation( [property_view_1, property_view_2]) self.assertEqual(result[0]['cycle']['id'], property_view_1.cycle_id) self.assertEqual(result[1]['cycle']['id'], property_view_2.cycle_id) self.assertEqual(result[0]['state']['id'], property_view_1.state_id) self.assertEqual(result[1]['state']['id'], property_view_2.state_id) self.assertEqual(result[0]['certifications'][0]['score'], 5) self.assertEqual(result[1]['certifications'][0]['score'], 4) self.assertEqual(result[0]['certifications'][0]['assessment']['name'], self.assessment.name) self.assertEqual(result[1]['certifications'][0]['assessment']['name'], self.assessment.name) # with queryset serializer = PropertyViewListSerializer(child=PropertyViewSerializer()) queryset = PropertyView.objects.filter( id__in=[property_view_1.id, property_view_2.id]).order_by('id') result = serializer.to_representation(queryset) self.assertEqual(result[0]['cycle']['id'], property_view_1.cycle_id) self.assertEqual(result[1]['cycle']['id'], property_view_2.cycle_id) self.assertEqual(result[0]['state']['id'], property_view_1.state_id) self.assertEqual(result[1]['state']['id'], property_view_2.state_id) self.assertEqual(result[0]['certifications'][0]['score'], 5) self.assertEqual(result[1]['certifications'][0]['score'], 4) self.assertEqual(result[0]['certifications'][0]['assessment']['name'], self.assessment.name) self.assertEqual(result[1]['certifications'][0]['assessment']['name'], self.assessment.name) def test_property_list_serializer(self): """Test PropertyListSerializer.to_representation""" # TODO test to representation property1 = self.property_factory.get_property() property2 = self.property_factory.get_property() label1 = self.label_factory.get_statuslabel() label2 = self.label_factory.get_statuslabel() # make sure that the second label didn't by chance turn out to be the same as label1 while label2 == label1: print("label was the same as the first, trying again") label2 = self.label_factory.get_statuslabel() property1.labels.add(label1) property1.labels.add(label2) property2.labels.add(label2) expected = [ OrderedDict([('id', property1.id), ('campus', False), ('parent_property', None), ('labels', [label1.id, label2.id])]), OrderedDict([('id', property2.id), ('campus', False), ('parent_property', None), ('labels', [label2.id])]), ] serializer = PropertyListSerializer(child=PropertyMinimalSerializer()) result = serializer.to_representation([property1, property2]) self.assertEqual(expected, result)