Esempio n. 1
0
    def test_merge_extra_data_does_not_override_with_blank_data(self):
        """Test that blank fields in extra data don't override real data"""
        self.bs1.extra_data = {
            'field_a': 'data-1a',
            'field_b': '',
            'field_c': '',
        }
        self.bs1.save()

        self.bs2.extra_data = {
            'field_a': 'data-2a',
            'field_b': 'data-2b',
            'field_c': '',
        }
        self.bs2.save()

        expected_extra = {
            'field_a': 'data-1a',
            'field_b': 'data-2b',
            'field_c': '',
        }
        expected_sources = {
            'field_a': self.bs1.pk,
            'field_b': self.bs2.pk,
            'field_c': self.bs1.pk,
        }

        actual_extra, actual_sources = mapper.merge_extra_data(self.bs1, self.bs2)

        self.assertDictEqual(actual_extra, expected_extra)
        self.assertDictEqual(actual_sources, expected_sources)
Esempio n. 2
0
    def test_merge_extra_data_does_not_override_with_blank_data(self):
        """Test that blank fields in extra data don't override real data"""
        self.bs1.extra_data = {
            'field_a': 'data-1a',
            'field_b': '',
            'field_c': '',
        }
        self.bs1.save()

        self.bs2.extra_data = {
            'field_a': 'data-2a',
            'field_b': 'data-2b',
            'field_c': '',
        }
        self.bs2.save()

        expected_extra = {
            'field_a': 'data-1a',
            'field_b': 'data-2b',
            'field_c': '',
        }
        expected_sources = {
            'field_a': self.bs1.pk,
            'field_b': self.bs2.pk,
            'field_c': self.bs1.pk,
        }

        actual_extra, actual_sources = mapper.merge_extra_data(self.bs1, self.bs2)

        self.assertDictEqual(actual_extra, expected_extra)
        self.assertDictEqual(actual_sources, expected_sources)
Esempio n. 3
0
def update_building(old_snapshot, updated_values, user, *args, **kwargs):
    """Creates a new snapshot with updated values."""
    from seed.mappings import seed_mappings, mapper as seed_mapper

    mappable, meta, sources = _get_filtered_values(updated_values)

    canon = old_snapshot.canonical_building or None
    # Need to hydrate sources
    sources = {
        k: BuildingSnapshot.objects.get(pk=v) for k, v in sources.items() if v
    }

    # Handle the mapping of "normal" attributes.
    new_snapshot = mapper.map_row(
        mappable,
        dict(seed_mappings.BuildingSnapshot_to_BuildingSnapshot),
        BuildingSnapshot,
        initial_data=sources  # Copy parent's source attributes.
    )

    diff_sources = _get_diff_sources(mappable, old_snapshot)
    for diff in diff_sources:
        setattr(new_snapshot, '{0}_source'.format(diff), new_snapshot)

    # convert dates to something django likes
    new_snapshot.clean()
    new_snapshot.canonical_building = canon
    new_snapshot.save()
    # All all the orgs the old snapshot had.
    new_snapshot.super_organization = old_snapshot.super_organization
    # Move the meta data over.
    for meta_val in meta:
        setattr(new_snapshot, meta_val, meta[meta_val])
    # Insert new_snapshot into the inheritence chain
    old_snapshot.children.add(new_snapshot)
    new_snapshot.import_file = old_snapshot.import_file

    # Update/override anything in extra data.
    extra, sources = seed_mapper.merge_extra_data(
        new_snapshot, old_snapshot, default=new_snapshot
    )
    new_snapshot.extra_data = extra
    new_snapshot.extra_data_sources = sources
    new_snapshot.save()

    # If we had a canonical building and its can_snapshot was old, update.
    if canon and canon.canonical_snapshot == old_snapshot:
        canon.canonical_snapshot = new_snapshot
        canon.save()

    return new_snapshot
Esempio n. 4
0
    def test_merge_extra_data(self):
        """extra_data dicts get merged proper-like."""
        self.bs1.extra_data = {"test": "dataface", "test2": "nuup"}
        self.bs1.save()

        self.bs2.extra_data = {"test": "getting overridden", "thing": "hi"}
        self.bs2.save()

        expected_extra = {"test": "dataface", "test2": "nuup", "thing": "hi"}
        expected_sources = {"test": self.bs1.pk, "test2": self.bs1.pk, "thing": self.bs2.pk}

        test_extra, test_sources = mapper.merge_extra_data(self.bs1, self.bs2)

        self.assertDictEqual(test_extra, expected_extra)
        self.assertDictEqual(test_sources, expected_sources)
Esempio n. 5
0
    def test_merge_extra_data(self):
        """extra_data dicts get merged proper-like."""
        self.bs1.extra_data = {'test': 'dataface', 'test2': 'nuup'}
        self.bs1.save()

        self.bs2.extra_data = {'test': 'getting overridden', 'thing': 'hi'}
        self.bs2.save()

        expected_extra = {'test': 'dataface', 'test2': 'nuup', 'thing': 'hi'}
        expected_sources = {
            'test': self.bs1.pk, 'test2': self.bs1.pk, 'thing': self.bs2.pk
        }

        test_extra, test_sources = mapper.merge_extra_data(self.bs1, self.bs2)

        self.assertDictEqual(test_extra, expected_extra)
        self.assertDictEqual(test_sources, expected_sources)
Esempio n. 6
0
    def test_merge_extra_data(self):
        """extra_data dicts get merged proper-like."""
        self.bs1.extra_data = {'test': 'dataface', 'test2': 'nuup'}
        self.bs1.save()

        self.bs2.extra_data = {'test': 'getting overridden', 'thing': 'hi'}
        self.bs2.save()

        expected_extra = {'test': 'dataface', 'test2': 'nuup', 'thing': 'hi'}
        expected_sources = {
            'test': self.bs1.pk, 'test2': self.bs1.pk, 'thing': self.bs2.pk
        }

        test_extra, test_sources = mapper.merge_extra_data(self.bs1, self.bs2)

        self.assertDictEqual(test_extra, expected_extra)
        self.assertDictEqual(test_sources, expected_sources)
Esempio n. 7
0
    def test_merge_extra_data_no_data(self):
        """Test edgecase where there is no extra_data to merge."""
        test_extra, test_sources = mapper.merge_extra_data(self.bs1, self.bs2)

        self.assertDictEqual(test_extra, {})
        self.assertDictEqual(test_sources, {})
Esempio n. 8
0
    def test_merge_extra_data_no_data(self):
        """Test edgecase where there is no extra_data to merge."""
        test_extra, test_sources = mapper.merge_extra_data(self.bs1, self.bs2)

        self.assertDictEqual(test_extra, {})
        self.assertDictEqual(test_sources, {})
Esempio n. 9
0
def update_building(old_snapshot, updated_values, user, *args, **kwargs):
    """Creates a new snapshot with updated values."""
    from seed.mappings import seed_mappings, mapper as seed_mapper

    mappable, meta, sources = _get_filtered_values(updated_values)

    # extra data will get filtered
    extra_data = updated_values['extra_data']
    extra_data = extra_data or old_snapshot.extra_data or {}

    canon = old_snapshot.canonical_building or None
    # Need to hydrate sources
    sources = {
        k: BuildingSnapshot.objects.get(pk=v) for k, v in sources.items() if v
        }

    # Handle the mapping of "normal" attributes.
    new_snapshot = mapper.map_row(
        mappable,
        dict(seed_mappings.BuildingSnapshot_to_BuildingSnapshot),
        BuildingSnapshot,
        initial_data=sources  # Copy parent's source attributes.
    )

    diff_sources = _get_diff_sources(mappable, old_snapshot)
    for diff in diff_sources:
        setattr(new_snapshot, '{0}_source'.format(diff), new_snapshot)

    # convert dates to something django likes
    new_snapshot.clean()
    new_snapshot.canonical_building = canon
    new_snapshot.save()
    # All all the orgs the old snapshot had.
    new_snapshot.super_organization = old_snapshot.super_organization
    # Move the meta data over.
    for meta_val in meta:
        setattr(new_snapshot, meta_val, meta[meta_val])
    # Insert new_snapshot into the inheritance chain
    old_snapshot.children.add(new_snapshot)
    new_snapshot.import_file_id = old_snapshot.import_file_id

    new_snapshot.extra_data = extra_data

    # Update/override anything in extra data.
    extra, sources = seed_mapper.merge_extra_data(
        new_snapshot, old_snapshot, default=new_snapshot
    )
    new_snapshot.extra_data = extra
    new_snapshot.extra_data_sources = sources
    new_snapshot.save()

    # If we had a canonical building and its can_snapshot was old, update.
    if canon and canon.canonical_snapshot == old_snapshot:
        canon.canonical_snapshot = new_snapshot
        canon.save()

    # If the old snapshot was in any project the ProjectBuilding set
    # needs to be updated to point to the new snapshot. We might want
    # to refactor ProjectBuildings to contain a CanonicalBuilding
    # foreign key in the future.
    old_snapshot.project_building_snapshots.all().update(
        building_snapshot=new_snapshot
    )

    # Check to see if there are any new ``extra_data`` fields added for this
    # org.
    save_column_names(new_snapshot)

    return new_snapshot