|
| 1 | +import sys |
| 2 | +from nose import with_setup |
| 3 | + |
| 4 | +import coderwall |
| 5 | +from coderwall import CoderWall |
| 6 | + |
| 7 | +real_get_json_data = coderwall.get_json_data |
| 8 | + |
| 9 | + |
| 10 | +def get_mock_data(*username): |
| 11 | + return """ |
| 12 | + { |
| 13 | + "name": "Cameron Currie", |
| 14 | + "location": "Texas", |
| 15 | + "endorsements": 7, |
| 16 | + "badges": [ |
| 17 | + { |
| 18 | + "name": "Test Badge 1", |
| 19 | + "description": "A test badge 1", |
| 20 | + "badge": "http://test.badge1" |
| 21 | + }, |
| 22 | + { |
| 23 | + "name": "Test Badge 2", |
| 24 | + "description": "A test badge 2", |
| 25 | + "badge": "http://test.badge2" |
| 26 | + } |
| 27 | + ] |
| 28 | + } |
| 29 | + """ |
| 30 | + |
| 31 | + |
| 32 | +def setUp(): |
| 33 | + coderwall.get_json_data = get_mock_data |
| 34 | + |
| 35 | + |
| 36 | +def tearDown(): |
| 37 | + coderwall.get_json_data = real_get_json_data |
| 38 | + |
| 39 | + |
| 40 | +@with_setup(setUp, tearDown) |
| 41 | +def test_parse_json_data(): |
| 42 | + result = coderwall.parse_json_data(get_mock_data()) |
| 43 | + |
| 44 | + assert result[0] == 'Cameron Currie' |
| 45 | + assert result[1] == 'Texas' |
| 46 | + assert result[2] == 7 |
| 47 | + |
| 48 | + assert len(result[3]) == 2 |
| 49 | + assert result[3][0]['name'] == 'Test Badge 1' |
| 50 | + assert result[3][0]['badge'] == 'http://test.badge1' |
| 51 | + |
| 52 | + |
| 53 | +@with_setup(setUp, tearDown) |
| 54 | +def test_parse_badges(): |
| 55 | + raw_badges = coderwall.parse_json_data(get_mock_data())[3] |
| 56 | + badges = coderwall.parse_badges(raw_badges) |
| 57 | + |
| 58 | + assert len(badges) == 2 |
| 59 | + assert badges[0].name == 'Test Badge 1' |
| 60 | + assert badges[0].image_uri == 'http://test.badge1' |
| 61 | + |
| 62 | + |
| 63 | +@with_setup(setUp, tearDown) |
| 64 | +def test_coderwall_parsing(): |
| 65 | + cwc = CoderWall('cwc') |
| 66 | + |
| 67 | + assert cwc.username == 'cwc' |
| 68 | + assert cwc.location == 'Texas' |
| 69 | + assert cwc.endorsements == 7 |
| 70 | + |
| 71 | + assert len(cwc.badges) == 2 |
| 72 | + assert cwc.badges[0].name == 'Test Badge 1' |
| 73 | + assert cwc.badges[0].image_uri == 'http://test.badge1' |
| 74 | + |
| 75 | + |
| 76 | +@with_setup(setUp, tearDown) |
| 77 | +def test_coderwall_to_str(): |
| 78 | + cwc = CoderWall('cwc') |
| 79 | + expStr2 = """Cameron Currie (cwc), Texas, Endorsed 7 times: [Badge(name=u'Test Badge 1',description=u'A test badge 1',image_uri=u'http://test.badge1'), Badge(name=u'Test Badge 2',description=u'A test badge 2',image_uri=u'http://test.badge2')]""" |
| 80 | + expStr3 = """Cameron Currie (cwc), Texas, Endorsed 7 times: [Badge(name='Test Badge 1',description='A test badge 1',image_uri='http://test.badge1'), Badge(name='Test Badge 2',description='A test badge 2',image_uri='http://test.badge2')]""" |
| 81 | + |
| 82 | + if sys.version_info[0] >= 3: |
| 83 | + expStr = expStr3 |
| 84 | + else: |
| 85 | + expStr = expStr2 |
| 86 | + |
| 87 | + assert str(cwc) == expStr, "str(cwc) was: %s, expected: %s" % (str(cwc), expStr) |
0 commit comments