Skip to content

Commit 0636063

Browse files
added initial test for creating courses
1 parent 7fac85a commit 0636063

3 files changed

Lines changed: 91 additions & 1 deletion

File tree

classroom/snippets/base_test.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
import sys
17+
import unittest
18+
import httplib2
19+
from oauth2client.client import GoogleCredentials
20+
from googleapiclient import errors
21+
from googleapiclient.discovery import build
22+
from oauth2client import file, client, tools
23+
24+
SCOPES = 'https://www.googleapis.com/auth/classroom.courses'
25+
26+
27+
class BaseTest(unittest.TestCase):
28+
@classmethod
29+
def setUpClass(cls):
30+
cls.credentials = cls.create_credentials()
31+
http = cls.credentials.authorize(httplib2.Http())
32+
cls.credentials.refresh(http)
33+
cls.service = build('classroom', 'v1', http=http)
34+
cls.stdout = sys.stdout
35+
sys.stdout = None
36+
37+
@classmethod
38+
def tearDownClass(cls):
39+
# Restore STDOUT.
40+
sys.stdout = cls.stdout
41+
42+
@classmethod
43+
def create_credentials(cls):
44+
store = file.Storage('token.json')
45+
credentials = None
46+
if not credentials or credentials.invalid:
47+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
48+
credentials = tools.run_flow(flow, store)
49+
return credentials
50+
51+
52+
if __name__ == '__main__':
53+
unittest.main()
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def create_course(self):
4040
course = service.courses().create(body=course).execute()
4141
print('Course created:', course.get('name'), course.get('id'))
4242
# [END classroom_create_course]
43+
return course
4344

4445
def get_course(self, course_id):
4546
""" Retrieves a classroom course by its id. """
@@ -134,7 +135,7 @@ def add_alias_existing(self, course_id):
134135
}
135136
try:
136137
courseAlias = service.courses().aliases().create(
137-
courseId=courseId,
138+
courseId=course_id,
138139
body=courseAlias).execute()
139140
except errors.HttpError:
140141
print('Alias Creation Failed')
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
# Copyright 2018 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import unittest
17+
from pprint import pformat
18+
from base_test import BaseTest
19+
from classroom_snippets import ClassroomSnippets
20+
21+
22+
class SnippetsTest(BaseTest):
23+
24+
@classmethod
25+
def setUpClass(cls):
26+
super(SnippetsTest, cls).setUpClass()
27+
cls.snippets = ClassroomSnippets(cls.service)
28+
29+
def test_create_course(self):
30+
course = self.snippets.create_course()
31+
self.assertIsNotNone(course)
32+
# delete course as part of cleanup
33+
34+
35+
if __name__ == '__main__':
36+
unittest.main()

0 commit comments

Comments
 (0)