<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> from contextlib import contextmanager <EOL> import requests <EOL> from pants . cache . artifact import ArtifactError <EOL> from pants . cache . artifact_cache import NonfatalArtifactCacheError , UnreadableArtifact <EOL> from pants . goal . artifact_cache_stats import ArtifactCacheStats <EOL> from pants . util . contextutil import temporary_dir <EOL> from pants_test . base_test import BaseTest <EOL> class ArtifactCacheStatsTest ( BaseTest ) : <EOL> TEST_CACHE_NAME_1 = '<STR_LIT>' <EOL> TEST_CACHE_NAME_2 = '<STR_LIT>' <EOL> TEST_LOCAL_ERROR = UnreadableArtifact ( '<STR_LIT:foo>' , ArtifactError ( '<STR_LIT>' ) ) <EOL> TEST_REMOTE_ERROR = UnreadableArtifact ( <EOL> '<STR_LIT:bar>' , <EOL> NonfatalArtifactCacheError ( requests . exceptions . ConnectionError ( '<STR_LIT>' ) ) <EOL> ) <EOL> TEST_SPEC_A = '<STR_LIT>' <EOL> TEST_SPEC_B = '<STR_LIT>' <EOL> TEST_SPEC_C = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> super ( ArtifactCacheStatsTest , self ) . setUp ( ) <EOL> self . target_a = self . make_target ( spec = self . TEST_SPEC_A ) <EOL> self . target_b = self . make_target ( spec = self . TEST_SPEC_B ) <EOL> self . target_c = self . make_target ( spec = self . TEST_SPEC_C ) <EOL> def test_add_hits ( self ) : <EOL> expected_stats = [ <EOL> { <EOL> '<STR_LIT>' : self . TEST_CACHE_NAME_2 , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ( self . TEST_SPEC_A , str ( self . TEST_LOCAL_ERROR . err ) ) ] <EOL> } , <EOL> { <EOL> '<STR_LIT>' : self . TEST_CACHE_NAME_1 , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : [ ( self . TEST_SPEC_B , '<STR_LIT>' ) ] , <EOL> '<STR_LIT>' : [ ( self . TEST_SPEC_C , str ( self . TEST_REMOTE_ERROR . err ) ) ] <EOL> } , <EOL> ] <EOL> expected_hit_or_miss_files = { <EOL> '<STR_LIT>' . format ( self . TEST_CACHE_NAME_2 ) : <EOL> '<STR_LIT>' . format ( self . TEST_SPEC_A , str ( self . TEST_LOCAL_ERROR . err ) ) , <EOL> '<STR_LIT>' . format ( self . TEST_CACHE_NAME_1 ) : <EOL> '<STR_LIT>' . format ( self . TEST_SPEC_B ) , <EOL> '<STR_LIT>' . format ( self . TEST_CACHE_NAME_1 ) : <EOL> '<STR_LIT>' . format ( self . TEST_SPEC_C , str ( self . TEST_REMOTE_ERROR . err ) ) , <EOL> } <EOL> with self . mock_artifact_cache_stats ( expected_stats , <EOL> expected_hit_or_miss_files = expected_hit_or_miss_files ) as artifact_cache_stats : <EOL> artifact_cache_stats . add_hits ( self . TEST_CACHE_NAME_1 , [ self . target_b ] ) <EOL> artifact_cache_stats . add_misses ( self . TEST_CACHE_NAME_1 , [ self . target_c ] , <EOL> [ self . TEST_REMOTE_ERROR ] ) <EOL> artifact_cache_stats . add_misses ( self . TEST_CACHE_NAME_2 , [ self . target_a ] , <EOL> [ self . TEST_LOCAL_ERROR ] ) <EOL> @ contextmanager <EOL> def mock_artifact_cache_stats ( self , <EOL> expected_stats , <EOL> expected_hit_or_miss_files = None ) : <EOL> with temporary_dir ( ) as tmp_dir : <EOL> artifact_cache_stats = ArtifactCacheStats ( tmp_dir ) <EOL> yield artifact_cache_stats <EOL> self . assertEquals ( expected_stats , artifact_cache_stats . get_all ( ) ) <EOL> self . assertEquals ( sorted ( list ( expected_hit_or_miss_files . keys ( ) ) ) , <EOL> sorted ( os . listdir ( tmp_dir ) ) ) <EOL> for hit_or_miss_file in expected_hit_or_miss_files . keys ( ) : <EOL> with open ( os . path . join ( tmp_dir , hit_or_miss_file ) ) as hit_or_miss_saved : <EOL> self . assertEquals ( expected_hit_or_miss_files [ hit_or_miss_file ] , hit_or_miss_saved . read ( ) ) </s>
