<commit_before>from corehq.apps.commtrack.helpers import make_supply_point
from corehq.apps.commtrack.tests.util import CommTrackTest, make_loc
from corehq.apps.locations.models import Location
from corehq.apps.locations.bulk import import_location
from mock import patch
from corehq.apps.consumption.shortcuts import get_default_consumption
from corehq.apps.commtrack.models import Product

class LocationImportTest(CommTrackTest):
    def names_of_locs(self):
        return [loc.name for loc in Location.by_domain(self.domain.name)]

    def test_import_new_top_level_location(self):
        data = {
            'name': 'importedloc'
        }

        import_location(self.domain.name, 'state', data)

        self.assertTrue(data['name'] in self.names_of_locs())

    def test_import_with_existing_parent_by_id(self):
        parent = make_loc('sillyparents')
        parent.location_type = 'state'  # state can't have outlet as child
        parent.save()

        data = {
            'name': 'importedloc',
            'parent_id': parent._id
        }

        result = import_location(self.domain.name, 'district', data)

        if result['id'] is None:
            self.fail('import failed with error: %s' % result['message'])

        self.assertTrue(data['name'] in self.names_of_locs())
        new_loc = Location.get(result['id'])
        self.assertEqual(new_loc.parent_id, parent._id)

    def test_id_of_invalid_parent_type(self):
        parent = make_loc('sillyparents')
        parent.location_type = 'state'  # state can't have outlet as child
        parent.save()

        data = {
            'name': 'oops',
            'outlet_type': 'SHG',
            'parent_id': parent._id
        }

        original_count = len(list(Location.by_domain(self.domain.name)))

        try:
            result = import_location(self.domain.name, 'outlet', data)
        except Exception as e:
            self.fail("import_location raised an error: %s" % e)

        self.assertEqual(result['id'], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))), original_count)
        self.assertTrue('Invalid parent type' in result['message'])

    def test_invalid_parent_id(self):
        data = {
            'name': 'oops',
            'outlet_type': 'SHG',
            'parent_id': 'banana'
        }

        try:
            result = import_location(self.domain.name, 'outlet', data)
        except Exception as e:
            self.fail("import_location raised an error: %s" % e)

        self.assertTrue('Parent with id banana does not exist' in result['message'])

    def test_updating_existing_location_properties(self):
        parent = make_loc('sillyparents')
        parent.location_type = 'village'
        parent.save()
        existing = make_loc('existingloc', parent=parent)
        existing.location_type = 'outlet'
        existing.save()

        data = {
            'id': existing._id,
            'name': existing.name,
            'site_code': 'wat',
            'outlet_type': 'SHG'<commit_msg>remove redundant try/catches in tests<commit_after>from corehq.apps.commtrack.helpers import make_supply_point
from corehq.apps.commtrack.tests.util import CommTrackTest, make_loc
from corehq.apps.locations.models import Location
from corehq.apps.locations.bulk import import_location
from mock import patch
from corehq.apps.consumption.shortcuts import get_default_consumption
from corehq.apps.commtrack.models import Product

class LocationImportTest(CommTrackTest):
    def names_of_locs(self):
        return [loc.name for loc in Location.by_domain(self.domain.name)]

    def test_import_new_top_level_location(self):
        data = {
            'name': 'importedloc'
        }

        import_location(self.domain.name, 'state', data)

        self.assertTrue(data['name'] in self.names_of_locs())

    def test_import_with_existing_parent_by_id(self):
        parent = make_loc('sillyparents')
        parent.location_type = 'state'  # state can't have outlet as child
        parent.save()

        data = {
            'name': 'importedloc',
            'parent_id': parent._id
        }

        result = import_location(self.domain.name, 'district', data)

        if result['id'] is None:
            self.fail('import failed with error: %s' % result['message'])

        self.assertTrue(data['name'] in self.names_of_locs())
        new_loc = Location.get(result['id'])
        self.assertEqual(new_loc.parent_id, parent._id)

    def test_id_of_invalid_parent_type(self):
        parent = make_loc('sillyparents')
        parent.location_type = 'state'  # state can't have outlet as child
        parent.save()

        data = {
            'name': 'oops',
            'outlet_type': 'SHG',
            'parent_id': parent._id
        }

        original_count = len(list(Location.by_domain(self.domain.name)))

        result = import_location(self.domain.name, 'outlet', data)

        self.assertEqual(result['id'], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))), original_count)
        self.assertTrue('Invalid parent type' in result['message'])

    def test_invalid_parent_id(self):
        data = {
            'name': 'oops',
            'outlet_type': 'SHG',
            'parent_id': 'banana'
        }

        result = import_location(self.domain.name, 'outlet', data)

        self.assertTrue('Parent with id banana does not exist' in result['message'])

    def test_updating_existing_location_properties(self):
        parent = make_loc('sillyparents')
        parent.location_type = 'village'
        parent.save()
        existing = make_loc('existingloc', parent=parent)
        existing.location_type = 'outlet'
        existing.save()

        data = {
            'id': existing._id,
            'name': existing.name,
            'site_code': 'wat',
            'outlet_type': 'SHG'
{'fraction_non_alphanumeric': 0.1133130081300813, 'fraction_numerical': 0.0, 'mean_word_length': 2.6540841584158414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4886696', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1725, 'n_words': 361}