Assert contents of DB tables

27 views
Skip to first unread message

sumau

unread,
Nov 22, 2019, 5:50:54 AM11/22/19
to sqlalchemy
Hello

I would like to assert the contents of tables in my PG schema i.e. make sure it contains the data I'm expecting

I am aware of various options:

1) Compare the actual and expected tables using a sql query, orchestrated by sqlalchemy (i.e. create the actual and expected tables in DB, run the sql comparison script, return the output)
2) Load the actual tables as tuples and compare them with expected tuples using something like assert_result
3) Load the actual tables as dataframes and compare them with expected dataframes using pandas assert_frame_equal

Any recommendations / thoughts would be much appreciated, both as to the approach and the implementation :-)

sumau

unread,
Nov 29, 2019, 9:13:23 AM11/29/19
to sqlalchemy
Hello

I think my original question was too generic so rephrasing... Is there a way in sqlalchemy to:
  1. Assert a ResultProxy against an expected ResultProxy (or list of RowProxies against expected list of RowProxies) 
  2. Show any differences
I wanted to check first before writing my own script :-)

Regards
S

Mike Bayer

unread,
Nov 29, 2019, 10:30:09 AM11/29/19
to noreply-spamdigest via sqlalchemy


On Fri, Nov 29, 2019, at 9:13 AM, sumau wrote:
Hello

I think my original question was too generic so rephrasing... Is there a way in sqlalchemy to:
  1. Assert a ResultProxy against an expected ResultProxy (or list of RowProxies against expected list of RowProxies) 
  2. Show any differences
I wanted to check first before writing my own script :-)

you fetch the rows into a list and compare:


assert result.fetchall() == otherresult.fetchall()

that's pretty much it, obviously does not scale for thousands or more of rows very well.





Regards
S

On Friday, 22 November 2019 10:50:54 UTC, sumau wrote:
Hello

I would like to assert the contents of tables in my PG schema i.e. make sure it contains the data I'm expecting

I am aware of various options:

1) Compare the actual and expected tables using a sql query, orchestrated by sqlalchemy (i.e. create the actual and expected tables in DB, run the sql comparison script, return the output)
2) Load the actual tables as tuples and compare them with expected tuples using something like assert_result
3) Load the actual tables as dataframes and compare them with expected dataframes using pandas assert_frame_equal

Any recommendations / thoughts would be much appreciated, both as to the approach and the implementation :-)


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Steven James

unread,
Dec 2, 2019, 11:54:16 AM12/2/19
to sqlalchemy
In case you want more details about the differences, you could also use difflib...

from difflib import SequenceMatcher

seq1 = [tuple(row.values()) for row in resultproxy1]
seq2 = [tuple(row.values()) for row in resultproxy2]

sm = SequenceMatcher(a=seq1, b=seq2, autojunk=False)
print(sm.get_opcodes())
print(f'similarity: {sm.ratio()}')

assert sm.ratio() == 1  # example to ensure results are equivalent
assert sm.ratio() == 1, sm.get_opcodes()  # pytest syntax to show the opcodes if the assertion fails

Steven James

sumau

unread,
Dec 2, 2019, 3:21:56 PM12/2/19
to sqlalchemy
Thanks! And to show the differences more clearly:

for tag, i1, i2, j1, j2 in sm.get_opcodes():
if tag == 'replace':
for i in range(i1,i2):
print (seq1[i])
print (seq2[i])

Soumaya
Reply all
Reply to author
Forward
0 new messages