From 4ce5054f722ed921e115b5fc9de2c90cffd95a75 Mon Sep 17 00:00:00 2001 From: Ashley Roach Date: Thu, 23 Apr 2015 14:43:20 -0600 Subject: [PATCH 1/7] Add gitignore and initial test case --- .gitignore | 57 +++++++++++++++++++++++++ coding102-REST-python/test_coding102.py | 21 +++++++++ 2 files changed, 78 insertions(+) create mode 100644 .gitignore create mode 100644 coding102-REST-python/test_coding102.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba74660 --- /dev/null +++ b/.gitignore @@ -0,0 +1,57 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ diff --git a/coding102-REST-python/test_coding102.py b/coding102-REST-python/test_coding102.py new file mode 100644 index 0000000..3a1a1d7 --- /dev/null +++ b/coding102-REST-python/test_coding102.py @@ -0,0 +1,21 @@ + +import os +import unittest +import json + +class Coding102TestCase(unittest.TestCase): + + def setUp(self): + + print("set up") + + def tearDown(self): + + print("tear down") + + def test_hello(self): + + self.assertTrue(1) + +if __name__ == '__main__': + unittest.main() From 614b2c17c8f2cb00ac59d1fcedff81df6d6a47a4 Mon Sep 17 00:00:00 2001 From: Ashley Roach Date: Thu, 23 Apr 2015 16:29:15 -0600 Subject: [PATCH 2/7] Refactor first apic script to be testable. --- coding102-REST-python/{apic-em1.py => apicEm1.py} | 15 +++++++++------ coding102-REST-python/test_coding102.py | 8 +++++--- 2 files changed, 14 insertions(+), 9 deletions(-) rename coding102-REST-python/{apic-em1.py => apicEm1.py} (82%) diff --git a/coding102-REST-python/apic-em1.py b/coding102-REST-python/apicEm1.py similarity index 82% rename from coding102-REST-python/apic-em1.py rename to coding102-REST-python/apicEm1.py index a597589..77ae1fd 100644 --- a/coding102-REST-python/apic-em1.py +++ b/coding102-REST-python/apicEm1.py @@ -23,11 +23,14 @@ import requests -# put the ip address or dns of your apic-em controller in this url -url = 'https://sandboxapic.cisco.com/api/v0/host/1/3' +def hello(): + # put the ip address or dns of your apic-em controller in this url + url = 'https://sandboxapic.cisco.com/api/v0/host/1/3' -# this statement performs a GET on the specified url -response = requests.get(url, verify=False) + # this statement performs a GET on the specified url + response = requests.get(url, verify=False) -# print the json that is returned -print(response.text) \ No newline at end of file + # print the json that is returned + print(response.text) + + return response diff --git a/coding102-REST-python/test_coding102.py b/coding102-REST-python/test_coding102.py index 3a1a1d7..f7e6898 100644 --- a/coding102-REST-python/test_coding102.py +++ b/coding102-REST-python/test_coding102.py @@ -2,20 +2,22 @@ import os import unittest import json +import apicem_1 class Coding102TestCase(unittest.TestCase): def setUp(self): - print("set up") + self.app = apicem_1 def tearDown(self): print("tear down") def test_hello(self): - - self.assertTrue(1) + + result = self.app.hello() + self.assertTrue(result is not None) if __name__ == '__main__': unittest.main() From 6bbab0acab0801b46735b00ef728de9260d8c1f4 Mon Sep 17 00:00:00 2001 From: Ashley Roach Date: Thu, 23 Apr 2015 16:44:12 -0600 Subject: [PATCH 3/7] Add travis CI support --- .travis.yml | 12 ++++++++++++ requirements.txt | 2 ++ 2 files changed, 14 insertions(+) create mode 100644 .travis.yml create mode 100644 requirements.txt diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..89f0235 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: python +python: + - "3.3" + - "2.7" + - "2.6" +# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors +install: pip install -r requirements.txt --use-mirrors +# command to run tests, e.g. python setup.py test +script: python coding102-REST-python/test_coding102.py test +branches: + only: + - clus-updates diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7b27fd3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +requests==2.6.2 +wsgiref==0.1.2 From e533b00f449a939e7ab41e0399a1bf1806140d95 Mon Sep 17 00:00:00 2001 From: Ashley Roach Date: Thu, 23 Apr 2015 16:47:54 -0600 Subject: [PATCH 4/7] Fix import to use correct module name --- .travis.yml | 2 -- coding102-REST-python/test_coding102.py | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 89f0235..ef4a2e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: python python: - - "3.3" - "2.7" - - "2.6" # command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors install: pip install -r requirements.txt --use-mirrors # command to run tests, e.g. python setup.py test diff --git a/coding102-REST-python/test_coding102.py b/coding102-REST-python/test_coding102.py index f7e6898..4f42912 100644 --- a/coding102-REST-python/test_coding102.py +++ b/coding102-REST-python/test_coding102.py @@ -2,13 +2,13 @@ import os import unittest import json -import apicem_1 +import apicEm1 class Coding102TestCase(unittest.TestCase): def setUp(self): - self.app = apicem_1 + self.app = apicEm1 def tearDown(self): From 546665dfed805ce03ada9f9f649cbdcf6713198c Mon Sep 17 00:00:00 2001 From: Ashley Roach Date: Thu, 23 Apr 2015 16:49:57 -0600 Subject: [PATCH 5/7] Fix build script --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ef4a2e5..f233b72 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: # command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors install: pip install -r requirements.txt --use-mirrors # command to run tests, e.g. python setup.py test -script: python coding102-REST-python/test_coding102.py test +script: python coding102-REST-python/test_coding102.py branches: only: - clus-updates From 7e400386a16a4b81e3a000985f852ef1b7626821 Mon Sep 17 00:00:00 2001 From: Ashley Roach Date: Thu, 23 Apr 2015 17:41:41 -0600 Subject: [PATCH 6/7] Add build status from travis-ci to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c6c1071..93295b4 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,5 @@ Sample code for the Cisco DevNet Coding Skills Learning Labs You can find step-by-step tutorials that walk through this sample code on [Cisco DevNet](http://learninglabs.cisco.com). +# Build Status +[![Build Status](https://travis-ci.org/CiscoDevNet/coding-skills-sample-code.svg?branch=clus-updates)](https://travis-ci.org/CiscoDevNet/coding-skills-sample-code) From 96cc2a4a89706875c9f3f3991fce3be9fb05b3f6 Mon Sep 17 00:00:00 2001 From: Ashley Roach Date: Thu, 23 Apr 2015 22:23:10 -0600 Subject: [PATCH 7/7] Update basic script to actually print something out after refactoring it. --- coding102-REST-python/apicEm1.py | 17 ++++++++++++++--- coding102-REST-python/test_coding102.py | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/coding102-REST-python/apicEm1.py b/coding102-REST-python/apicEm1.py index 77ae1fd..b1d01cf 100644 --- a/coding102-REST-python/apicEm1.py +++ b/coding102-REST-python/apicEm1.py @@ -30,7 +30,18 @@ def hello(): # this statement performs a GET on the specified url response = requests.get(url, verify=False) - # print the json that is returned - print(response.text) - return response + +# This is special python syntax that enables you to run this script +# directly or use it as a module and test your code! +# Read more about it here: http://stackoverflow.com/questions/419163/what-does-if-name-main-do +if __name__ == '__main__': + + # this statement calls the method hello() and + # stores it into result + result = hello() + + # print the result as text. + # The responses library provides the .text convenience method to access + # the result body or text + print(result.text) diff --git a/coding102-REST-python/test_coding102.py b/coding102-REST-python/test_coding102.py index 4f42912..1930ba0 100644 --- a/coding102-REST-python/test_coding102.py +++ b/coding102-REST-python/test_coding102.py @@ -18,6 +18,7 @@ def test_hello(self): result = self.app.hello() self.assertTrue(result is not None) + self.assertEquals(200, result.status_code) if __name__ == '__main__': unittest.main()