How to write data sets

While writing tests,i would like to use data sets as stores for the test data,But couldn’t find better documentation on doing this,could we be having some good links on this? cc @dkayiwa @ibacher @mogoodrich @mseaton

@herbert24 kindly have a look at the already existing datasets in core https://github.com/openmrs/openmrs-core/tree/4a0feb8da351088f25fdc4e6d324a1f277aa3410/api/src/test/resources/org/openmrs/api/include

Hope it can be of help

1 Like

thanks let me look through

Thanks @gcliff for that link you shared, adding on what @gcliff shared, @herbert24 can you also deeply take a look at this link, Initialising a dataset, it has some good information

1 Like

And also hopefully you have looked at https://wiki.openmrs.org/display/docs/Unit+Tests and this: https://wiki.openmrs.org/display/docs/Mock+Doc

3 Likes

Thanks every one for the links,this https://wiki.openmrs.org/display/docs/Unit+Tests suggests use of CreateInitialDataSet class to create a data set.I access the class and run it as a junit,then i look out for the writen data set for example as per the location here, FlatXmlDataSet.write(initialDataSet, new FileOutputStream(“test/resources/org/openmrs/api/include/LocationServiceTest-attributes.xml”)); unfortunately,i do not get any thing overwriten in LocationServiceTest-attributes.xml.I do have C:\Users\ODEL\AppData\Roaming\OpenMRS\openmrs-runtime file arleady.Could i be missing out some thing on this cc @dkayiwa

@herbert24 Personally, I just hand-write the XML, since generally I need far fewer elements than included in an entire database. The format is really straight-forward, thought it does require some knowledge of the OpenMRS data model (specifically what fields must be filled in and what kind of fields are expected). The test database usually has both the initialMemoryTestDataSet.xml and standardTestDataset.xml data loaded, which gives a pretty good background for tests. From there, it’s a matter of adding the specific rows to specific tables you need in the format:

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
    <table column_1="value" column_2="value"/>
    <table column_1="value" column_2="value"/>
</dataset>

While the CreateInitialDataSet thing might be “easier” in the sense that you don’t have to think too much about the XML format, it will ultimately result in more data than you would generally need and is a bit tricky to use. In particular, the reason it’s likely failing is that if you run it inside a unit test, it’s running against the empty test H2 instance. You need to somehow point it to an actual existing OpenMRS database with the data that you want included in your tests.

2 Likes

Yes, agree with @ibacher, I usually just handrite the test data set… there are a lot of example hand-written data sets in the HTML Form Entry much you could take a look at @herbert24

Take care, Mark

2 Likes

thanks much @ibacher and @mogoodrich,let me checkout the different hand written data sets as i look towards writing a data set made of visit locations with there child locations