<commit_before>import pytest
import json
from mock import patch, Mock
from name import models


class TestIdentifier_Type:
    def test_has_unicode_method(self):
        label = "Test Label"
        identifer = models.Identifier_Type(label=label)
        assert label == unicode(identifer)


class TestIdentifier:
    def test_has_unicode_method(self):
        value = "Test Value"
        identifer = models.Identifier(value=value)
        assert value == unicode(identifer)


class TestNote:
    def test_has_unicode_method(self):
        msg = "Test Note"
        note = models.Note(note=msg)
        assert msg == unicode(note)


class TestVariant:
    def test_has_unicode_method(self):
        variant_variant = "Test Variant"
        variant = models.Variant(variant=variant_variant)
        assert variant_variant == unicode(variant)


class TestBaseTicketing:
    def test_has_unicode_method(self):
        ticket_id = 1
        ticket = models.BaseTicketing(id=ticket_id)
        assert 'nm{:07d}'.format(ticket_id) == unicode(ticket)


class TestName:
    @pytest.mark.django_db
    def test_saving_name_increments_base_ticketing_id(self):
        """Test that the BaseTicketing ID is properly incremented
        when a Name object is saved to the database.
        """
        # Create an initial Name so we can be confident that
        # a BaseTicketing Object will exist
        models.Name.objects.create(name="<NAME>", name_type=1)
        first_id = models.BaseTicketing.objects.all().last().id
        models.Name.objects.create(name="<NAME>", name_type=1)
        second_id = models.BaseTicketing.objects.all().last().id
        assert second_id == first_id + 1

    @pytest.mark.django_db
    def test_saving_name_creates_location(self):
        """Test that a Location is created when the Name object
        is a location (4) and does not have any associated Location
        objects.
        """
        lat, lng = 33.210241, -97.148857
        with patch('name.models.urlopen') as mock_urlopen:
            mock_urlopen.return_value = mock_response = Mock()
            mock_response.status_code = 200

            # The json payload from map.googleapis.com is expected to
            # look something like this.
            mock_response.read.return_value = json.dumps(
                {
                    'status': "OK",
                    'results': [{
                        'geometry': {
                            'location': {'lat': lat, 'lng': lng}
                        }
                    }]
                }
            )

            name = models.Name.objects.create(
                name="<NAME>",
                name_type=4)

            locations = (models.Location.objects.all()
                         .filter(belong_to_name=name))

            assert 1 == locations.count()
            # Assert the location matches the data from the json
            # payload.
            assert lng == float(locations.first().longitude)
            assert lat == float(locations.first().latitude)

    @pytest.mark.django_db
    def test_has_geocode(self):
        """Test that has_geocode returns True."""

        lat, lng = 33.210241, -97.148857
        name = models.Name.objects.create(name="<NAME>", name_type=4)
        models.Location.objects.create(belong_to_name=name, longitude=lng,
                                       latitude=lat)
        assert name.has_geocode()

    @pytest.mark.django_db
    def test_does_not_have_geocode(self):
        """Test that has_geocode returns False."""
        name = models.Name.objects.create(name="<NAME>", name_type=4)
        assert not name.has_geocode()

    def test_has_unicode_method(self):
        name_id = "0001"
        name = models.Name(name_id=name_id)
        assert name_id == unicode(name)


class TestLocation:
    def test_has_unicode_method(self):
        lat_lng = 239
        location = models.Location(latitude=lat_lng, longitude=lat_lng)<commit_msg>Add a test to verify that a location is only added by default if one resource is returned from the API.<commit_after>import pytest
import json
from mock import patch, Mock
from name import models


class TestIdentifier_Type:
    def test_has_unicode_method(self):
        label = "Test Label"
        identifer = models.Identifier_Type(label=label)
        assert label == unicode(identifer)


class TestIdentifier:
    def test_has_unicode_method(self):
        value = "Test Value"
        identifer = models.Identifier(value=value)
        assert value == unicode(identifer)


class TestNote:
    def test_has_unicode_method(self):
        msg = "Test Note"
        note = models.Note(note=msg)
        assert msg == unicode(note)


class TestVariant:
    def test_has_unicode_method(self):
        variant_variant = "Test Variant"
        variant = models.Variant(variant=variant_variant)
        assert variant_variant == unicode(variant)


class TestBaseTicketing:
    def test_has_unicode_method(self):
        ticket_id = 1
        ticket = models.BaseTicketing(id=ticket_id)
        assert 'nm{:07d}'.format(ticket_id) == unicode(ticket)


class TestName:
    @pytest.mark.django_db
    def test_saving_name_increments_base_ticketing_id(self):
        """Test that the BaseTicketing ID is properly incremented
        when a Name object is saved to the database.
        """
        # Create an initial Name so we can be confident that
        # a BaseTicketing Object will exist
        models.Name.objects.create(name="<NAME>", name_type=1)
        first_id = models.BaseTicketing.objects.all().last().id
        models.Name.objects.create(name="Test Name", name_type=1)
        second_id = models.BaseTicketing.objects.all().last().id
        assert second_id == first_id + 1

    @pytest.mark.django_db
    def test_saving_name_creates_location(self):
        """Test that a Location is created when the Name object
        is a location (4) and does not have any associated Location
        objects.
        """
        lat, lng = 33.210241, -97.148857
        with patch('name.models.urlopen') as mock_urlopen:
            mock_urlopen.return_value = mock_response = Mock()
            mock_response.status_code = 200

            # The json payload from map.googleapis.com is expected to
            # look something like this.
            mock_response.read.return_value = json.dumps(
                {
                    'status': "OK",
                    'results': [{
                        'geometry': {
                            'location': {'lat': lat, 'lng': lng}
                        }
                    }]
                }
            )

            name = models.Name.objects.create(
                name="<NAME>",
                name_type=4)

            locations = (models.Location.objects.all()
                         .filter(belong_to_name=name))

            assert 1 == locations.count()
            # Assert the location matches the data from the json
            # payload.
            assert lng == float(locations.first().longitude)
            assert lat == float(locations.first().latitude)

    @pytest.mark.django_db
    def test_saving_name_does_not_create_location(self):
        """Test that a Location is not created when the API returns
        more than one resource.
        """
        lat, lng = 33.210241, -97.148857
        with patch('name.models.urlopen') as mock_urlopen:
            mock_urlopen.return_value = mock_response = Mock()
            mock_response.status_code = 200

            # The json payload from map.googleapis.com is expected to
            # look something like this.
            mock_response.read.return_value = json.dumps(
                {
                    'status': "OK",
                    'results': [
                        {
                            'geometry': {
                                'location': {'lat': lat, 'lng': lng}
                            }
                        },
                        {
                            'geometry': {
                                'location': {'lat': lat + 5, 'lng': lng + 5}
                            }
                        }
                    ]
                }
            )

            name = models.Name.objects.create(
                name="<NAME>",
                name_type=4)

            assert 0 == name.location_set.count()

    @pytest.mark.django_db
    def test_has_geocode(self):
        """Test that has_geocode returns True."""

        lat, lng = 33.210241, -97.148857
        name = models.Name.objects.create(name="<NAME>", name_type=4)
        models.Location.objects.create(belong_to_name=name, longitude=lng,
                                       latitude=lat)
        assert name.has_geocode()

    @pytest.mark.django_db
    def test_does_not_have_geocode(self):
        """Test that has_geocode returns False."""
        name = models.Name.objects.create(name="<NAME>", name_type=4)
        assert not name.has_geocode()

    def test_has_unicode_method(self):
        name_id = "0001"
        name = models.Name(name_id=name_id)
        assert name_id == unicode(name)


class TestLocation:
    def test_has_unicode_method(self):
        lat_lng = 239
        location = models.Location(latitude=lat_lng, longitude=lat_lng)
{'fraction_non_alphanumeric': 0.09443906636549425, 'fraction_numerical': 0.013875443691513391, 'mean_word_length': 1.9368288060644345, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6096563', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2544, 'n_words': 609}