def test_diff_records_multikey(self): lhs = [ {'name': 'a', 'type': 1, 'sheep': 7}, {'name': 'b', 'type': 1, 'sheep': 12}, {'name': 'c', 'type': 1, 'sheep': 0}, ] rhs = [ {'name': 'a', 'type': 1, 'sheep': 7}, {'name': 'c', 'type': 1, 'sheep': 2}, {'name': 'd', 'type': 1, 'sheep': 8}, ] diff = csvdiff.diff_records(lhs, rhs, ['name', 'type']) assert patch.is_valid(diff) assert patch.is_typed(diff) self.assertEqual(diff['added'], [ {'name': 'd', 'sheep': 8, 'type': 1} ]) self.assertEqual(diff['removed'], [ {'name': 'b', 'sheep': 12, 'type': 1} ]) self.assertEqual(diff['changed'], [ {'key': ['c', 1], 'fields': {'sheep': {'from': 0, 'to': 2}}} ]) # check that we can apply the diff patched = csvdiff.patch_records(diff, lhs) self.assertRecordsEqual(rhs, patched)
def test_diff_records_str_values(self): lhs = [ {'name': 'a', 'sheep': '7'}, {'name': 'b', 'sheep': '12'}, {'name': 'c', 'sheep': '0'}, ] rhs = [ {'name': 'a', 'sheep': '7'}, {'name': 'c', 'sheep': '2'}, {'name': 'd', 'sheep': '8'}, ] diff = csvdiff.diff_records(lhs, rhs, ['name']) assert patch.is_valid(diff) assert not patch.is_typed(diff) # check the contents of the diff self.assertEqual(diff['added'], [ {'name': 'd', 'sheep': '8'} ]) self.assertEqual(diff['removed'], [ {'name': 'b', 'sheep': '12'} ]) self.assertEqual(diff['changed'], [ {'key': ['c'], 'fields': {'sheep': {'from': '0', 'to': '2'}}} ]) # check that we can apply the diff patched = csvdiff.patch_records(diff, lhs) self.assertRecordsEqual(rhs, patched)
def test_diff_records_multikey(self): lhs = [ { 'name': 'a', 'type': 1, 'sheep': 7 }, { 'name': 'b', 'type': 1, 'sheep': 12 }, { 'name': 'c', 'type': 1, 'sheep': 0 }, ] rhs = [ { 'name': 'a', 'type': 1, 'sheep': 7 }, { 'name': 'c', 'type': 1, 'sheep': 2 }, { 'name': 'd', 'type': 1, 'sheep': 8 }, ] diff = csvdiff.diff_records(lhs, rhs, ['name', 'type']) assert patch.is_valid(diff) assert patch.is_typed(diff) self.assertEqual(diff['added'], [{'name': 'd', 'sheep': 8, 'type': 1}]) self.assertEqual(diff['removed'], [{ 'name': 'b', 'sheep': 12, 'type': 1 }]) self.assertEqual(diff['changed'], [{ 'key': ['c', 1], 'fields': { 'sheep': { 'from': 0, 'to': 2 } } }]) # check that we can apply the diff patched = csvdiff.patch_records(diff, lhs) self.assertRecordsEqual(rhs, patched)
def test_diff_records_str_values(self): lhs = [ { 'name': 'a', 'sheep': '7' }, { 'name': 'b', 'sheep': '12' }, { 'name': 'c', 'sheep': '0' }, ] rhs = [ { 'name': 'a', 'sheep': '7' }, { 'name': 'c', 'sheep': '2' }, { 'name': 'd', 'sheep': '8' }, ] diff = csvdiff.diff_records(lhs, rhs, ['name']) assert patch.is_valid(diff) assert not patch.is_typed(diff) # check the contents of the diff self.assertEqual(diff['added'], [{'name': 'd', 'sheep': '8'}]) self.assertEqual(diff['removed'], [{'name': 'b', 'sheep': '12'}]) self.assertEqual(diff['changed'], [{ 'key': ['c'], 'fields': { 'sheep': { 'from': '0', 'to': '2' } } }]) # check that we can apply the diff patched = csvdiff.patch_records(diff, lhs) self.assertRecordsEqual(rhs, patched)
def test_summarize_identical(self): lhs = [ {'name': 'a', 'sheep': '7'}, {'name': 'b', 'sheep': '12'}, {'name': 'c', 'sheep': '0'}, ] diff = csvdiff.diff_records(lhs, lhs, ['name']) assert patch.is_valid(diff) assert not patch.is_typed(diff) o = StringIO() csvdiff._summarize_diff(diff, len(lhs), stream=o) self.assertEqual( o.getvalue(), 'files are identical\n' )
def test_diff_command_valid_usage_with_separator(self): result = self.csvdiff_cmd('--sep', '\t', 'id', self.a_file_tsv, self.b_file_tsv) self.assertEqual(result.exit_code, 1) diff = result.diff patch.validate(diff) assert patch.is_valid(diff) expected = { '_index': ['id'], 'added': [{ 'id': '5', 'name': 'mira', 'amount': '81' }], 'removed': [{ 'id': '2', 'name': 'eva', 'amount': '63' }], 'changed': [ { 'key': ['1'], 'fields': { 'amount': { 'from': '20', 'to': '23' } } }, { 'key': ['6'], 'fields': { 'amount': { 'from': '10', 'to': '13' } } }, ], } self.assertPatchesEqual(diff, expected)
def test_diff_command_valid_usage_with_difference(self): result = self.csvdiff_cmd('id', self.a_file, self.b_file) self.assertEqual(result.exit_code, 1) diff = result.diff patch.validate(diff) assert patch.is_valid(diff) expected = { '_index': ['id'], 'added': [{'id': '5', 'name': 'mira', 'amount': '81'}], 'removed': [{'id': '2', 'name': 'eva', 'amount': '63'}], 'changed': [ {'key': ['1'], 'fields': {'amount': {'from': '20', 'to': '23'}}}, {'key': ['6'], 'fields': {'amount': {'from': '10', 'to': '13'}}}, ], } self.assertPatchesEqual(diff, expected)
def test_summarize_identical(self): lhs = [ { 'name': 'a', 'sheep': '7' }, { 'name': 'b', 'sheep': '12' }, { 'name': 'c', 'sheep': '0' }, ] diff = csvdiff.diff_records(lhs, lhs, ['name']) assert patch.is_valid(diff) assert not patch.is_typed(diff) o = StringIO() csvdiff._summarize_diff(diff, len(lhs), stream=o) self.assertEqual(o.getvalue(), 'files are identical\n')
def test_summarize(self): lhs = [ {'name': 'a', 'sheep': '7'}, {'name': 'b', 'sheep': '12'}, {'name': 'c', 'sheep': '0'}, ] rhs = [ {'name': 'a', 'sheep': '7'}, {'name': 'c', 'sheep': '2'}, {'name': 'd', 'sheep': '8'}, ] diff = csvdiff.diff_records(lhs, rhs, ['name']) assert patch.is_valid(diff) assert not patch.is_typed(diff) o = StringIO() csvdiff._summarize_diff(diff, len(lhs), stream=o) self.assertEqual( o.getvalue(), "1 rows removed (33.3%)\n" "1 rows added (33.3%)\n" "1 rows changed (33.3%)\n" )
def test_summarize(self): lhs = [ { 'name': 'a', 'sheep': '7' }, { 'name': 'b', 'sheep': '12' }, { 'name': 'c', 'sheep': '0' }, ] rhs = [ { 'name': 'a', 'sheep': '7' }, { 'name': 'c', 'sheep': '2' }, { 'name': 'd', 'sheep': '8' }, ] diff = csvdiff.diff_records(lhs, rhs, ['name']) assert patch.is_valid(diff) assert not patch.is_typed(diff) o = StringIO() csvdiff._summarize_diff(diff, len(lhs), stream=o) self.assertEqual( o.getvalue(), "1 rows removed (33.3%)\n" "1 rows added (33.3%)\n" "1 rows changed (33.3%)\n")
def test_patch_schema_is_valid(self): assert not patch.is_valid({})