我已经令人满意地处理了选择1所有其他人都在空中。 我已将新书附加到文件末尾,但我无法在现有书籍中搜索查询。我也没有正确的代码来处理发行书籍并将其从记录中删除并将其从students.txt文件中删除。
from django.test import TestCase, Client
from django.test.client import RequestFactory
from django.contrib.auth.models import AnonymousUser, User
from .views import my_view
from .forms import MyForm
from django.contrib.sessions.middleware import SessionMiddleware
# If Python >= 3.4
from unittest.mock import patch, MagicMock
# Else
from mock import patch, MagicMock
class SimpleTest(TestCase):
def setUp(self):
# Create a RequestFactory accessible by the entire class.
self.factory = RequestFactory()
# Create a new user object accessible by the entire class.
self.user = User.objects.create_user(username='username',
email='email', password='password')
def test_my_view(self):
# Create an instance of a GET request.
request = self.factory.get('/my-url/')
# Middleware is not supported so simulate a
# logged-in user by setting request.user.
request.user = self.user
# Or add anonymous user to request.
request.user = AnonymousUser()
# Test view() at '/my-url/'
response = my_view(request)
self.assertEqual(response.status_code, 200)
@patch('app.models.ModelName.save', MagicMock(name="save"))
def test_my_form_view_with_factory(self):
# Set up form data.
form_data = {'something': 'something'}
# Create an instance of a POST request.
request = self.factory.post('/my-form-url/', form_data)
# Simulate logged-in user
request.user = self.user
# Setup session.
middleware = SessionMiddleware()
middleware.process_request(request)
request.session.save()
# Or you should just be able to do
request.session['somekey'] = 'test'
request.session.save()
# Get response from form view, and test passing
# request/data to form.
form = MyForm(request=request, data=form_data)
response = my_form_view(request)
self.assertTrue(form.is_valid())
self.assertEqual(response.status_code, 200)
# If model form you can do
self.assertTrue(ModelName.save.called)
@patch('app.models.ModelName.save', MagicMock(name="save"))
def test_my_form_view_with_client(self):
# Use Client instead of RequestFactory.
self.client = Client()
# Login with Client.
self.client.login(username='username', password='password')
# Set up form data.
form_data = {'something': 'something'}
# Get/set session.
session = self.client.session
session['somekey'] = 'test'
session.save()
# Get response with Client.
response = self.client.post('/my-form-url/', form_data)
self.assertEqual(response.status_code, 200)
# If model form you can do
self.assertTrue(ModelName.save.called)
答案 0 :(得分:0)
您应该做的第一件事是为书籍定义数据结构。类似的东西:
struct Book {
std::string name;
std::string author;
std::string ISBN;
}
然后你可以重载输入和输出操作符
std::ostream& operator<<( std::ostream &o, const Book& b) {
o << b.name;
o << b.author;
o << b.ISBN;
return output;
}
和
std::istream& operator>>( std::istream& in, Book& b ) {
in >> b.name;
in >> b.author;
in >> b.ISBN;
return in;
}
完成此操作后,您可以使用任何输入流或输出流(例如std::cout
/ std::cin
或ifstream
)来读取/写入图书,例如
Book b;
std::cin >> b;
std::cout << b << std::endl;
其实你的问题太宽泛了,我只能给你一些提示。你必须从你的讲座或一些书中学到这一切。但是,如果我是一名教师,以上是我将要讲的关于C ++的第一件事。