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
def test_add_labels_to_oil_no_product_type(): """ this oil should get no labels """ oil = Oil('XXXXX') assert get_suggested_labels(oil) == []
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 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
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']
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
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']
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) == []
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)
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_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)
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
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)
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
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
def make_oil_with_densities(self, densities, temps): oil = Oil(oil_id="DENSITY_TESTER") sample = Sample() sample.metadata.name = "only density" oil.sub_samples.append(sample) for d, t in zip(densities, temps): dp = DensityPoint(meas.Density(d, unit="kg/m^3"), meas.Temperature(t, unit="K")) sample.physical_properties.densities.append(dp) return oil
def test_find_one(self): session = connect_mongodb(self.settings) # create a minimal oil ID = session.new_oil_id() oil = Oil(ID) # add it: session.insert_one(oil) new_oil = session.find_one(ID) assert new_oil['oil_id'] == ID
def test_add_labels_to_oil_api_and_visc(pt, api, kvis, kvis_temp, labels): """ refined products with density and viscosity limits """ oil = Oil('XXXXX') oil.metadata.API = api oil.metadata.product_type = pt add_kin_viscosity_to_oil(oil, (kvis, ), 15, 'cSt', kvis_temp, 'C') print("Product type:", pt) print("API:", api) print("expected labels:", labels) print("labels:", get_suggested_labels(oil)) assert get_suggested_labels(oil) == sorted(labels)
def test_bad_oil_id(self): """ it's easy to accidentally pass who knows what into the id param a really long string probably isn't what you meant """ whoops = { 'oil_id': 'AD00123', 'metadata': { 'name': 'An oil name', 'comments': "an arbitrary comment" }, } with pytest.raises(ValueError): Oil(str(whoops))
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
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
def no_api_with_one_density_13C(): oil = Oil(oil_id='XXXXXX') oil.metadata.product_type = "Condensate" print(oil) # create a sample for fresh oil s = Sample() # add some densities # p = PhysicalProperties() p = s.physical_properties p.densities = DensityList([ DensityPoint(density=Density(value=0.8751, unit="g/cm^3"), ref_temp=Temperature(value=13.0, unit="C")), ]) oil.sub_samples.append(s) return oil
def test_delete_one(self): session = connect_mongodb(self.settings) ID = session.new_oil_id() # create a minimal oil oil = Oil(ID) # add it: session.insert_one(oil) oil_json = session.find_one(ID) assert oil_json['oil_id'] == ID # delete it: session.delete_one(oil.oil_id) oil_json = session.find_one(ID) assert oil_json is None
def test_init_empty(self): """ You must specify at least an oil_id """ with pytest.raises(TypeError): Oil()
def test_empty_oil_id(self): """ you must specify at least an oil_id """ with pytest.raises(TypeError): Oil(oil_id="")