def test_client_add_address(self): model = resources.get_model_class("contact") client = model(self.valid_data) model = resources.get_model_class("address") address = model() address.city = u"Duisburg" address.address1 = u"Foo Street" address.address2 = u"Appartment Bar" address.address_type = u"work" msg = "data is: %s" % (address.to_json()) self.assertTrue(len(address.to_json()) > 0, msg) msg = "data is: %s" % (client.to_json()) self.assertTrue(len(client.to_json()) > 0, msg) client.addresses = [address] msg = "data is: %s" % (client.to_json()) self.assertTrue(client.to_json().find(u"Duisburg") > 0, msg)
def _post_load(self,response): """ post load processing """ if response is not None and response.status_code == 200: types = helpers.pluralize(self.resource_type) body = json.loads(response.content, encoding='utf-8') self.total_entries = body['collection']['total_entries'] self.total_pages = body['collection']['total_pages'] self.current_page = body['collection']['current_page'] # in case this obj gets reused to run another query reset the result if self.total_entries == 0 and self.total_pages == 1: self.items = [] ## now get the items from the class factory for object in body[types]: item_cls = resources.get_model_class(self.resource_type) properties_dict = object[self.resource_type] new_dict = helpers.remove_properties_containing_None(properties_dict) item = item_cls(new_dict) ## add the items self.items.append(item) #autoload is true, so lets fetch all the other pages recursivly if(self.autoload == True and self.total_pages > 1 and page == None): for x in xrange(2,self.total_pages): self.load(x) return self else: raise SalesKingException("LOAD_ERROR","Fetching failed, an error happend",response)
def setUp(self): """ this test is slow as it gets over the air creates 201 resources of client named fb-counter if they do not exist """ super(LiveCollectionBaseTestCase,self).setUp() self.api_client = api.APIClient() # check if we need to setup 21 sk-clients valid_filters = {"organisation": self.org_name_token} col = collection.get_collection_instance("contact") col.set_filters(valid_filters) # executes the query col.load() items_per_page_cnt = col.get_per_page() self.total_pages = col.get_total_pages() total_entries = col.get_total_entries() current_page = col.get_current_page() msg = u"filters:%s,\nper page cnt %s, total pages %s, total entries %s, current_page %s" % ( valid_filters, items_per_page_cnt, self.total_pages, total_entries, current_page) #print msg if total_entries <= 21: # create 21 clients model = resources.get_model_class("contact", api=self.api_client) for x in xrange(total_entries, 21): self.valid_data["organisation"] = "%s%s" % (self.org_name_token, x) client = model(self.valid_data) client = client.save()
def test_initialize_client_empty_success(self): model = resources.get_model_class("contact") thrown = False client = None try: client=model() except ValueError, ex: thrown = True
def test_invoice_noLineitem_instaciated_mock_success(self): clnt = api.APIClient() klass = resources.get_model_class("invoice", api=clnt) invoice = klass() response = MockInvoiceNoLineItemResponse() obj = invoice.to_instance(response) self.assertIsNotNone(obj) self.assertEqual(obj.number, "R-069-2011-215")
def test_invoice_instaciated_mock_fails(self): clnt = api.APIClient() klass = resources.get_model_class("invoice", api=clnt) invoice = klass() response = MockInvoiceResponse() obj = invoice.to_instance(response) self.assertEqual(obj.number, "R-069-2011-215a") self.assertEqual(obj['client']['client']['number'], "K-01012-728")
def test_live_404_exception_thrown(self): clnt = api.APIClient() model = resources.get_model_class("client", api=clnt) client = model(self.valid_data) client.__api__.base_url += "foo" with self.assertRaises(exceptions.NotFound): client = client.save()
def test_initialize_client_required_missing_success(self): """ test schema loading patched required to False """ model = resources.get_model_class("contact") thrown = False try: model(self.required_missing_data) except ValueError, ex: thrown = True
def _response_item_to_object(self, resp_item): """ take json and make a resource out of it """ item_cls = resources.get_model_class(self.resource_type) properties_dict = resp_item[self.resource_type] new_dict = helpers.remove_properties_containing_None(properties_dict) # raises exception if something goes wrong obj = item_cls(new_dict) return obj
def test_contact_resource_save_and_delete_success(self): clnt = api.APIClient() model = resources.get_model_class("contact", api=clnt) client = model(self.valid_data) msg = u"data is: %s" % (client.to_json()) self.assertTrue(len(client.to_json()) > 0, msg) client = client.save() self.assertTrue(client.get_id() is not None) response = client.delete() self.assertEquals(response.status_code, 200)
def test_client_add_address_embedded_save_and_delete_success(self): model = resources.get_model_class("client") client = model(self.valid_data) model = resources.get_model_class("address") address = model() address.city = u"Duisburg" address.address1 = u"Foo Street" address.address2 = u"Appartment Bar" address.address_type = u"work" msg = "data is: %s" % (address.get_data()) self.assertTrue(len(address.get_data())>0,msg) msg = "data is: %s" % (client.get_data()) self.assertTrue(len(client.get_data())>0,msg) client.addresses = [address] msg = "data is: %s" % (client.get_data()) self.assertTrue(client.get_data().find(u"Duisburg")>0,msg) client = client.save() msg = "data is: %s" % (client.get_data()) self.assertTrue(client.get_data().find(u"Duisburg")>0,msg) self.assertTrue(client.id is not None ,msg)
def test_get_invoice_by_id(self): model = resources.get_model_class("invoice") client = model() # invoices/a6SaCWlb8r4yBvabxfpGMl client = client.load(id=u'a6SaCWlb8r4yBvabxfpGMl') self.assertEqual(client.get_id(), u'a6SaCWlb8r4yBvabxfpGMl')
def test_initialize_client_resource_success(self): model = resources.get_model_class("contact") client = model(self.valid_data) self.assertEquals(client.__class__.__name__,u'contact') self.assertEquals(client.organisation,u'salesking') self.assertEquals(client.first_name,u"Dow") #autoinitialize ? self.assertFalse(client.__api__ is None) msg = "data is: %s" % (client.to_json()) self.assertTrue(len(client.to_json()) > 0, msg) client.first_name = u"honey" self.assertEquals(client.first_name,u"honey") client.gender="male"
def test_client_save_response_mock_success(self): clnt = api.APIClient() model = resources.get_model_class("contact",api=clnt) client = model(self.valid_data) self.assertEquals(client.__class__.__name__,u'contact') msg = "data is: %s" % (client.to_json()) self.assertTrue(len(client.to_json()) > 0, msg) ### mock the save response ### response = self.mock_response obj = client.to_instance(response) self.assertEquals(obj.__class__.__name__,u'contact') self.assertEquals(obj.first_name, response.mock_first_name) self.assertEquals(obj.number, response.mock_number) self.assertEquals(obj.get_id(), response.mock_id) self.assertEquals(obj.id, response.mock_id) obj.last_name = u"lasst" self.assertEquals(obj.last_name, u"lasst")
def test_initialize_all_json_schemes_as_models(self): all_file_names = self._load_all_filenames_from_disk() all_files_cnt = len(all_file_names) needs_fix = [ 'company', # he ? ] ok = True for afile in all_file_names: schema_name = afile.split(".") is_loadable = (schema_name[0] not in needs_fix) if is_loadable: try: # print "loading %s" % (schema_name[0]) model = resources.get_model_class(schema_name[0]) empty_instance = model() except: print "failed as model %s" % schema_name[0] ok = False #raise self.assertTrue(ok)
def test_empty_invoice(self): model = resources.get_model_class("invoice") empty_instance = model() msg = u"%s" % empty_instance.schema self.assertIsNotNone(empty_instance.to_json(), msg=msg)
def test_empty_credit_note(self): model = resources.get_model_class("credit_note") empty_instance = model() msg = u"should have some data inside" self.assertIsNotNone(empty_instance, msg=msg) self.assertIsNotNone(empty_instance.to_json(), msg=msg)
def test_empty_recurring(self): model = resources.get_model_class("payment_reminder") empty_instance = model() msg = u"should have some data inside" self.assertIsNotNone(empty_instance, msg=msg) self.assertIsNotNone(empty_instance.to_json(), msg=msg)