Skip to content

Commit

Permalink
fix(tests): added coverage for CLI and backends
Browse files Browse the repository at this point in the history
  • Loading branch information
tabbott36 committed Jul 8, 2024
1 parent c253a7b commit 07fd67b
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 59 deletions.
41 changes: 41 additions & 0 deletions tests/test_backend/test_subscriber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest

from frbvoe.models.subscriber import Subscriber


def test_create_subscriber():
# Test creating a valid subscriber
subscriber = Subscriber(
name="John Smith",
contact_email="[email protected]",
requested_service="emails",
)
assert subscriber.name == "John Smith"
assert subscriber.contact_email == "[email protected]"
assert subscriber.requested_service == "emails"
assert subscriber.subscriber_email is None
assert subscriber.ip_address is None


def test_create_subscriber_with_optional_fields():
# Test creating a subscriber with optional fields
subscriber = Subscriber(
name="Jane Doe",
contact_email="[email protected]",
requested_service="both",
subscriber_email="[email protected]",
ip_address="192.168.0.1",
)
assert subscriber.name == "Jane Doe"
assert subscriber.contact_email == "[email protected]"
assert subscriber.requested_service == "both"
assert subscriber.subscriber_email == "[email protected]"
assert subscriber.ip_address == "192.168.0.1"


def test_create_subscriber_with_invalid_fields():
# Test creating a subscriber with invalid fields
with pytest.raises(ValueError):
Subscriber(
name="", contact_email="invalid_email", requested_service="invalid_service"
)
Empty file added tests/test_backend/test_voe.py
Empty file.
10 changes: 10 additions & 0 deletions tests/test_cli/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from click.testing import CliRunner

from frbvoe.cli.main import cli


def test_version_command():
runner = CliRunner()
result = runner.invoke(cli, ["version"])
assert result.exit_code == 0
assert "FRB VOE version" in result.output
Empty file added tests/test_cli/test_tns.py
Empty file.
56 changes: 56 additions & 0 deletions tests/test_cli/test_voe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from datetime import datetime

from frbvoe.models.voe import VOEvent


def test_voevent():
# Create a valid VOEvent object
voevent = VOEvent(
kind="detection",
observatory_name="CHIME",
date="2020-01-13 16:55:08.844845",
email="[email protected]",
right_ascension=55.2938,
declination=14.2049,
pos_error_deg_95=0.001,
importance=0.9979,
)

# Check that the object is valid
assert voevent.is_valid()

# Check that the attributes are set correctly
assert voevent.kind == "detection"
assert voevent.observatory_name == "CHIME"
assert voevent.date == datetime.strptime(
"2020-01-13 16:55:08.844845", "%Y-%m-%d %H:%M:%S.%f"
)
assert voevent.email == "[email protected]"
assert voevent.right_ascension == 55.2938
assert voevent.declination == 14.2049
assert voevent.pos_error_deg_95 == 0.001
assert voevent.importance == 0.9979

# Check that optional attributes are set to None by default
assert voevent.semi_major is None
assert voevent.semi_minor is None
assert voevent.sampling_time is None
assert voevent.bandwidth is None
assert voevent.central_frequency is None
assert voevent.npol is None
assert voevent.bits_per_sample is None
assert voevent.gain is None
assert voevent.tsys is None
assert voevent.internal_id is None
assert voevent.dm is None
assert voevent.dm_error is None
assert voevent.width is None
assert voevent.snr is None
assert voevent.flux is None
assert voevent.website is None
assert voevent.tns_name is None
assert voevent.update_message is None

# Check that tokenized attributes are set to None by default
assert voevent.comet_port == 8098
assert voevent.email_password is None
95 changes: 95 additions & 0 deletions tests/test_utilities/test_TNSAgent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import unittest
from unittest.mock import patch

from frbvoe.utilities.TNSAgent import TNSAgent


class TestTNSAgent(unittest.TestCase):
def setUp(self):
self.agent = TNSAgent(debug=False)

def test_set_sandbox(self):
self.agent.set_sandbox()
self.assertEqual(self.agent.url, "https://sandbox.wis-tns.org/api")

def test_set_live(self):
self.agent.set_live()
self.assertEqual(self.agent.url, "https://www.wis-tns.org/api")

def test_format_payload(self):
payload = {"key": "value"}
formatted_payload = self.agent.format_payload(payload)
self.assertEqual(formatted_payload, '{"key": "value"}')

@patch("frbvoe.utilities.TNSAgent.requests.get")
def test_try_get_tns_name(self, mock_get):
json_data = {"name": "FRB123"}
mock_get.return_value.json.return_value = json_data

tns_name = self.agent.try_get_tns_name(json_data)
self.assertEqual(tns_name, "FRB123")

@patch("frbvoe.utilities.TNSAgent.requests.Response")
def test_check_response(self, mock_response):
mock_response.status_code = 200
self.assertTrue(self.agent.check_response(mock_response))

@patch("frbvoe.utilities.TNSAgent.requests.get")
def test_get_bulk_report_reply(self, mock_get):
report_id = "12345"
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {"status": "success"}

response = self.agent.get_bulk_report_reply(report_id)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"status": "success"})

@patch("frbvoe.utilities.TNSAgent.requests.get")
def test_print_bulk_report_reply(self, mock_get):
report_id = "12345"
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {"status": "success"}

with patch("builtins.print") as mock_print:
self.agent.print_bulk_report_reply(report_id)
mock_print.assert_called_with({"status": "success"})

@patch("frbvoe.utilities.TNSAgent.requests.post")
def test_send_report(self, mock_post):
payload = {"key": "value"}
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {"status": "success"}

self.assertTrue(self.agent.send_report(payload))

@patch("frbvoe.utilities.TNSAgent.requests.post")
def test_search_by_internal_name(self, mock_post):
payload = {"name": "FRB123"}
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {"status": "success"}

response = self.agent.search_by_internal_name(payload)
self.assertEqual(response, "success")

@patch("frbvoe.utilities.TNSAgent.requests.post")
def test_change_prop_period(self, mock_post):
payload = {"name": "FRB123", "period": 10}
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {"status": "success"}

self.assertTrue(self.agent.change_prop_period(payload))

def test_reset(self):
self.agent.reset()
self.assertIsNone(self.agent.url)
self.assertIsNone(self.agent.tns_name)
self.assertIsNone(self.agent.report_id)
self.assertIsNone(self.agent.id_code)
self.assertIsNone(self.agent.id_message)
self.assertIsNone(self.agent.sleep_interval)
self.assertIsNone(self.agent.loop_counter)
self.assertIsNone(self.agent.http_errors)


if __name__ == "__main__":
unittest.main()
51 changes: 0 additions & 51 deletions tests/test_utilities/test_comet

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import pytest
from frbvoe.models.voe import VOE
from frbvoe.utilities.email import report, retract, update

from frbvoe.models.voe import VOEvent


def test_report(voe, test_parameter, test_value):
with pytest.raises(ValueError):
voe[test_parameter] = test_value
print(voe)



def test_retract(voe, test_parameter, test_value):
with pytest.raises(ValueError):
voe[test_parameter] = test_value
print(voe)



def test_update(voe, test_parameter, test_value):
with pytest.raises(ValueError):
voe[test_parameter] = test_value
print(voe)


# Example usage
sample_voe = VOE(
sample_voe = VOEvent(
kind="detection",
author="John Smith",
email="[email protected]",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from frbvoe.models.voe import VOE
from frbvoe.utilities.tns import submit

from frbvoe.models.voe import VOEvent


def test_submit(voe, test_parameter, test_value):
with pytest.raises(ValueError):
Expand All @@ -9,7 +10,7 @@ def test_submit(voe, test_parameter, test_value):


# Example usage
sample_voe = VOE(
sample_voe = VOEvent(
kind="detection",
author="John Smith",
email="[email protected]",
Expand Down

0 comments on commit 07fd67b

Please sign in to comment.