tests.py 3.87 KB
from django.test import TestCase
from common.models import PyrosUser
from .configpyros import ConfigPyros
from django.urls import reverse
import unittest,os
class ObservatoryConfigurationTests(TestCase):
    fixtures = ['tests/common_test_TZ.json']
    @unittest.expectedFailure
    def test_OCF_view_config_ko(self):
        if os.path.exists("obsconfig/fixtures/obsconfig.p"):
            os.remove("obsconfig/fixtures/obsconfig.p")
        
        u1 = PyrosUser.objects.get(username="haribo")
        config_ko = ConfigPyros("obsconfig/fixtures/observatory_configuration_ko.yml")
        if os.path.exists("obsconfig/fixtures/obsconfig.p"):
            os.remove("obsconfig/fixtures/obsconfig.p")
        
    def test_OCF_read_config_get_content(self):
        os.environ["PATH_TO_OBSCONF_FILE"] = "obsconfig/fixtures/observatory_configuration_ok_simple.yml"
        config = ConfigPyros("obsconfig/fixtures/observatory_configuration_ok_simple.yml")
        # get_devices, and get_computers are already tested in load
        self.assertEqual(config.get_obs_name(),"observatory_test")
        self.assertEqual(len(config.get_units()),1)
        first_unit = config.get_units()["test-unit"]
        self.assertEqual(first_unit["name"],"test-unit")
        self.assertEqual(len(config.get_channels(first_unit)),1)
        self.assertEqual(len(config.get_agents_per_device(first_unit)),4)
        self.assertIn("AstroMecCA",config.get_devices().keys())
        self.assertIn("MainComputer",config.get_computers().keys())
        self.assertEqual(config.get_device_capabilities("AstroMecCA")[0]["component"],"MountPointing")
        self.assertEquals(len(config.devices_links),0)
        if os.path.exists("obsconfig/fixtures/obsconfig.p"):
            os.remove("obsconfig/fixtures/obsconfig.p")
        

    def test_OCF_view_config_simple(self):
        os.environ["PATH_TO_OBSCONF_FILE"] = "obsconfig/fixtures/observatory_configuration_ok_simple.yml"
        config = ConfigPyros("obsconfig/fixtures/observatory_configuration_ok_simple.yml")
        u1 = PyrosUser.objects.get(username="haribo")
        u1.set_password("password123")
        u1.save()
        self.client.post(reverse("login_validation"),{"email":"haribo","password":"password123"})
        response = self.client.get(reverse('obs_astronomer_config'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'base.html')
        self.assertTemplateUsed(response, 'obsconfig/obs_astronomer_config.html')
        self.assertContains(response, 'AstroMecCA')
        self.assertContains(response,"PLC")
        self.assertContains(response,"OpticalChannel")
        if os.path.exists("obsconfig/fixtures/obsconfig.p"):
            os.remove("obsconfig/fixtures/obsconfig.p")
        

    def test_OCF_view_config_complex(self):
        os.environ["PATH_TO_OBSCONF_FILE"] = "obsconfig/fixtures/observatory_configuration_ok_complex.yml"
        config = ConfigPyros("obsconfig/fixtures/observatory_configuration_ok_complex.yml")
        u1 = PyrosUser.objects.get(username="haribo")
        u1.set_password("password123")
        u1.save()
        #self.client.login(username="haribo",password="password123")
        self.client.post(reverse("login_validation"),{"email":"haribo","password":"password123"})
        response = self.client.get(reverse('obs_astronomer_config'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'base.html')
        self.assertTemplateUsed(response, 'obsconfig/obs_astronomer_config.html')
        self.assertContains(response, 'AstroMecCA')
        self.assertContains(response,"PLC")
        self.assertContains(response,"OpticalChannel")
        # testing if GPS capability is attached to Camera ZWO
        self.assertContains(response,"DetectorTimer")
        if os.path.exists("obsconfig/fixtures/obsconfig.p"):
            os.remove("obsconfig/fixtures/obsconfig.p")