def test_report(self): """ Should successfully get a transactions report when GETting the endpoint providing a company cnpj that is in the database, returning HTTP Status 200 and the expected data """ transaction_one = TransactionFactory.build() transaction_one.company.save() transaction_one.save() transaction_two = TransactionFactory.build() transaction_two.company = transaction_one.company transaction_two.save() company = transaction_one.company expected_data = { "estabelecimento": CompanyReportSerializer(company).data, "recebimentos": [ TransactionReportSerializer(transaction_one).data, TransactionReportSerializer(transaction_two).data, ], "total_recebido": transaction_one.value + transaction_two.value, } url = reverse(REPORT_VIEW_NAME) cnpj = company.cnpj response = self.client.get(url, {"cnpj": cnpj}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, expected_data)
def test_report_serializer(self): """ Should successfully serialize a Company Transactions Report when using the Report serializer """ transaction_one = TransactionFactory.build() transaction_one.company.save() transaction_one.save() company = transaction_one.company transaction_two = TransactionFactory.build() transaction_two.company = company transaction_two.save() # query for annotating the total value company = ( Company.objects.filter(cnpj=company.cnpj) .prefetch_related("transactions") .annotate(total_value=Sum("transactions__value")) .first() ) serializer = ReportSerializer(company) expected_data = { "estabelecimento": CompanyReportSerializer(company).data, "recebimentos": [ TransactionReportSerializer(transaction_one).data, TransactionReportSerializer(transaction_two).data, ], "total_recebido": company.total_value, } self.assertEqual(serializer.data, expected_data)
def test_writer_serializer_save_invalid_client(self): """ Should fail to save data with the Transaction writer serializer when the client cpf is invalid """ transaction = TransactionFactory.build() transaction.company.save() transaction.client = "111.111.111-11" self.assertEqual(Transaction.objects.count(), 0) data = WritableTransactionSerializer(transaction).data serializer = WritableTransactionSerializer(data=data) self.assertTrue(serializer.is_valid()) expected_messages = { "client": [ f"Ensure the CPF is valid (it is {transaction.client})." ] } with self.assertRaises(ValidationError) as raised: serializer.save() self.assertEqual(raised.exception.message_dict, expected_messages) self.assertEqual(Transaction.objects.count(), 0)
def test_transaction_string_representation(self): transaction = TransactionFactory.build() self.assertEqual( str(transaction), f"Transaction (Value: {transaction.value} | " f"Company: {transaction.company.cnpj} | " f"Client: {transaction.client})", )
def test_report_company_not_found(self): """ Should fail to get a transactions report when GETting the endpoint and providing a cnpj of a company that is not in the database, returning HTTP Status 404 and the expected message """ url = reverse(REPORT_VIEW_NAME) transaction = TransactionFactory.build() cnpj = transaction.company.cnpj expected_message = f"Estabelecimento com cnpj '{cnpj}' nao encontrado" response = self.client.get(url, {"cnpj": cnpj}) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(response.data, {"erro": expected_message})
def test_model_creation(self): """ Should successfully create an instance of Transation in the database """ self.assertEqual(Transaction.objects.count(), 0) transaction = TransactionFactory.create() self.assertEqual(Transaction.objects.count(), 1) retrieved_transaction = Transaction.objects.first() self.assertEqual(transaction.company, retrieved_transaction.company) self.assertEqual(transaction.client, retrieved_transaction.client) self.assertEqual(transaction.value, retrieved_transaction.value) self.assertEqual(transaction.description, retrieved_transaction.description)
def test_model_creation_invalid_client(self): """ Should fail to create an instance of Transaction in the database when the client value is invalid """ self.assertEqual(Transaction.objects.count(), 0) # tests invalid client (cpf) value transaction = TransactionFactory.build() transaction.company.save() transaction.client = "111.111.111-11" expected_messages = { "client": [f"Ensure the CPF is valid (it is {transaction.client})."] } with self.assertRaises(ValidationError) as raised: transaction.save() self.assertEqual(raised.exception.message_dict, expected_messages) self.assertEqual(Transaction.objects.count(), 0) # tests invalid cpf length transaction = TransactionFactory.build() transaction.company.save() transaction.client = transaction.client + "1" expected_messages = { "client": [ f"Ensure the CPF is valid (it is {transaction.client}).", f"Ensure this value has at most {CPF_SIZE} characters " f"(it has {len(transaction.client)}).", ], } with self.assertRaises(ValidationError) as raised: transaction.save() self.assertEqual(raised.exception.message_dict, expected_messages) self.assertEqual(Transaction.objects.count(), 0)
def test_writer_serializer_save(self): """ Should successfully write data with the Transaction writer serializer when the Company for the given Transaction exists """ transaction = TransactionFactory.build() transaction.company.save() self.assertEqual(Transaction.objects.count(), 0) data = WritableTransactionSerializer(transaction).data serializer = WritableTransactionSerializer(data=data) self.assertTrue(serializer.is_valid()) serializer.save() self.assertEqual(Transaction.objects.count(), 1)
def test_transaction_factory(self): """ Should successfully get an instance of Transaction with populated attributes """ transaction = TransactionFactory() self.assertIsInstance(transaction, Transaction) self.assertIsInstance(transaction.company, Company) self.assertIsInstance(transaction.client, str) self.assertEqual(len(transaction.client), CPF_SIZE) self.assertIsInstance(transaction.value, float) self.assertGreaterEqual(transaction.value, MIN_VALUE) self.assertLessEqual(transaction.value, MAX_VALUE) self.assertIsInstance(transaction.description, str) self.assertLessEqual(len(transaction.description), DESCRIPTION_LENGTH)
def test_write_with_reader_serializer(self): """Should fail to write data with the Transaction reader serializer""" transaction = TransactionFactory.build() serialized_data = TransactionSerializer(transaction).data serializer = TransactionSerializer(data=serialized_data) self.assertTrue(serializer.is_valid()) self.assertEqual(Transaction.objects.count(), 0) expected_message = ( "method does not support writable dotted-source fields by default" ) with self.assertRaises(AssertionError) as raised: serializer.save() self.assertIn(expected_message, str(raised.exception)) self.assertEqual(Transaction.objects.count(), 0)
def test_transaction_creation_company_does_not_exist(self): """ Should fail to create a Transaction record in the database when POSTing to the endpoint with a company cnpj that does not exist in the database, returning HTTP Status 404 and the expected message """ url = reverse(TRANSACTION_VIEW_NAME) transaction = TransactionFactory.build() payload = TransactionSerializer(transaction).data self.assertEqual(Transaction.objects.count(), 0) response = self.client.post(url, payload) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(response.data, {"aceito": False}) self.assertEqual(Transaction.objects.count(), 0)
def test_model_creation_empty_value(self): """ Should fail to create an instance of Transaction in the database when the value is invalid """ self.assertEqual(Transaction.objects.count(), 0) transaction = TransactionFactory.build() transaction.company.save() transaction.value = None expected_messages = {"value": ["This field cannot be null."]} with self.assertRaises(ValidationError) as raised: transaction.save() self.assertEqual(raised.exception.message_dict, expected_messages) self.assertEqual(Transaction.objects.count(), 0)
def test_transaction_creation(self): """ Should successfully create a Transaction record in the database when POSTing to the endpoint, returning HTTP Status 201 and the expected message """ url = reverse(TRANSACTION_VIEW_NAME) transaction = TransactionFactory.build() transaction.company.save() payload = TransactionSerializer(transaction).data self.assertEqual(Transaction.objects.count(), 0) response = self.client.post(url, payload) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, {"aceito": True}) self.assertEqual(Transaction.objects.count(), 1)
def test_writer_serializer_no_company(self): """ Should fail to validate data with the Transaction writer serializer when the Company for the given Transaction does not exist """ transaction = TransactionFactory.build() self.assertEqual(Transaction.objects.count(), 0) data = WritableTransactionSerializer(transaction).data serializer = WritableTransactionSerializer(data=data) self.assertFalse(serializer.is_valid()) expected_key = "estabelecimento" expected_text = "object does not exist." self.assertIn(expected_key, serializer.errors) self.assertIn(expected_text, str(serializer.errors[expected_key])) self.assertEqual(Transaction.objects.count(), 0)
def test_transaction_creation_invalid_value(self): """ Should fail to create a Transaction record in the database when POSTing to the endpoint with an invalid value, returning HTTP Status 400 and the expected message """ url = reverse(TRANSACTION_VIEW_NAME) transaction = TransactionFactory.build() transaction.company.save() payload = TransactionSerializer(transaction).data payload["valor"] = "invalid" self.assertEqual(Transaction.objects.count(), 0) response = self.client.post(url, payload) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {"aceito": False}) self.assertEqual(Transaction.objects.count(), 0)
def test_transaction_report_serializer(self): """ Should successfully serialize a Transaction instance when using the Transaction serializer for reports """ transaction = TransactionFactory.build() serializer = TransactionReportSerializer(transaction) data = serializer.data for ( serializer_field, model_field, ) in transaction_report_fields_mapping.items(): self.assertIn(serializer_field, data) value = getattr(transaction, model_field) self.assertEqual(data[serializer_field], value) # tests if serializer validates serializer = TransactionReportSerializer(data=data) self.assertTrue(serializer.is_valid())
def test_writer_serializer(self): """Should successfully serialize a Transaction instance""" transaction = TransactionFactory.build() transaction.company.save() serializer = WritableTransactionSerializer(transaction) data = serializer.data for ( serializer_field, model_field, ) in transaction_writer_fields_mapping.items(): self.assertIn(serializer_field, data) value = getattr(transaction, model_field) if serializer_field == "estabelecimento": value = transaction.company.id self.assertEqual(data[serializer_field], value) # tests if serializer validates serializer = WritableTransactionSerializer(data=data) self.assertTrue(serializer.is_valid())
def test_reader_serializer(self): """Should successfully serialize a Transaction instance""" transaction = TransactionFactory.build() serializer = TransactionSerializer(transaction) serialized_data = serializer.data for ( serializer_field, model_field, ) in transaction_reader_fields_mapping.items(): self.assertIn(serializer_field, serialized_data) if serializer_field == "estabelecimento": value = transaction.company.cnpj else: value = getattr(transaction, model_field) self.assertEqual(serialized_data[serializer_field], value) # tests if serializer validates serializer = TransactionSerializer(data=serialized_data) self.assertTrue(serializer.is_valid())