def test_can_get_entities(self):
     OrganizationBuilder().with_id('First').create()
     OrganizationBuilder().with_id('Second').create()
     url = '/v1/organizations/'
     response = self.client.get(url)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(len(response.json()), 2)
 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)
Exemple #3
0
    def setUp(self):
        organization_1 = OrganizationBuilder().with_id('one').build()
        add_base_translation(organization_1,
                             name='organization_one_translation_name_msgid',
                             description='')
        add_translation(organization_1,
                        'fr',
                        name='organization_one_translation_name_msgstr_fr')

        organization_2 = OrganizationBuilder().with_id('two').build()
        add_base_translation(
            organization_2,
            name='',
            description='organization_two_translation_description_msgid')
        add_translation(
            organization_2,
            'fr',
            description='organization_two_translation_description_msgstr_fr')

        organization_3 = OrganizationBuilder().with_id('three').build()
        add_base_translation(
            organization_3,
            name='organization_three_translation_name_msgid',
            description='organization_three_translation_description_msgid')

        self.translatable_objects = [
            organization_1, organization_2, organization_3
        ]

        self.out_dir = TemporaryDirectory()
Exemple #4
0
    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_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')
Exemple #6
0
 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 setUp(self):
        self.first_organization = OrganizationBuilder().create()
        self.second_organization = OrganizationBuilder().create()

        self.first_location = LocationBuilder(self.first_organization).create()
        self.second_location = LocationBuilder(self.first_organization).create()

        self.first_service = ServiceBuilder(self.first_organization).create()
        self.second_service = ServiceBuilder(self.second_organization).create()

        add_service_to_location(self.first_service, self.first_location)
        add_service_to_location(self.second_service, self.second_location)
Exemple #8
0
    def setUp(self):
        self.translatable = OrganizationBuilder().with_id('translatable_string_tests_id').build()

        self.base_translation = add_base_translation(
            self.translatable, name='translation_name_msgid'
        )

        if self.translatable.has_translation('fr'):
            self.translatable.delete_translation('fr')

        self.translatable_string = TranslatableString(
            self.translatable, 'name', 'translation_name_msgid', 'translation_name_msgstr'
        )
Exemple #9
0
 def test_empty_website_is_saved_as_none(self):
     empty_website = ''
     no_website = None
     organization = OrganizationBuilder().with_website(
         empty_website).build()
     organization_from_db = validate_save_and_reload(organization)
     self.assertEqual(organization_from_db.website, no_website)
Exemple #10
0
    def test_returns_services_ordered_by_score(self):
        task_id = a_string()
        task = Task(id=task_id, name=a_string(), description=a_string())
        task.save()

        organization = OrganizationBuilder().create()
        more_related_service = ServiceBuilder(organization).create()
        less_related_service = ServiceBuilder(organization).create()

        higher_score = 0.9
        lower_score = 0.1
        TaskServiceSimilarityScore(task=task,
                                   service=more_related_service,
                                   similarity_score=higher_score).save()

        TaskServiceSimilarityScore(task=task,
                                   service=less_related_service,
                                   similarity_score=lower_score).save()

        url = '/v1/tasks/{}/related_services/'.format(task_id)
        response = self.client.get(url)

        self.assertEqual(response.json()[0]['service_id'],
                         more_related_service.id)
        self.assertEqual(response.json()[1]['service_id'],
                         less_related_service.id)
Exemple #11
0
    def test_all_from_po_entry(self):
        translatable = OrganizationBuilder().with_id('test_all_from_po_entry_id').build()

        base_translation = add_base_translation(
            translatable, name='translation_name_msgid'
        )

        po_entry = polib.POEntry()
        po_entry.occurrences = [
            ('organizations.organization@name@test_all_from_po_entry_id', None)
        ]
        po_entry.msgid = 'translation_name_msgid'
        po_entry.msgstr = 'translation_name_msgstr'

        with patch.object(TranslatableString, 'from_po_entry') as from_po_entry:
            errors_list = []
            results_iter = TranslatableString.all_from_po_entry(po_entry, errors_out=errors_list)
            results_list = [n for n in results_iter]
            self.assertEqual(from_po_entry.call_count, 1)
            from_po_entry.assert_has_calls([
                call(po_entry, po_entry.occurrences[0])
            ])

        self.assertEqual(len(errors_list), 0)
        self.assertEqual(len(results_list), 1)
Exemple #12
0
 def create_many(self, count=3):
     organization = OrganizationBuilder().create()
     return ([
         ServiceAtLocation.objects.create(
             location=LocationBuilder(organization).create(),
             service=ServiceBuilder(organization).create())
         for _ in range(0, count)
     ])
    def setUp(self):
        self.organization = OrganizationBuilder().create()
        self.three_task_ids = [a_string() for i in range(3)]
        create_tasks(self.three_task_ids)

        services = [
            ServiceBuilder(self.organization).create() for i in range(3)
        ]
        self.three_service_ids = [service.id for service in services]
Exemple #14
0
    def test_description_is_multilingual(self):
        organization = OrganizationBuilder().build()
        set_description_in_language(organization, 'en', 'In English')
        set_description_in_language(organization, 'fr', 'En français')
        organization_from_db = validate_save_and_reload(organization)

        self.assert_description_in_language_equals(organization_from_db, 'en',
                                                   'In English')
        self.assert_description_in_language_equals(organization_from_db, 'fr',
                                                   'En français')
Exemple #15
0
    def test_create_from_translation_with_missing_msgid_raises_error(self):
        translatable = OrganizationBuilder().with_id('test_create_from_translation_with_missing_msgid_raises_error_id').build()

        base_translation = add_base_translation(
            translatable, name=''
        )
        translation = add_translation(
            translatable, 'fr', name='translation_name_msgstr'
        )

        with self.assertRaises(MissingMsgidError):
            TranslatableString.from_translation(translation, 'name')
Exemple #16
0
    def test_create_from_po_entry_with_invalid_occurrence_format_raise_error(self):
        translatable = OrganizationBuilder().with_id('test_create_from_po_entry_with_invalid_occurrence_format_raise_error_id').build()

        po_entry = polib.POEntry()
        po_entry.occurrences = [
            ('bad format', None)
        ]
        po_entry.msgid = 'translation_name_msgid'
        po_entry.msgstr = 'translation_name_msgstr'

        with self.assertRaises(MasterInstanceLookupError):
            TranslatableString.from_po_entry(po_entry, po_entry.occurrences[0])
Exemple #17
0
    def test_create_from_po_entry_with_nonexistent_master_instance_raises_error(self):
        translatable = OrganizationBuilder().with_id('test_create_from_po_entry_with_nonexistent_master_instance_raises_error_id').build()

        po_entry = polib.POEntry()
        po_entry.occurrences = [
            ('organizations.organization@name@not_an_id', None)
        ]
        po_entry.msgid = 'translation_name_msgid'
        po_entry.msgstr = 'translation_name_msgstr'

        with self.assertRaises(MasterInstanceLookupError):
            TranslatableString.from_po_entry(po_entry, po_entry.occurrences[0])
Exemple #18
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()
Exemple #19
0
    def test_create_with_translatable_model(self):
        translatable = OrganizationBuilder().with_id('test_create_with_translatable_model_id').build()

        add_base_translation(
            translatable, name='translation_name_msgid'
        )

        translatable_string = TranslatableString(translatable, 'name', 'translation_name_msgid', 'translation_name_msgstr')

        self.assertEqual(translatable_string._instance, translatable)
        self.assertEqual(translatable_string._field_id, 'name')
        self.assertEqual(translatable_string._source_str, 'translation_name_msgid')
        self.assertEqual(translatable_string._translated_str, 'translation_name_msgstr')

        self.assertEqual(str(translatable_string), 'organizations.organization@name@test_create_with_translatable_model_id')
Exemple #20
0
    def test_can_create_row(self):
        organization = OrganizationBuilder().create()
        service = ServiceBuilder(organization).create()
        task_id = a_string()
        score = a_float()

        create_tasks([task_id])
        score_record = TaskServiceSimilarityScore(task_id=task_id,
                                                  service=service,
                                                  similarity_score=score)
        score_record_from_db = validate_save_and_reload(score_record)

        self.assertEqual(score_record_from_db.task_id, task_id)
        self.assertEqual(score_record_from_db.service_id, service.id)
        self.assertAlmostEqual(score_record_from_db.similarity_score, score)
Exemple #21
0
    def test_create_from_translation(self):
        translatable = OrganizationBuilder().with_id('test_create_from_translation_id').build()

        base_translation = add_base_translation(
            translatable, name='translation_name_msgid'
        )
        translation = add_translation(
            translatable, 'fr', name='translation_name_msgstr'
        )

        translatable_string = TranslatableString.from_translation(translation, 'name')

        self.assertEqual(translatable_string._instance, translatable)
        self.assertEqual(translatable_string._field_id, 'name')
        self.assertEqual(translatable_string._source_str, 'translation_name_msgid')
        self.assertEqual(translatable_string._translated_str, 'translation_name_msgstr')
Exemple #22
0
    def setUp(self):
        translation.activate('en')
        self.task_id = a_string()

        self.task = Task(id=self.task_id,
                         name=a_string(),
                         description=a_string())
        self.task.save()

        organization = OrganizationBuilder().create()
        self.service = ServiceBuilder(organization).create()

        self.similarity_score = a_float()
        TaskServiceSimilarityScore(
            task=self.task,
            service=self.service,
            similarity_score=self.similarity_score).save()
Exemple #23
0
 def setUp(self):
     self.task_id = a_string()
     self.english_task_title = a_string()
     self.english_task_description = a_string()
     self.data = {
         'taskMap': {
             self.task_id: {
                 'completed': False,
                 'id': self.task_id,
                 'title': {
                     'en': self.english_task_title,
                 },
                 'description': {
                     'en': self.english_task_description
                 }
             }
         }
     }
     self.organization = OrganizationBuilder().create()
Exemple #24
0
    def test_all_from_translation(self):
        translatable = OrganizationBuilder().with_id('test_all_from_translation_id').build()

        base_translation = add_base_translation(
            translatable, name='translation_name_msgid', description='translation_description_msgid'
        )
        translation = add_translation(
            translatable, 'fr', name='translation_name_msgstr'
        )

        with patch.object(TranslatableString, 'from_translation') as from_translation:
            errors_list = []
            results_iter = TranslatableString.all_from_translation(translation, errors_out=errors_list)
            results_list = [n for n in results_iter]
            self.assertEqual(from_translation.call_count, 2)
            from_translation.assert_has_calls([
                call(translation, 'name'),
                call(translation, 'description')
            ])

        self.assertEqual(len(errors_list), 0)
        self.assertEqual(len(results_list), 2)
Exemple #25
0
    def test_create_from_po_entry(self):
        translatable = OrganizationBuilder().with_id('test_create_from_po_entry_id').build()

        base_translation = add_base_translation(
            translatable, name='translation_name_msgid'
        )

        po_entry = polib.POEntry()
        po_entry.occurrences = [
            ('organizations.organization@name@test_create_from_po_entry', None)
        ]
        po_entry.msgid = 'translation_name_msgid'
        po_entry.msgstr = 'translation_name_msgstr'

        with patch('translation.translatable_string.parse_instance_field_id') as parse_instance_field_id:
            parse_instance_field_id.return_value = (translatable, 'name')
            translatable_string = TranslatableString.from_po_entry(po_entry, po_entry.occurrences[0])
            parse_instance_field_id.assert_called_once_with('organizations.organization@name@test_create_from_po_entry')

        self.assertEqual(translatable_string._instance, translatable)
        self.assertEqual(translatable_string._field_id, 'name')
        self.assertEqual(translatable_string._source_str, 'translation_name_msgid')
        self.assertEqual(translatable_string._translated_str, 'translation_name_msgstr')
 def setUp(self):
     self.organization = OrganizationBuilder().build()
     self.organization.save()
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)
Exemple #28
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)
 def setUp(self):
     self.organization = OrganizationBuilder().create()
Exemple #30
0
    def test_create_with_different_msgid_from_base_translation_raises_error(self):
        translatable = OrganizationBuilder().with_id('test_create_with_different_msgid_from_base_translation_raises_error_id').with_name('translated_name_msgid').build()

        with self.assertRaises(InvalidMsgidError):
            TranslatableString(translatable, 'name', 'not_translation_name_msgid', 'translation_name_msgstr')