paulfitz / daff

align and compare tables
https://paulfitz.github.io/daff
MIT License
790 stars 68 forks source link

diff record based on primary key doesn't work #168

Open KaiWW opened 3 years ago

KaiWW commented 3 years ago

The "age" has been assigned as the primary key, but it's using "key" as the primary key.

before_data = [["key", "id", "name", "age"], ["1001", "1", "Cleo", "4"], ["1010", "2", "Pancakes", "2"]]

after_data = [["key", "id", "name", "age"], ["1001", "1", "Cleo", "5"], ["1009", "3", "Bailey", "1"], ["1012", "234", "John", "10"], ["1010", "32", "Dave", "21"]]

table1 = daff.PythonTableView(before_data)
table2 = daff.PythonTableView(after_data)

compare_object = daff.Coopy.compareTables(table1, table2)

align_tables = compare_object.align()

data_diff = []
table_diff = daff.PythonTableView(data_diff)

flags = daff.CompareFlags()
**flags.addPrimaryKey("age")**

highlighter = daff.TableDiff(align_tables, flags)
highlighter.hilite(table_diff)

sys.stdout.writelines(str(data_diff))

This gives me: [['@@', 'key', 'id', 'name', 'age'], ['->', '1001', '1', 'Cleo', '4->5'], ['+++', '1009', '3', 'Bailey', '1'], ['+++', '1012', '234', 'John', '10'], ['->', '1010', '2->32', 'Pancakes->Dave', '2->21']]

paulfitz commented 3 years ago

Hi @KaiWW, I think the problem is that the initial call to compareTables is being done without the flags available to specify the primary key. Try calling it as compareTables(table1, table2, flags). For example in harness/BasicTest.hx there is a test for primary keys that goes like this:

    public function testNamedID(){
        var table1 = Native.table(data1);
        var table2 = Native.table(data2);
        var flags = new coopy.CompareFlags();
        flags.addPrimaryKey("Capital");
        var alignment = coopy.Coopy.compareTables(table1,table2,flags).align();
        var data_diff = [];
        var table_diff = Native.table(data_diff);
        var highlighter = new coopy.TableDiff(alignment,flags);
        highlighter.hilite(table_diff);
        assertEquals(""+table_diff.getCell(3,6),"Barcelona");
    }