Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-40101: Use vcr to provide a mock EFD for testing #55

Merged
merged 8 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,752 changes: 1,752 additions & 0 deletions tests/data/cassettes/setUpClass.yaml

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions tests/data/cassettes/test_clipDataToEvent.yaml

Large diffs are not rendered by default.

294 changes: 294 additions & 0 deletions tests/data/cassettes/test_endToEnd.yaml

Large diffs are not rendered by default.

2,338 changes: 2,338 additions & 0 deletions tests/data/cassettes/test_findEvent.yaml

Large diffs are not rendered by default.

926 changes: 926 additions & 0 deletions tests/data/cassettes/test_getAxisData.yaml

Large diffs are not rendered by default.

792 changes: 792 additions & 0 deletions tests/data/cassettes/test_getEfdData.yaml

Large diffs are not rendered by default.

318 changes: 318 additions & 0 deletions tests/data/cassettes/test_getMostRecentRowWithDataBefore.yaml

Large diffs are not rendered by default.

158 changes: 158 additions & 0 deletions tests/data/cassettes/test_getSubTopics.yaml

Large diffs are not rendered by default.

294 changes: 294 additions & 0 deletions tests/data/cassettes/test_helperFunctions.yaml

Large diffs are not rendered by default.

950 changes: 950 additions & 0 deletions tests/data/cassettes/test_noDataBehaviour.yaml

Large diffs are not rendered by default.

1,874 changes: 1,874 additions & 0 deletions tests/data/cassettes/test_plottingAndCommands.yaml

Large diffs are not rendered by default.

586 changes: 586 additions & 0 deletions tests/data/cassettes/test_printing.yaml

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion tests/test_efdUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
"""Test cases for utils."""

import unittest
import os
import lsst.utils.tests
import astropy
import pandas as pd
import datetime
import asyncio
from astropy.time import Time
import vcr

from lsst.utils import getPackageDir
from lsst.summit.utils.tmaUtils import TMAEvent, TMAState

from lsst.summit.utils.efdUtils import (
Expand All @@ -51,12 +54,24 @@
except ImportError:
HAS_EFD_CLIENT = False

# Use mode="none" to run tests for normal operation.
# To update files or generate new ones, make sure you have a working
# connection to lsst-schema-registry-efd.ncsa.illinois.edu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this address true? I'm a bit surprised to still see nasa dependency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nvm... I see you corrected this in a later commit. I guess that commit could be squashed with the first one.

# and temporarily run with mode="once".
packageDir = getPackageDir('summit_utils')
safe_vcr = vcr.VCR(
record_mode="none",
cassette_library_dir=os.path.join(packageDir, "tests", "data", "cassettes"),
path_transformer=vcr.VCR.ensure_suffix(".yaml"),
)


@unittest.skipIf(not HAS_EFD_CLIENT, "No EFD client available")
@unittest.skip("Skipping until DM-40101 is resolved.")
@safe_vcr.use_cassette()
class EfdUtilsTestCase(lsst.utils.tests.TestCase):

@classmethod
@safe_vcr.use_cassette()
def setUpClass(cls):
try:
cls.client = makeEfdClient()
Expand All @@ -80,10 +95,12 @@ def setUpClass(cls):
_endRow=255,
)

@safe_vcr.use_cassette()
def tearDown(self):
loop = asyncio.get_event_loop()
loop.run_until_complete(self.client.influx_client.close())

@safe_vcr.use_cassette()
def test_makeEfdClient(self):
self.assertIsInstance(self.client, lsst_efd_client.efd_helper.EfdClient)

Expand Down Expand Up @@ -111,6 +128,7 @@ def test_getDayObsAsTimes(self):
self.assertGreater(dayEnd, dayStart)
self.assertEqual(dayEnd.jd, dayStart.jd + 1)

@safe_vcr.use_cassette()
def test_getSubTopics(self):
subTopics = getSubTopics(self.client, 'lsst.sal.MTMount')
self.assertIsInstance(subTopics, list)
Expand All @@ -120,6 +138,7 @@ def test_getSubTopics(self):
self.assertIsInstance(subTopics, list)
self.assertEqual(len(subTopics), 0)

@safe_vcr.use_cassette()
def test_getEfdData(self):
dayStart = getDayObsStartTime(self.dayObs)
dayEnd = getDayObsEndTime(self.dayObs)
Expand Down Expand Up @@ -176,6 +195,7 @@ def test_getEfdData(self):
# good query, except the topic doesn't exist
_ = getEfdData(self.client, 'badTopic', begin=dayStart, end=dayEnd)

@safe_vcr.use_cassette()
def test_getMostRecentRowWithDataBefore(self):
time = Time(1687845854.736784, scale='utc', format='unix')
rowData = getMostRecentRowWithDataBefore(self.client,
Expand All @@ -197,6 +217,7 @@ def test_astropyToEfdTimestamp(self):
self.assertIsInstance(efdTimestamp, float)
return

@safe_vcr.use_cassette()
def test_clipDataToEvent(self):
# get 10 mins of data either side of the event we'll clip to
duration = datetime.timedelta(seconds=10*60)
Expand Down
30 changes: 28 additions & 2 deletions tests/test_tmaUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import unittest
import os
import lsst.utils.tests
import vcr

import pandas as pd
import numpy as np
Expand Down Expand Up @@ -53,6 +54,17 @@
'writeNewTmaEventTestTruthValues',
]

# Use mode="none" to run tests for normal operation.
# To update files or generate new ones, make sure you have a working
# connection to lsst-schema-registry-efd.ncsa.illinois.edu
# and temporarily run with mode="once".
packageDir = getPackageDir('summit_utils')
safe_vcr = vcr.VCR(
record_mode="none",
cassette_library_dir=os.path.join(packageDir, "tests", "data", "cassettes"),
path_transformer=vcr.VCR.ensure_suffix(".yaml"),
)


def getTmaEventTestTruthValues():
"""Get the current truth values for the TMA event test cases.
Expand Down Expand Up @@ -135,7 +147,6 @@ def _turnOn(tma):
tma._parts['elevationSystemState'] = PowerState.ON


@unittest.skip("Skipping until DM-40101 is resolved.")
class TmaUtilsTestCase(lsst.utils.tests.TestCase):

def test_tmaInit(self):
Expand Down Expand Up @@ -216,9 +227,11 @@ def test_initStateLogic(self):
# tma._axesInPosition()


@unittest.skip("Skipping until DM-40101 is resolved.")
@safe_vcr.use_cassette()
class TMAEventMakerTestCase(lsst.utils.tests.TestCase):

@classmethod
@safe_vcr.use_cassette()
def setUpClass(cls):
try:
cls.client = makeEfdClient()
Expand All @@ -231,15 +244,18 @@ def setUpClass(cls):
cls.events = cls.tmaEventMaker.getEvents(cls.dayObs) # does the fetch
cls.sampleData = cls.tmaEventMaker._data[cls.dayObs] # pull the data from the object and test length

@safe_vcr.use_cassette()
def tearDown(self):
loop = asyncio.get_event_loop()
loop.run_until_complete(self.client.influx_client.close())

@safe_vcr.use_cassette()
def test_events(self):
data = self.sampleData
self.assertIsInstance(data, pd.DataFrame)
self.assertEqual(len(data), 993)

@safe_vcr.use_cassette()
def test_rowDataForValues(self):
rowsFor = set(self.sampleData['rowFor'])
self.assertEqual(len(rowsFor), 6)
Expand All @@ -254,11 +270,13 @@ def test_rowDataForValues(self):
'elevationSystemState'}
self.assertSetEqual(rowsFor, correct)

@safe_vcr.use_cassette()
def test_monotonicTimeInDataframe(self):
# ensure that each row is later than the previous
times = self.sampleData['private_efdStamp']
self.assertTrue(np.all(np.diff(times) > 0))

@safe_vcr.use_cassette()
def test_monotonicTimeApplicationOfRows(self):
# ensure you can apply rows in the correct order
tma = TMAStateMachine()
Expand All @@ -275,6 +293,7 @@ def test_monotonicTimeApplicationOfRows(self):
tma.apply(row2)
tma.apply(row1)

@safe_vcr.use_cassette()
def test_fullDaySequence(self):
# make sure we can apply all the data from the day without falling
# through the logic sieve
Expand All @@ -286,6 +305,7 @@ def test_fullDaySequence(self):
for rowNum, row in self.sampleData.iterrows():
tma.apply(row)

@safe_vcr.use_cassette()
def test_endToEnd(self):
eventMaker = self.tmaEventMaker
events = eventMaker.getEvents(self.dayObs)
Expand All @@ -306,6 +326,7 @@ def test_endToEnd(self):
self.assertEqual(event.type.name, types[eventNum])
self.assertEqual(event.endReason.name, endReasons[eventNum])

@safe_vcr.use_cassette()
def test_noDataBehaviour(self):
eventMaker = self.tmaEventMaker
noDataDayObs = 19500101 # do not use 19700101 - there is data for that day!
Expand All @@ -314,6 +335,7 @@ def test_noDataBehaviour(self):
self.assertIsInstance(events, list)
self.assertEqual(len(events), 0)

@safe_vcr.use_cassette()
def test_helperFunctions(self):
eventMaker = self.tmaEventMaker
events = eventMaker.getEvents(self.dayObs)
Expand All @@ -325,6 +347,7 @@ def test_helperFunctions(self):
self.assertEqual(slews, foundSlews)
self.assertEqual(tracks, foundTracks)

@safe_vcr.use_cassette()
def test_printing(self):
eventMaker = self.tmaEventMaker
events = eventMaker.getEvents(self.dayObs)
Expand All @@ -348,6 +371,7 @@ def test_printing(self):
_initializeTma(tma) # the uninitialized state contains wrong types for printing
eventMaker.printTmaDetailedState(tma)

@safe_vcr.use_cassette()
def test_getAxisData(self):
eventMaker = self.tmaEventMaker
events = eventMaker.getEvents(self.dayObs)
Expand All @@ -367,6 +391,7 @@ def test_getAxisData(self):
# data in
plotEvent(self.client, events[0], azimuthData=azData, elevationData=elData)

@safe_vcr.use_cassette()
def test_plottingAndCommands(self):
eventMaker = self.tmaEventMaker
events = eventMaker.getEvents(self.dayObs)
Expand All @@ -388,6 +413,7 @@ def test_plottingAndCommands(self):

del fig

@safe_vcr.use_cassette()
def test_findEvent(self):
eventMaker = self.tmaEventMaker
events = eventMaker.getEvents(self.dayObs)
Expand Down