Skip to content

Commit

Permalink
Add test for ARES VR
Browse files Browse the repository at this point in the history
  • Loading branch information
SukiCZ committed Mar 28, 2024
1 parent e2c7957 commit c91f908
Show file tree
Hide file tree
Showing 6 changed files with 6,162 additions and 0 deletions.
77 changes: 77 additions & 0 deletions tests/ares_vr_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# !/usr/bin/python
# coding=utf-8

from __future__ import (absolute_import, division, print_function, unicode_literals)

import os
import pathlib
from unittest import TestCase

import requests
from requests.status_codes import codes

import responses

from ares_util.ares import call_ares_vr
from ares_util.exceptions import AresConnectionError, AresServerError
from ares_util.helpers import normalize_company_id_length
from ares_util.settings import ARES_VR_API_URL


def _json_response(company_id):
this_dir = pathlib.Path(os.path.realpath(__file__)).parent
return (this_dir / "responses" / "vr" / f"{company_id}.json").read_text()


class CallARESVRTestCase(TestCase):
def test_invalid_values(self):
invalid_values = [False, True, 42, -42, "foo", 25596640]

for invalid_value in invalid_values:
self.assertFalse(call_ares_vr(invalid_value))

@responses.activate
def test_not_found(self):
company_id = "01603094"
responses.get(f'{ARES_VR_API_URL}{company_id}', body=_json_response(company_id), status=codes.not_found)

self.assertFalse(call_ares_vr(company_id=company_id))
self.assertEqual(1, len(responses.calls))

@responses.activate
def test_valid_values(self):
company_ids = ('25063677', '25834151', '27074358')

try:
for company_id in company_ids:
responses.get(f'{ARES_VR_API_URL}{company_id}', body=_json_response(company_id))

ares_data = call_ares_vr(company_id=company_id)
self.assertEqual(normalize_company_id_length(company_id), ares_data['company_id'])
except KeyError as error:
self.fail(error)

self.assertEqual(len(company_ids), len(responses.calls))

@responses.activate
def test_raises_ares_connection_exception(self):
company_id = '62739913'
responses.get(f'{ARES_VR_API_URL}{company_id}', body=requests.ConnectionError("Connection refused"))

with self.assertRaises(AresConnectionError):
call_ares_vr(company_id=company_id)

@responses.activate
def test_raises_ares_server_error(self):
company_id = '60159014'
responses.get(
f'{ARES_VR_API_URL}{company_id}',
body=_json_response('server_error'),
status=codes.internal_server_error
)

with self.assertRaises(AresServerError) as context:
call_ares_vr(company_id=company_id)

self.assertEqual(context.exception.fault_code, 'OBECNA_CHYBA')
self.assertIsNotNone(context.exception.fault_message)
5 changes: 5 additions & 0 deletions tests/responses/vr/01603094.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"kod": "NENALEZENO",
"popis": "Nebyl nalezen žádný subjekt, který by odpovídal zadaným hodnotám. Upravte parametry vyhledávání.|Záznam nenalezen",
"subKod": "VYSTUP_SUBJEKT_NENALEZEN"
}
Loading

0 comments on commit c91f908

Please sign in to comment.