Best Python code snippet using molecule_python
test_utils.py
Source:test_utils.py
...5import numpy as np6import pandas as pd7from tests.helper_functions import read_test_data8should_days_in_month = np.array([31., 29., 31., 30., 31., 30., 31., 31., 30., 31., 30., 31.])9def test_merge_dicts():10 d1 = {0:0, 1:1}11 d2 = {2:2, 3:3}12 d = merge_dicts(d1, d2)13 for i, (k, v) in enumerate(d.items()):14 assert k == v == i15def test_conditional_temp_resample():16 ds = pd.Series(index=pd.date_range(start='2000-01-01', end='2000-12-31', freq='D'),17 data=[1.]*366)18 assert dt_freq(ds.index) == (1., 'D')19 resampled, count = conditional_temp_resample(ds, 'M', threshold=0.1)20 assert dt_freq(resampled.index) == (1., 'M')21 np.testing.assert_almost_equal(count['count_is'], should_days_in_month)22 np.testing.assert_almost_equal(count['count_should'], should_days_in_month / 10.)23 assert all(resampled.values == 1.)24def test_df_conditional_temp_resample():25 df = pd.DataFrame(index=pd.date_range(start='2000-01-01', end='2000-12-31', freq='D'),26 data={'data1': [1.] * 366, 'data2': [10.] * 366})27 assert dt_freq(df.index) == (1., 'D')28 resampled = df_conditional_temp_resample(df, 'M', resample_threshold=0.1)29 assert dt_freq(df.index) == (1., 'D')30 assert dt_freq(resampled.index) == (1., 'M')31 assert all(resampled['data1'].values == 1.)32 assert all(resampled['data2'].values == 10.)33def test_filter_by_qunatiles():34 df = pd.DataFrame(index=range(10), data={'data':[1,5,5,5,5,5,5,5,5,10]})35 filter_mask = filter_by_quantiles(df, 'data', 0.1, 0.9)36 assert filter_mask.iloc[0] == 137 assert filter_mask.iloc[-1] == 138 np.testing.assert_equal(filter_mask.iloc[1:-1].values, np.array([0,0,0,0,0,0,0,0,]))39def test_autocorr():40 df = pd.Series(index=range(10), data=[0,1] * 5)41 ac = autocorr(df, lag=[0,1,2])42 np.testing.assert_almost_equal([1, -1 , 1], ac, decimal=5)43def test_crosscorr():44 df = pd.Series(index=range(10), data=[0,1] * 5)45 cc = crosscorr(df, df, lag=[0,1,2])46 np.testing.assert_almost_equal([1, -1 , 1], cc, decimal=5)47def test_fatten_dict():48 test_dict = {'dict1': {'d1k1' : 'v1', 'd1k2' : 'v2'},49 'dict2': {'d2k1' : 'v3', 'd2k2' : 'v4'} }50 flat_dict = flatten_dict(test_dict)51 assert(flat_dict['dict1_d1k1'] == 'v1')52 assert(flat_dict['dict1_d1k2'] == 'v2')53 assert(flat_dict['dict2_d2k1'] == 'v3')54 assert(flat_dict['dict2_d2k2'] == 'v4')55def test_days_in_month():56 dim = should_days_in_month.copy()57 for year in [2016, 2017]: # leap, no leap58 days = days_in_month(month=range(1,13), year=year, astype=float)59 if year == 2017:60 dim[1] = 28.61 else:62 dim[1] = 29.63 np.testing.assert_equal(days, dim)64def test_midmonth_target_values():65 M = np.array([1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.])66 t = mid_month_target_values(M)67 np.testing.assert_almost_equal(t, M)68def test_dt_freq():69 ts = read_test_data(654079)70 assert dt_freq(ts.index) == (1., 'D')71 assert dt_freq(ts['CCI'].dropna().resample('M').mean().index) == (1., 'M')72if __name__ == '__main__':73 test_merge_dicts()74 test_conditional_temp_resample()75 test_df_conditional_temp_resample()76 test_filter_by_qunatiles()77 test_autocorr()78 test_crosscorr()79 test_fatten_dict()80 test_days_in_month()81 test_midmonth_target_values()...
config_test.py
Source:config_test.py
2# stdlib imports3import os4import json5from gmprocess.config import merge_dicts6def test_merge_dicts():7 test_subject = [8 {9 "a": "yes",10 "b": [0, 1, 2],11 "c": 1.0,12 "d": 2.0,13 "e": {14 "e.1": 2,15 "e.2": [1, 2],16 "e.3": {17 "e.3.1": "a",18 "e.3.2": 12.0,19 },20 },21 "h": {22 "h.1": 10.0,23 "h.2": 11.0,24 },25 },26 {27 "c": 1.1, # change value28 "e": {29 "e.2": [1, 2, 3], # change array30 },31 },32 {33 "d": { # change value to dict34 "d.1": 2.2,35 "d.2": 3.3,36 },37 "e": { # change value in nested dict38 "e.3": {39 "e.3.1": "ccc",40 },41 },42 },43 {44 "f": 4.4, # add value45 },46 {47 "g": { # add nested dict48 "g.1": 2,49 "g.2": 3,50 },51 "h": 0.1, # change dict to value52 },53 ]54 result_expected = {55 "a": "yes",56 "b": [0, 1, 2],57 "c": 1.1,58 "d": {59 "d.1": 2.2,60 "d.2": 3.3,61 },62 "e": {63 "e.1": 2,64 "e.2": [1, 2, 3],65 "e.3": {66 "e.3.1": "ccc",67 "e.3.2": 12.0,68 },69 },70 "f": 4.4,71 "g": {72 "g.1": 2,73 "g.2": 3,74 },75 "h": 0.1,76 }77 test_result = merge_dicts(test_subject)78 dump_expected = json.dumps(result_expected, sort_keys=True)79 dump_result = json.dumps(test_result, sort_keys=True)80 assert(dump_expected == dump_result)81if __name__ == '__main__':82 os.environ['CALLED_FROM_PYTEST'] = 'True'...
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!!