Best Python code snippet using autotest_python
test_sums.py
Source:test_sums.py  
1import pandas as pd2import unittest3import datetime4from dwapi import datawiz5import random6import pandas as pd7from dwapi.test.settings import SettingsManager8class TestMain(unittest.TestCase):9    10    TEST_SETTINGS = SettingsManager()11    dw = TEST_SETTINGS.USER12    def setUp(self):13        pass14 15    def tearDown(self):16        pass17    def assertEqual(self, x, y, rnd=2):18        # assert round(x,2)==round(y,2)19        return super(TestMain, self).assertEqual(round(x, rnd), round(y,rnd))20    def test_get_products_sale(self):21        interval  = self.TEST_SETTINGS.get_sample_interval()22        products = self.TEST_SETTINGS.get_samples('products')23        shops = self.TEST_SETTINGS.get_samples('shops')24        df = self.dw.get_products_sale(products = products,25                                by='turnover',26                                shops = shops,27                                date_from = interval[0],28                                date_to = interval[1],29                                interval = datawiz.DAYS)30        31        list_colums = df.columns32        if df.empty:33            return True34        result = 035        for d_f,d_t in self.TEST_SETTINGS.split_date_range(interval):36            df_periods = self.dw.get_products_sale(products = products,by='turnover',37                                    shops = shops,38                                    date_from = d_f,39                                    date_to = d_t,40                                    interval = datawiz.DAYS)41            result += df_periods.sum().sum()42        self.assertEqual(df.sum().sum() , result)43        44    def test_get_categories_sale(self):45        categories = self.TEST_SETTINGS.get_samples('categories')46        interval = self.TEST_SETTINGS.get_sample_interval()47        df = self.dw.get_categories_sale(categories = categories,by='turnover', 48                                date_from = interval[0],49                                date_to = interval[1],50                                interval = datawiz.DAYS 51                                )52        list_colums = df.columns53        if df.empty:54            return True55        result = 056        for d_f,d_t in self.TEST_SETTINGS.split_date_range(interval):57            df_periods = self.dw.get_categories_sale(categories = categories,by='turnover', 58                                date_from = d_f,59                                date_to = d_t,60                                interval = datawiz.DAYS )61            62            result += df_periods[list_colums].sum().sum()63        self.assertEqual(df.sum().sum() , result)64    65    def test_get_products_stock(self):66        products = self.TEST_SETTINGS.get_samples('products')67        shops = self.TEST_SETTINGS.get_samples('shops')68        interval = self.TEST_SETTINGS.get_sample_interval()69        df = self.dw.get_products_stock(products = products, by='stock_value',70                                                shops = shops,71                                                date_from = interval[0],72                                                date_to = interval[1],73                                                )74        75        list_colums = df.columns76        if df.empty:77            return True78        result = 079        for d_f,d_t in self.TEST_SETTINGS.split_date_range(interval):80            df_periods = self.dw.get_products_stock(products = products, by='stock_value',81                                                shops = shops,82                                                date_from = d_f,83                                                date_to = d_t,84                                                )85            86            result += df_periods.sum().sum()87        self.assertEqual(df.sum().sum() , result)88        89    def test_get_categories_stock(self):90        categories = self.TEST_SETTINGS.get_samples('categories')91        shops = self.TEST_SETTINGS.get_samples('shops')92        interval = self.TEST_SETTINGS.get_sample_interval()93        df = self.dw.get_categories_stock(categories = categories, by='stock_value',94                                                shops = shops,95                                                date_from = interval[0],96                                                date_to = interval[1],97                                                )98        list_colums = df.columns99        if df.empty:100            return True101        result = 0102        for d_f,d_t in self.TEST_SETTINGS.split_date_range(interval):103            df_periods = self.dw.get_categories_stock(categories = categories, by='stock_value',104                                                shops =shops ,105                                                date_from = d_f,106                                                date_to = d_t,107                                                )108            result += df_periods.sum().sum()109        self.assertEqual(df.sum().sum() , result)110        111    def test_get_lost_sales(self):112        category = self.TEST_SETTINGS.get_sample('categories')113        shops = self.TEST_SETTINGS.get_samples('shops')114        interval = self.TEST_SETTINGS.get_sample_interval()115        df = self.dw.get_lost_sales(category = category,116                                    shops = shops,117                                    date_from = interval[0],118                                    date_to = interval[1],119                                    )120        if df.empty:121            return True122        df = df[[u'Avg sales' , u'Losing days' , u'Losing turnover' , u'Lost sales quantity']]123        list_colums = df.columns124        result = 0125        for d_f,d_t in self.TEST_SETTINGS.split_date_range(interval):126            df_periods = self.dw.get_lost_sales(category = category,127                                                shops = shops,128                                                date_from = d_f,129                                                date_to = d_t,130                                                )131            result += df_periods[list_colums].sum().sum()132        self.assertEqual(df.sum().sum() , result)133    def test_get_sales(self):134        interval = self.TEST_SETTINGS.get_sample_interval()135        df =  self.dw.get_sales(136                    date_from = interval[0],137                    date_to = interval[1],138        )139        if df.empty:140            return True141        result = 0142        for d_f,d_t in self.TEST_SETTINGS.split_date_range(interval):143            df_periods = self.dw.get_sales(144                                date_from = d_f,145                                date_to = d_t,146                                )147            result += df_periods["turnover"].sum()148        self.assertEqual(df["turnover"].sum() , result)149    150    151    def test_get_loyalty_sales(self):152        153        interval = self.TEST_SETTINGS.get_sample_interval()154        shops = self.TEST_SETTINGS.get_samples('shops')155        df = self.dw.get_loyalty_sales(156                shops = shops,157                date_from = interval[0],158                date_to = interval[1],159                )160        if df.empty:161            return True162        list_colums = df.columns163        result = 0164        for d_f,d_t in self.TEST_SETTINGS.split_date_range(interval):165            df_periods = self.dw.get_loyalty_sales(166                                                    shops = shops,167                                                    date_from = d_f,168                                                    date_to = d_t,169                                                    )170            result += df_periods["turnover"].sum()171        self.assertEqual(df["turnover"].sum() , result/2)...test_pypispy.py
Source:test_pypispy.py  
1"""Tests for the ``PyPiSpy`` server side script."""2import json3import mock4import unittest5from pypispy import PyPiSpyAgent6import test_settings7def create_agent():8    agent = PyPiSpyAgent(9        test_settings.SERVER_NAME,10        test_settings.VENVS,11        test_settings.VENVS_DIR,12        test_settings.API_KEY,13        test_settings.API_URL,)14    return agent15class FakeResponse(mock.Mock):16    """17    This is a substitution class for the ``call_url`` method of PyPiSpyAgent18    """19    status_code = mock.Mock()20    read = mock.Mock()21class PyPiSpyAgentTestCase(unittest.TestCase):22    """Tests for the ``PyPiSpyAgent`` class."""23    def setUp(self):24        self.expected_package_list = 'argparse==1.2.1\n-e hg+http://bitbucket.org/mbrochh/pypispy-bitbucket-test@788deeeeb6324bd9e042686ef8f436ad61be4f72#egg=pypispy_bitbucket_test-dev\n-e git://github.com/bitmazk/pypispy-github-test.git@15cea4f850b12e9bfb74b361e83780d180102d5c#egg=pypispy_github_test-dev\npypispy-pypi-test==0.1\nwsgiref==0.1.2\n'  # NOQA25    def test_init(self):26        agent = create_agent()27        self.assertEqual(agent.server_name, test_settings.SERVER_NAME,28            msg="Init should set the server name to '%s' but got '%s'"\29                % (test_settings.SERVER_NAME, agent.server_name))30        self.assertEqual(agent.venvs, test_settings.VENVS,31            msg="Init should set the virtual environments to '%s' but got '%s'"\32                % (test_settings.VENVS, agent.venvs))33        self.assertEqual(agent.venvs_dir, test_settings.VENVS_DIR,34            msg="Init should set the virtual environment directory to '%s' but\35                got '%s'" % (test_settings.VENVS_DIR, agent.venvs_dir))36        self.assertEqual(agent.api_key, test_settings.API_KEY,37            msg="Init should set the api key to '%s' but got '%s'"\38                % (test_settings.API_KEY, agent.api_key))39    def test_call_pypispy_api(self):  # TODO40        # create the agent and mock out the API call41        old_call_url = PyPiSpyAgent.call_url42        PyPiSpyAgent.call_url = FakeResponse()43        agent = create_agent()44        agent.call_url.read.return_value = 'success'45        data = {}46        # if response is not 'success' it should raise an exception47        agent.call_url.read.return_value = json.dumps(48            {'TestMessage': 'Test message to test this message.'})49        self.assertRaises(Exception, agent.call_pypispy_api, (),50            (data, agent.venvs[0]))51        PyPiSpyAgent.call_url = old_call_url52    def test_get_package_list(self):53        agent = create_agent()54        package_list = agent.get_package_list(agent.venvs[0])55        self.assertEqual(package_list, self.expected_package_list)56    def test_inspect_venv(self):57        agent = PyPiSpyAgent(58            test_settings.SERVER_NAME,59            test_settings.VENVS,60            test_settings.VENVS_DIR,61            test_settings.API_KEY)62        data = agent.inspect_venv(agent.venvs[0])63        server_name = data.get('server_name')64        api_key = data.get('api_key')65        package_info = data.get('package_info')66        self.assertEqual(test_settings.SERVER_NAME, server_name,67            msg="Expected server name to be '%s' but go '%s'"\68                % (test_settings.SERVER_NAME, server_name))69        self.assertEqual(test_settings.API_KEY, api_key,70            msg="Expected api key to be '%s' but go '%s'"\71                % (test_settings.API_KEY, api_key))72        self.assertEqual(package_info, self.expected_package_list,73            msg="Got wrong package info")74    def test_run(self):75        old_call_pypispy_api = PyPiSpyAgent.call_pypispy_api76        PyPiSpyAgent.call_pypispy_api = mock.Mock()77        agent = PyPiSpyAgent(78            test_settings.SERVER_NAME,79            test_settings.VENVS,80            test_settings.VENVS_DIR,81            test_settings.API_KEY)82        agent.run()83        # test if called for each virtual environment84        self.assertEqual(agent.call_pypispy_api.call_count,85            len(test_settings.VENVS),86            msg="Call count doesn't match number of environments. \87                Expected '%s' but got '%s'" % (88                len(test_settings.VENVS),89                agent.call_pypispy_api.call_count)90            )...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!!
