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

Cdf attribute tests #637

Merged

Conversation

anamanica
Copy link
Contributor

@anamanica anamanica commented Jun 17, 2024

Change Summary

Overview

Creating CDF attribute code tests for the two files:
1.) cdf_attribute_manager.py
2.) imap_cdf_manager.py

New Dependencies

None

New Files

For CDF Attribute Manager:

  • imap_default_global_test_cdf_attribues.yaml
    • Contains test .yaml for global variables
    • General info (tests load_global_attributes)
    • Also used in testing variable attributes
  • imap_test_global.yaml
    • Contains test .yaml for global cdf attributes
    • INSTRUMENT SPECIFIC (tests get_global_attributes)
  • imap_test_variable.yaml
    • Contains test .yaml for variable cdf attributes
    • INSTRUMENT SPECIFIC (tests get_variable_attributes)

For IMAP CDF Manager

  • test_imap_cdf_manager.py
    • Contains unit tests for imap_cdf_manager.py
  • imap_instrument_global_cdf_attrs.yaml
    • Tests add_instrument_global_attrs
    • Same information as imap_test_global.yaml, but needed a specific file naming convention.
  • imap_instrument2_global_cdf_attrs.yaml
    • Tests add_instrument_global_attrs
    • Same information as imap_swe_global.yaml, but needed a specific file naming convention.
  • imap_instrument_level_variable_attrs.yaml
    • Tests add_instrument_variable_attrs
    • Same information as imap_test_variable.yaml, but again, needed a specific file naming convention
  • imap_instrument2_level2_variable_attrs.yaml
    • Tests add_instrument_variable_attrs
    • Same information as imap_ultra_l1b_variable.yaml, but again, needed a specific file naming convention

Deleted Files

None

Updated Files

  • test_cdf_attribute_manager.py
    • Creating and improving unit tests
  • imap_cdf_manager.py
    • Added an optional parameter to __init__ so that I could reach my test files while also preserving the API.

Testing

The purpose of this ticket is to specifically create unit tests, and thus all the work this ticket is unit testing.

closes #592

@anamanica anamanica added enhancement New feature or request Repo: Testing Related to testing Student Tasks suitable for student work CDF Related to CDF files labels Jun 17, 2024
@anamanica anamanica requested a review from maxinelasp June 17, 2024 20:07
@anamanica anamanica changed the title Cdf attribute tests [WIP] Cdf attribute tests Jun 17, 2024
Copy link
Contributor

@maxinelasp maxinelasp left a comment

Choose a reason for hiding this comment

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

Looks like a great first start! Not approving only because it's a draft rn, but it's looking really good!

@@ -0,0 +1,23 @@
default_attrs: &default
Copy link
Contributor

Choose a reason for hiding this comment

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

These YAML files look good, but they should go under tests/

Logical_source_description: IMAP Mission MAGi Normal Rate Instrument Level-1A Data.


imap_test_section_5:
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need more than 2 imap_test_sections IMO. What is adding additional sections adding from a test perspective? One test is good to validate that you can get into the section. Two is good to validate that you can choose between the sections. But adding additional sections doesn't test anything extra.


# Default global tests
# Check false case
assert cdf_manager.global_attribute_schema["DOI"]["required"] is False
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a very minor thing, but you can also check falseness by saying assert not [statement] or check trueness with assert [statement]

cdf_manager = CdfAttributeManager(Path(__file__).parent.parent / "config")
# Test that default information was loaded in from "imap_default_global_cdf_attrs.yaml"
Copy link
Contributor

Choose a reason for hiding this comment

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

hm, this might actually be a bug within CdfAttributeManger. I don't think it should load in any information to start. This is irrelevant to your PR, just a mental note for me to go back and fix that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah! Okay, I see. This is the code that shows information is loaded in:
# Load Default IMAP Global Attributes self.global_attributes = CdfAttributeManager._load_yaml_data( self.source_dir / DEFAULT_GLOBAL_CDF_ATTRS_FILE ) self.variable_attributes = dict()

That is line 78 in cdf_attribute_manager.py, and DEFAULT_GLOBAL_CDF_ATTRS_FILE is defined as

DEFAULT_GLOBAL_CDF_ATTRS_FILE = "imap_default_global_cdf_attrs.yaml"

in line 12.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm ok in that case, leave this test in, but I probably will want to update that lol

Copy link
Contributor

Choose a reason for hiding this comment

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

Should not block or really affect you

cdf_manager.load_global_attributes("imap_default_global_cdf_attrs.yaml")
# Load in different data, test what was carried over
cdf_manager.load_global_attributes("imap_default_global_test_cdf_attrs.yaml")
assert cdf_manager.global_attributes["Project"] == "STP>Solar-Terrestrial Physics"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

imap_processing/cdf/tests/test_cdf_attribute_manager.py Outdated Show resolved Hide resolved
imap_processing/cdf/tests/test_cdf_attribute_manager.py Outdated Show resolved Hide resolved
imap_processing/cdf/tests/test_cdf_attribute_manager.py Outdated Show resolved Hide resolved

# test add_instrument_global_attrs(self, instrument: str):


Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, I like this file, but don't add much more than this to it. ImapCdfAttributes is very minimal, and you don't want to duplicate tests. Really, the only test needed is that self.source_dir is set correctly, because right now that's the only thing done in ImapCdfAttributes, with everything else needing to be tested in test_cdf_attribute_manager

imap_processing/cdf/tests/test_cdf_attribute_manager.py Outdated Show resolved Hide resolved
@@ -164,9 +197,6 @@ def test_global_attribute():
if required_schema is True:
assert attr_name in test_get_global_attrs.keys()

# Testing second elif statement
# Should throw error
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is testing that the code successfully throws an error, you can use pytest.raises. If this test is failing because the code it's testing is incomplete or wrong, but it shouldn't, that's when you use pytest.mark.xfail. Basically, if someone should come back and fix it, xfail is your friend.

@anamanica anamanica self-assigned this Jun 18, 2024
@anamanica anamanica changed the title [WIP] Cdf attribute tests Cdf attribute tests Jun 18, 2024
@anamanica anamanica marked this pull request as ready for review June 18, 2024 23:20
Copy link
Contributor

@maxinelasp maxinelasp left a comment

Choose a reason for hiding this comment

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

Not done with my review yet - but nice work!

if source_dir_input is None:
super().__init__(Path(__file__).parent / "config")
else:
super().__init__(Path(__file__).parent / "config")
Copy link
Contributor

Choose a reason for hiding this comment

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

You should pass the source_dir_input into the super() init function because there are steps in __init__ that depend on the source_dir and it would be confusing to have it set to different values for the init vs for the rest of processing.

Then you can remove line 21

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has actually been causing me a lot of weird struggles with testing this class. In CdfAttributeManager , the default schema and IMAPS default variables are in the "config" file path and not guaranteed in the source_dir_input file path. I can't get the tests to run correctly without loading in that default info first, and then switching the file path... Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

In those tests, you can pass in the "config" path (or pass in nothing and use the default) and then override it. This way it will still get the default schemas and global variables from "config" but you can also access your test files in the other file path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hahaha, happy to say I just got it. Thank you so much! I see now.

@@ -0,0 +1,246 @@
DOI:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any way/is it reasonable to use the real schema? I know this goes back to the source_dir question again, but I think it would be valuable to test against the real schema - because if that ever changes, and the person updating doesn't realize that the file is duplicated in the tests, it could cause issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! Sorry, I actually am using the real schema, I should have deleted these files. This is why we have to initially set the CdfAttributeManager.source_dir to end in the "config" folder to get those real schema...

# Testing that required schema keys are in get_global_attributes
for attr_name in cdf_manager.global_attribute_schema.keys():
required_schema = cdf_manager.global_attribute_schema[attr_name]["required"]
if required_schema is True:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

imap_processing/cdf/tests/test_cdf_attribute_manager.py Outdated Show resolved Hide resolved
cdf_manager.add_global_attribute("Project", "Test Project")
assert cdf_manager.global_attributes["Project"] == "Test Project"
test_get_global_attrs = cdf_manager.get_global_attributes("imap_test_T1_test")
assert test_get_global_attrs["Project"] == "Test Project"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice test, I like it. Can you also add a test for adding a new attribute that didn't exist in the file? For example, delete one of the required attributes (you can delete it out of cdf_manager.global_attributes) and then assert that get_global_attributes fails for that attribute, then run add_global_attribute and assert that it now exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah! You will see the updated test in some of my new pushes, but I did have a few questions about this:

  • The code in get_global_attributes does not throw an error if a required attribute is missing, it just sets that attribute to None. Shall this be changed in the method definition?
  • I can't add attributes that are instrument specific because all instrument specific data is accessed through dictionary objects. and not CdfAttributeManager objects (such as cdf_manager.global_attributes["instrument"] or instrument = cdf_manager.get_global_attributes("instrument)). Is that okay?

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. No, don't change the code, but instead validate that it's None.
  2. You can add instrument specific attributes through add_global_attribute because the global attributes are flat and contain the global attributes for the instruments and for the shared values.

I'm not totally sure I understand your second question, did I answer it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah. I think I got it now, thanks!

imap_processing/cdf/tests/test_cdf_attribute_manager.py Outdated Show resolved Hide resolved
Copy link
Contributor

@maxinelasp maxinelasp left a comment

Choose a reason for hiding this comment

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

Finished review, ping me when you're ready for another one! Also, let me know if you want to meet to discuss any of this since there are some larger questions here that might be easier to have a conversation about.

cdf_manager.add_global_attribute("Project", "Test Project")
assert cdf_manager.global_attributes["Project"] == "Test Project"
test_get_global_attrs = cdf_manager.get_global_attributes("imap_test_T1_test")
assert test_get_global_attrs["Project"] == "Test Project"
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. No, don't change the code, but instead validate that it's None.
  2. You can add instrument specific attributes through add_global_attribute because the global attributes are flat and contain the global attributes for the instruments and for the shared values.

I'm not totally sure I understand your second question, did I answer it?

assert imap_instrument["Data_type"] == "T1_test-one>Test-1 test one"
assert imap_instrument["Project"] == "STP>Solar-Terrestrial Physics"

# Testing reloading data
Copy link
Contributor

Choose a reason for hiding this comment

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

This is already tested in the test_cdf_attribute_manager code - most of these tests are somewhat duplicating it. It's ok to have some duplicate tests, but generally, you should really try to focus in and make sure you only have one test per thing. If we changed the way we add global attrs, we would have to go and update 2 tests that test the same thing, so it's a little annoying.

This isn't a blocker for me since this test file is pretty minimal - so I will leave it up to your judgement which tests are duplicating and which ones are adding value. Just wanted to point that out as something to think about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The particular reason I included both of these tests is because they are loaded in from different files. That being said though, I definitely hear what you are saying. I'll rake through my tests, and get rid of the silly ones.

Copy link
Contributor

@maxinelasp maxinelasp left a comment

Choose a reason for hiding this comment

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

Nice work! Really really close now, if you address my newest comments then you can go ahead and merge! Congrats for making it through the first review thunderdome 😁

imap_processing/cdf/tests/test_cdf_attribute_manager.py Outdated Show resolved Hide resolved
imap_cdf_manager = ImapCdfAttributes("tests")
# Create an ImapCdfAttributes object, set to correct file path
imap_cdf_manager = ImapCdfAttributes()
imap_cdf_manager.source_dir = Path(__file__).parent.parent / "tests"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

imap_processing/cdf/tests/test_imap_cdf_manager.py Outdated Show resolved Hide resolved
imap_processing/cdf/imap_cdf_manager.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@bourque bourque left a comment

Choose a reason for hiding this comment

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

Awesome work! I just have a few non-blocking nit-picky suggestions, if you so chose to accept them.

imap_processing/cdf/imap_cdf_manager.py Outdated Show resolved Hide resolved
Comment on lines +3 to +4
Mission_group: Dysfunctional Cats
PI_name: Ana Manica
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice 😂

imap_processing/cdf/imap_cdf_manager.py Outdated Show resolved Hide resolved
imap_processing/cdf/imap_cdf_manager.py Outdated Show resolved Hide resolved
Comment on lines 4 to 7
This file is nothing more than a test yamal. This description? This is a test description.
This Testinstrument will contribute to our understanding of the cdf_attribute_manager.py file.
This test file is led by me, Ana Manica. Lasp undergraduate employee.
I don't have a website. Sorry.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Beautifully written 😂

Comment on lines +248 to +255
"CATDESC",
"DEPEND_0",
"DISPLAY_TYPE",
"FIELDNAM",
"FILLVAL",
"FORMAT",
"LABLAXIS",
"UNITS",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yay for alphabetical order!

imap_processing/cdf/tests/test_imap_cdf_manager.py Outdated Show resolved Hide resolved
@anamanica anamanica force-pushed the cdf-attribute-tests branch from 951d0dc to 4a7712a Compare June 25, 2024 20:00
@anamanica anamanica merged commit d012a9b into IMAP-Science-Operations-Center:dev Jun 25, 2024
16 of 17 checks passed
@anamanica anamanica deleted the cdf-attribute-tests branch June 25, 2024 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CDF Related to CDF files enhancement New feature or request Repo: Testing Related to testing Student Tasks suitable for student work
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Update CDF attribute generator unit tests
3 participants