Introduction to Query Testing#

Intro#

We are using pytest as a testing framework. If you are not familiar with it you can view the small example by the link above. The reason why pytest framework was chosen:

  • easy to read and write test

  • auto discovery feature, that allows you to execute all available tests using only one command, because it finds every test starts with test_ prefix in every subfolder.

  • many useful plugins, we are suing some of them to evaluate memory consumption, time of execution, coverage and mocking.

Constructing Ticks#

Onetick python library (otp) has many sources that allow you to work with data. Ticks - is one of the most important in terms of testing. This source allows you to generate tick flow. Let’s consider an example:

ticks = otp.Ticks({'PRICE': [13.5, 13.6, 13.3],
                   'QRY'  : [ 100,  200,  150]})

# .to_dataframe() executes the query and converts ticks into pandas DataFrame
print(ticks.to_dataframe())

This code will print the following data:

                     Time  PRICE  QRY
0 2003-12-01 00:00:00.000  13.5   100
1 2003-12-01 00:00:00.001  13.6   200
2 2003-12-01 00:00:00.002  13.3   150

The first field Time is created automatically. It defines the timestamp of the tick. We will describe how the one can change the default value later. PRICE and QRY are user-defined fields. String, float, int as well as milliseconds and nanoseconds datetimes are supported for now.

otp.Session#

Every onetick.py code should be run inside a otp.Session. Session is a special singleton-object, in technical terms it is a wrapper for the OneTickLib library, that is responsible for the integration with OneTick database and keeping different config files.

Warning

It is allowed to have only one alive instance in the process.

Every code that works with OneTick through OneTick python API, onetick-py or tests should be in scope of Session.

Note

The only one case is exception, when ONE_TICK_CONFIG environment variable is set; for that case Session object declaration is ignored. In the most cases it is not related to tests, but It’s very important thing to know: make sure that ONE_TICK_CONFIG environment is not set when you run tests.

onetick.py.session.Session class allows to manage config files, locator files, acl files and licenses.

The simplest session is the following:

import onetick.py as otp
s = otp.Session()  # All required configs will be automatically generated.
s.close()

To simplify session cleanup process otp.Session class implements context manager protocol.