def test_cannot_delete(self):
     organization = OrganizationBuilder().build()
     organization.save()
     url = '/v1/organizations/{0}/'.format(organization.pk)
     response = self.client.delete(url)
     self.assertEqual(response.status_code,
                      status.HTTP_405_METHOD_NOT_ALLOWED)
 def test_can_get_one_entity(self):
     organization = OrganizationBuilder().with_description(
         'Organization description').build()
     organization.save()
     url = '/v1/organizations/{0}/'.format(organization.pk)
     response = self.client.get(url)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.json()['description'],
                      'Organization description')
Example #3
0
class TestServiceAtLocationModel(TestCase):
    def setUp(self):
        self.organization = OrganizationBuilder().build()
        self.organization.save()

        self.service = ServiceBuilder(self.organization).build()
        self.service.save()

        self.location = LocationBuilder(self.organization).build()
        self.location.save()

    def test_has_service_field(self):
        service_at_location = ServiceAtLocation(service=self.service,
                                                location=self.location)
        service_location_from_db = validate_save_and_reload(
            service_at_location)
        self.assertEqual(service_location_from_db.service, self.service)

    def test_service_cannot_be_none(self):
        service_at_location = ServiceAtLocation(service=None,
                                                location=self.location)
        with self.assertRaises(exceptions.ValidationError):
            service_at_location.full_clean()

    def test_has_location_field(self):
        service_at_location = ServiceAtLocation(service=self.service,
                                                location=self.location)
        service_location_from_db = validate_save_and_reload(
            service_at_location)
        self.assertEqual(service_location_from_db.location, self.location)

    def test_location_cannot_be_none(self):
        service_at_location = ServiceAtLocation(service=self.service,
                                                location=None)
        with self.assertRaises(exceptions.ValidationError):
            service_at_location.full_clean()
class TestServiceModel(TestCase):
    def setUp(self):
        self.organization = OrganizationBuilder().build()
        self.organization.save()

    def test_has_id_field(self):
        service_id = 'the_id'
        service = ServiceBuilder(self.organization).with_id(service_id).build()
        service_from_db = validate_save_and_reload(service)
        self.assertEqual(service_from_db.id, service_id)

    def test_id_cannot_be_none(self):
        null_id = None
        service = ServiceBuilder(self.organization).with_id(null_id).build()
        with self.assertRaises(exceptions.ValidationError):
            service.full_clean()

    def test_id_cannot_be_empty(self):
        empty_id = ''
        service = ServiceBuilder(self.organization).with_id(empty_id).build()
        with self.assertRaises(exceptions.ValidationError):
            service.full_clean()

    def test_id_cannot_contain_space(self):
        service_id = 'the id'
        service = ServiceBuilder(self.organization).with_id(service_id).build()
        with self.assertRaises(exceptions.ValidationError):
            service.full_clean()

    def test_has_name(self):
        name = 'The service name'
        service = ServiceBuilder(self.organization).with_name(name).build()
        service_from_db = validate_save_and_reload(service)
        self.assertEqual(service_from_db.name, name)

    @unittest.expectedFailure
    def test_cannot_be_empty(self):
        name = ''
        service = ServiceBuilder(self.organization).with_name(name).build()
        with self.assertRaises(exceptions.ValidationError):
            service.full_clean()

    def test_name_cannot_be_none(self):
        null_name = None
        service = ServiceBuilder(
            self.organization).with_name(null_name).build()
        # Note that we're getting an integrity error from the database here,
        # haven't figured out how to make this fail validation which would be cleaner
        # and would also allow us invalidate on the empty string.
        with self.assertRaises(django_utils.IntegrityError):
            validate_save_and_reload(service)

    def test_can_set_description(self):
        description = 'The service description'
        service = ServiceBuilder(
            self.organization).with_description(description).build()
        service_from_db = validate_save_and_reload(service)
        self.assertEqual(service_from_db.description, description)

    def test_description_can_be_none(self):
        null_description = None
        service = ServiceBuilder(
            self.organization).with_description(null_description).build()
        service_from_db = validate_save_and_reload(service)
        self.assertEqual(service_from_db.description, null_description)

    @unittest.expectedFailure
    def test_empty_description_is_saved_as_null(self):
        empty_description = ''
        null_description = None
        service = ServiceBuilder(
            self.organization).with_description(empty_description).build()
        service_from_db = validate_save_and_reload(service)
        self.assertEqual(service_from_db.description, null_description)

    def test_description_is_multilingual(self):
        service = ServiceBuilder(self.organization).build()

        self.set_description_in_language(service, 'en', 'In English')
        self.set_description_in_language(service, 'fr', 'En français')
        service_from_db = validate_save_and_reload(service)

        self.assert_description_in_language_equals(service_from_db, 'en',
                                                   'In English')
        self.assert_description_in_language_equals(service_from_db, 'fr',
                                                   'En français')

    def test_has_locations_attribute(self):
        service = ServiceBuilder(self.organization).build()
        validate_save_and_reload(service)

        location = LocationBuilder(self.organization).build()
        validate_save_and_reload(location)

        service_at_location = ServiceLocationBuilder(service, location).build()
        validate_save_and_reload(service_at_location)

        self.assertEqual(service.locations.first(), location)

    def test_locations_is_empty_if_no_service_location_exists(self):
        service = ServiceBuilder(self.organization).build()
        service_from_db = validate_save_and_reload(service)

        self.assertEqual(service_from_db.locations.count(), 0)

    def set_description_in_language(self, service, language, text):
        service.set_current_language(language)
        service.description = text

    def assert_description_in_language_equals(self, service, language,
                                              expected_text):
        service.set_current_language(language)
        self.assertEqual(service.description, expected_text)
Example #5
0
class LocationsApiTests(rest_test.APITestCase):
    def setUp(self):
        self.organization_id = 'the_organization_id'
        self.organization = OrganizationBuilder().with_id(
            self.organization_id).build()
        self.organization.save()
        self.data = {
            'id': 'the_location_id',
            'name': 'the name',
            'organization_id': self.organization_id,
            'description': 'the description'
        }

    def test_can_get_locations(self):
        LocationBuilder(self.organization).with_id('First').create()
        LocationBuilder(self.organization).with_id('Second').create()
        url = '/v1/locations/'
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.json()), 2)

    def test_can_get_one_location(self):
        location = LocationBuilder(
            self.organization).with_description('The description').build()
        location.save()
        url = '/v1/locations/{0}/'.format(location.pk)
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.json()['description'], 'The description')

    def test_cannot_post(self):
        url = '/v1/locations/'
        response = self.client.post(url, self.data)
        self.assertEqual(response.status_code,
                         status.HTTP_405_METHOD_NOT_ALLOWED)

    def test_cannot_put(self):
        location = LocationBuilder(self.organization).build()
        location.save()
        url = '/v1/locations/{0}/'.format(location.pk)
        response = self.client.put(url, self.data)
        self.assertEqual(response.status_code,
                         status.HTTP_405_METHOD_NOT_ALLOWED)

    def test_cannot_delete(self):
        location = LocationBuilder(self.organization).build()
        location.save()
        url = '/v1/locations/{0}/'.format(location.pk)
        response = self.client.delete(url)
        self.assertEqual(response.status_code,
                         status.HTTP_405_METHOD_NOT_ALLOWED)

    def test_can_get_locations_for_organization(self):
        LocationBuilder(self.organization).with_id('First').create()
        LocationBuilder(self.organization).with_id('Second').create()
        url = '/v1/organizations/{0}/locations/'.format(self.organization_id)
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.json()), 2)

    def test_can_get_one_location_for_organization(self):
        location = LocationBuilder(
            self.organization).with_description('The description').build()
        location.save()
        url = '/v1/organizations/{0}/locations/{1}/'.format(
            self.organization_id, location.pk)
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.json()['description'], 'The description')

    def test_cannot_post_to_organization(self):
        url = '/v1/organizations/{0}/locations/'.format(self.organization_id)
        response = self.client.post(url, self.data)
        self.assertEqual(response.status_code,
                         status.HTTP_405_METHOD_NOT_ALLOWED)

    def test_cannot_put_to_organization(self):
        location = LocationBuilder(self.organization).build()
        location.save()
        url = '/v1/organizations/{0}/locations/{1}/'.format(
            self.organization_id, location.pk)
        response = self.client.put(url, self.data)
        self.assertEqual(response.status_code,
                         status.HTTP_405_METHOD_NOT_ALLOWED)

    def test_cannot_delete_on_organization(self):
        location = LocationBuilder(self.organization).build()
        location.save()
        url = '/v1/organizations/{0}/locations/{1}/'.format(
            self.organization_id, location.pk)
        response = self.client.delete(url)
        self.assertEqual(response.status_code,
                         status.HTTP_405_METHOD_NOT_ALLOWED)

    def location_has_address_of_type(self, address_type_id):
        LocationBuilder(self.organization).create()
        AddressBuilder().create()
        LocationAddress(
            location=Location.objects.first(),
            address=Address.objects.first(),
            address_type=AddressType.objects.get(pk=address_type_id)).save()
        url = '/v1/locations/'
        response = self.client.get(url)
        location_addresses = response.json()[0]['addresses']
        self.assertEqual(location_addresses[0]['address_type'],
                         address_type_id)

    def test_has_physical_address(self):
        self.location_has_address_of_type('physical_address')

    def test_has_postal_address(self):
        self.location_has_address_of_type('postal_address')

    def test_has_point_values(self):
        location = LocationBuilder(self.organization).with_long_lat(
            a_float(), a_float()).create()
        url = '/v1/locations/'
        response = self.client.get(url)
        self.assertEqual(response.json()[0]['latitude'], location.point.x)
        self.assertEqual(response.json()[0]['longitude'], location.point.y)

    def test_has_phone_numbers(self):
        location = LocationBuilder(self.organization).create()
        phone_at_location = PhoneAtLocationBuilder(location).create()
        url = '/v1/locations/'
        response = self.client.get(url)
        self.assertEqual(
            response.json()[0]['phone_numbers'][0]['phone_number_type'],
            phone_at_location.phone_number_type.id)
        self.assertEqual(
            response.json()[0]['phone_numbers'][0]['phone_number'],
            phone_at_location.phone_number)
class TestLocationModel(TestCase):
    def setUp(self):
        self.organization = OrganizationBuilder().build()
        self.organization.save()

    def test_has_id_field(self):
        location_id = 'the_id'
        location = LocationBuilder(
            self.organization).with_id(location_id).build()
        location_from_db = validate_save_and_reload(location)
        self.assertEqual(location_from_db.id, location_id)

    def test_id_cannot_be_none(self):
        null_id = None
        location = LocationBuilder(self.organization).with_id(null_id).build()
        with self.assertRaises(exceptions.ValidationError):
            location.full_clean()

    def test_id_cannot_be_empty(self):
        empty_id = ''
        location = LocationBuilder(self.organization).with_id(empty_id).build()
        with self.assertRaises(exceptions.ValidationError):
            location.full_clean()

    def test_id_cannot_contain_space(self):
        location_id = 'the id'
        location = LocationBuilder(
            self.organization).with_id(location_id).build()
        with self.assertRaises(exceptions.ValidationError):
            location.full_clean()

    def test_has_name(self):
        name = 'The location name'
        location = LocationBuilder(self.organization).with_name(name).build()
        location_from_db = validate_save_and_reload(location)
        self.assertEqual(location_from_db.name, name)

    @unittest.expectedFailure
    def test_cannot_be_empty(self):
        name = ''
        location = LocationBuilder(self.organization).with_name(name).build()
        with self.assertRaises(exceptions.ValidationError):
            location.full_clean()

    def test_name_cannot_be_none(self):
        null_name = None
        location = LocationBuilder(
            self.organization).with_name(null_name).build()
        # Note that we're getting an integrity error from the database here,
        # haven't figured out how to make this fail validation which would be cleaner
        # and would also allow us invalidate on the empty string.
        with self.assertRaises(django_utils.IntegrityError):
            validate_save_and_reload(location)

    def test_has_latitude(self):
        latitude = 123.456
        location = LocationBuilder(
            self.organization).with_latitude(latitude).build()
        location_from_db = validate_save_and_reload(location)
        self.assertAlmostEqual(location_from_db.latitude, latitude)

    def test_has_longitude(self):
        longitude = 234.567
        location = LocationBuilder(
            self.organization).with_longitude(longitude).build()
        location_from_db = validate_save_and_reload(location)
        self.assertAlmostEqual(location_from_db.longitude, longitude)

    def test_latitude_and_longitude_can_both_be_null(self):
        location = (LocationBuilder(self.organization).with_latitude(
            None).with_longitude(None).build())
        location_from_db = validate_save_and_reload(location)
        self.assertEqual(location_from_db.latitude, None)
        self.assertEqual(location_from_db.longitude, None)

    def test_only_latitude_cannot_be_null(self):
        location = (LocationBuilder(
            self.organization).with_latitude(None).with_longitude(0.0).build())
        with self.assertRaises(exceptions.ValidationError):
            location.full_clean()

    def test_only_longitude_cannot_be_null(self):
        location = (LocationBuilder(
            self.organization).with_latitude(0.0).with_longitude(None).build())
        with self.assertRaises(exceptions.ValidationError):
            location.full_clean()

    def test_can_set_description(self):
        description = 'The location description'
        location = LocationBuilder(
            self.organization).with_description(description).build()
        location_from_db = validate_save_and_reload(location)
        self.assertEqual(location_from_db.description, description)

    def test_description_can_be_none(self):
        null_description = None
        location = LocationBuilder(
            self.organization).with_description(null_description).build()
        location_from_db = validate_save_and_reload(location)
        self.assertEqual(location_from_db.description, null_description)

    @unittest.expectedFailure
    def test_empty_description_is_saved_as_null(self):
        empty_description = ''
        null_description = None
        location = LocationBuilder(
            self.organization).with_description(empty_description).build()
        location_from_db = validate_save_and_reload(location)
        self.assertEqual(location_from_db.description, null_description)

    def test_description_is_multilingual(self):
        location = LocationBuilder(self.organization).build()

        self.set_description_in_language(location, 'en', 'In English')
        self.set_description_in_language(location, 'fr', 'En français')
        location_from_db = validate_save_and_reload(location)

        self.assert_description_in_language_equals(location_from_db, 'en',
                                                   'In English')
        self.assert_description_in_language_equals(location_from_db, 'fr',
                                                   'En français')

    def set_description_in_language(self, location, language, text):
        location.set_current_language(language)
        location.description = text

    def assert_description_in_language_equals(self, location, language,
                                              expected_text):
        location.set_current_language(language)
        self.assertEqual(location.description, expected_text)