Compare commits

...

4 Commits

Author SHA1 Message Date
Aby James
cd941fb20f Merge 7c57e650da into 88e5be6e4b 2024-09-16 11:58:35 -07:00
Julien Le Coupanec
88e5be6e4b Merge pull request #391 from Philip-Walsh/master
fix spelling and whitespace in cheatsheets
2024-08-24 23:07:02 +02:00
Philip-Walsh
2f9037b24e fix spelling and whitespace in cheatsheets 2024-08-22 12:02:10 +01:00
Aby James
7c57e650da Update django.py cheat sheet to include unit tests and coverage
Extend Django.py Cheat Sheet: Incorporate Unit Testing and Coverage Information

This commit enhances the existing Django.py cheat sheet by integrating guidance on unit testing and coverage analysis. The update includes detailed instructions on writing unit tests within Django applications, setting up test fixtures, executing tests locally, and leveraging coverage tools for assessing code coverage.

Changes:
- Added comprehensive instructions on writing unit tests for Django applications.
- Included guidance on setting up test fixtures and organizing test files.
- Provided commands and examples for running tests locally using Django's testing framework.
- Introduced information on using test coverage tools like `coverage.py` to assess code coverage.
- Enhanced the documentation to ensure clarity and completeness.

This update aims to empower developers with the necessary knowledge and resources to ensure the robustness and reliability of their Django projects through effective testing and coverage analysis.
2024-03-19 00:00:17 +05:30
9 changed files with 172 additions and 98 deletions

View File

@@ -118,3 +118,77 @@ django-admin version # display the current django version
# 8. $ django-admin startproject myproject
# 9. $ django-admin startapp myapp
# 10. $ python manage.py runserver
# *****************************************************************************
# Writing unit tests in django projects
# *****************************************************************************
# 1. Set Up Your Test Environment:
# Before writing tests, ensure that you have your Django project set up. Typically, you'll have a structure similar to this:
myproject/
myproject/
__init__.py
settings.py
urls.py
wsgi.py
myapp/
__init__.py
models.py
views.py
tests.py # This is where your tests will go
manage.py
venv/
# 2. Create test cases by subclassing django.test.TestCase or django.test.TransactionTestCase
# myapp/tests.py
```
from django.test import TestCase
from .models import YourModel
class YourModelTestCase(TestCase):
@classmethod
def setUpClass(cls):
# Set up resources for the entire test case class
pass
@classmethod
def tearDownClass(cls):
# Clean up resources for the entire test case class
pass
def setUp(self):
# Set up resources before each test method
pass
def tearDown(self):
# Clean up resources after each test method
pass
```
# --> setUp(): This method is called before each test method is executed. You can use it to set up any necessary state or resources needed for the test.
# --> tearDown(): This method is called after each test method is executed. It's used to clean up any resources or state that was set up in setUp().
# 3. Configure your test database: Django creates a separate database for running tests, ensuring that your production data is not affected. This is specified in your settings.py file.
# settings.py
```
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'test': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'test_db.sqlite3',
}
}
```
# 4. Run your tests: Django provides a management command, manage.py test, to run your tests. Navigate to your project directory in the terminal and run:
$ python manage.py test
$ python manage.py test your_app_name
$ python manage.py test your_app_name.YourModelTestCase
$ python manage.py test your_app_name.YourModelTestCase.test_something
# Django will discover your tests and run them. It will output the results to the terminal.
# 5. View test coverage: You may also want to measure the coverage of your tests. To do this, you can use third-party packages like coverage. First, install it:
$ pip install coverage
$ coverage run manage.py test
# 6. After running tests, you can generate a report:
$ coverage report
# This will show you which parts of your code are covered by tests and which are not.

View File

@@ -88,7 +88,7 @@ CHEATSHEET C#
string newStr = oldStr.Replace("old","new");
//IndexOf
//Finds the first ocurrence of a string in a larger string
//Finds the first occurrence of a string in a larger string
//Returns -1 if the string is not found
String.IndexOf(val, start, num)
val - string to search for
@@ -102,7 +102,7 @@ CHEATSHEET C#
String.Split(Char[]);
//ToCharArray
//Places selected characteres in a string in a char array
//Places selected characters in a string in a char array
String str = "AaBbCcDd";
//create array of 8 vowels
var chars = str.ToCharArray();
@@ -135,7 +135,7 @@ CHEATSHEET C#
6.1 TimeSpan Constructor
TimpeSpan(hour, minute, sec)
TimeSpan(hour, minute, sec)
TimeSpan timeS = new TimeSpan(10, 14, 50);
TimeSpan timeS_Hours = TimeSpan.FromDays(3640);
@@ -144,8 +144,8 @@ CHEATSHEET C#
Format item syntax: {index[,alignment][:format string]}
index - Specifies element in list of values to which format is applied
aligment - Indicates minimun width (in characters) to display value
format string - Contains the code which specififes the format of the displayed value
alignment - Indicates minimum width (in characters) to display value
format string - Contains the code which specifies the format of the displayed value
7.1 Numeric
@@ -293,7 +293,7 @@ CHEATSHEET C#
[access modifier] className (parameters) [:initializer]
initializer -base calls constructor in base class.
this calls constuctor within class.
this calls constructor within class.
public class nameClass : Initializer {
public className(dataType param1 , dataType param2, ...) : base(param1, param2)
@@ -313,8 +313,8 @@ CHEATSHEET C#
abstract must be implemented by subclass
Passing parameters:
1. By default, parametres are passed by value
2. Passing by reference: ref, in and out modifers
1. By default, parameters are passed by value
2. Passing by reference: ref, in and out modifiers
To pass a parameter by reference with the intent of changing the value, use the ref, or out keyword. To pass by reference with the intent of avoiding copying but not changing the value, use the in modifier

View File

@@ -524,14 +524,14 @@ Cyan='\033[0;36m' # Cyan
White='\033[0;97m' # White
# Additional colors
LGrey='\033[0;37m' # Ligth Gray
LGrey='\033[0;37m' # Light Gray
DGrey='\033[0;90m' # Dark Gray
LRed='\033[0;91m' # Ligth Red
LGreen='\033[0;92m' # Ligth Green
LYellow='\033[0;93m'# Ligth Yellow
LBlue='\033[0;94m' # Ligth Blue
LRed='\033[0;91m' # Light Red
LGreen='\033[0;92m' # Light Green
LYellow='\033[0;93m'# Light Yellow
LBlue='\033[0;94m' # Light Blue
LPurple='\033[0;95m'# Light Purple
LCyan='\033[0;96m' # Ligth Cyan
LCyan='\033[0;96m' # Light Cyan
# Bold

View File

@@ -397,8 +397,8 @@ d, t := doubleAndTriple(5)
_, t := doubleAndTriple(3)
// t = 9
// Functions can defer commands. Defered commands are
// runned in a stack order after the execution and
// Functions can defer commands. Deferred commands are
// ran in a stack order after the execution and
// returning of a function
var aux = 0
@@ -488,7 +488,7 @@ person3.Age // 0
## Maps
Maps are data structures that holds values assigneds to a key.
Maps are data structures that holds values assigned to a key.
```go
// Declaring a map
@@ -508,7 +508,7 @@ newYork // "EUA"
// Delete
delete(cities, "NY")
// Check if a key is setted
// Check if a key is set
value, ok := cities["NY"]
ok // false
value // ""

View File

@@ -308,7 +308,7 @@ for(dataType item : array) {
//Declare a variable, object name
String s;
//Invoke a contructor to create an object
//Invoke a constructor to create an object
s = new String ("Hello World");
//Invoke an instance method that operates on the object's value

View File

@@ -1,8 +1,8 @@
<?php
// Exit the file, string inside get's echo'ed
die("This file is not ment to be ran. ¯\_(ツ)_/¯");
exit("This file is not ment to be ran. ¯\_(ツ)_/¯");
die("This file is not meant to be ran. ¯\_(ツ)_/¯");
exit("This file is not meant to be ran. ¯\_(ツ)_/¯");
/**
* Printing
@@ -17,7 +17,7 @@ var_dump($arr); // Print anything, with type hints for any value and sizes
$string = 'Awesome cheatsheets';
str_contains($string, 'cheat'); // Find if the string contains the specified string (PHP >= 8.0)
str_replace('Awesome', 'Bonjour', $string); // Replace all occurence
str_replace('Awesome', 'Bonjour', $string); // Replace all occurrence
strcmp($string, 'Awesome cheatsheets'); // Compare two strings
strpos($string, 'a', 0); // Get position in the string
str_split($string, 2); // Split the string
@@ -541,7 +541,7 @@ u Pattern is treated as UTF-8
\w Any "word" character (a-z 0-9 _)
\W Any non "word" character
\s Whitespace (space, tab CRLF)
\S Any non whitepsace character
\S Any non whitespace character
\d Digits (0-9)
\D Any non digit character
. (Period) - Any character except newline

View File

@@ -71,7 +71,7 @@
| import | import libraries/modules/packages | import |
| from | import specific function/classes from modules/packages | import |
| try | this block will be tried to get executed | exception handling |
| except | is any exception/error has occured it'll be executed | exception handling |
| except | is any exception/error has occurred it'll be executed | exception handling |
| finally | It'll be executed no matter exception occurs or not | exception handling |
| raise | throws any specific error/exception | exception handling |
| assert | throws an AssertionError if condition is false | exception handling |