Пример #1
0
def test_round_trip():
    """
    tests that a large Oil object loaded from JSON, then saved as JSON,
    then reloaded again, results in an equal object

    Not really much a test at this point, but why not?
    """
    filein = TEST_DATA_DIR / "EC" / "EC00506.json"
    fileout = OUTPUT_DIR / "temp_oil.json"

    # read it in:
    oilin = Oil.from_file(filein)

    # check that they are not exactly the same
    # this used to be a test for when we had equal data written in
    # different order it's not longer valid
    oilin.to_file(fileout)

    # file1 = open(filein, encoding="utf-8").read().strip()
    # file2 = open(fileout, encoding="utf-8").read().strip()

    # assert not file1 == file2, "output is the same as input -- test not fully valid"

    # read the new one to an Oil object

    oilout = Oil.from_file(fileout)

    assert oilin == oilout
Пример #2
0
    def test_add_new_attribute(self):
        """
        You should not be able to add an arbitrary new attribute
        """
        oil = Oil(oil_id=OIL_ID)

        with pytest.raises(AttributeError):
            oil.random_attr = 456
Пример #3
0
    def test_permanent_warnings(self):
        oil = Oil('XXXXXX')

        warn = "Something is very wrong with this record."
        oil.permanent_warnings.append(warn)
        print(oil)

        msgs = oil.validate()

        print(msgs)

        assert "W000: " + warn in msgs
Пример #4
0
def test_version_none():
    """
    If it doesn't have a version string, it should get the current one.
    """
    oil = Oil('XXXXXX')
    oil.metadata.name = 'An oil name'
    pyjs = oil.py_json()
    # remove the version:
    pyjs.pop('adios_data_model_version', None)

    oil = Oil.from_py_json(pyjs)

    assert oil.adios_data_model_version == ADIOS_DATA_MODEL_VERSION
Пример #5
0
def test_add_labels_to_oil_no_product_type():
    """
    this oil should get no labels
    """
    oil = Oil('XXXXX')

    assert get_suggested_labels(oil) == []
Пример #6
0
def test_reasonable_name(name):
    # unreasonable names should fail
    oil = Oil(oil_id='AD00123')
    oil.metadata.name = name
    validate(oil)

    assert snippet_in_oil_status("W001:", oil)
    def test_replace_one(self):
        session = connect_mongodb(self.settings)

        ID = session.new_oil_id()
        orig_name = 'original name'
        new_name = 'new name'

        # create a minimal oil
        oil = Oil(ID)
        oil.metadata.name = orig_name

        # add it:
        session.insert_one(oil)

        oil_json = session.find_one(ID)

        assert oil_json['oil_id'] == ID
        assert oil_json['metadata']['name'] == orig_name

        # update it:
        oil.metadata.name = new_name
        res = session.replace_one(oil)
        assert res.matched_count == 1
        assert res.modified_count == 1

        oil_json = session.find_one(ID)

        assert oil_json['oil_id'] == ID
        assert oil_json['metadata']['name'] == new_name
Пример #8
0
def get_all_records(data_dir):
    """
    gets all the records from the JSON data stored in gitLab

    This is a generator that gets all the records, returning them one by one
    as record, path pairs

    :param data_dir: the directory that holds the data

    The record returned is an Oil object

    Use as such::
       for oil, path in get_all_records(data_dir):
            work_with_the_record
    """
    for fname in sorted(Path(data_dir).rglob("*.json")):
        with open(fname, encoding='utf-8') as jfile:
            try:
                pyjson = json.load(jfile)
            except Exception:
                print("Something went wrong loading:", fname)
                raise

        rec = Oil.from_py_json(pyjson)

        yield rec, fname
def test_full_round_trip():
    """
    This test makes sure that it can read a full record,
    save it as JSON, and then read it back again
    """
    record = ExxonDataReader.read_excel_file(
        example_dir / "Crude_Oil_HOOPS_Blend_assay_xls.xlsx")

    assert record[0][0] == "ExxonMobil"

    oil = ExxonMapper(('HOOPS Blend Example', record))

    assert oil.metadata.name == 'HOOPS Blend Example'

    print(oil.oil_id)

    filename = example_dir / "ExampleOutput.json"
    oil.to_file(filename)

    oil2 = Oil.from_file(filename)

    for bc2, bc in zip(oil2.sub_samples[0].bulk_composition,
                       oil.sub_samples[0].bulk_composition):
        assert bc2 == bc
    assert oil2.sub_samples[0].bulk_composition == oil.sub_samples[
        0].bulk_composition
    assert oil2.sub_samples[0].industry_properties == oil.sub_samples[
        0].industry_properties
    assert oil2 == oil
Пример #10
0
def minimal_oil():
    oil = Oil('XXXXX')
    oil.metadata.name = "Minimal Oil for Tests"
    fresh = Sample()
    # print(fresh.physical_properties)
    oil.sub_samples.append(fresh)
    return oil
Пример #11
0
    def test_metadata_in_oil(self, attr, value, expected):
        oil = Oil(oil_id=OIL_ID)

        assert hasattr(oil.metadata, attr)
        assert getattr(oil.metadata, attr) == expected['default']

        setattr(oil.metadata, attr, value)
        assert getattr(oil.metadata, attr) == expected['value']
Пример #12
0
    def test_from_py_json_nothing(self):
        """
        You must specify at least an oil_id
        """
        py_json = {}

        with pytest.raises(TypeError):
            _oil = Oil.from_py_json(py_json)
Пример #13
0
def test_add_labels_to_oil_no_API():
    """
    this oil should get only labels with no API limits
    """
    oil = Oil('XXXXX')
    oil.metadata.product_type = 'Crude Oil NOS'

    assert get_suggested_labels(oil) == ['Crude Oil']
Пример #14
0
def test_from_file_name():
    """
    test loading a Oil object directly from a file name
    """
    oil = Oil.from_file(TEST_DATA_DIR / "EC" / "EC02234.json")

    # maybe it would be better to do more of a test,
    # but the full loading should be tested elsewhere
    assert oil.oil_id == "EC02234"
Пример #15
0
    def test_json_minimal(self):
        """
        When we convert our dataclass into a JSON struct, empty fields
        will be sparse.  The field will not be included in the struct.
        So if we create an Oil with only a name, it will look like this:

            {'name': 'A name for an oil'}
        """
        oil = Oil(oil_id=OIL_ID)
        assert oil.oil_id == OIL_ID

        py_json = oil.py_json()

        assert py_json["oil_id"] == OIL_ID
        assert py_json['adios_data_model_version'] == str(
            ADIOS_DATA_MODEL_VERSION)
        print(py_json)
        assert len(py_json) == 3
Пример #16
0
    def py_json(self):
        rec = {}

        for attr in self.oil_props:
            rec[attr] = getattr(self, attr)

        obj = Oil.from_py_json(rec)

        return obj.py_json()
Пример #17
0
def get_obj_json(obj_path, collection_name):
    obj = json.load(open(obj_path, 'r', encoding='utf-8'))

    if collection_name == 'oil':
        oil = Oil.from_py_json(obj)
        oil.reset_validation()
        obj = oil.py_json()

    return obj
Пример #18
0
def test_add_labels_to_oil_no_labels_other():
    """
    we should never get a label for 'Other'
    """
    oil = Oil('XXXXX')
    oil.metadata.API = 32.0
    oil.metadata.product_type = 'Other'

    assert get_suggested_labels(oil) == []
Пример #19
0
def test_from_file():
    """
    test loading a Oil object directly from an open file
    """
    oil = Oil.from_file(
        open(TEST_DATA_DIR / "EC" / "EC02234.json", encoding="utf-8"))

    # maybe it would be better to do more of a test,
    # but the full loading should be tested elsewhere
    assert oil.oil_id == "EC02234"
Пример #20
0
    def test_json_minimal_nonsparse(self):
        oil = Oil(oil_id=OIL_ID)
        py_json = oil.py_json(sparse=False)

        pprint(py_json)

        assert len(py_json) == 8

        for attr in [
                'oil_id',
                'adios_data_model_version',
                'metadata',
                'sub_samples',
                'status',
                'permanent_warnings',
                'extra_data',
                'review_status',
        ]:
            assert attr in py_json
Пример #21
0
def test_version_too_high():
    pyjs = {
        'oil_id': 'AD00123',
        'adios_data_model_version': "2.0.0",
        'metadata': {
            'name': 'An oil name'
        }
    }
    with pytest.raises(VersionError):
        _oil = Oil.from_py_json(pyjs)
Пример #22
0
    def test_wrong_thing_in_oil_id(self):
        """
        it's easy to accidentally pass who knows what into the id param

        that should get flagged if it doesn't make sense
        """
        whoops = {'oil_id': 'AD00123', 'metadata': {'name': 'An oil name'}}
        with pytest.raises(ValueError):
            # error because we're passing the whole dict in
            Oil(whoops)
Пример #23
0
    def test_repr_minimal(self):
        """
        The repr should be reasonable
        """
        oil = Oil("XX00000")

        result = repr(oil)

        assert result.startswith("Oil(")
        assert "oil_id='XX00000'" in result
def test_metadata_from_full_record():
    oil = Oil.from_file(full_oil_filename)

    log = oil.metadata.change_log

    print(log)
    assert len(log) == 1
    assert log[0].name == "Bozo the Clown"
    assert log[0].date == "2021-04-01"
    assert log[0].comment == "Any random old thing"
Пример #25
0
    def py_json(self):
        rec = self.record.dict()

        rec['sub_samples'] = [s.dict() for s in self.sub_samples]

        self.resolve_oil_api(rec)

        rec = Oil.from_py_json(rec)

        return rec.py_json()
Пример #26
0
    def test_init_minimal(self):
        """
        Even though we initialized with only an ID, all attributes
        should be present.
        """
        oil = Oil(oil_id=OIL_ID)
        assert oil.oil_id == OIL_ID

        for attr in ('oil_id', 'metadata', 'sub_samples', 'status',
                     'extra_data'):
            assert hasattr(oil, attr)
Пример #27
0
    def test_from_py_json_minimal(self):
        """
        Note: It seems we are not only checking for existence, but specific
              values.  Parametrize??
        """
        py_json = {"oil_id": OIL_ID}
        oil = Oil.from_py_json(py_json)

        assert oil.oil_id == OIL_ID
        assert oil.status == []
        assert oil.metadata.API is None
Пример #28
0
    def test_subsamples(self):
        """
        Is it getting all the subsamples
        """
        oil = Oil.from_py_json(BIG_RECORD)

        print("working with:", oil.metadata.name)

        assert len(oil.sub_samples) == 5
        assert oil.sub_samples[0].metadata.name == "Fresh Oil Sample"
        assert oil.sub_samples[3].metadata.name == "25.3% Evaporated"
    def test_insert_one(self):
        session = connect_mongodb(self.settings)

        # create a minimal oil
        ID = session.new_oil_id()
        oil = Oil(ID)

        # add it:
        inserted_id = session.insert_one(oil)

        assert inserted_id == ID
Пример #30
0
def test_add_labels_to_oil_api(pt, api, labels):
    """
    we should never get a label for 'Other'
    """
    oil = Oil('XXXXX')
    oil.metadata.API = api
    oil.metadata.product_type = pt

    print("Product type:", pt)
    print("API:", api)
    print("labels:", get_suggested_labels(oil))
    assert get_suggested_labels(oil) == sorted(labels)