Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Oct 26, 2023
1 parent 2745320 commit aba28f8
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions posthog/api/test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def test_update_plugin_source(self, mock_get, mock_reload):
self.assertEqual(response.json(), {"plugin.json": '{"name":"my plugin"}'})
self.assertEqual(mock_reload.call_count, 3)

def test_create_plugin_frontend_source(self, mock_get, mock_reload):
def test_transpile_plugin_frontend_source(self, mock_get, mock_reload):
# Setup
assert mock_reload.call_count == 0
response = self.client.post(
Expand Down Expand Up @@ -733,43 +733,34 @@ def test_create_plugin_frontend_source(self, mock_get, mock_reload):
assert Plugin.objects.count() == 1
assert mock_reload.call_count == 0

# First source file, frontend.tsx

response = self.client.patch(
# Add first source file, frontend.tsx
self.client.patch(
f"/api/organizations/@current/plugins/{id}/update_source",
{"frontend.tsx": "export const scene = {}"},
)
assert Plugin.objects.count() == 1
assert PluginSourceFile.objects.count() == 1
assert mock_reload.call_count == 1

# Fetch transpiled source via API call
plugin = Plugin.objects.get(pk=id)
plugin_config = PluginConfig.objects.create(plugin=plugin, team=self.team, enabled=True, order=1)

# no frontend, since no pluginserver transpiles the code
response = self.client.get(f"/api/plugin_config/{plugin_config.id}/frontend")
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content,
b'export function getFrontendApp () { return {"transpiling": true} }',
)

# mock the plugin server's transpilation
plugin_source = PluginSourceFile.objects.get(plugin_id=id)
assert plugin_source.source == "export const scene = {}"
assert plugin_source.transpiled == (
response.content.decode("utf-8"),
'"use strict";\nexport function getFrontendApp (require) { let exports = {}; '
'"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.scene = void 0;\n'
"var scene = exports.scene = {};" # this is it
"; return exports; }"
"; return exports; }",
)
assert plugin_source.status == PluginSourceFile.Status.TRANSPILED
assert plugin_source.error is None

# Can get transpiled code back
response = self.client.get(f"/api/plugin_config/{plugin_config.id}/frontend")
assert response.status_code == 200
assert response.content == bytes(plugin_source.transpiled, "utf8")
# Check in the database
plugin_source = PluginSourceFile.objects.get(plugin_id=id)
assert plugin_source.source == "export const scene = {}"
assert plugin_source.error is None
assert plugin_source.transpiled == response.content.decode("utf-8")
assert plugin_source.status == PluginSourceFile.Status.TRANSPILED

# Updates work
self.client.patch(
Expand All @@ -778,18 +769,14 @@ def test_create_plugin_frontend_source(self, mock_get, mock_reload):
)
plugin_source = PluginSourceFile.objects.get(plugin_id=id)
assert plugin_source.source == "export const scene = { name: 'new' }"
assert plugin_source.error is None
assert plugin_source.transpiled == (
'"use strict";\nexport function getFrontendApp (require) { let exports = {}; "use strict";\n\n'
'Object.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.scene = void 0;\n'
"var scene = exports.scene = {\n name: 'new'\n};" # this is it
"; return exports; }"
)
assert plugin_source.status == PluginSourceFile.Status.TRANSPILED
assert plugin_source.error is None

response = self.client.get(f"/api/plugin_config/{plugin_config.id}/frontend")
assert response.status_code == 200
assert response.content == bytes(plugin_source.transpiled, "utf8")

# Errors as well
self.client.patch(
Expand All @@ -816,19 +803,19 @@ def test_create_plugin_frontend_source(self, mock_get, mock_reload):
except PluginSourceFile.DoesNotExist:
assert True

# Site app syntax
# Check that the syntax for "site.ts" is slightly different
self.client.patch(
f"/api/organizations/@current/plugins/{id}/update_source",
{"site.ts": "console.log('hello')"},
)
plugin_source = PluginSourceFile.objects.get(plugin_id=id)
assert plugin_source.source == "console.log('hello')"
assert plugin_source.error is None
assert (
plugin_source.transpiled
== "(function () {let exports={};\"use strict\";\n\nconsole.log('hello');;return exports;})"
)
assert plugin_source.status == PluginSourceFile.Status.TRANSPILED
assert plugin_source.error is None

def test_plugin_repository(self, mock_get, mock_reload):
response = self.client.get("/api/organizations/@current/plugins/repository/")
Expand Down

0 comments on commit aba28f8

Please sign in to comment.