Created
February 16, 2023 22:36
-
-
Save Sonictherocketman/dd7c06c3792a55cba420324bab365b19 to your computer and use it in GitHub Desktop.
ChatGPT Code Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import reverse | |
from rest_framework.test import APITestCase | |
from rest_framework import status | |
from .models import User | |
class UserTests(APITestCase): | |
def test_create_user(self): | |
""" | |
Ensure we can create a new user object. | |
""" | |
url = reverse('user-list') | |
data = {'username': 'testuser', 'email': '[email protected]'} | |
response = self.client.post(url, data, format='json') | |
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | |
self.assertEqual(User.objects.count(), 1) | |
self.assertEqual(User.objects.get().username, 'testuser') | |
def test_fetch_user(self): | |
""" | |
Ensure we can fetch an existing user object. | |
""" | |
user = User.objects.create(username='testuser', email='[email protected]') | |
url = reverse('user-detail', args=[user.pk]) | |
response = self.client.get(url, format='json') | |
self.assertEqual(response.status_code, status.HTTP_200_OK) | |
self.assertEqual(response.data['username'], 'testuser') | |
self.assertEqual(response.data['email'], '[email protected]') | |
def test_delete_user(self): | |
""" | |
Ensure we can delete an existing user object. | |
""" | |
user = User.objects.create(username='testuser', email='[email protected]') | |
url = reverse('user-detail', args=[user.pk]) | |
response = self.client.delete(url, format='json') | |
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | |
self.assertEqual(User.objects.count(), 0) | |
def test_update_user(self): | |
""" | |
Ensure we can update an existing user object. | |
""" | |
user = User.objects.create(username='testuser', email='[email protected]') | |
url = reverse('user-detail', args=[user.pk]) | |
data = {'username': 'updateduser'} | |
response = self.client.put(url, data, format='json') | |
self.assertEqual(response.status_code, status.HTTP_200_OK) | |
user.refresh_from_db() | |
self.assertEqual(user.username, 'updateduser') | |
def test_partial_update_user(self): | |
""" | |
Ensure we can partially update an existing user object. | |
""" | |
user = User.objects.create(username='testuser', email='[email protected]') | |
url = reverse('user-detail', args=[user.pk]) | |
data = {'username': 'updateduser'} | |
response = self.client.patch(url, data, format='json') | |
self.assertEqual(response.status_code, status.HTTP_200_OK) | |
user.refresh_from_db() | |
self.assertEqual(user.username, 'updateduser') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prompt: write for me a Swift RSS feed parser that takes in a string of XML data and produces a Post object with a title, description, pubdate, and enclosure fields. Use only the swift standard library | |
// Sure, here's an example Swift RSS feed parser that uses the Swift standard library to parse a string of XML data and produce a Post object with a title, description, pubdate, and enclosure fields: | |
import Foundation | |
struct Post { | |
let title: String | |
let description: String | |
let pubdate: Date | |
let enclosure: String? | |
} | |
func parseXML(xmlString: String) -> [Post] { | |
guard let xmlData = xmlString.data(using: .utf8) else { return [] } | |
let parser = XMLParser(data: xmlData) | |
let rssParserDelegate = RSSParserDelegate() | |
parser.delegate = rssParserDelegate | |
parser.parse() | |
return rssParserDelegate.posts | |
} | |
class RSSParserDelegate: NSObject, XMLParserDelegate { | |
var posts: [Post] = [] | |
var currentElement: String = "" | |
var currentTitle: String = "" | |
var currentDescription: String = "" | |
var currentPubDate: String = "" | |
var currentEnclosure: String? | |
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { | |
currentElement = elementName | |
if elementName == "enclosure" { | |
currentEnclosure = attributeDict["url"] | |
} | |
} | |
func parser(_ parser: XMLParser, foundCharacters string: String) { | |
switch currentElement { | |
case "title": | |
currentTitle += string | |
case "description": | |
currentDescription += string | |
case "pubDate": | |
currentPubDate += string | |
default: | |
break | |
} | |
} | |
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { | |
if elementName == "item" { | |
if let pubDate = parsePubDate(currentPubDate) { | |
let post = Post(title: currentTitle.trimmingCharacters(in: .whitespacesAndNewlines), | |
description: currentDescription.trimmingCharacters(in: .whitespacesAndNewlines), | |
pubdate: pubDate, | |
enclosure: currentEnclosure) | |
posts.append(post) | |
} | |
currentTitle = "" | |
currentDescription = "" | |
currentPubDate = "" | |
currentEnclosure = nil | |
} | |
currentElement = "" | |
} | |
private func parsePubDate(_ pubDate: String) -> Date? { | |
let dateFormatter = DateFormatter() | |
dateFormatter.locale = Locale(identifier: "en_US_POSIX") | |
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z" | |
return dateFormatter.date(from: pubDate) | |
} | |
} | |
// To use this parser, you can call the parseXML function and pass in a string of XML data. It will return an array of Post objects: | |
let xmlString = """ | |
<?xml version="1.0" encoding="UTF-8"?> | |
<rss version="2.0"> | |
<channel> | |
<title>Example RSS Feed</title> | |
<item> | |
<title>Post 1</title> | |
<description>This is the first post.</description> | |
<pubDate>Tue, 15 Feb 2022 12:00:00 -0500</pubDate> | |
<enclosure url="https://example.com/post1.jpg" type="image/jpeg"/> | |
</item> | |
<item> | |
<title>Post 2</title> | |
<description>This is the second post.</description> | |
<pubDate>Mon, 14 Feb 2022 12:00:00 -0500</pubDate> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment