def test_contributor(self): contributor = Contributor.from_scm_log( 'Author: Jonathan Bedard <*****@*****.**>') commit = Commit(revision=1, identifier=1, author=contributor) self.assertEqual(commit.author, contributor) commit = Commit(revision=1, identifier=1, author=Contributor.Encoder().default(contributor)) self.assertEqual(commit.author, contributor)
def test_json_decode(self): contributor = Contributor.from_scm_log( 'Author: Jonathan Bedard <*****@*****.**>') commit_a = Commit(revision=1, hash='c3bd784f8b88bd03f64467ddd3304ed8be28acbe', identifier='1@main', timestamp=1000, author=Contributor.Encoder().default(contributor), message='Message') dictionary = json.loads(json.dumps(commit_a, cls=Commit.Encoder)) commit_b = Commit(**dictionary) self.assertEqual(commit_a, commit_b)
def default(self, obj): if not isinstance(obj, Commit): return super(Commit.Encoder, self).default(obj) result = dict() for attribute in [ 'hash', 'revision', 'branch', 'timestamp', 'message' ]: value = getattr(obj, attribute, None) if value is not None: result[attribute] = value if obj.author: result['author'] = Contributor.Encoder().default(obj.author) if obj.identifier is not None: result['identifier'] = str(obj) return result
def test_from_json_str(self): contributor = Contributor.from_scm_log( 'Author: [email protected] <*****@*****.**>') self.assertEqual( Commit.from_json('''{ "revision": 1, "hash": "c3bd784f8b88bd03f64467ddd3304ed8be28acbe", "identifier": "1@main", "timestamp": 1000, "author": "*****@*****.**", "message": "Message" }'''), Commit(revision=1, hash='c3bd784f8b88bd03f64467ddd3304ed8be28acbe', identifier='1@main', timestamp=1000, author=Contributor.Encoder().default(contributor), message='Message'), )
def default(self, obj): if isinstance(obj, dict): return {key: self.default(value) for key, value in obj.items()} if isinstance(obj, list): return [self.default(value) for value in obj] if not isinstance(obj, Commit): return super(Commit.Encoder, self).default(obj) result = dict() for attribute in [ 'hash', 'revision', 'branch', 'timestamp', 'order', 'message', 'repository_id' ]: value = getattr(obj, attribute, None) if value is not None: result[attribute] = value if obj.author: result['author'] = Contributor.Encoder().default(obj.author) if obj.identifier is not None: result['identifier'] = str(obj) return result