Best Python code snippet using pytest-benchmark
dsdb_schema_attributes.py
Source:dsdb_schema_attributes.py  
1# -*- coding: utf-8 -*-2#3# Unix SMB/CIFS implementation.4# Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 20105#6# This program is free software; you can redistribute it and/or modify7# it under the terms of the GNU General Public License as published by8# the Free Software Foundation; either version 3 of the License, or9# (at your option) any later version.10#11# This program is distributed in the hope that it will be useful,12# but WITHOUT ANY WARRANTY; without even the implied warranty of13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the14# GNU General Public License for more details.15#16# You should have received a copy of the GNU General Public License17# along with this program.  If not, see <http://www.gnu.org/licenses/>.18#19#20# Usage:21#  export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun22#  PYTHONPATH="$PYTHONPATH:$samba4srcdir/dsdb/tests/python" $SUBUNITRUN dsdb_schema_attributes23#24import time25import random26import samba.tests27import ldb28from ldb import SCOPE_BASE, LdbError29class SchemaAttributesTestCase(samba.tests.TestCase):30    def setUp(self):31        super(SchemaAttributesTestCase, self).setUp()32        self.lp = samba.tests.env_loadparm()33        self.samdb = samba.tests.connect_samdb(self.lp.samdb_url())34        # fetch rootDSE35        res = self.samdb.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])36        self.assertEqual(len(res), 1)37        self.schema_dn = res[0]["schemaNamingContext"][0]38        self.base_dn = res[0]["defaultNamingContext"][0]39        self.forest_level = int(res[0]["forestFunctionality"][0])40    def tearDown(self):41        super(SchemaAttributesTestCase, self).tearDown()42    def _ldap_schemaUpdateNow(self):43        ldif = """44dn:45changetype: modify46add: schemaUpdateNow47schemaUpdateNow: 148"""49        self.samdb.modify_ldif(ldif)50    def _make_obj_names(self, prefix):51        obj_name = prefix + time.strftime("%s", time.gmtime())52        obj_ldap_name = obj_name.replace("-", "")53        obj_dn = "CN=%s,%s" % (obj_name, self.schema_dn)54        return (obj_name, obj_ldap_name, obj_dn)55    def _make_attr_ldif(self, attr_name, attr_dn, sub_oid, extra=None):56        ldif = """57dn: """ + attr_dn + """58objectClass: top59objectClass: attributeSchema60adminDescription: """ + attr_name + """61adminDisplayName: """ + attr_name + """62cn: """ + attr_name + """63attributeId: 1.3.6.1.4.1.7165.4.6.1.8.%d.""" % sub_oid + str(random.randint(1, 100000)) + """64attributeSyntax: 2.5.5.1265omSyntax: 6466instanceType: 467isSingleValued: TRUE68systemOnly: FALSE69"""70        if extra is not None:71            ldif += extra + "\n"72        return ldif73    def test_AddIndexedAttribute(self):74        # create names for an attribute to add75        (attr_name, attr_ldap_name, attr_dn) = self._make_obj_names("schemaAttributes-IdxAttr-")76        ldif = self._make_attr_ldif(attr_name, attr_dn, 1,77                                    "searchFlags: %d" % samba.dsdb.SEARCH_FLAG_ATTINDEX)78        # add the new attribute79        self.samdb.add_ldif(ldif)80        self._ldap_schemaUpdateNow()81        # Check @ATTRIBUTES82        attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)83        self.assertIn(attr_ldap_name, attr_res[0])84        self.assertEqual(len(attr_res[0][attr_ldap_name]), 1)85        self.assertEqual(str(attr_res[0][attr_ldap_name][0]), "CASE_INSENSITIVE")86        # Check @INDEXLIST87        idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)88        self.assertIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])89    def test_AddUnIndexedAttribute(self):90        # create names for an attribute to add91        (attr_name, attr_ldap_name, attr_dn) = self._make_obj_names("schemaAttributes-UnIdxAttr-")92        ldif = self._make_attr_ldif(attr_name, attr_dn, 2)93        # add the new attribute94        self.samdb.add_ldif(ldif)95        self._ldap_schemaUpdateNow()96        # Check @ATTRIBUTES97        attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)98        self.assertIn(attr_ldap_name, attr_res[0])99        self.assertEqual(len(attr_res[0][attr_ldap_name]), 1)100        self.assertEqual(str(attr_res[0][attr_ldap_name][0]), "CASE_INSENSITIVE")101        # Check @INDEXLIST102        idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)103        self.assertNotIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])104    def test_AddTwoIndexedAttributes(self):105        # create names for an attribute to add106        (attr_name, attr_ldap_name, attr_dn) = self._make_obj_names("schemaAttributes-2IdxAttr-")107        ldif = self._make_attr_ldif(attr_name, attr_dn, 3,108                                    "searchFlags: %d" % samba.dsdb.SEARCH_FLAG_ATTINDEX)109        # add the new attribute110        self.samdb.add_ldif(ldif)111        self._ldap_schemaUpdateNow()112        # create names for an attribute to add113        (attr_name2, attr_ldap_name2, attr_dn2) = self._make_obj_names("schemaAttributes-Attr-")114        ldif = self._make_attr_ldif(attr_name2, attr_dn2, 4,115                                    "searchFlags: %d" % samba.dsdb.SEARCH_FLAG_ATTINDEX)116        # add the new attribute117        self.samdb.add_ldif(ldif)118        self._ldap_schemaUpdateNow()119        # Check @ATTRIBUTES120        attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)121        self.assertIn(attr_ldap_name, attr_res[0])122        self.assertEqual(len(attr_res[0][attr_ldap_name]), 1)123        self.assertEqual(str(attr_res[0][attr_ldap_name][0]), "CASE_INSENSITIVE")124        self.assertIn(attr_ldap_name2, attr_res[0])125        self.assertEqual(len(attr_res[0][attr_ldap_name2]), 1)126        self.assertEqual(str(attr_res[0][attr_ldap_name2][0]), "CASE_INSENSITIVE")127        # Check @INDEXLIST128        idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)129        self.assertIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])130        self.assertIn(attr_ldap_name2, [str(x) for x in idx_res[0]["@IDXATTR"]])131    def test_modify_at_attributes(self):132        m = {"dn": "@ATTRIBUTES",133             "@TEST_EXTRA": ["HIDDEN"]134             }135        msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_ADD)136        self.samdb.modify(msg)137        res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE,138                                attrs=["@TEST_EXTRA"])139        self.assertEqual(len(res), 1)140        self.assertEqual(str(res[0].dn), "@ATTRIBUTES")141        self.assertEqual(len(res[0]), 1)142        self.assertTrue("@TEST_EXTRA" in res[0])143        self.assertEqual(len(res[0]["@TEST_EXTRA"]), 1)144        self.assertEqual(str(res[0]["@TEST_EXTRA"][0]), "HIDDEN")145        samdb2 = samba.tests.connect_samdb(self.lp.samdb_url())146        # We now only update the @ATTRIBUTES when a transaction happens147        # rather than making a read of the DB do writes.148        #149        # This avoids locking issues and is more expected150        samdb2.transaction_start()151        samdb2.transaction_commit()152        res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE,153                                attrs=["@TEST_EXTRA"])154        self.assertEqual(len(res), 1)155        self.assertEqual(str(res[0].dn), "@ATTRIBUTES")156        self.assertEqual(len(res[0]), 0)157        self.assertFalse("@TEST_EXTRA" in res[0])158    def test_modify_at_indexlist(self):159        m = {"dn": "@INDEXLIST",160             "@TEST_EXTRA": ["1"]161             }162        msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_ADD)163        self.samdb.modify(msg)164        res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE,165                                attrs=["@TEST_EXTRA"])166        self.assertEqual(len(res), 1)167        self.assertEqual(str(res[0].dn), "@INDEXLIST")168        self.assertEqual(len(res[0]), 1)169        self.assertTrue("@TEST_EXTRA" in res[0])170        self.assertEqual(len(res[0]["@TEST_EXTRA"]), 1)171        self.assertEqual(str(res[0]["@TEST_EXTRA"][0]), "1")172        samdb2 = samba.tests.connect_samdb(self.lp.samdb_url())173        # We now only update the @INDEXLIST when a transaction happens174        # rather than making a read of the DB do writes.175        #176        # This avoids locking issues and is more expected177        samdb2.transaction_start()178        samdb2.transaction_commit()179        res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE,180                                attrs=["@TEST_EXTRA"])181        self.assertEqual(len(res), 1)182        self.assertEqual(str(res[0].dn), "@INDEXLIST")183        self.assertEqual(len(res[0]), 0)184        self.assertFalse("@TEST_EXTRA" in res[0])185    def test_modify_fail_of_at_indexlist(self):186        m = {"dn": "@INDEXLIST",187             "@TEST_NOT_EXTRA": ["1"]188             }189        msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_DELETE)190        try:191            self.samdb.modify(msg)192            self.fail("modify of @INDEXLIST with a failed constraint should fail")193        except LdbError as err:194            enum = err.args[0]...test_extra.py
Source:test_extra.py  
...54        self.create_test_user(auth=False)55        response = self.delete_test_extra_api()56        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)57    @staticmethod58    def create_test_extra():59        try:60            test_extra = Extra.objects.get(name='Extrawurst')61        except Extra.DoesNotExist:62            test_extra = Extra(name='Extrawurst', points=100,63                               deadline=timezone.now() + timedelta(days=5), result='Salami')64            test_extra.save()65        return test_extra66    def get_test_extra_api(self):67        test_extra = self.create_test_extra()68        return self.client.get('%s%i/' % (self.EXTRAS_BASEURL, test_extra.pk))69    def create_test_extra_api(self):70        return self.client.post(self.EXTRAS_BASEURL,71                                {'name': 'Extrawurst', 'points': 100, 'deadline': '2016-06-06 18:00:00+0000',72                                 'result': 'Salami'}, format='json')73    def update_test_extra_api(self):74        test_extra = self.create_test_extra()75        return self.client.put("%s%i/" % (self.EXTRAS_BASEURL, test_extra.pk),76                               {'name': 'Sonderbehandlung', 'points': 101, 'deadline': '2016-08-08 20:00:00+0000',77                                'result': 'Schinkenwurst'}, format='json')78    def delete_test_extra_api(self):79        test_extra = self.create_test_extra()...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!!
