Coverage for tests\api\test_api_v1.py: 100%

1 import json

2 from unittest import mock

3 import httpx

4 from datetime import timedelta

5

6 from zapy.api.server import server

7 from zapy.store import use_store

8 from fastapi.testclient import TestClient

9 import pytest

10

11

12@pytest.fixture()

13 def client():

14 yield TestClient(server)

15 store = use_store()

16 store.clear()

17

18

19 def test_info(client):

20 response = client.get("/v1/info")

21

22 assert response.status_code == 200

23 actual = response.json()

24 assert actual['key'] == "zapy"

25 assert actual['application'] == "Zapy"

26 assert actual['version'] == "0.0.1a0"

27 assert actual['virtualEnv'] == True

28 assert actual['documentation'] == "https://docs.zapy.dev"

29 assert len(actual['sys_prefix']) > 0

30 assert len(actual['directory']) > 0

31 assert "current_time" in actual

32 assert "start_time" in actual

33

34

35@mock.patch.object(

36 httpx.AsyncClient, 'send',

37 return_value = httpx.Response(201, json={'id': 'test-id'})

38)

39 def test_exec_without_tests(mocked_request, client):

40 mocked_request.return_value.elapsed = timedelta(seconds=2.0)

41 with open('tests/assets/request1.zapy') as request_file:

42 request_dict = json.load(request_file)

43 response = client.post("/v1/request/exec", json=request_dict)

44 actual = response.json()

45

46 assert response.status_code == 200

47 assert actual['time'] == 2.0

48 assert actual['headers']['content-type'] == 'application/json'

49 assert actual['content'] == '{"id": "test-id"}'

50 assert actual['test_result'] == []

51 assert actual['status'] == 201

52

53

54 def test_store_empty(client):

55 response = client.get("/v1/stores/default")

56 actual = response.json()

57

58 assert response.status_code == 200

59 assert actual == {

60 'attributes': [],

61 'field_name': 'default',

62 'path': 'default',

63 'type_str': 'Store',

64 'value_repr': '{}'

65 }

66

67 def test_store(client):

68 store = use_store()

69 store.test_var1 = 'dummy'

70

71 response = client.get("/v1/stores/default")

72 actual = response.json()

73

74

75 assert response.status_code == 200

76 assert actual == {

77 'attributes': [

78 {

79 'attributes': [],

80 'field_name': "'test_var1'",

81 'path': "default['test_var1']",

82 'type_str': 'str',

83 'value_repr': "'dummy'",

84 }

85 ],

86 'field_name': 'default',

87 'path': 'default',

88 'type_str': 'Store',

89 'value_repr': "{'test_var1': 'dummy'}"

90 }