Best Python code snippet using pandera_python
immutable.py
Source:immutable.py
1# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Rickard Lindberg, Roger Lindberg2#3# This file is part of Timeline.4#5# Timeline is free software: you can redistribute it and/or modify6# it under the terms of the GNU General Public License as published by7# the Free Software Foundation, either version 3 of the License, or8# (at your option) any later version.9#10# Timeline is distributed in the hope that it will be useful,11# but WITHOUT ANY WARRANTY; without even the implied warranty of12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13# GNU General Public License for more details.14#15# You should have received a copy of the GNU General Public License16# along with Timeline. If not, see <http://www.gnu.org/licenses/>.17from timelinelib.canvas.data.immutable import ImmutableCategory18from timelinelib.canvas.data.immutable import ImmutableContainer19from timelinelib.canvas.data.immutable import ImmutableDB20from timelinelib.canvas.data.immutable import ImmutableEra21from timelinelib.canvas.data.immutable import ImmutableEvent22from timelinelib.canvas.data.immutable import ImmutableMilestone23from timelinelib.canvas.data.immutable import InvalidOperationError24from timelinelib.general.immutable import ImmutableDict25from timelinelib.test.cases.unit import UnitTestCase26class DBTestCase(UnitTestCase):27 def assertDifferentIdentity(self, o1, o2):28 self.assertEqual(type(o1), type(o2))29 self.assertFalse(o1 is o2)30class describe_saving_event(DBTestCase):31 def test_db_is_not_mutated(self):32 db1 = ImmutableDB()33 db2 = db1.save_event(ImmutableEvent(), 1)34 self.assertDifferentIdentity(db1, db2)35 def test_event_is_added(self):36 db = ImmutableDB()37 db = db.save_category(ImmutableCategory(name="work"), 1)38 db = db.save_event(ImmutableEvent(text="meeting", category_id=1), 2)39 self.assertEqual(db, ImmutableDB(40 categories=ImmutableDict({41 1: ImmutableCategory(name="work"),42 }),43 events=ImmutableDict({44 2: ImmutableEvent(text="meeting", category_id=1),45 }),46 ))47 def test_event_is_updated(self):48 db = ImmutableDB()49 db = db.save_event(ImmutableEvent(text="meeting"), 1)50 db = db.save_event(ImmutableEvent(text="lunch"), 1)51 self.assertEqual(db, ImmutableDB(52 events=ImmutableDict({53 1: ImmutableEvent(text="lunch"),54 }),55 ))56 def test_fails_if_category_does_not_exist(self):57 db = ImmutableDB()58 self.assertRaisesRegexp(59 InvalidOperationError,60 r"^Category with id 99 does not exist$",61 db.save_event, ImmutableEvent(category_id=99), 162 )63 def test_fails_if_container_does_not_exist(self):64 db = ImmutableDB()65 self.assertRaisesRegexp(66 InvalidOperationError,67 r"^Container with id 99 does not exist$",68 db.save_event, ImmutableEvent(container_id=99), 169 )70class describe_deleting_event(DBTestCase):71 def test_db_is_not_mutated(self):72 db1 = ImmutableDB().save_event(ImmutableEvent(), 1)73 db2 = db1.delete_event(1)74 self.assertDifferentIdentity(db1, db2)75 def test_event_is_removed(self):76 db = ImmutableDB()77 db = db.save_event(ImmutableEvent(text="meeting"), 1)78 db = db.save_event(ImmutableEvent(text="football"), 2)79 db = db.delete_event(1)80 self.assertEqual(db, ImmutableDB(81 events=ImmutableDict({82 2: ImmutableEvent(text="football"),83 }),84 ))85 def test_fails_if_event_does_not_exist(self):86 db = ImmutableDB()87 self.assertRaisesRegexp(88 InvalidOperationError,89 r"^Event with id 99 does not exist$",90 db.delete_event, 9991 )92class describe_saving_category(DBTestCase):93 def test_db_is_not_mutated(self):94 db1 = ImmutableDB()95 db2 = db1.save_category(ImmutableCategory(), 1)96 self.assertDifferentIdentity(db1, db2)97 def test_category_is_added(self):98 db = ImmutableDB()99 db = db.save_category(ImmutableCategory(name="foo"), 1)100 db = db.save_category(ImmutableCategory(name="bar"), 2)101 self.assertEqual(db, ImmutableDB(102 categories=ImmutableDict({103 1: ImmutableCategory(name="foo"),104 2: ImmutableCategory(name="bar"),105 })106 ))107 def test_category_is_updated(self):108 db = ImmutableDB()109 db = db.save_category(ImmutableCategory(name="foo"), 1)110 db = db.save_category(ImmutableCategory(name="bar"), 2)111 db = db.save_category(ImmutableCategory(name="foo2"), 1)112 self.assertEqual(db, ImmutableDB(113 categories=ImmutableDict({114 1: ImmutableCategory(name="foo2"),115 2: ImmutableCategory(name="bar"),116 })117 ))118 def test_fails_if_name_exists(self):119 db = ImmutableDB()120 db = db.save_category(ImmutableCategory(name="foo"), 1)121 self.assertRaisesRegexp(122 InvalidOperationError,123 r"^Category name 'foo' is not unique$",124 db.save_category, ImmutableCategory(name="foo"), 2125 )126 def test_fails_if_parent_does_not_exist(self):127 db = ImmutableDB()128 self.assertRaisesRegexp(129 InvalidOperationError,130 r"^Category with id 99 does not exist$",131 db.save_category, ImmutableCategory(parent_id=99), 1132 )133 def test_fails_if_parent_creates_circular_reference(self):134 db = ImmutableDB()135 db = db.save_category(ImmutableCategory(name="root"), 1)136 db = db.save_category(ImmutableCategory(name="child", parent_id=1), 2)137 self.assertRaisesRegexp(138 InvalidOperationError,139 r"^Circular category parent$",140 db.save_category, ImmutableCategory(parent_id=2), 1141 )142class describe_deleting_category(DBTestCase):143 def test_db_is_not_mutated(self):144 db1 = ImmutableDB().save_category(ImmutableCategory(name="work"), 1)145 db2 = db1.delete_category(1)146 self.assertDifferentIdentity(db1, db2)147 def test_category_is_removed(self):148 db = ImmutableDB()149 db = db.save_category(ImmutableCategory(name="work"), 1)150 db = db.save_category(ImmutableCategory(name="play"), 2)151 db = db.delete_category(1)152 self.assertEqual(db, ImmutableDB(153 categories=ImmutableDict({154 2: ImmutableCategory(name="play"),155 }),156 ))157 def test_category_parent_id_attribute_is_updated(self):158 db = ImmutableDB()159 db = db.save_category(ImmutableCategory(name="root"), 1)160 db = db.save_category(ImmutableCategory(name="sub", parent_id=1), 2)161 db = db.save_category(ImmutableCategory(name="child1", parent_id=2), 3)162 db = db.save_category(ImmutableCategory(name="child2", parent_id=2), 4)163 db = db.delete_category(2)164 self.assertEqual(db, ImmutableDB(165 categories=ImmutableDict({166 1: ImmutableCategory(name="root"),167 3: ImmutableCategory(name="child1", parent_id=1),168 4: ImmutableCategory(name="child2", parent_id=1),169 }),170 ))171 def test_event_category_id_attribute_is_updated(self):172 db = ImmutableDB()173 db = db.save_category(ImmutableCategory(name="root"), 1)174 db = db.save_category(ImmutableCategory(name="sub", parent_id=1), 2)175 db = db.save_event(ImmutableEvent(text="lunch", category_id=2), 3)176 db = db.save_event(ImmutableEvent(text="dinner", category_id=2), 4)177 db = db.delete_category(2)178 self.assertEqual(db, ImmutableDB(179 categories=ImmutableDict({180 1: ImmutableCategory(name="root"),181 }),182 events=ImmutableDict({183 3: ImmutableEvent(text="lunch", category_id=1),184 4: ImmutableEvent(text="dinner", category_id=1),185 }),186 ))187 def test_milestone_category_id_attribute_is_updated(self):188 db = ImmutableDB()189 db = db.save_category(ImmutableCategory(name="root"), 1)190 db = db.save_category(ImmutableCategory(name="sub", parent_id=1), 2)191 db = db.save_milestone(ImmutableMilestone(text="lunch", category_id=2), 3)192 db = db.save_milestone(ImmutableMilestone(text="dinner", category_id=2), 4)193 db = db.delete_category(2)194 self.assertEqual(db, ImmutableDB(195 categories=ImmutableDict({196 1: ImmutableCategory(name="root"),197 }),198 milestones=ImmutableDict({199 3: ImmutableMilestone(text="lunch", category_id=1),200 4: ImmutableMilestone(text="dinner", category_id=1),201 }),202 ))203 def test_container_category_id_attribute_is_updated(self):204 db = ImmutableDB()205 db = db.save_category(ImmutableCategory(name="root"), 1)206 db = db.save_category(ImmutableCategory(name="sub", parent_id=1), 2)207 db = db.save_container(ImmutableContainer(text="lunch", category_id=2), 3)208 db = db.save_container(ImmutableContainer(text="dinner", category_id=2), 4)209 db = db.delete_category(2)210 self.assertEqual(db, ImmutableDB(211 categories=ImmutableDict({212 1: ImmutableCategory(name="root"),213 }),214 containers=ImmutableDict({215 3: ImmutableContainer(text="lunch", category_id=1),216 4: ImmutableContainer(text="dinner", category_id=1),217 }),218 ))219 def test_fails_if_category_does_not_exist(self):220 db = ImmutableDB()221 self.assertRaisesRegexp(222 InvalidOperationError,223 r"^Category with id 99 does not exist$",224 db.delete_category, 99225 )226class describe_saving_era(DBTestCase):227 def test_db_is_not_mutated(self):228 db1 = ImmutableDB()229 db2 = db1.save_era(ImmutableEra(), 1)230 self.assertDifferentIdentity(db1, db2)231 def test_era_is_added(self):232 db = ImmutableDB()233 db = db.save_era(ImmutableEra(name="present"), 1)234 db = db.save_era(ImmutableEra(name="past"), 2)235 self.assertEqual(db, ImmutableDB(236 eras=ImmutableDict({237 1: ImmutableEra(name="present"),238 2: ImmutableEra(name="past"),239 }),240 ))241 def test_era_is_updated(self):242 db = ImmutableDB()243 db = db.save_era(ImmutableEra(name="present"), 1)244 db = db.save_era(ImmutableEra(name="past"), 1)245 self.assertEqual(db, ImmutableDB(246 eras=ImmutableDict({247 1: ImmutableEra(name="past"),248 }),249 ))250class describe_deleting_era(DBTestCase):251 def test_db_is_not_mutated(self):252 db1 = ImmutableDB().save_era(ImmutableEra(), 1)253 db2 = db1.delete_era(1)254 self.assertDifferentIdentity(db1, db2)255 def test_era_is_removed(self):256 db = ImmutableDB()257 db = db.save_era(ImmutableEra(name="past"), 1)258 db = db.save_era(ImmutableEra(name="present"), 2)259 db = db.delete_era(1)260 self.assertEqual(db, ImmutableDB(261 eras=ImmutableDict({262 2: ImmutableEra(name="present"),263 }),264 ))265 def test_fails_if_era_does_not_exist(self):266 db = ImmutableDB()267 self.assertRaisesRegexp(268 InvalidOperationError,269 r"^Era with id 99 does not exist$",270 db.delete_era, 99271 )272class describe_saving_milestone(DBTestCase):273 def test_db_is_not_mutated(self):274 db1 = ImmutableDB()275 db2 = db1.save_milestone(ImmutableMilestone(), 1)276 self.assertDifferentIdentity(db1, db2)277 def test_milestone_is_added(self):278 db = ImmutableDB()279 db = db.save_category(ImmutableCategory(name="work"), 1)280 db = db.save_milestone(ImmutableMilestone(text="release", category_id=1), 2)281 self.assertEqual(db, ImmutableDB(282 categories=ImmutableDict({283 1: ImmutableCategory(name="work"),284 }),285 milestones=ImmutableDict({286 2: ImmutableMilestone(text="release", category_id=1),287 }),288 ))289 def test_milestone_is_updated(self):290 db = ImmutableDB()291 db = db.save_milestone(ImmutableMilestone(text="release"), 1)292 db = db.save_milestone(ImmutableMilestone(text="alpha"), 1)293 self.assertEqual(db, ImmutableDB(294 milestones=ImmutableDict({295 1: ImmutableMilestone(text="alpha"),296 }),297 ))298 def test_fails_if_category_does_not_exist(self):299 db = ImmutableDB()300 self.assertRaisesRegexp(301 InvalidOperationError,302 r"^Category with id 99 does not exist$",303 db.save_milestone, ImmutableMilestone(category_id=99), 1304 )305class describe_deleting_milestone(DBTestCase):306 def test_db_is_not_mutated(self):307 db1 = ImmutableDB().save_milestone(ImmutableMilestone(text="release"), 1)308 db2 = db1.delete_milestone(1)309 self.assertDifferentIdentity(db1, db2)310 def test_milestone_is_removed(self):311 db = ImmutableDB()312 db = db.save_milestone(ImmutableMilestone(text="release"), 1)313 db = db.save_milestone(ImmutableMilestone(text="alpha"), 2)314 db = db.delete_milestone(1)315 self.assertEqual(db, ImmutableDB(316 milestones=ImmutableDict({317 2: ImmutableMilestone(text="alpha"),318 }),319 ))320 def test_fails_if_milestone_does_not_exist(self):321 db = ImmutableDB()322 self.assertRaisesRegexp(323 InvalidOperationError,324 r"^Milestone with id 99 does not exist$",325 db.delete_milestone, 99326 )327class describe_saving_container(DBTestCase):328 def test_db_is_not_mutated(self):329 db1 = ImmutableDB()330 db2 = db1.save_container(ImmutableContainer(), 1)331 self.assertDifferentIdentity(db1, db2)332 def test_container_is_added(self):333 db = ImmutableDB()334 db = db.save_category(ImmutableCategory(name="work"), 1)335 db = db.save_container(ImmutableContainer(text="foo"), 2)336 db = db.save_container(ImmutableContainer(text="bar", category_id=1), 3)337 self.assertEqual(db, ImmutableDB(338 categories=ImmutableDict({339 1: ImmutableCategory(name="work"),340 }),341 containers=ImmutableDict({342 2: ImmutableContainer(text="foo"),343 3: ImmutableContainer(text="bar", category_id=1),344 })345 ))346 def test_container_is_updated(self):347 db = ImmutableDB()348 db = db.save_container(ImmutableContainer(text="foo"), 1)349 db = db.save_container(ImmutableContainer(text="bar"), 2)350 db = db.save_container(ImmutableContainer(text="bar2"), 2)351 self.assertEqual(db, ImmutableDB(352 containers=ImmutableDict({353 1: ImmutableContainer(text="foo"),354 2: ImmutableContainer(text="bar2"),355 })356 ))357 def test_fails_if_category_does_not_exist(self):358 db = ImmutableDB()359 self.assertRaisesRegexp(360 InvalidOperationError,361 r"^Category with id 99 does not exist$",362 db.save_container, ImmutableContainer(category_id=99), 1363 )364class describe_deleting_container(DBTestCase):365 def test_db_is_not_mutated(self):366 db1 = ImmutableDB().save_container(ImmutableContainer(text="group"), 1)367 db2 = db1.delete_container(1)368 self.assertDifferentIdentity(db1, db2)369 def test_container_is_removed(self):370 db = ImmutableDB()371 db = db.save_container(ImmutableContainer(text="foo"), 1)372 db = db.save_container(ImmutableContainer(text="bar"), 2)373 db = db.delete_container(1)374 self.assertEqual(db, ImmutableDB(375 containers=ImmutableDict({376 2: ImmutableContainer(text="bar"),377 })378 ))379 def test_event_container_id_attribute_is_updated(self):380 db = ImmutableDB()381 db = db.save_container(ImmutableContainer(text="group"), 1)382 db = db.save_event(ImmutableEvent(text="lunch", container_id=1), 2)383 db = db.save_event(ImmutableEvent(text="dinner", container_id=1), 3)384 db = db.delete_container(1)385 self.assertEqual(db, ImmutableDB(386 events=ImmutableDict({387 2: ImmutableEvent(388 text="lunch",389 container_id=None390 ),391 3: ImmutableEvent(392 text="dinner",393 container_id=None394 ),395 }),396 ))397 def test_fails_if_container_does_not_exist(self):398 db = ImmutableDB()399 self.assertRaisesRegexp(400 InvalidOperationError,401 r"^Container with id 99 does not exist$",402 db.delete_container, 99...
test_hashable.py
Source:test_hashable.py
1from openpyxl.styles import HashableObject2import pytest3@pytest.fixture4def Immutable():5 class Immutable(HashableObject):6 __fields__ = ('value',)7 def __init__(self, value=None):8 self.value = value9 return Immutable10class TestHashable:11 def test_ctor(self, Immutable):12 d = Immutable()13 d.value = 114 assert d.value == 115 def test_cannot_change_attrs(self, Immutable):16 d = Immutable()17 d.value = 118 with pytest.raises(TypeError):19 d.value = 220 def test_cannot_delete_attrs(self, Immutable):21 d = Immutable()22 d.value = 123 with pytest.raises(TypeError):24 del d['value']25 def test_copy(self, Immutable):26 d = Immutable()27 d.value = 128 c = d.copy()29 assert c == d30 def test_hash(self, Immutable):31 d1 = Immutable()32 d2 = Immutable()33 assert hash(d1) == hash(d2)34 def test_str(self, Immutable):35 d = Immutable()36 assert str(d) == "Immutable(value=None)"37 d2 = Immutable("hello")38 assert str(d2) == "Immutable(value='hello')"39 def test_repr(self, Immutable):40 d = Immutable()41 assert repr(d) == ""42 d2 = Immutable("hello")43 assert repr(d2) == "Immutable(value='hello')"44 class ImmutableBase(Immutable):45 __base__ = True46 d = ImmutableBase()47 assert repr(d) == "ImmutableBase()"48 def test_eq(self, Immutable):49 d1 = Immutable(1)50 d2 = Immutable(1)51 assert d1 is not d252 assert d1 == d253 def test_ne(self, Immutable):54 d1 = Immutable(1)55 d2 = Immutable(2)...
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!!