Best JavaScript code snippet using root
test_fields.py
Source:test_fields.py
...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))...
information_schema.py
Source:information_schema.py
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",...
number-property.spec.ts
Source:number-property.spec.ts
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 });...
coerce.js
Source:coerce.js
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 }...
Using AI Code Generation
1var schema = {2 "properties": {3 "name": {4 },5 "id": {6 }7 }8};9var data = {10};11var validator = new Validator();12validator.addSchema(schema, 'root');13var result = validator.validate(data, schema);14console.log(result.errors);15var schema = {16 "properties": {17 "name": {18 },19 "id": {20 },21 "address": {22 "properties": {23 "street": {24 },25 "city": {26 }27 }28 }29 }30};31var data = {32 "address": {33 }34};35var validator = new Validator();36validator.addSchema(schema, 'root');37var result = validator.validate(data, schema);38console.log(result.errors);39var schema = {40 "properties": {41 "name": {42 },43 "id": {44 },45 "address": {46 "properties": {47 "street": {48 },49 "city": {50 },51 "state": {52 }53 }54 }55 }56};57var data = {58 "address": {59 }60};61var validator = new Validator();62validator.addSchema(schema, 'root');63var result = validator.validate(data, schema);64console.log(result.errors);65[ { keyword: 'type',
Using AI Code Generation
1var schema = {2 properties: {3 name: {4 },5 age: {6 }7 }8};9var validate = ajv.compile(schema);10var data = {11};12var valid = validate(data);13var schema = {14 properties: {15 name: {16 },17 age: {18 }19 }20};21var validate = ajv.compile(schema);22var data = {23};24var valid = validate(data);25var schema = {26 properties: {27 name: {28 },29 age: {30 coerce: function(value) {31 return +value;32 }33 }34 }35};36var validate = ajv.compile(schema);37var data = {38};39var valid = validate(data);40var schema = {41 properties: {42 name: {43 },44 age: {45 coerce: function(value) {46 if (value > 18) {47 return value;48 } else {49 throw new Error('Age should be greater than 18');50 }51 }52 }53 }54};55var validate = ajv.compile(schema);56var data = {57};58var valid = validate(data);
Using AI Code Generation
1var schema = {2 properties: {3 name: { type: 'string' },4 age: { type: 'number' }5 }6};7var data = {8};9var validate = ajv.compile(schema);
Using AI Code Generation
1var obj = {2};3var obj1 = Object.create(obj);4obj1.a = 4;5obj1.b = 5;6obj1.c = 6;7var obj2 = Object.create(obj1);8obj2.a = 7;9obj2.b = 8;10obj2.c = 9;11console.log(obj2.a);12console.log(obj2.b);13console.log(obj2.c);14console.log(obj2.hasOwnProperty('a'));15console.log(obj2.hasOwnProperty('b'));16console.log(obj2.hasOwnProperty('c'));
Using AI Code Generation
1const root = require('./root');2const coerce = root.coerce;3const type = root.type;4const a = 1;5const b = 2;6const result = coerce(a, b);7console.log(result);8const type = require('./type');9const coerce = function(a, b) {10 if (type(a) !== type(b)) {11 throw new Error('a and b must be of the same type');12 }13 return a + b;14};15module.exports = {16};17const type = function(a) {18 return typeof a;19};20module.exports = type;
Using AI Code Generation
1root.print(root.coerce(1) + root.coerce(2));2root.print(root.coerce(1) + root.coerce(2));3root.print(root.coerce(1) + root.coerce(2));4root.print(root.coerce(1) + root.coerce(2));5root.print(root.coerce(1) + root.coerce(2));6root.print(root.coerce(1) + root.coerce(2));7root.print(root.coerce(1) + root.coerce(2));8root.print(root.coerce(1) + root.coerce(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!!