How to mock dgraph for unit test


(Abdhesh Kumar) #1

Hi Dgraph team,

We are using Dgraph in our project so we come up with question how to mock dgraph in unit tests?

Thanks.


(Manish R Jain) #2

We use docker-compose to run a 6-node Dgraph cluster, and our integration tests use that cluster. See:


(Michel Conrado (Support Engineer)) #3

First of all, are you talking about doing tests on Dgraph itself or unit testing on your project?

If so, what language do you use?

All Clients created by Dgraph have some testing approach. As the example that was mentioned by Mrjn. I think you can get a lot done by exploring the test folders and analyzing how the Mocks are made.

In practice mocks and approaches will depend a lot on your unit test framework. Dgraph works just like any DB. So technically the tests should be very common.

Also if you’re using JS, In this link below I have an example that might help you create something (for code unit test). It’s creating a “Seed” using JSON File to add data. But the test approach you can create a similar function. Where you use “Drop-all” for each test level.

Also Dgraph-js and dgraph-js-http has good test samples and uses jest.

Link: https://github.com/MichelDiz/apollo-universal-starter-kit-With-Dgraph-DB/blob/86160bae53595290703221192f1b7a405ae8bc38/packages/server/src/dgraph/dgraphconnector.js#L114

Dgo client " examples_test.go" https://github.com/dgraph-io/dgo

Pydgraph client https://github.com/dgraph-io/pydgraph/tree/master/tests

Dgraph-js-http https://github.com/dgraph-io/dgraph-js-http/tree/master/tests

Dgraph-js https://github.com/dgraph-io/dgraph-js/tree/master/tests

Cheers.


(Abdhesh Kumar) #4

Thank you so much @mrjn for the quick reply. Actually, my tests are unit tests. so I wonder how to test functions that use dgraph in unit tests.

I could use your approach in my integration tests. is there no way to write unit tests without running dgraph docker?

Thanks.


(Abdhesh Kumar) #5

@MichelDiz thanks for the reply. I was talking about unit tests of my project that uses dgraph. I am using Scala language.
I have a method signature like,

def get(query: String, vars: util.Map[String, String]): DgraphProto.Response

Here now i want to mock DgraphProto.Response. I can use any mocking framework like java mockito

Thanks


(Michel Conrado (Support Engineer)) #6

I have no idea how I would do with Scalar, however as I said it should be quite common. You can download the Dgraph binaries in https://github.com/dgraph-io/dgraph/releases. Then you will put them in a suitable path.

You will need to create a kind of “Life cycle” to ascend and kill the Dgraph instances used for testing. In the hypothesis of not using Docker. By docker it would be much simpler.

Why do not you use the HTTP endpoint? You will have a simplified JSON response. Are you using any 3rd party Scalar client?

Dgraph has a Java Client- Dgraph4j. See how the tests are done. https://github.com/dgraph-io/dgraph4j/tree/master/src/test/java/io/dgraph