Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_validators.py
# Copyright (C) 2015 The Software Heritage developers | # Copyright (C) 2015 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import datetime | import datetime | ||||
import unittest | import unittest | ||||
from nose.tools import istest | from swh.model import exceptions, hashutil, validators | ||||
from swh.model import validators, hashutil, exceptions | |||||
class TestValidators(unittest.TestCase): | class TestValidators(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
self.valid_visible_content = { | self.valid_visible_content = { | ||||
'status': 'visible', | 'status': 'visible', | ||||
'length': 5, | 'length': 5, | ||||
'data': b'1984\n', | 'data': b'1984\n', | ||||
Show All 13 Lines | def setUp(self): | ||||
'sha1_git': self.valid_visible_content['sha1_git'], | 'sha1_git': self.valid_visible_content['sha1_git'], | ||||
'origin': 42, | 'origin': 42, | ||||
} | } | ||||
self.invalid_content_hash_mismatch = self.valid_visible_content.copy() | self.invalid_content_hash_mismatch = self.valid_visible_content.copy() | ||||
self.invalid_content_hash_mismatch.update( | self.invalid_content_hash_mismatch.update( | ||||
hashutil.hash_data(b"this is not the data you're looking for")) | hashutil.hash_data(b"this is not the data you're looking for")) | ||||
@istest | def test_validate_content(self): | ||||
def validate_content(self): | |||||
self.assertTrue( | self.assertTrue( | ||||
validators.validate_content(self.valid_visible_content)) | validators.validate_content(self.valid_visible_content)) | ||||
self.assertTrue( | self.assertTrue( | ||||
validators.validate_content(self.valid_absent_content)) | validators.validate_content(self.valid_absent_content)) | ||||
@istest | def test_validate_content_hash_mismatch(self): | ||||
def validate_content_hash_mismatch(self): | |||||
with self.assertRaises(exceptions.ValidationError) as cm: | with self.assertRaises(exceptions.ValidationError) as cm: | ||||
validators.validate_content(self.invalid_content_hash_mismatch) | validators.validate_content(self.invalid_content_hash_mismatch) | ||||
# All the hashes are wrong. The exception should be of the form: | # All the hashes are wrong. The exception should be of the form: | ||||
# ValidationError({ | # ValidationError({ | ||||
# NON_FIELD_ERRORS: [ | # NON_FIELD_ERRORS: [ | ||||
# ValidationError('content-hash-mismatch', 'sha1'), | # ValidationError('content-hash-mismatch', 'sha1'), | ||||
# ValidationError('content-hash-mismatch', 'sha1_git'), | # ValidationError('content-hash-mismatch', 'sha1_git'), | ||||
Show All 17 Lines |