Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout Feys committed Dec 4, 2024
1 parent 8964a99 commit a0a1a52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion aikido_zen/background_process/commands/sync_data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def setup_connection_manager():
connection_manager.conf.blocked_uids = ["user1", "user2"]
connection_manager.conf.last_updated_at = 200
connection_manager.statistics.requests = {"total": 0} # Initialize total requests
connection_manager.middleware_installed = False
return connection_manager


Expand All @@ -38,6 +39,7 @@ def test_process_sync_data_initialization(setup_connection_manager):
},
},
"reqs": 10, # Total requests to be added
"middleware_installed": False,
}

result = process_sync_data(connection_manager, data, None)
Expand All @@ -63,6 +65,7 @@ def test_process_sync_data_initialization(setup_connection_manager):
# Check that the return value is correct
assert result["routes"] == dict(connection_manager.routes.routes)
assert result["config"] == connection_manager.conf
assert connection_manager.middleware_installed == False


def test_process_sync_data_with_last_updated_at_below_zero(setup_connection_manager):
Expand All @@ -85,6 +88,7 @@ def test_process_sync_data_with_last_updated_at_below_zero(setup_connection_mana
},
},
"reqs": 10, # Total requests to be added
"middleware_installed": True,
}

result = process_sync_data(connection_manager, data, None)
Expand All @@ -106,7 +110,7 @@ def test_process_sync_data_with_last_updated_at_below_zero(setup_connection_mana

# Check that the total requests were updated
assert connection_manager.statistics.requests["total"] == 10

assert connection_manager.middleware_installed == True
# Check that the return value is correct
assert result == {}

Expand Down Expand Up @@ -154,6 +158,7 @@ def test_process_sync_data_existing_route(setup_connection_manager):

# Check that the total requests were updated
assert connection_manager.statistics.requests["total"] == 20 # 5 + 15
assert connection_manager.middleware_installed == False

# Check that the return value is correct
assert result["routes"] == dict(connection_manager.routes.routes)
Expand Down
5 changes: 5 additions & 0 deletions aikido_zen/middleware/init_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,22 @@ def test_with_context_with_cache():

thread_cache.config.blocked_uids = ["123"]
assert get_current_context().executed_middleware == False
assert thread_cache.middleware_installed == False
assert should_block_request() == {
"block": True,
"trigger": "user",
"type": "blocked",
}
assert get_current_context().executed_middleware == True
assert thread_cache.middleware_installed == True

thread_cache.config.blocked_uids = []
assert should_block_request() == {"block": False}

thread_cache.config.blocked_uids = ["23", "234", "456"]
assert should_block_request() == {"block": False}
assert get_current_context().executed_middleware == True
assert thread_cache.middleware_installed == True


def test_cache_comms_with_endpoints():
Expand All @@ -88,11 +91,13 @@ def test_cache_comms_with_endpoints():
}
]
assert get_current_context().executed_middleware == False
assert thread_cache.middleware_installed == False

with patch("aikido_zen.background_process.comms.get_comms") as mock_get_comms:
mock_get_comms.return_value = None # Set the return value of get_comms
assert should_block_request() == {"block": False}
assert get_current_context().executed_middleware == True
assert thread_cache.middleware_installed == True

with patch("aikido_zen.background_process.comms.get_comms") as mock_get_comms:
mock_comms = MagicMock()
Expand Down

0 comments on commit a0a1a52

Please sign in to comment.