Test encoding/decoding of standard data types and arrays thereof.
(self)
| 468 | class TestCodecs(tb.ConnectedTestCase): |
| 469 | |
| 470 | async def test_standard_codecs(self): |
| 471 | """Test encoding/decoding of standard data types and arrays thereof.""" |
| 472 | for (typname, intname, sample_data, *metadata) in type_samples: |
| 473 | if metadata and self.server_version < metadata[0]: |
| 474 | continue |
| 475 | |
| 476 | st = await self.con.prepare( |
| 477 | "SELECT $1::" + typname |
| 478 | ) |
| 479 | |
| 480 | text_in = await self.con.prepare( |
| 481 | "SELECT $1::text::" + typname |
| 482 | ) |
| 483 | |
| 484 | text_out = await self.con.prepare( |
| 485 | "SELECT $1::" + typname + "::text" |
| 486 | ) |
| 487 | |
| 488 | for sample in sample_data: |
| 489 | with self.subTest(sample=sample, typname=typname): |
| 490 | stmt = st |
| 491 | if isinstance(sample, dict): |
| 492 | if 'textinput' in sample: |
| 493 | inputval = sample['textinput'] |
| 494 | stmt = text_in |
| 495 | else: |
| 496 | inputval = sample['input'] |
| 497 | |
| 498 | if 'textoutput' in sample: |
| 499 | outputval = sample['textoutput'] |
| 500 | if stmt is text_in: |
| 501 | raise ValueError( |
| 502 | 'cannot test "textin" and' |
| 503 | ' "textout" simultaneously') |
| 504 | stmt = text_out |
| 505 | else: |
| 506 | outputval = sample['output'] |
| 507 | |
| 508 | if sample.get('query'): |
| 509 | stmt = await self.con.prepare(sample['query']) |
| 510 | else: |
| 511 | inputval = outputval = sample |
| 512 | |
| 513 | result = await stmt.fetchval(inputval) |
| 514 | err_msg = ( |
| 515 | "unexpected result for {} when passing {!r}: " |
| 516 | "received {!r}, expected {!r}".format( |
| 517 | typname, inputval, result, outputval)) |
| 518 | |
| 519 | if typname.startswith('float'): |
| 520 | if math.isnan(outputval): |
| 521 | if not math.isnan(result): |
| 522 | self.fail(err_msg) |
| 523 | else: |
| 524 | self.assertTrue( |
| 525 | math.isclose(result, outputval, rel_tol=1e-6), |
| 526 | err_msg) |
| 527 | else: |
nothing calls this directly
no test coverage detected