Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu committed Jan 24, 2024
1 parent 04354c1 commit 5d949fa
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
run: poetry install
- name: Run lint
run: poetry run ruff --output-format=github .
- name: Run unit tests
run: poetry run python manage.py test

integration-tests:
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def handle(self, *args, **options):
global current_dialect
current_dialect = options.get("dialect", Dialect.sqlite)
selected_apps = options.get("apps", None)
print(self.get_ddl(selected_apps))
return self.get_ddl(selected_apps)

# Load migrations and get the sql statements describing the migrations.
def get_ddl(self, selected_apps):
Expand Down
8 changes: 8 additions & 0 deletions atlas_provider_django/settings.py
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
INSTALLED_APPS = ["atlas_provider_django", "tests.app1", "tests.app2"]

# if there are no databases defined, the tests tear down will fail
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "atlas_provider_django.db",
}
}
22 changes: 22 additions & 0 deletions tests/expected_all_apps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
BEGIN;
--
-- Create model Musician
--
CREATE TABLE `app1_musician` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `first_name` varchar(50) NOT NULL, `last_name` varchar(50) NOT NULL, `instrument` varchar(100) NOT NULL);
--
-- Create model Album
--
CREATE TABLE `app1_album` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL, `release_date` date NOT NULL, `num_stars` integer NOT NULL, `artist_id` bigint NOT NULL);
ALTER TABLE `app1_album` ADD CONSTRAINT `app1_album_artist_id_aed0987a_fk_app1_musician_id` FOREIGN KEY (`artist_id`) REFERENCES `app1_musician` (`id`);
COMMIT;
BEGIN;
--
-- Create model User
--
CREATE TABLE `app2_user` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `first_name` varchar(50) NOT NULL, `last_name` varchar(50) NULL, `roll` varchar(100) NOT NULL);
--
-- Create model Blog
--
CREATE TABLE `app2_blog` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL, `created_at` date NOT NULL, `num_stars` integer NOT NULL, `author_id` bigint NOT NULL);
ALTER TABLE `app2_blog` ADD CONSTRAINT `app2_blog_author_id_1675e606_fk_app2_user_id` FOREIGN KEY (`author_id`) REFERENCES `app2_user` (`id`);
COMMIT;
11 changes: 11 additions & 0 deletions tests/expected_app1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
BEGIN;
--
-- Create model Musician
--
CREATE TABLE `app1_musician` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `first_name` varchar(50) NOT NULL, `last_name` varchar(50) NOT NULL, `instrument` varchar(100) NOT NULL);
--
-- Create model Album
--
CREATE TABLE `app1_album` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL, `release_date` date NOT NULL, `num_stars` integer NOT NULL, `artist_id` bigint NOT NULL);
ALTER TABLE `app1_album` ADD CONSTRAINT `app1_album_artist_id_aed0987a_fk_app1_musician_id` FOREIGN KEY (`artist_id`) REFERENCES `app1_musician` (`id`);
COMMIT;
18 changes: 18 additions & 0 deletions tests/tests_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.test import TestCase
from django.core.management import call_command
from io import StringIO


# add unit test for the atlas_provider_django.py command
class TestAtlasProviderDjango(TestCase):
def test_atlas_provider_django_all_apps(self):
out = StringIO()
call_command("atlas-provider-django", stdout=out)
with open("tests/expected_all_apps.sql", "r") as f:
self.assertEqual(out.getvalue(), f.read())

def test_atlas_provider_django_specific_app(self):
out = StringIO()
call_command("atlas-provider-django", "--app", "app1", stdout=out)
with open("tests/expected_app1.sql", "r") as f:
self.assertEqual(out.getvalue(), f.read())

0 comments on commit 5d949fa

Please sign in to comment.