Best Python code snippet using pytest-play_python
test_statsd.py
Source:test_statsd.py
1"""Test statsd logging."""2# Tests adapted from https://github.com/bbelyeu/flask-statsdclient3from datetime import timedelta4from unittest.mock import patch5from flask import Flask6from statsd.client.timer import Timer7from statsd.client.udp import Pipeline8import pytest9from baseframe import forms10from baseframe.extensions import Babel11from baseframe.statsd import Statsd12@pytest.fixture()13def app():14 """Redefine app without Baseframe for statsd tests."""15 return Flask(__name__)16@pytest.fixture()17def statsd(app):18 s = Statsd()19 s.init_app(app)20 return s21@pytest.fixture()22def view(app):23 @app.route('/')24 def index():25 return 'index'26 return index27@pytest.fixture()28def form(app):29 Babel(app) # Needed for form validator message translations30 class SimpleForm(forms.Form):31 field = forms.StringField(32 "Required", validators=[forms.validators.DataRequired()]33 )34 f = SimpleForm(meta={'csrf': False})35 del f.form_nonce36 return f37def test_default_config(app, statsd):38 # pylint: disable=protected-access39 assert app.extensions['statsd_core']._addr == ('127.0.0.1', 8125)40def test_custom_config(app):41 # pylint: disable=protected-access42 app.config['STATSD_HOST'] = '1.2.3.4'43 app.config['STATSD_PORT'] = 1234544 Statsd(app)45 assert app.extensions['statsd_core']._addr == ('1.2.3.4', 12345)46# The wrapper methods in Statsd will:47# 1. Prefix ``app.`` and `app.name` to the stat name48# 2. Insert STATSD_RATE if no rate is specified49# 3. Insert tags if tags are enabled and specified50def test_incr(ctx, statsd):51 with patch('statsd.StatsClient.incr') as mock_incr:52 statsd.incr('test.counter')53 mock_incr.assert_called_once_with(54 'flask_app.tests.test_statsd.test.counter', 1, rate=155 )56 with patch('statsd.StatsClient.incr') as mock_incr:57 statsd.incr('test.counter', rate=0.5)58 mock_incr.assert_called_once_with(59 'flask_app.tests.test_statsd.test.counter', 1, rate=0.560 )61 with patch('statsd.StatsClient.incr') as mock_incr:62 statsd.incr('test.counter', 2, rate=0.5)63 mock_incr.assert_called_once_with(64 'flask_app.tests.test_statsd.test.counter', 2, rate=0.565 )66def test_decr(ctx, statsd):67 with patch('statsd.StatsClient.decr') as mock_decr:68 statsd.decr('test.counter')69 mock_decr.assert_called_once_with(70 'flask_app.tests.test_statsd.test.counter', 1, rate=171 )72 with patch('statsd.StatsClient.decr') as mock_decr:73 statsd.decr('test.counter', rate=0.5)74 mock_decr.assert_called_once_with(75 'flask_app.tests.test_statsd.test.counter', 1, rate=0.576 )77 with patch('statsd.StatsClient.decr') as mock_decr:78 statsd.decr('test.counter', 2, rate=0.5)79 mock_decr.assert_called_once_with(80 'flask_app.tests.test_statsd.test.counter', 2, rate=0.581 )82def test_gauge(ctx, statsd):83 with patch('statsd.StatsClient.gauge') as mock_gauge:84 statsd.gauge('test.gauge', 5)85 mock_gauge.assert_called_once_with(86 'flask_app.tests.test_statsd.test.gauge', 5, rate=1, delta=False87 )88 with patch('statsd.StatsClient.gauge') as mock_gauge:89 statsd.gauge('test.gauge', 5, rate=0.5)90 mock_gauge.assert_called_once_with(91 'flask_app.tests.test_statsd.test.gauge', 5, rate=0.5, delta=False92 )93 with patch('statsd.StatsClient.gauge') as mock_gauge:94 statsd.gauge('test.gauge', 10, rate=0.5, delta=True)95 mock_gauge.assert_called_once_with(96 'flask_app.tests.test_statsd.test.gauge', 10, rate=0.5, delta=True97 )98def test_set(ctx, statsd):99 with patch('statsd.StatsClient.set') as mock_set:100 statsd.set('test.set', 'item')101 mock_set.assert_called_once_with(102 'flask_app.tests.test_statsd.test.set', 'item', rate=1103 )104 with patch('statsd.StatsClient.set') as mock_set:105 statsd.set('test.set', 'item', rate=0.5)106 mock_set.assert_called_once_with(107 'flask_app.tests.test_statsd.test.set', 'item', rate=0.5108 )109def test_timing(ctx, statsd):110 with patch('statsd.StatsClient.timing') as mock_timing:111 statsd.timing('test.timing', 10)112 mock_timing.assert_called_once_with(113 'flask_app.tests.test_statsd.test.timing', 10, rate=1114 )115 with patch('statsd.StatsClient.timing') as mock_timing:116 statsd.timing('test.timing', timedelta(seconds=5), rate=0.5)117 mock_timing.assert_called_once_with(118 'flask_app.tests.test_statsd.test.timing', timedelta(seconds=5), rate=0.5119 )120def test_timer(ctx, statsd):121 timer = statsd.timer('test.timer', rate=1)122 assert isinstance(timer, Timer)123 assert timer.stat == 'flask_app.tests.test_statsd.test.timer'124 assert timer.rate == 1125 timer = statsd.timer('test.timer', rate=0.5)126 assert isinstance(timer, Timer)127 assert timer.stat == 'flask_app.tests.test_statsd.test.timer'128 assert timer.rate == 0.5129def test_pipeline(ctx, statsd):130 pipeline = statsd.pipeline()131 assert isinstance(pipeline, Pipeline)132def test_custom_rate(app, ctx, statsd):133 app.config['STATSD_RATE'] = 0.3134 with patch('statsd.StatsClient.incr') as mock_incr:135 statsd.incr('test.counter')136 mock_incr.assert_called_once_with(137 'flask_app.tests.test_statsd.test.counter', 1, rate=0.3138 )139 with patch('statsd.StatsClient.incr') as mock_incr:140 statsd.incr('test.counter', rate=0.5)141 mock_incr.assert_called_once_with(142 'flask_app.tests.test_statsd.test.counter', 1, rate=0.5143 )144def test_tags(app, ctx, statsd):145 # Tags are converted into buckets if statsd doesn't support them146 with patch('statsd.StatsClient.incr') as mock_incr:147 statsd.incr('test.counter', tags={'tag': 'value'})148 mock_incr.assert_called_once_with(149 'flask_app.tests.test_statsd.test.counter.tag_value', 1, rate=1150 )151 # Tags are enabled if a separator character is specified in config.152 # The app name is then included as a tag instead of as a prefix.153 # `flask_app` is retained as a prefix.154 app.config['STATSD_TAGS'] = ';'155 with patch('statsd.StatsClient.incr') as mock_incr:156 statsd.incr('test.counter', tags={'tag': 'value'})157 mock_incr.assert_called_once_with(158 'flask_app.test.counter;tag=value;app=tests.test_statsd', 1, rate=1159 )160 with patch('statsd.StatsClient.incr') as mock_incr:161 statsd.incr('test.counter', tags={'tag': 'value', 't2': 'v2'})162 mock_incr.assert_called_once_with(163 'flask_app.test.counter;tag=value;t2=v2;app=tests.test_statsd',164 1,165 rate=1,166 )167 with patch('statsd.StatsClient.incr') as mock_incr:168 statsd.incr('test.counter', tags={'tag': 'val', 't2': 'v2', 't3': None})169 mock_incr.assert_called_once_with(170 'flask_app.test.counter;tag=val;t2=v2;t3;app=tests.test_statsd',171 1,172 rate=1,173 )174 with patch('statsd.StatsClient.incr') as mock_incr:175 statsd.incr('test.counter', tags={'tag': 'val', 't2': None, 't3': 'v3'})176 mock_incr.assert_called_once_with(177 'flask_app.test.counter;tag=val;t2;t3=v3;app=tests.test_statsd',178 1,179 rate=1,180 )181 # Other separator characters are supported too182 app.config['STATSD_TAGS'] = ','183 with patch('statsd.StatsClient.incr') as mock_incr:184 statsd.incr('test.counter', tags={'tag': 'value', 't2': 'v2'})185 mock_incr.assert_called_once_with(186 'flask_app.test.counter,tag=value,t2=v2,app=tests.test_statsd',187 1,188 rate=1,189 )190def test_request_handler_notags(app, statsd, view):191 with patch('statsd.StatsClient.incr') as mock_incr:192 with patch('statsd.StatsClient.timing') as mock_timing:193 with app.test_client() as client:194 client.get('/')195 # First call196 mock_incr.assert_any_call(197 'flask_app.tests.test_statsd.request_handlers.endpoint_index'198 '.status_code_200',199 1,200 rate=1,201 )202 # Second and last call203 mock_incr.assert_called_with(204 'flask_app.tests.test_statsd.request_handlers.endpoint__overall'205 '.status_code_200',206 1,207 rate=1,208 )209 mock_timing.assert_called()210def test_request_handler_tags(app, statsd, view):211 app.config['STATSD_TAGS'] = ','212 with patch('statsd.StatsClient.incr') as mock_incr:213 with patch('statsd.StatsClient.timing') as mock_timing:214 with app.test_client() as client:215 client.get('/')216 mock_incr.assert_called_once_with(217 'flask_app.request_handlers,endpoint=index,status_code=200'218 ',app=tests.test_statsd',219 1,220 rate=1,221 )222 mock_timing.assert_called_once()223def test_request_handler_disabled(app, view):224 app.config['STATSD_REQUEST_LOG'] = False225 Statsd(app)226 with patch('statsd.StatsClient.incr') as mock_incr:227 with patch('statsd.StatsClient.timing') as mock_timing:228 with app.test_client() as client:229 client.get('/')230 mock_incr.assert_not_called()231 mock_timing.assert_not_called()232def test_form_success(ctx, app, statsd, form):233 app.config['STATSD_TAGS'] = ','234 with patch('statsd.StatsClient.incr') as mock_incr:235 form.field.data = "test"236 assert form.validate() is True237 mock_incr.assert_called_once_with(238 'flask_app.form_validation_success,form=SimpleForm,app=tests.test_statsd',239 1,240 rate=1,241 )242def test_form_error(ctx, app, statsd, form):243 app.config['STATSD_TAGS'] = ','244 with patch('statsd.StatsClient.incr') as mock_incr:245 form.field.data = None246 assert form.validate() is False247 mock_incr.assert_called_once_with(248 'flask_app.form_validation_error,form=SimpleForm,field=field,app=tests'249 '.test_statsd',250 1,251 rate=1,252 )253def test_form_nolog(ctx, app, statsd, form):254 app.config['STATSD_TAGS'] = ','255 app.config['STATSD_FORM_LOG'] = False256 with patch('statsd.StatsClient.incr') as mock_incr:257 form.field.data = 'test'258 assert form.validate() is True259 mock_incr.assert_not_called()260 form.field.data = False261 assert form.validate() is False262 mock_incr.assert_not_called()263def test_form_signals_off(ctx, app, statsd, form):264 app.config['STATSD_TAGS'] = ','265 app.config['STATSD_FORM_LOG'] = True266 with patch('statsd.StatsClient.incr') as mock_incr:267 form.field.data = 'test'268 assert form.validate(send_signals=False) is True269 mock_incr.assert_not_called()270 form.field.data = False271 assert form.validate(send_signals=False) is False...
dont_statsd.py
Source:dont_statsd.py
...16CARBON_PORT = 200317STATSD_PORT = 812518STATSD_HOST = CARBON_SERVER19import statsd 20def test_statsd():21 c = statsd.StatsClient(STATSD_HOST, STATSD_PORT)22 for i in range(1000):23 c.incr('rhaptos2.statsd.verify') 24if __name__ == '__main__':...
test_config.py
Source:test_config.py
1# -*- coding: utf-8 -*-2def test_statsd():3 from pytest_play.config import STATSD4 assert STATSD is True5def test_pytest_statsd():6 from pytest_play.config import PYTEST_STATSD...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!