I am building some complex object set logic where I need to walk a tree of objects (dataset definitions) and get the connected entities (prices) in a coalesce-style so that in the end I dont have duplicates. Since the coalesce logic uses its own key definition / criteria of what is a duplicate, I need to test the logic. For this reason, I use the Objects.create() api and also .set() on the respective relations to create my objects with linked prices, and also determine the hierarchy between the stub objects, all according to the documentation: Functions • Unit testing • Stub object searches and aggregations • Palantir
This is the .getDataSetHierarchy method:
public getDatasetHierarchy(dataset: Dataset): Dataset[] {
const parent = dataset.parentDataset.get()
if (!parent){
return [dataset]
}
const outerParents = this.getDatasetHierarchy(parent)
const concat = [dataset].concat(outerParents)
return concat
}
This is how my stubs factory:
async function stubsFactory() {
const helpers = new TestHelpers()
const parentDataSet = Objects.create().dataset("parent")
parentDataSet.rid = "rid.p1"
const childDataSet = Objects.create().dataset("child")
childDataSet.rid = "rid.c1"
childDataSet.parentDatasetKey = "parent"
childDataSet.parentDataset.set(parentDataSet)
const prices_ok = await helpers.readCsvAsObjects<MockPriceRow>(path.join(__dirname, 'mock_data', "selected_prices_ok.csv"))
const pricesParent = prices_ok.filter(price => price.datasetKey === "parent").map(price => createConsolidatedPrice(price))
const pricesChild = prices_ok.filter(price => price.datasetKey === "child").map(price => createConsolidatedPrice(price))
pricesChild.forEach(price => price.dataset.set(childDataSet))
pricesParent.forEach(price => price.dataset.set(parentDataSet))
// console.log(pricesParent)
return { parentDataSet, childDataSet }
}
Unfortunately, this test has some unexpected assertion failures:
test("test get DatasetHierarchy", async () => {
const { parentDataSet, childDataSet } = await stubsFactory()
childDataSet.parentDataset.set(parentDataSet)
const dc = new DataSetMethods()
const allDatasets = dc.getDatasetHierarchy(childDataSet)
expect(allDatasets[0].parentDataset.get()).toBeDefined()
expect(allDatasets[0].prices.all()).toBeDefined()
expect(allDatasets[0].prices.all().length).toEqual(1)
expect(allDatasets[1].parentDataset.get()).toBeUndefined()
expect(allDatasets[1].prices.all()).toBeDefined()
expect(parentDataSet.prices.all().length).toEqual(2) // passes
expect(allDatasets[1].prices.all().length).toEqual(2) // fails
expect(childDataSet.parentDataset.get()).toBe(parentDataSet) // --> rid does not match
})
The thing is: The method works in the ontology as expected which I figured out via e2e testing! I am primarily worried about the lines
expect(parentDataSet.prices.all().length).toEqual(2) // passes
expect(allDatasets[1].prices.all().length).toEqual(2) // fails
They should both return the same result and I cant come up with a reason why this may not be the case… Could it be that if you .set() and object in a test environment, it does not create a deep copy but only a shallow copy of the object expect(childDataSet.parentDataset.get()).toBe(parentDataSet) // --> rid does not match
returns a failure since te two objects are not exactly the same (missing rid) and I assume the linked prices are also missing.
I know that ontology edits take only place after the function returns. However, this will not happen in a test scenario and if there is no other way to stub ontology objects like I do and to simulate the ontology in this way, I seriously doubt the value of this test environment
Ps.: I changed some names here and there to make it more general for obvious reasons. Hopefully, I did not introduce more bugs