def test_not_valid_stream(self):
        """Check whether it prints an error when parsing invalid streams"""

        with self.assertRaisesRegex(InvalidFormatError,
                                    MOZILLIANS_INVALID_JSON_FORMAT_ERROR):
            s = self.read_file(datadir('mozillians_invalid.json'))
            MozilliansParser(s)

        with self.assertRaisesRegex(InvalidFormatError,
                                    MOZILLIANS_IDS_MISSING_KEYS_ERROR):
            s = self.read_file(datadir('mozillians_ids_missing_keys.json'))
            MozilliansParser(s)
    def test_valid_organizations_stream(self):
        """Check whether it parses organizations section from a valid stream"""

        stream = self.read_file(datadir('mozillians_valid.json'))

        parser = MozilliansParser(stream)
        orgs = parser.organizations

        # Check parsed organizations
        self.assertEqual(len(orgs), 1)

        org = orgs[0]
        self.assertIsInstance(org, Organization)
        self.assertEqual(org.name, MOZILLIANS_ORG)
    def test_valid_identities_stream(self):
        """Check insertion of valid data from a file"""

        stream = self.read_file(datadir('mozillians_valid.json'))

        parser = MozilliansParser(stream, source='unknown')
        uids = parser.identities

        # Check parsed identities
        self.assertEqual(len(uids), 3)

        # John Doe identity
        uid = uids[0]
        self.assertIsInstance(uid, UniqueIdentity)
        self.assertEqual(uid.uuid, 'john_doe')
        self.assertEqual(len(uid.identities), 2)

        id0 = uid.identities[0]
        self.assertIsInstance(id0, Identity)
        self.assertEqual(id0.id, None)
        self.assertEqual(id0.name, None)
        self.assertEqual(id0.email, None)
        self.assertEqual(id0.username, 'john_doe')
        self.assertEqual(id0.uuid, 'john_doe')
        self.assertEqual(id0.source, 'unknown')

        # IRC identity
        id1 = uid.identities[1]
        self.assertIsInstance(id1, Identity)
        self.assertEqual(id1.id, None)
        self.assertEqual(id1.name, None)
        self.assertEqual(id1.email, None)
        self.assertEqual(id1.username, 'jdoe')
        self.assertEqual(id1.uuid, 'john_doe')
        self.assertEqual(id1.source, 'unknown')

        enrollments = uid.enrollments
        self.assertEqual(len(enrollments), 1)

        rol0 = enrollments[0]
        self.assertIsInstance(rol0.organization, Organization)
        self.assertEqual(rol0.organization.name, MOZILLIANS_ORG)
        self.assertEqual(rol0.start, datetime.datetime(2015, 6, 1, 0, 0, 0))

        # Jane Rae unique identity
        uid = uids[1]
        self.assertIsInstance(uid, UniqueIdentity)
        self.assertEqual(uid.uuid, 'jrae')
        self.assertEqual(len(uid.identities), 3)

        id0 = uid.identities[0]
        self.assertIsInstance(id0, Identity)
        self.assertEqual(id0.id, None)
        self.assertEqual(id0.name, 'Jane Rae')
        self.assertEqual(id0.email, None)
        self.assertEqual(id0.username, 'jrae')
        self.assertEqual(id0.uuid, 'jrae')
        self.assertEqual(id0.source, 'unknown')

        id1 = uid.identities[1]
        self.assertIsInstance(id1, Identity)
        self.assertEqual(id1.id, None)
        self.assertEqual(id1.name, 'Jane Rae')
        self.assertEqual(id1.email, '*****@*****.**')
        self.assertEqual(id1.username, 'jrae')
        self.assertEqual(id1.uuid, 'jrae')
        self.assertEqual(id1.source, 'unknown')

        id2 = uid.identities[2]
        self.assertIsInstance(id2, Identity)
        self.assertEqual(id2.id, None)
        self.assertEqual(id2.name, 'Jane Rae')
        self.assertEqual(id2.email, '*****@*****.**')
        self.assertEqual(id2.username, 'jrae')
        self.assertEqual(id2.uuid, 'jrae')
        self.assertEqual(id2.source, 'unknown')

        enrollments = uid.enrollments
        self.assertEqual(len(enrollments), 1)

        rol0 = enrollments[0]
        self.assertIsInstance(rol0.organization, Organization)
        self.assertEqual(rol0.organization.name, MOZILLIANS_ORG)
        self.assertEqual(rol0.start, datetime.datetime(1900, 1, 1, 0, 0, 0))

        # John Smith unique identity
        uid = uids[2]
        self.assertIsInstance(uid, UniqueIdentity)
        self.assertEqual(uid.uuid, 'jsmith')
        self.assertEqual(len(uid.identities), 1)

        # Only one identity found. No alternative emails
        # and ircname is equal to username value
        id0 = uid.identities[0]
        self.assertIsInstance(id0, Identity)
        self.assertEqual(id0.id, None)
        self.assertEqual(id0.name, 'John Smith')
        self.assertEqual(id0.email, '*****@*****.**')
        self.assertEqual(id0.username, 'jsmith')
        self.assertEqual(id0.uuid, 'jsmith')
        self.assertEqual(id0.source, 'unknown')

        enrollments = uid.enrollments
        self.assertEqual(len(enrollments), 1)

        rol0 = enrollments[0]
        self.assertIsInstance(rol0.organization, Organization)
        self.assertEqual(rol0.organization.name, MOZILLIANS_ORG)
        self.assertEqual(rol0.start, datetime.datetime(1900, 1, 1, 0, 0, 0))
    def test_none_stream(self):
        """Check whether it raises an exception when the stream is None"""

        with self.assertRaisesRegex(InvalidFormatError,
                                    MOZILLIANS_STREAM_INVALID_ERROR):
            MozilliansParser(None)