Skip to content

Add tests #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add tests #15

wants to merge 1 commit into from

Conversation

slezica
Copy link
Owner

@slezica slezica commented Oct 6, 2016

@ppolewicz @lurch @eugene-eeo

This project is seeing some activity lately (yay), I think it's time to add some tests.

I incorporated test cases written in an earlier fork, linked by @ppolewicz in #14, but I'm torn between two positions: on one hand, the cases are pretty basic and not much is being actually tested, but on the other hand, most of the real action happens inside dict and OrderedDict, both provided by the stdlib.

I wanted to know your opinions before I merged this in. If you have time, please take a look!

@slezica slezica mentioned this pull request Oct 6, 2016
@@ -1 +1,2 @@
include *.txt
include tests.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not ship tests.py in the package

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nowadays it's recommended to ship tests too. For example, downstream packagers like Fedora want to run the tests to make sure it's working.

def assertMappingEqual(self, m1, m2):
# `assertDictEqual` will make a failing type check when comparing a `dict`
# with a `frozendict`. Instead, we'll compare the item sets:
self.assertSetEqual(set(m1.items()), set(m2.items()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no assertSetEqual in Python 2.6

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need assertSetEqual? Because set equality can be tested via == anyways and we can just use assertEqual.

self.assertSetEqual(set(m1.items()), set(m2.items()))

def assertRaisesAssignmentError(self, *args, **kwargs):
self.assertRaisesRegexp(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no assertRaisesRegexp in Python 2.6

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 2020 I strongly recommend dropping 2.6 (and consider 2.7).


class TestFrozenDict(BaseTestCase):
def test_assignment_fails(self):
self.assertRaisesAssignmentError(setitem, frozendict(), 'key', 'value')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with self.assertRaises(TypeError):
    frozendict.key = value

but assertRaises needs to be backported. I suggest that you copy B2 CLI TestBase.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise not a problem in 2020 if dropping 2.6.

def test_assignment_fails(self):
self.assertRaisesAssignmentError(setitem, frozendict(), 'key', 'value')

def test_init(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is practically worthless, please remove

keys = {key for key in fd}
self.assertSetEqual(keys, {'a', 'b'})

def test_len(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len of empty dict should also be checked

def test_repr(self):
fd = frozendict(a=0, b=1)
r = repr(fd)
self.assertTrue(r.startswith('<frozendict '))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertion is too loose, we want to check what is actually in there, not just the header and the footer


def test_hash(self):
fd = frozendict(a=0, b=1)
hash(fd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hashes of two frozendicts which have the same contents, should be identical

hash(fd)


class TestFrozenOrderedDict(BaseTestCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

many tests in this class are copies of the tests from class above. Use inheritance and a class field to prevent repetition of code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the FrozenOrderedDict tests should use an assertOrderedMappingEqual function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, FrozenOrderedDict cannot be tested on Python 2.6 where it is not supported. Maybe raise nose.SkipTest() in setUp?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK in 2020

self.assertIs(type(copied), type(fd))
self.assertMappingEqual(copied, dict(a=0, b=2, c=3))

def test_iter(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a 1/16th chance that this test will pass with frozendict. Please increase the test data size to reduce the chance to a more reasonable value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, could you explain further?

@eugene-eeo
Copy link
Contributor

While stdlib's unittest module is quite good, maybe we should use pytest or nosetests as they allow us to write more concise tests. Also we can package them in tests_require attribute so that normal installs don't install the whole test library.


def test_init_with_keys(self):
fd = frozendict(a=0, b=1)
self.assertMappingEqual(fd, dict(a=0, b=1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also be tempted to add something like:

fd1 = frozendict(a=0, b=1)
fd2 = frozendict(b=1, a=0)
self.assertMappingEqual(fd1, fd2)

(and then perhaps the opposite for FrozenOrderedDict)

@AlJohri
Copy link

AlJohri commented Nov 19, 2016

can this be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants