Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Sridhar
GitHub-Team
Commits
c0617b32
Commit
c0617b32
authored
Dec 02, 2025
by
Sridhar
Browse files
test file
parent
dd31379e
Changes
1
Hide whitespace changes
Inline
Side-by-side
testfile.py
0 → 100644
View file @
c0617b32
import
os
import
json
import
shutil
import
pytest
from
datetime
import
datetime
from
unittest.mock
import
patch
,
MagicMock
from
document
import
Document
from
document_writer
import
DocumentWriter
import
data_fetch
as
main
# <-- updated import
# ----------------------------
# Fixture: temporary output directory
# ----------------------------
@
pytest
.
fixture
def
temp_dir
():
d
=
"test_output"
if
os
.
path
.
exists
(
d
):
shutil
.
rmtree
(
d
)
os
.
makedirs
(
d
)
yield
d
shutil
.
rmtree
(
d
)
# ----------------------------
# Test: Document class
# ----------------------------
def
test_document_class
():
doc
=
Document
()
doc
.
set_name
(
"file.txt"
)
doc
.
set_title
(
"Title"
)
doc
.
set_main_content
(
"Content"
)
doc
.
set_attributes
({
"a"
:
1
})
doc
.
set_attribute
(
"b"
,
2
)
assert
doc
.
get_name
()
==
"file.txt"
assert
doc
.
get_title
()
==
"Title"
assert
doc
.
get_main_content
()
==
"Content"
assert
doc
.
get_attributes
()
==
{
"a"
:
1
,
"b"
:
2
}
assert
repr
(
doc
)
==
"<Document 'file.txt' 'Title'>"
# ----------------------------
# Test: _iso_to_milliseconds
# ----------------------------
def
test_iso_to_milliseconds_valid
(
temp_dir
):
writer
=
DocumentWriter
(
temp_dir
)
assert
writer
.
_iso_to_milliseconds
(
"2023-01-01T00:00:00Z"
)
==
1672531200000
def
test_iso_to_milliseconds_invalid
(
temp_dir
):
writer
=
DocumentWriter
(
temp_dir
)
now
=
int
(
datetime
.
now
().
timestamp
()
*
1000
)
result
=
writer
.
_iso_to_milliseconds
(
"INVALID"
)
assert
abs
(
result
-
now
)
<
3000
# fallback to now
# ----------------------------
# Test: write_document
# ----------------------------
def
test_write_document
(
temp_dir
):
writer
=
DocumentWriter
(
temp_dir
)
doc
=
Document
()
doc
.
set_name
(
"repo/issues/1.txt"
)
doc
.
set_title
(
"Issue Title"
)
doc
.
set_main_content
(
"Body text"
)
doc
.
set_attributes
({
"creation_date"
:
"2024-06-01T12:00:00Z"
,
# ISO string
"updated_at"
:
"2024-06-02T15:30:00Z"
,
# ISO string
"html_url"
:
"http://example.com"
})
writer
.
write_document
(
doc
)
# Check main file
assert
os
.
path
.
exists
(
os
.
path
.
join
(
temp_dir
,
"repo/issues/1.txt"
))
# Check metadata file
meta_path
=
os
.
path
.
join
(
temp_dir
,
".meta/repo/issues/1.txt.json"
)
assert
os
.
path
.
exists
(
meta_path
)
with
open
(
meta_path
,
"r"
,
encoding
=
"utf-8"
)
as
f
:
meta
=
json
.
load
(
f
)
assert
meta
[
"title"
]
==
"Issue Title"
assert
meta
[
"external_link"
]
==
"http://example.com"
assert
"creation_date"
in
meta
assert
"last_modify_date"
in
meta
# ----------------------------
# Test: write_documents
# ----------------------------
def
test_write_documents_bulk
(
temp_dir
):
writer
=
DocumentWriter
(
temp_dir
)
d1
=
Document
()
d1
.
set_name
(
"a/1.txt"
)
d1
.
set_attributes
({
"creation_date"
:
"2024-01-01T00:00:00Z"
})
d2
=
Document
()
d2
.
set_name
(
"a/2.txt"
)
d2
.
set_attributes
({
"creation_date"
:
"2024-01-02T00:00:00Z"
})
writer
.
write_documents
([
d1
,
d2
])
assert
os
.
path
.
exists
(
os
.
path
.
join
(
temp_dir
,
"a/1.txt"
))
assert
os
.
path
.
exists
(
os
.
path
.
join
(
temp_dir
,
".meta/a/1.txt.json"
))
assert
os
.
path
.
exists
(
os
.
path
.
join
(
temp_dir
,
"a/2.txt"
))
assert
os
.
path
.
exists
(
os
.
path
.
join
(
temp_dir
,
".meta/a/2.txt.json"
))
# ----------------------------
# Test: serialize_issue
# ----------------------------
def
test_serialize_issue_basic
():
issue
=
{
"id"
:
100
,
"number"
:
5
,
"title"
:
"Bug Found"
,
"body"
:
"There is a bug"
,
"state"
:
"open"
,
"created_at"
:
"2024-02-01T10:00:00Z"
,
"updated_at"
:
"2024-02-01T12:00:00Z"
,
"user"
:
{
"login"
:
"dev"
,
"id"
:
10
},
"labels"
:
[{
"name"
:
"bug"
}],
"comments"
:
2
,
"assignees"
:
[{
"login"
:
"alice"
}],
"html_url"
:
"http://example.com/issue5"
}
doc
=
main
.
serialize_issue
(
issue
,
"owner"
,
"repo"
)
assert
isinstance
(
doc
,
Document
)
assert
doc
.
get_title
()
==
"Bug Found"
assert
"There is a bug"
in
doc
.
get_main_content
()
assert
doc
.
get_attributes
()[
"number"
]
==
5
assert
"Labels: bug"
in
doc
.
get_main_content
()
# ----------------------------
# Test: fetch_issues success
# ----------------------------
@
patch
(
"data_fetch.requests.get"
)
def
test_fetch_issues_success
(
mock_get
):
mock_resp
=
MagicMock
()
mock_resp
.
json
.
return_value
=
[{
"id"
:
1
}]
mock_resp
.
raise_for_status
.
return_value
=
None
mock_get
.
return_value
=
mock_resp
issues
=
main
.
fetch_issues
(
"owner"
,
"repo"
)
assert
issues
==
[{
"id"
:
1
}]
# ----------------------------
# Test: fetch_issues failure
# ----------------------------
@
patch
(
"data_fetch.requests.get"
)
def
test_fetch_issues_failure
(
mock_get
):
mock_resp
=
MagicMock
()
mock_resp
.
raise_for_status
.
side_effect
=
Exception
(
"Bad Request"
)
mock_get
.
return_value
=
mock_resp
with
pytest
.
raises
(
Exception
)
as
e
:
main
.
fetch_issues
(
"owner"
,
"repo"
)
assert
"Failed to fetch issues"
in
str
(
e
.
value
)
# ----------------------------
# Test: create_user_config
# ----------------------------
def
test_create_user_config
(
temp_dir
):
cfg
=
{
"url"
:
"http://x"
,
"username"
:
"u"
,
"tenant"
:
"t"
,
"password"
:
"p"
,
}
plugin_id
=
"TEST-UUID"
main
.
create_user_config
(
cfg
,
temp_dir
,
plugin_id
)
assert
os
.
path
.
exists
(
"user-config.json"
)
with
open
(
"user-config.json"
,
"r"
,
encoding
=
"utf-8"
)
as
f
:
data
=
json
.
load
(
f
)
assert
data
[
"plugin_id"
]
==
"TEST-UUID"
assert
data
[
"source_paths"
]
==
[
temp_dir
]
# ----------------------------
# Test: main() success path
# ----------------------------
@
patch
(
"data_fetch.fetch_issues"
)
@
patch
(
"data_fetch.DocumentWriter"
)
def
test_main_success
(
mock_writer_cls
,
mock_fetch
):
mock_fetch
.
return_value
=
[
{
"id"
:
1
,
"number"
:
1
,
"title"
:
"Test"
,
"body"
:
None
,
"state"
:
"open"
,
"created_at"
:
"2024-01-01T10:00:00Z"
,
"updated_at"
:
"2024-01-01T12:00:00Z"
,
"user"
:
{
"login"
:
"tester"
,
"id"
:
10
},
"labels"
:
[],
"assignees"
:
[],
"comments"
:
0
,
"html_url"
:
"http://example.com"
}
]
mock_writer
=
MagicMock
()
mock_writer
.
get_plugin_id
.
return_value
=
"UUID123"
mock_writer_cls
.
return_value
=
mock_writer
result
=
main
.
main
()
assert
result
==
0
mock_writer
.
write_document
.
assert_called
()
# ----------------------------
# Test: main() failure path
# ----------------------------
@
patch
(
"data_fetch.fetch_issues"
)
def
test_main_failure
(
mock_fetch
):
mock_fetch
.
side_effect
=
Exception
(
"GitHub down"
)
result
=
main
.
main
()
assert
result
==
1
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment