How to mock dgraph for unit test

Hi Dgraph team,

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


1 Like

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

1 Like

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: apollo-universal-starter-kit-With-Dgraph-DB/dgraphconnector.js at 86160bae53595290703221192f1b7a405ae8bc38 · MichelDiz/apollo-universal-starter-kit-With-Dgraph-DB · GitHub

Dgo client " examples_test.go" GitHub - dgraph-io/dgo: Official Dgraph Go client

Pydgraph client pydgraph/tests at master · dgraph-io/pydgraph · GitHub

Dgraph-js-http dgraph-js-http/tests at master · dgraph-io/dgraph-js-http · GitHub

Dgraph-js dgraph-js/tests at master · dgraph-io/dgraph-js · GitHub


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?


@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


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 Releases · dgraph-io/dgraph · GitHub. 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. dgraph4j/src/test/java/io/dgraph at master · dgraph-io/dgraph4j · GitHub

@abdheshkumar did you get the solution to write the unit tests? It seems there is no way currently to mock the dgraph client since its not been written with interfaces, I am not able figure out how can i mock the internal dgraph methods. please let me now if you have found some solution.

For scalar, you can compile an application using a plugin named sbt-protoc that invokes two code generators, one for ScalaPB that generates the case classes for all messages, and some gRPC-related code that ZIO-gRPC interfaces with, and a second generator is ZIO gRPC code generate, which generates ZIO interface to the dgraph service.

(reference: Basics Tutorial | ZIO gRPC)