How to use coerce method in pandera

Best Python code snippet using pandera_python

test_fields.py

Source:test_fields.py Github

copy

Full Screen

...24from nova import test25from nova.tests.unit import fake_instance26from nova import utils27class FakeFieldType(fields.FieldType):28 def coerce(self, obj, attr, value):29 return '*%s*' % value30 def to_primitive(self, obj, attr, value):31 return '!%s!' % value32 def from_primitive(self, obj, attr, value):33 return value[1:-1]34class FakeEnum(fields.Enum):35 FROG = "frog"36 PLATYPUS = "platypus"37 ALLIGATOR = "alligator"38 ALL = (FROG, PLATYPUS, ALLIGATOR)39 def __init__(self, **kwargs):40 super(FakeEnum, self).__init__(valid_values=FakeEnum.ALL,41 **kwargs)42class FakeEnumAlt(fields.Enum):43 FROG = "frog"44 PLATYPUS = "platypus"45 AARDVARK = "aardvark"46 ALL = (FROG, PLATYPUS, AARDVARK)47 def __init__(self, **kwargs):48 super(FakeEnumAlt, self).__init__(valid_values=FakeEnumAlt.ALL,49 **kwargs)50class FakeAddress(fields.AddressBase):51 PATTERN = '[a-z]+[0-9]+'52class FakeAddressField(fields.AutoTypedField):53 AUTO_TYPE = FakeAddress()54class FakeEnumField(fields.BaseEnumField):55 AUTO_TYPE = FakeEnum()56class FakeEnumAltField(fields.BaseEnumField):57 AUTO_TYPE = FakeEnumAlt()58class TestField(test.NoDBTestCase):59 def setUp(self):60 super(TestField, self).setUp()61 self.field = fields.Field(FakeFieldType())62 self.coerce_good_values = [('foo', '*foo*')]63 self.coerce_bad_values = []64 self.to_primitive_values = [('foo', '!foo!')]65 self.from_primitive_values = [('!foo!', 'foo')]66 def test_coerce_good_values(self):67 for in_val, out_val in self.coerce_good_values:68 self.assertEqual(out_val, self.field.coerce('obj', 'attr', in_val))69 def test_coerce_bad_values(self):70 for in_val in self.coerce_bad_values:71 self.assertRaises((TypeError, ValueError),72 self.field.coerce, 'obj', 'attr', in_val)73 def test_to_primitive(self):74 for in_val, prim_val in self.to_primitive_values:75 self.assertEqual(prim_val, self.field.to_primitive('obj', 'attr',76 in_val))77 def test_from_primitive(self):78 class ObjectLikeThing(object):79 _context = 'context'80 for prim_val, out_val in self.from_primitive_values:81 self.assertEqual(out_val, self.field.from_primitive(82 ObjectLikeThing, 'attr', prim_val))...

Full Screen

Full Screen

information_schema.py

Source:information_schema.py Github

copy

Full Screen

1# mssql/information_schema.py2# Copyright (C) 2005-2020 the SQLAlchemy authors and contributors3# <see AUTHORS file>4#5# This module is part of SQLAlchemy and is released under6# the MIT License: http://www.opensource.org/licenses/mit-license.php7# TODO: should be using the sys. catalog with SQL Server, not information8# schema9from ... import cast10from ... import Column11from ... import MetaData12from ... import Table13from ... import util14from ...ext.compiler import compiles15from ...sql import expression16from ...types import Boolean17from ...types import Integer18from ...types import String19from ...types import TypeDecorator20from ...types import Unicode21ischema = MetaData()22class CoerceUnicode(TypeDecorator):23 impl = Unicode24 def process_bind_param(self, value, dialect):25 if util.py2k and isinstance(value, util.binary_type):26 value = value.decode(dialect.encoding)27 return value28 def bind_expression(self, bindvalue):29 return _cast_on_2005(bindvalue)30class _cast_on_2005(expression.ColumnElement):31 def __init__(self, bindvalue):32 self.bindvalue = bindvalue33@compiles(_cast_on_2005)34def _compile(element, compiler, **kw):35 from . import base36 if (37 compiler.dialect.server_version_info is None38 or compiler.dialect.server_version_info < base.MS_2005_VERSION39 ):40 return compiler.process(element.bindvalue, **kw)41 else:42 return compiler.process(cast(element.bindvalue, Unicode), **kw)43schemata = Table(44 "SCHEMATA",45 ischema,46 Column("CATALOG_NAME", CoerceUnicode, key="catalog_name"),47 Column("SCHEMA_NAME", CoerceUnicode, key="schema_name"),48 Column("SCHEMA_OWNER", CoerceUnicode, key="schema_owner"),49 schema="INFORMATION_SCHEMA",50)51tables = Table(52 "TABLES",53 ischema,54 Column("TABLE_CATALOG", CoerceUnicode, key="table_catalog"),55 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),56 Column("TABLE_NAME", CoerceUnicode, key="table_name"),57 Column("TABLE_TYPE", CoerceUnicode, key="table_type"),58 schema="INFORMATION_SCHEMA",59)60columns = Table(61 "COLUMNS",62 ischema,63 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),64 Column("TABLE_NAME", CoerceUnicode, key="table_name"),65 Column("COLUMN_NAME", CoerceUnicode, key="column_name"),66 Column("IS_NULLABLE", Integer, key="is_nullable"),67 Column("DATA_TYPE", String, key="data_type"),68 Column("ORDINAL_POSITION", Integer, key="ordinal_position"),69 Column(70 "CHARACTER_MAXIMUM_LENGTH", Integer, key="character_maximum_length"71 ),72 Column("NUMERIC_PRECISION", Integer, key="numeric_precision"),73 Column("NUMERIC_SCALE", Integer, key="numeric_scale"),74 Column("COLUMN_DEFAULT", Integer, key="column_default"),75 Column("COLLATION_NAME", String, key="collation_name"),76 schema="INFORMATION_SCHEMA",77)78constraints = Table(79 "TABLE_CONSTRAINTS",80 ischema,81 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),82 Column("TABLE_NAME", CoerceUnicode, key="table_name"),83 Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"),84 Column("CONSTRAINT_TYPE", CoerceUnicode, key="constraint_type"),85 schema="INFORMATION_SCHEMA",86)87column_constraints = Table(88 "CONSTRAINT_COLUMN_USAGE",89 ischema,90 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),91 Column("TABLE_NAME", CoerceUnicode, key="table_name"),92 Column("COLUMN_NAME", CoerceUnicode, key="column_name"),93 Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"),94 schema="INFORMATION_SCHEMA",95)96key_constraints = Table(97 "KEY_COLUMN_USAGE",98 ischema,99 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),100 Column("TABLE_NAME", CoerceUnicode, key="table_name"),101 Column("COLUMN_NAME", CoerceUnicode, key="column_name"),102 Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"),103 Column("CONSTRAINT_SCHEMA", CoerceUnicode, key="constraint_schema"),104 Column("ORDINAL_POSITION", Integer, key="ordinal_position"),105 schema="INFORMATION_SCHEMA",106)107ref_constraints = Table(108 "REFERENTIAL_CONSTRAINTS",109 ischema,110 Column("CONSTRAINT_CATALOG", CoerceUnicode, key="constraint_catalog"),111 Column("CONSTRAINT_SCHEMA", CoerceUnicode, key="constraint_schema"),112 Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"),113 # TODO: is CATLOG misspelled ?114 Column(115 "UNIQUE_CONSTRAINT_CATLOG",116 CoerceUnicode,117 key="unique_constraint_catalog",118 ),119 Column(120 "UNIQUE_CONSTRAINT_SCHEMA",121 CoerceUnicode,122 key="unique_constraint_schema",123 ),124 Column(125 "UNIQUE_CONSTRAINT_NAME", CoerceUnicode, key="unique_constraint_name"126 ),127 Column("MATCH_OPTION", String, key="match_option"),128 Column("UPDATE_RULE", String, key="update_rule"),129 Column("DELETE_RULE", String, key="delete_rule"),130 schema="INFORMATION_SCHEMA",131)132views = Table(133 "VIEWS",134 ischema,135 Column("TABLE_CATALOG", CoerceUnicode, key="table_catalog"),136 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),137 Column("TABLE_NAME", CoerceUnicode, key="table_name"),138 Column("VIEW_DEFINITION", CoerceUnicode, key="view_definition"),139 Column("CHECK_OPTION", String, key="check_option"),140 Column("IS_UPDATABLE", String, key="is_updatable"),141 schema="INFORMATION_SCHEMA",142)143computed_columns = Table(144 "computed_columns",145 ischema,146 Column("object_id", Integer),147 Column("name", CoerceUnicode),148 Column("is_computed", Boolean),149 Column("is_persisted", Boolean),150 Column("definition", CoerceUnicode),151 schema="sys",...

Full Screen

Full Screen

number-property.spec.ts

Source:number-property.spec.ts Github

copy

Full Screen

1import {coerceNumberProperty} from './number-property';2describe('coerceNumberProperty', () => {3 it('should coerce undefined to 0 or default', () => {4 expect(coerceNumberProperty(undefined)).toBe(0);5 expect(coerceNumberProperty(undefined, 111)).toBe(111);6 });7 it('should coerce null to 0 or default', () => {8 expect(coerceNumberProperty(null)).toBe(0);9 expect(coerceNumberProperty(null, 111)).toBe(111);10 });11 it('should coerce true to 0 or default', () => {12 expect(coerceNumberProperty(true)).toBe(0);13 expect(coerceNumberProperty(true, 111)).toBe(111);14 });15 it('should coerce false to 0 or default', () => {16 expect(coerceNumberProperty(false)).toBe(0);17 expect(coerceNumberProperty(false, 111)).toBe(111);18 });19 it('should coerce the empty string to 0 or default', () => {20 expect(coerceNumberProperty('')).toBe(0);21 expect(coerceNumberProperty('', 111)).toBe(111);22 });23 it('should coerce the string "1" to 1', () => {24 expect(coerceNumberProperty('1')).toBe(1);25 expect(coerceNumberProperty('1', 111)).toBe(1);26 });27 it('should coerce the string "123.456" to 123.456', () => {28 expect(coerceNumberProperty('123.456')).toBe(123.456);29 expect(coerceNumberProperty('123.456', 111)).toBe(123.456);30 });31 it('should coerce the string "-123.456" to -123.456', () => {32 expect(coerceNumberProperty('-123.456')).toBe(-123.456);33 expect(coerceNumberProperty('-123.456', 111)).toBe(-123.456);34 });35 it('should coerce an arbitrary string to 0 or default', () => {36 expect(coerceNumberProperty('pink')).toBe(0);37 expect(coerceNumberProperty('pink', 111)).toBe(111);38 });39 it('should coerce an arbitrary string prefixed with a number to 0 or default', () => {40 expect(coerceNumberProperty('123pink')).toBe(0);41 expect(coerceNumberProperty('123pink', 111)).toBe(111);42 });43 it('should coerce the number 1 to 1', () => {44 expect(coerceNumberProperty(1)).toBe(1);45 expect(coerceNumberProperty(1, 111)).toBe(1);46 });47 it('should coerce the number 123.456 to 123.456', () => {48 expect(coerceNumberProperty(123.456)).toBe(123.456);49 expect(coerceNumberProperty(123.456, 111)).toBe(123.456);50 });51 it('should coerce the number -123.456 to -123.456', () => {52 expect(coerceNumberProperty(-123.456)).toBe(-123.456);53 expect(coerceNumberProperty(-123.456, 111)).toBe(-123.456);54 });55 it('should coerce an object to 0 or default', () => {56 expect(coerceNumberProperty({})).toBe(0);57 expect(coerceNumberProperty({}, 111)).toBe(111);58 });59 it('should coerce an array to 0 or default', () => {60 expect(coerceNumberProperty([])).toBe(0);61 expect(coerceNumberProperty([], 111)).toBe(111);62 });...

Full Screen

Full Screen

coerce.js

Source:coerce.js Github

copy

Full Screen

1"use strict";2var assert = require("chai").assert3 , coerceToInteger = require("../../integer/coerce");4describe("integer/coerce", function () {5 it("Should coerce float to integer", function () {6 assert.equal(coerceToInteger(123.123), 123);7 assert.equal(coerceToInteger(123.823), 123);8 assert.equal(coerceToInteger(-123.123), -123);9 assert.equal(coerceToInteger(-123.823), -123);10 });11 it("Should coerce string", function () { assert.equal(coerceToInteger("12.123"), 12); });12 it("Should coerce booleans", function () { assert.equal(coerceToInteger(true), 1); });13 it("Should coerce number objects", function () {14 assert.equal(coerceToInteger(new Number(343)), 343);15 });16 it("Should coerce objects", function () {17 assert.equal(coerceToInteger({ valueOf: function () { return 23; } }), 23);18 });19 it("Should coerce number beyond Number.MAX_SAFE_INTEGER", function () {20 assert.equal(coerceToInteger(9007199254740992), 9007199254740992);21 });22 it("Should coerce number beyond Number.MIN_SAFE_INTEGER", function () {23 assert.equal(coerceToInteger(-9007199254740992), -9007199254740992);24 });25 it("Should reject infinite number", function () {26 assert.equal(coerceToInteger(Infinity), null);27 });28 it("Should reject NaN", function () { assert.equal(coerceToInteger(NaN), null); });29 if (typeof Object.create === "function") {30 it("Should not coerce objects with no number representation", function () {31 assert.equal(coerceToInteger(Object.create(null)), null);32 });33 }34 it("Should not coerce null", function () { assert.equal(coerceToInteger(null), null); });35 it("Should not coerce undefined", function () {36 assert.equal(coerceToInteger(undefined), null);37 });38 if (typeof Symbol === "function") {39 it("Should not coerce symbols", function () {40 assert.equal(coerceToInteger(Symbol("foo")), null);41 });42 }...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful