1#! /usr/bin/python 2 3# Copyright 2014 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Unit tests for resource_method.py.""" 8 9import mox 10import unittest 11 12import common 13from fake_device_server import common_util 14from fake_device_server import resource_method 15from fake_device_server import resource_delegate 16from fake_device_server import server_errors 17 18 19class ResourceMethodTest(mox.MoxTestBase): 20 """Tests for the ResourceMethod class.""" 21 22 def setUp(self): 23 """Sets up resource_method object and dict of resources.""" 24 mox.MoxTestBase.setUp(self) 25 self.resources = {} 26 self.resource_method = resource_method.ResourceMethod( 27 resource_delegate.ResourceDelegate(self.resources)) 28 29 30 def testPatch(self): 31 """Tests that we correctly patch a resource.""" 32 expected_resource = dict(id=1234, blah='hi') 33 update_resource = dict(blah='hi') 34 self.resources[(1234, None)] = dict(id=1234) 35 36 self.mox.StubOutWithMock(common_util, 'parse_serialized_json') 37 38 common_util.parse_serialized_json().AndReturn(update_resource) 39 40 self.mox.ReplayAll() 41 returned_json = self.resource_method.PATCH(1234) 42 self.assertEquals(expected_resource, returned_json) 43 self.mox.VerifyAll() 44 45 46 def testPut(self): 47 """Tests that we correctly replace a resource.""" 48 update_resource = dict(id=12345, blah='hi') 49 self.resources[(12345, None)] = dict(id=12345) 50 51 self.mox.StubOutWithMock(common_util, 'parse_serialized_json') 52 53 common_util.parse_serialized_json().AndReturn(update_resource) 54 55 self.mox.ReplayAll() 56 returned_json = self.resource_method.PUT(12345) 57 self.assertEquals(update_resource, returned_json) 58 self.mox.VerifyAll() 59 60 self.mox.ResetAll() 61 62 # Ticket id doesn't match. 63 update_resource = dict(id=12346, blah='hi') 64 common_util.parse_serialized_json().AndReturn(update_resource) 65 66 self.mox.ReplayAll() 67 self.assertRaises(server_errors.HTTPError, 68 self.resource_method.PUT, 12345) 69 self.mox.VerifyAll() 70 71 72if __name__ == '__main__': 73 unittest.main() 74