Skip to content

Instantly share code, notes, and snippets.

@humitos
Created November 10, 2016 02:07
Show Gist options
  • Save humitos/bc2db6d23dd6d9432b4f75ff80380b23 to your computer and use it in GitHub Desktop.
Save humitos/bc2db6d23dd6d9432b4f75ff80380b23 to your computer and use it in GitHub Desktop.
# python 2.7.12
# it shows more differences than it should, since shows all the 'u' from
# Unicode keys from the dictionaries
# my main problem with this is when we do not control the data (for example
# when it's generated in another file that does not use `unicode_literals` from
# `__future__` since we have no control over that
# [21:05] humitos: ~> python2 /home/humitos/test_assertDictEqual.py
# .F.F.F
# ======================================================================
# FAIL: test_assert_dict_equal_not_unicode_key__differ (__main__.MyTest)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "/home/humitos/test_assertDictEqual.py", line 92, in test_assert_dict_equal_not_unicode_key__differ
# self.assertDictEqual(d1, d2)
# AssertionError: {u'int': 30, u'string': u'humitos'} != {u'int': 100, u'string': u'humitos'}
# - {u'int': 30, u'string': u'humitos'}
# ? ^
# + {u'int': 100, u'string': u'humitos'}
# ? ^^
# ======================================================================
# FAIL: test_assert_dict_equal_unicode_key__differ (__main__.MyTest)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "/home/humitos/test_assertDictEqual.py", line 78, in test_assert_dict_equal_unicode_key__differ
# self.assertDictEqual(d1, d2)
# AssertionError: {u'int': 30, u'string': u'humitos'} != {u'int': 100, u'string': u'humitos'}
# - {u'int': 30, u'string': u'humitos'}
# ? ^
# + {u'int': 100, u'string': u'humitos'}
# ? ^^
# ======================================================================
# FAIL: test_assert_dict_equal_with_unicode_key_and_other_with_no_unicode_literals (__main__.MyTest)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "/home/humitos/test_assertDictEqual.py", line 113, in test_assert_dict_equal_with_unicode_key_and_other_with_no_unicode_literals
# self.assertDictEqual(d1, d3)
# AssertionError: {u'int': 100, u'string': u'humitos'} != {'int': 30, 'string': 'humitos'}
# - {u'int': 100, u'string': u'humitos'}
# ? - ^^ - - <-------- HERE IS MY MAIN PROBLEM WITH THOSE `u`
# + {'int': 30, 'string': 'humitos'}
# ? ^
# ----------------------------------------------------------------------
# Ran 6 tests in 0.002s
# FAILED (failures=3)
# [21:05] humitos: ~>
########################
# python 3.5.2
# not a problem
# [21:05] humitos: ~> python3 /home/humitos/test_assertDictEqual.py
# .F.F.F
# ======================================================================
# FAIL: test_assert_dict_equal_not_unicode_key__differ (__main__.MyTest)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "/home/humitos/test_assertDictEqual.py", line 140, in test_assert_dict_equal_not_unicode_key__differ
# self.assertDictEqual(d1, d2)
# AssertionError: {'int': 30, 'string': 'humitos'} != {'int': 100, 'string': 'humitos'}
# - {'int': 30, 'string': 'humitos'}
# ? ^
# + {'int': 100, 'string': 'humitos'}
# ? ^^
# ======================================================================
# FAIL: test_assert_dict_equal_unicode_key__differ (__main__.MyTest)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "/home/humitos/test_assertDictEqual.py", line 126, in test_assert_dict_equal_unicode_key__differ
# self.assertDictEqual(d1, d2)
# AssertionError: {'int': 30, 'string': 'humitos'} != {'int': 100, 'string': 'humitos'}
# - {'int': 30, 'string': 'humitos'}
# ? ^
# + {'int': 100, 'string': 'humitos'}
# ? ^^
# ======================================================================
# FAIL: test_assert_dict_equal_with_unicode_key_and_other_with_no_unicode_literals (__main__.MyTest)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "/home/humitos/test_assertDictEqual.py", line 161, in test_assert_dict_equal_with_unicode_key_and_other_with_no_unicode_literals
# self.assertDictEqual(d1, d3)
# AssertionError: {'int': 100, 'string': 'humitos'} != {'int': 30, 'string': 'humitos'}
# - {'int': 100, 'string': 'humitos'}
# ? ^^
# + {'int': 30, 'string': 'humitos'}
# ? ^
# ----------------------------------------------------------------------
# Ran 6 tests in 0.002s
# FAILED (failures=3)
# [21:07] humitos: ~>
from __future__ import unicode_literals
import unittest
from module_without_unicode_literals import d3
###### module_without_unicode_literals
# d3 = {
# 'int': 30,
# 'string': 'humitos',
# }
class MyTest(unittest.TestCase):
def test_assert_dict_equal_unicode_key(self):
d1 = {
u'int': 30,
'string': 'humitos',
}
d2 = {
'int': 30,
'string': 'humitos',
}
assert d1 == d2
self.assertDictEqual(d1, d2)
def test_assert_dict_equal_not_unicode_key(self):
d1 = {
'int': 30,
'string': 'humitos',
}
d2 = {
'int': 30,
'string': 'humitos',
}
assert d1 == d2
self.assertDictEqual(d1, d2)
def test_assert_dict_equal_unicode_key__differ(self):
d1 = {
u'int': 30,
'string': 'humitos',
}
d2 = {
'int': 100,
'string': 'humitos',
}
assert d1 != d2
self.assertDictEqual(d1, d2)
def test_assert_dict_equal_not_unicode_key__differ(self):
d1 = {
'int': 30,
'string': 'humitos',
}
d2 = {
'int': 100,
'string': 'humitos',
}
assert d1 != d2
self.assertDictEqual(d1, d2)
###### test from module_without_unicode_literals
def test_assert_dict_equal_with_other_dict_and_no_unicode_literals(self):
d1 = {
'int': 30,
'string': 'humitos',
}
assert d1 == d3
self.assertDictEqual(d1, d3)
def test_assert_dict_equal_with_unicode_key_and_other_with_no_unicode_literals(self):
d1 = {
u'int': 100,
'string': 'humitos',
}
assert d1 != d3
self.assertDictEqual(d1, d3)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment