Beispiel #1
0
    def test_dbf_without_an_expected_column_raises_ValidationError(self):
        """
        If the file does not have all columns we expect in a SINAN file, we
        should raise an error.
        """

        missing_column_file = self._get_file_from_filename(
            "missing_nu_ano.dbf")
        with self.assertRaises(ValidationError):
            is_valid_dbf(missing_column_file, 2016)
Beispiel #2
0
    def test_dbf_with_wrong_date_datatypes_raises_ValidationError(self):
        """
        The notification year validation should be triggered even if some of
        the data is poiting to the correct year
        """

        wrong_data_type_file = self._get_file_from_filename(
            "wrong_date_datatype.dbf")
        notification_year = 2015

        with self.assertRaises(ValidationError):
            is_valid_dbf(wrong_data_type_file, notification_year)
Beispiel #3
0
 def clean(self):
     cleaned_data = super(DBFForm, self).clean()
     chunked_upload_id = cleaned_data.get('chunked_upload_id')
     user = cleaned_data.get('uploaded_by')
     # If the user tries to give the id of a chunked_upload by
     # another user, or an inexistent id, we raise a validation
     # error.
     try:
         uploaded_file = DBFChunkedUpload.objects.get(id=chunked_upload_id,
                                                      user=user)
     except DBF.DoesNotExist:
         raise ValidationError(
             _("Houve um erro durante o envio do arquivo. "
               "Por favor, tente novamente."))
     # This might be a performance problem for really large DBFs
     is_valid_dbf(uploaded_file.file, cleaned_data['notification_year'])
     return cleaned_data
Beispiel #4
0
    def clean(self):
        if self.notification_year > date.today().year:
            raise ValidationError({
                "notification_year":
                _("O ano de notificação "
                  "não pode ser maior do que o ano atual")
            })

        if not is_valid_dbf(self.file, self.notification_year):
            raise ValidationError({"file": _("Arquivo DBF inválido")})
Beispiel #5
0
 def test_can_receive_file_with_name_that_does_not_exist(self):
     """
     This is a regression test. We had an error because we were testing this
     function with data that was already saved to disk. In the upload
     process, what happens is more similiar to what is happening in this
     test: the instance exists, but was never saved to disk, so calling
     `dbf.file.path` would return a path that had no file there (because the
     save process was not complete yet).
     """
     inexistent_filename = "{}.dbf".format(datetime.datetime.now())
     with open(os.path.join(TEST_DATA_DIR, "simple.dbf"), "rb") as fp:
         # Instead of using ".objects.create()" we only instantiate the file.
         # This will trigger the error when calling dbf.clean() on an
         # unsaved instance.
         dbf = DBF(uploaded_by=User.objects.all()[0],
                   file=File(fp, name=inexistent_filename),
                   export_date=date.today(),
                   notification_year=date.today().year)
         self.assertTrue(is_valid_dbf(dbf.file, dbf.notification_year))
Beispiel #6
0
    def test_invalid_dbf_raises_ValidationError(self):
        """If the file is not in dbf format, we should raise an error."""

        invalid_file = self._get_file_from_filename("invalid.dbf")
        with self.assertRaises(ValidationError):
            is_valid_dbf(invalid_file, 2016)
Beispiel #7
0
 def test_valid_dbf_returns_true(self):
     valid_file = self._get_file_from_filename("simple.dbf")
     self.assertTrue(is_valid_dbf(valid_file, 2016))