def test_valid_organizations_stream(self):
        """Check whether it parses organizations section from a valid stream"""

        stream = self.read_file('data/stackalytics_valid.json')

        parser = StackalyticsParser(stream)
        orgs = parser.organizations
        orgs.sort(key=lambda x: x.name)

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

        # Bitergia entry
        org = orgs[0]
        self.assertIsInstance(org, Organization)
        self.assertEqual(org.name, 'Bitergia')

        doms = org.domains
        self.assertEqual(len(org.domains), 2)

        dom = doms[0]
        self.assertIsInstance(dom, Domain)
        self.assertEqual(dom.domain, 'bitergia.net')

        dom = doms[1]
        self.assertIsInstance(dom, Domain)
        self.assertEqual(dom.domain, 'bitergia.com')

        # Example entry
        org = orgs[1]
        self.assertIsInstance(org, Organization)
        self.assertEqual(org.name, 'Example')
        self.assertEqual(len(org.domains), 0)
    def test_not_valid_stream(self):
        """Check whether it prints an error when parsing invalid streams"""

        with self.assertRaisesRegexp(InvalidFormatError,
                                     STACKALYTICS_INVALID_JSON_FORMAT_ERROR):
            s = self.read_file('data/stackalytics_invalid.json')
            StackalyticsParser(s)

        with self.assertRaisesRegexp(InvalidFormatError,
                                     STACKALYTICS_IDS_MISSING_KEYS_ERROR):
            s = self.read_file('data/stackalytics_ids_missing_keys.json')
            StackalyticsParser(s)

        with self.assertRaisesRegexp(InvalidFormatError,
                                     STACKALYTICS_ORGS_MISSING_KEYS_ERROR):
            s = self.read_file('data/stackalytics_orgs_missing_keys.json')
            StackalyticsParser(s)
    def test_valid_identities_stream(self):
        """Check insertion of valid data from a file"""

        stream = self.read_file('data/stackalytics_valid.json')

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

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

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

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

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

        rol0 = enrollments[0]
        self.assertIsInstance(rol0, Enrollment)
        self.assertIsInstance(rol0.organization, Organization)
        self.assertEqual(rol0.organization.name, 'Bitergia')
        self.assertEqual(rol0.start, MIN_PERIOD_DATE)
        self.assertEqual(rol0.end, MAX_PERIOD_DATE)

        rol1 = enrollments[1]
        self.assertIsInstance(rol1, Enrollment)
        self.assertIsInstance(rol1.organization, Organization)
        self.assertEqual(rol1.organization.name, 'Example')
        self.assertEqual(rol1.start, MIN_PERIOD_DATE)
        self.assertEqual(rol1.end, datetime.datetime(2010, 1, 1, 0, 0, 0))

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

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

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

        id2 = uid.identities[2]
        self.assertIsInstance(id2, Identity)
        self.assertEqual(id2.id, None)
        self.assertEqual(id2.name, 'John Smith')
        self.assertEqual(id2.email, None)
        self.assertEqual(id2.username, 'jsmith')
        self.assertEqual(id2.uuid, 'John Smith')
        self.assertEqual(id2.source, 'unknown:gerrit')

        id3 = uid.identities[3]
        self.assertIsInstance(id3, Identity)
        self.assertEqual(id3.id, None)
        self.assertEqual(id3.name, 'John Smith')
        self.assertEqual(id3.email, None)
        self.assertEqual(id3.username, '0-jsmith')
        self.assertEqual(id3.uuid, 'John Smith')
        self.assertEqual(id3.source, 'unknown:launchpad')

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

        rol0 = enrollments[0]
        self.assertIsInstance(rol0, Enrollment)
        self.assertIsInstance(rol0.organization, Organization)
        self.assertEqual(rol0.organization.name, 'Example')
        self.assertEqual(rol0.start, MIN_PERIOD_DATE)
        self.assertEqual(rol0.end, MAX_PERIOD_DATE)
    def test_none_stream(self):
        """Check whether it raises an exception when the stream is None"""

        with self.assertRaisesRegexp(InvalidFormatError,
                                     STACKALYTICS_STREAM_INVALID_ERROR):
            StackalyticsParser(None)